-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplot_caland.r
4245 lines (3851 loc) · 375 KB
/
plot_caland.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# plot_caland.r
####
# California Natural and Working Lands Carbon and Greenhouse Gas
# Model (CALAND) Copyright (c) 2020, The Regents of the University of
# California, through Lawrence Berkeley National Laboratory (subject to
# receipt of any required approvals from the U.S. Dept. of Energy). All
# rights reserved.
# If you have questions about your rights to use or distribute this software,
# please contact Berkeley Lab's Intellectual Property Office at
# [email protected].
#
# NOTICE. This Software was developed under funding from the U.S. Department
# of Energy and the U.S. Government consequently retains certain rights. As
# such, the U.S. Government has been granted for itself and others acting on
# its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the
# Software to reproduce, distribute copies to the public, prepare derivative
# works, and perform publicly and display publicly, and to permit others to do so.
####
# This software and its associated input data are licensed under a modified BSD open source license
# Please see license.txt for details
# Compute differences between baseline scenario and alternative scenario outputs from CALAND(); make diagnostic plots
# (.pdf files) and associated .csv files
# See readme file for details on computer optimizing processing time
################################################### Overview of `plot_caland()`#################################################
# The `plot_caland()` function compares outputs from the `CALAND()` model. It is designed to compare the`CALAND()` output .xls
# file for the baseline scenario to any number of alternative scenarios (at least one required), and will output a suite of
# individual data tables (.csv files) and corresponding graphics (.pdf files) that summarize various variables at your desired
# level of spatial aggregation of region, land type, and ownership combination(s). The aggregation level can be zoomed out to
# the entire State of California (summing across all regions, land types, and ownerships) or as granular as a specific region,
# land type, and ownership combination (i.e., land category), such as North Coast, Forest on Private lands.
# Sensitivity tests of individual practices:
# `plot_caland()` has the ability to generate per area outputs for sensitivity tests of individual practices by designating
# `INDIVIDUAL = TRUE` in `plot_caland()`. These diagnostics are designed to estimate effects of a practice *in isolation*.
# Thus, these outputs are only valid when the following conditions are met:
# Comparison of a static run (i.e., baseline with only ecosystem exchange enabled, no lulcc, no management practices,
# no wildfire, except when evaluating effects on fire emissions) with a run with a single practice enabled. One
# exception is for evaluating avoided conversion effects, in which case lulcc has to be on in the baseline so that
# urban growth rate can be reduced in the scenario.
# For any runs with restoration/reforestation/afforestation/lulcc, the land type must be "All_land" in order to capture
# the dynamics of lulcc and wildfire; any other land type will not give valid results
# These individual effects change over time because most practices have effects beyond the year(s) of implementation
# (e.g. rangeland compost application, forest harvest, forest fuel reduction practices). One consideration is that
# restoration activities have secondary effects on land use/cover change related to forcing conversion of other land
# types and the proportion of land types burned by wildfire.
# Example `CALAND()` input scenario files designed for sensitivity testing can be found in /caland/inputs/ind_sims_27aug2018.
################################################### Inputs to `plot_caland()`###################################################
# The input .xls files for `plot_caland()` are the main .xls output files from `CALAND()`, located in caland/outputs/
# (unless the sub-directory `data_dir` is specified differently from the default of no subdirectory (`data_dir = ""`)
# (see Arguments section below). Two or more of these files are required, one of which will serve as the reference baseline
# scenario and the other as an alternative scenario that tests the impacts of change(s) to the baseline. Additional alternative
# scenarios can be compared to the baseline in a single run as well.
################################################## Arguments in `plot_caland()` ################################################
# 1. `scen_fnames` is a vector of scenario output file names written by `CALAND()`.
# - The first scenario file in `scen_fnames` must be the name of the baseline scenario to which the other scenarios will be
# compared. For example: `scen_fnames = c("Baseline.xls", "A.xls", "B.xls", "C.xls", "D.xls")`.
# - The number of scenario files in `scen_fnames` must correspond directly to the labels in `scen_snames` (see argument #2).
# 2. `scen_snames` vector of abbreviated scenario labels which will be printed in the plot legends (8 characters maximum for
# each label)
# 3. `data_dir` path to the directory containing the `CALAND()` output files; do not include the "/" character at the end;
# default is `data_dir = "./outputs"`
# 4. `reg` vector of individual regions to plot, including the entire State of California `All_region`; can be any number
# of available regions. Default is all of them:
# reg = c("All_region", "Central_Coast", "Central_Valley", "Delta", "Deserts", "Eastside", "Klamath", "North_Coast",
# "Sierra_Cascades", "South_Coast", "Ocean")
# 5. `lt` vector of land types to plot; can be any number of available types. Default is all of them:
# lt = c("All_land", "Water", "Ice", "Barren", "Sparse", "Desert", "Shrubland", "Grassland", "Savanna", "Woodland",
# "Forest", "Meadow", "Coastal_marsh", "Fresh_marsh", "Cultivated", "Developed_all", "Seagrass")
# 6. `own` array of ownerships to plot; can be any number of available types. Default is the aggregation of all ownerships
# (All_own): own = "All_own"`
# However any number of them can be specified:
# own = c("All_own", "BLM", "DoD", "Easement", "Local_gov", "NPS", "Other_fed", "Private", "State_gov", "USFS_nonwild")
# 7. `figdir` folder within data_dir to save the graphs (.pdf) and corresponding data tables (.csv); do not include the "/"
# character at the end. Default is: `figdir = "figures"`
# 8. `INDIVIDUAL` Indicates whether a sensitivity test is being performed on scenarios that isolate the effects of
# individual practices. This test is only valid if the scenarios were configured for this purpose. The default is not to
# compute these outputs: `INDIVIDUAL = FALSE`.
# 9. `units_ha` TRUE = units of `plot_caland()` outputs will be in "ha" (same as `CALAND()` outputs), FALSE = units of
# `plot_caland()` outputs will be converted to "ac". Default is `units_ha = "ac"`.
# 10. `blackC` TRUE = black GWP equal to 900, FALSE = black GWP equal to 1. Default is `blackC = FALSE`. Note: this need
# to match the `CALAND()` `blackC` argument that was used.
# 11. `blackC_plot` TRUE = plot BC, CO2, and CH4, FALSE = plot only CO2 and CH4 (BC added to CO2 which is only valid if
# black C is FALSE). Default is `blackC_plot = FALSE`. Note: this need to match the `CALAND()` `blackC` argument that
# was used.
# 12. `last_year` the last year to plot; this should be the final run year + 1 because cumulative and area outputs reflect
# previous years e.g., 2050 is the final run year, but everything is defined and output to 2051, so last_year = 2051.
# Default is `last_year = 2051`
###################################################### start script #################################################################
# setwd("<your_path>/caland/")
setwd("./")
# this enables java to use up to 4GB of memory for reading and writing excel files
options(java.parameters = "-Xmx8g" )
# Load all the required packages
libs <- c( "XLConnect", "ggplot2", "grid", "RColorBrewer", "reshape2")
for( i in libs ) {
if( !require( i, character.only=T ) ) {
cat( "Couldn't load", i, "\n" )
stop( "Use install.packages() to download this library\nOr use the GUI Package Installer\nInclude dependencies, and install it for
local user if you do not have root access\n" )
}
library( i, character.only=T )
}
### Assign plot_caland()
plot_caland <- function(scen_fnames, scen_snames, data_dir = "./outputs", reg = c("All_region", "Central_Coast", "Central_Valley",
"Delta", "Deserts", "Eastside", "Klamath", "North_Coast", "Sierra_Cascades", "South_Coast", "Ocean"),
lt = c("All_land", "Water", "Ice", "Barren", "Sparse", "Desert", "Shrubland", "Grassland", "Savanna", "Woodland", "Forest",
"Meadow", "Coastal_marsh", "Fresh_marsh", "Cultivated", "Developed_all", "Seagrass"),
own = c("All_own"), figdir = "figures", INDIVIDUAL = FALSE, units_ha=FALSE, blackC = FALSE, blackC_plot = FALSE, last_year = 2051) {
cat("Start plot_caland() at", date(), "\n")
# CALAND starts in 2010 and its outputs are referenced to that so plots must start in this year
start_year = 2010
# first check to make sure that the combination of blackC and backC_plot are valid
if (blackC == TRUE & blackC_plot == FALSE) {
cat( "Invalid settings for black carbon\n" )
stop( "If black carbon GWP was computed as 900 (blackC==TRUE), it must be plotted as black carbon (blackC_plot==TRUE)\n" )
}
outputdir = paste0(data_dir, "/")
num_scen_names = length(scen_fnames)
num_lt = length(lt)
num_reg = length(reg)
num_own = length(own)
# create the long scenario names for the outfiles from the input file names
# just drop the .xls because some output file distinctions are at the end
scen_lnames = substr(scen_fnames, 1, regexpr(".xls", scen_fnames)-1)
theme_set( theme_bw() )
FIGURE_DIMS <- list(dpi=300, width=2560/300, height=1440/300)
# Mg C to Million Metric tons C
Mg2MMT = 1 / 1000000
# ha (or ac) to Thousand ha
ha2kha = 1 / 1000
# ha to ac
ha2ac = 2.47105
c_lab = "MMT C"
ha_lab = "kha"
ac_lab = "kac"
dh_lab = "MgC/ha"
da_lab = "MgC/ac"
g_lab = "MMT CO2-eq"
# these are the sheets for plotting C summary data
stock_sheets = c("All_orgC_stock", "All_biomass_C_stock", "Above_main_C_stock", "Below_main_C_stock", "Understory_C_stock", "StandDead_C_stock",
"DownDead_C_stock", "Litter_C_stock","Soil_orgC_stock", "Total_Wood_C_stock")
num_stock_sheets = length(stock_sheets)
ann_sheets = c("Total_Wood_AnnGain_C_stock", "Eco_AnnGain_C_stock", "Total_Atmos_AnnGain_C_stock", "Manage_Atmos_AnnGain_C_stock",
"Fire_Atmos_AnnGain_C_stock", "LCC_Atmos_AnnGain_C_stock", "Wood_Atmos_AnnGain_C_stock", "Man_Atmos_AnnGain_Harv2EnergyC", "Man_Atmos_AnnGain_Slash2EnergyC")
num_ann_sheets = length(ann_sheets)
# use only the first 7 for bar graphs
num_ann_comp_sheets = 7
cum_sheets = c("Total_Wood_CumGain_C_stock", "Eco_CumGain_C_stock", "Total_Atmos_CumGain_C_stock", "Manage_Atmos_CumGain_C_stock",
"Fire_Atmos_CumGain_C_stock", "LCC_Atmos_CumGain_C_stock", "Wood_Atmos_CumGain_C_stock", "Man_Atmos_CumGain_Harv2EnergyC", "Man_Atmos_CumGain_Slash2EnergyC")
num_cum_sheets = length(cum_sheets)
# use only the first 7 for bar graphs
num_cum_comp_sheets = 7
area_sheets = c("Area", "Managed_area", "Wildfire_area")
num_area_sheets = length(area_sheets)
den_sheets = c("All_orgC_den", "All_biomass_C_den", "Above_main_C_den", "Below_main_C_den", "Understory_C_den", "StandDead_C_den",
"DownDead_C_den", "Litter_C_den", "Soil_orgC_den")
num_den_sheets = length(den_sheets)
bar_plot_labels = c("Wood_Gain_from_Eco", "Ecosystem_Gain_from_Atmos", "Total_Atmosphere_Gain", "Loss_to_Atmos_from_Manage",
"Loss_to_Atmos_from_Fire", "Loss_to_Atmos_from_LCC", "Loss_to_Atmos_from_Wood")
# these are the sheets for reading/plotting GHG summary data
# do line plots only for the totals
# do stacked plots for the components by scenario
ann_ghg_sheets = c("Total_AnnCO2eq_all", "TotalEnergy_AnnCO2eq_all", "TotalFire_AnnCO2eq_all", "TotalNonBurn_AnnCO2eq_all",
"TotalWood_AnnCO2eq_all", "Total_AnnCO2", "Total_AnnCH4eq", "Total_AnnBCeq", "Wood_AnnCO2", "Wildfire_AnnCO2",
"ManTotEnergy_AnnCO2", "LCCTotEnergy_AnnCO2", "Eco_AnnCO2", "ManFire_AnnCO2", "ManNonBurn_AnnCO2", "LCCFire_AnnCO2",
"LCCNonBurn_AnnCO2", "Wood_AnnCH4eq", "Wildfire_AnnCH4eq", "ManTotEnergy_AnnCH4eq", "LCCTotEnergy_AnnCH4eq",
"Eco_AnnCH4eq", "ManFire_AnnCH4eq", "LCCFire_AnnCH4eq","Wildfire_AnnBCeq", "ManTotEnergy_AnnBCeq", "LCCTotEnergy_AnnBCeq",
"ManFire_AnnBCeq", "LCCFire_AnnBCeq")
num_ann_ghg_sheets = length(ann_ghg_sheets)
# The numbers below denote that the first 8 names in ann_ghg_sheets are totals for one type of plotting, and that 6-8 are the totals for
# the three species.
# So in this case the order of names in the sheets does matter.
num_plot_ann_ghg_sheets = 8
start_spec_ann = 6
end_spec_ann = 8
cum_ghg_sheets = c("Total_CumCO2eq_all", "TotalEnergy_CumCO2eq_all", "TotalFire_CumCO2eq_all", "TotalNonBurn_CumCO2eq_all",
"TotalWood_CumCO2eq_all", "Total_CumCO2", "Total_CumCH4eq", "Total_CumBCeq", "Wood_CumCO2", "Wildfire_CumCO2",
"ManTotEnergy_CumCO2", "LCCTotEnergy_CumCO2", "Eco_CumCO2", "ManFire_CumCO2", "ManNonBurn_CumCO2", "LCCFire_CumCO2",
"LCCNonBurn_CumCO2", "Wood_CumCH4eq", "Wildfire_CumCH4eq", "ManTotEnergy_CumCH4eq", "LCCTotEnergy_CumCH4eq",
"Eco_CumCH4eq", "ManFire_CumCH4eq", "LCCFire_CumCH4eq", "Wildfire_CumBCeq", "ManTotEnergy_CumBCeq", "LCCTotEnergy_CumBCeq",
"ManFire_CumBCeq", "LCCFire_CumBCeq")
num_cum_ghg_sheets = length(cum_ghg_sheets)
num_plot_cum_ghg_sheets = 8
start_spec_cum = 6
end_spec_cum = 8
# loop over the regions
for (r in 1:num_reg) {
reg_lab = reg[r]
# loop over the land types
for (l in 1:num_lt) {
lt_lab = lt[l]
# land regions do not have seagrass
# All_region has all land types and seagrass
# The Ocean region has only the Seagrass land type
if ((reg_lab != "All_region" & reg_lab != "Ocean" & lt_lab != "Seagrass") | (reg_lab == "All_region") |
(reg_lab == "Ocean" & lt_lab == "Seagrass")) {
for ( o in 1:num_own ) {
own_lab = own[o]
#### no longer need to exclude any cases #####
# All_own is the default that sums all the ownerships, and it exists for All_region plus each individual land type
# and for All_land plus each individual region
# the individual ownerships are plotted for individual regions and land types and for All_land and All_region
# if All_own or a specific region and land type (not All_region and not All_land), which includes:
# All_own, All_land and All_region (i.e. statewide)
# All_own for individual Land_Type in individual region
# All_own for individual Land_Type in All_region (i.e. statewide Land_Type)
# All_own for All_land in individual region
# individual ownership, Land_Type, and region
# individual ownership for All_land in individual region
# individual ownership for individual Land_Type in All_region
# previously excluded statewide individual ownerships
# if (own_lab == "All_own" | (reg_lab != "All_region" & lt_lab != "All_land")) {
out_dir = paste0(outputdir, figdir, "/", reg_lab, "/", lt_lab, "/", own_lab, "/")
dir.create(out_dir, recursive=TRUE)
# data frames for plotting
out_stock_df_list <- list()
out_ann_df_list <- list()
out_cum_df_list <- list()
out_area_df_list <- list()
out_den_df_list <- list()
out_ann_ghg_df_list <- list()
out_cum_ghg_df_list <- list()
for (i in 1:num_stock_sheets) {
out_stock_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1:num_ann_sheets) {
out_ann_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1:num_cum_sheets) {
out_cum_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1:num_area_sheets) {
out_area_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1:num_den_sheets) {
out_den_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1: num_ann_ghg_sheets) {
out_ann_ghg_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
for (i in 1: num_cum_ghg_sheets) {
out_cum_ghg_df_list[[i]] <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL,
Value=NULL)
}
names(out_stock_df_list) = stock_sheets
names(out_ann_df_list) = ann_sheets
names(out_cum_df_list) = cum_sheets
names(out_area_df_list) = area_sheets
names(out_den_df_list) = den_sheets
names(out_ann_ghg_df_list) = ann_ghg_sheets
names(out_cum_ghg_df_list) = cum_ghg_sheets
cum_comp_df <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Component=NULL, Units=NULL, Year=NULL,
Value=NULL)
ann_comp_df <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Component=NULL, Units=NULL, Year=NULL,
Value=NULL)
#dev_man_data_df = NULL
man_df = NULL
al_area_df = NULL
# loop over the scenario outputs to read them in and put the data into data frames for plotting
# aggregate the ownserhips to the land type
# only get the year columns (not the change column)
for(s in 1:num_scen_names) {
ann_ghg_comp_df <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL, Value=NULL,
Diff=NULL, Component = NULL)
cum_ghg_comp_df <- data.frame(Scenario=NULL, Region=NULL, Land_Type=NULL, Ownership=NULL, Units=NULL, Year=NULL, Value=NULL,
Diff=NULL, Component = NULL)
# Load the data file
data_file = paste0(outputdir, scen_fnames[s])
scen_wrkbk = loadWorkbook(data_file)
# worksheet/table names
scen_sheets = getSheets(scen_wrkbk)
num_scen_sheets = length(scen_sheets)
# there shouldn't be any NA values in these sheets, so the data types should convert properly
# Load the worksheets into a list of data frames
scen_df_list <- list()
for (i in 1:num_scen_sheets) {
scen_df_list[[i]] <- readWorksheet(scen_wrkbk, i, startRow = 1)
# remove the Xs added to the front of the year columns, and get the years as numbers only
yinds = which(substr(names(scen_df_list[[i]]),1,1) == "X")
names(scen_df_list[[i]])[yinds] = substr(names(scen_df_list[[i]]),2,5)[yinds]
# remove the years that are not to be plotted
# need to keep the extra change column because it is removed throughout the code
# i don't think it is used at all
remove_cols = which( as.integer(names(scen_df_list[[i]])) > last_year )
if (length(remove_cols) > 0) {
scen_df_list[[i]] = scen_df_list[[i]][,-remove_cols]
}
# convert the 3 area sheets to from ha to ac if units_ha == FALSE
if (scen_sheets[i] %in% area_sheets & units_ha == FALSE) {
if (i==1) {
scen_df_list[[i]][,5:(ncol(scen_df_list[[i]]))] <- scen_df_list[[i]][,5:(ncol(scen_df_list[[i]]))] * ha2ac
} else {
scen_df_list[[i]][,6:(ncol(scen_df_list[[i]]))] <- scen_df_list[[i]][,6:(ncol(scen_df_list[[i]]))] * ha2ac
}
}
######### get the stock data
if (scen_sheets[i] %in% stock_sheets) {
oind = which(stock_sheets == scen_sheets[i])
startcol = 5
# extract and convert the values for this region (including All_region)
# For all All_own cases: 1) All_own in specific region & landtype, 2) All_own, All_land & All_region,
# 3) All_own & All_land, 4) All_own & All_region
if (own_lab == "All_own") {
### 1) Extract data for All_own in specific region & landtype (only option for >1 row of reg_lab and lt_lab)
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) > 1) {
# aggregate the ownerships across the specific region and landtype
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
### 2) Extract data for All_own, All_land & All_region, 2) All_own & All_land, or 3) All_own & All_region
} else if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
# get the correct single summary row from outputs (3 types possible)
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# Otherwise this land type does not exist in this region
val_col = 0
}
} else { # end All_own
###### Extract individual ownership cases ######
# (1) landcat, (2) All_region & All_land (not in outputs - must aggregate; exclude seagrass because not land), (3) single Land_Type & All_region
# (not in outputs - must aggregate), and (4) All_land & single region (not in outputs - must aggregate)
### (1) get landcat if it exists ###
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
# (This can include Seagrass)
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] ==
own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
### (2) get individual statewide ownership ###
# aggregate individual ownership across regions and Land_Types
if (lt_lab == "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all Land_Types and regions except Seagrass
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
### (3) get individual statewide ownership-Land_Type ###
# aggregate current ownership & Land_Type across regions
if (lt_lab != "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all regions for specific Land_Type (This can extract Seagrass: All_region, Other_Fed, Seagrass)
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else { # end statewide Land_Type-ownership level
### (4) get regional individual ownership ###
# aggregate specific ownership within specific region across all landtypes
if (lt_lab == "All_land" & reg_lab != "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum data across Land_Types in current ownership and region excluding Ocean/Seagrass
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
# ownership does not exist in this land type and region
val_col = 0
} # end individual ownership does not exist for this land type and region
} # end regional individual ownership
} # end individual statewide ownership-Land_Type
} # end individual statewide ownership
} # end landcat level
scen_col = rep(scen_lnames[s], length(val_col))
reg_col = rep(reg_lab, length(val_col))
lt_col = rep(lt_lab, length(val_col))
own_col = rep(own_lab, length(val_col))
unit_col = rep(c_lab, length(val_col))
year_col = as.numeric(names(scen_df_list[[i]])[startcol:(ncol(scen_df_list[[i]])-1)])
temp_df = data.frame(Scenario=scen_col, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Units=unit_col,
Year=year_col, Value=val_col)
#### stock data stored here
out_stock_df_list[[oind]] = rbind(out_stock_df_list[[oind]],temp_df)
} # end get stock data
# get the annual flux data
if (scen_sheets[i] %in% ann_sheets) {
oind = which(ann_sheets == scen_sheets[i])
startcol = 5
# extract and convert the values for this region
if (reg_lab != "Ocean" & lt_lab != "Seagrass") {
# land
# For all All_own cases: 1) All_own in specific region & landtype, 2) All_own, All_land & All_region,
# 3) All_own & All_land, 4) All_own & All_region
if (own_lab == "All_own") {
### 1) Extract data for All_own in specific region & landtype (only option for >1 row of reg_lab and lt_lab)
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) > 1) {
# aggregate the ownerships across the specific region and landtype
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
### 2) Extract data for All_own, All_land & All_region, 2) All_own & All_land, or 3) All_own & All_region
} else if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
# get the correct single summary row from outputs (3 types possible)
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this land type does not exist in this region
val_col = 0
}
} else { # end All_own
###### Extract individual ownership cases ######
# (1) landcat, (2) All_region & All_land (not in outputs - must aggregate; exclude seagrass because not land), (3) single Land_Type & All_region
# (not in outputs - must aggregate), and (4) All_land & single region (not in outputs - must aggregate)
### (1) get landcat if it exists ###
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1){
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] ==
own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
### (2) get individual statewide ownership ###
# aggregate individual ownership across regions and Land_Types
if (lt_lab == "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all Land_Types and regions except Seagrass
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
### (3) get individual statewide ownership-Land_Type ###
# aggregate current ownership & Land_Type across regions
if (lt_lab != "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all regions for specific Land_Type (This can extract Seagrass: All_region, Other_Fed, Seagrass)
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else { # end statewide Land_Type-ownership level
### (4) get regional individual ownership ###
# aggregate specific ownership within specific region across all landtypes
if (lt_lab == "All_land" & reg_lab != "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum data across Land_Types in current ownership and region excluding Ocean/Seagrass
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
# ownership does not exist in this land type and region
val_col = 0
} # end individual ownership does not exist for this land type and region
} # end regional individual ownership
} # end individual statewide ownership-Land_Type
} # end individual statewide ownership
} # end landcat level
scen_col = rep(scen_lnames[s], length(val_col))
reg_col = rep(reg_lab, length(val_col))
lt_col = rep(lt_lab, length(val_col))
own_col = rep(own_lab, length(val_col))
unit_col = rep(c_lab, length(val_col))
year_col = as.numeric(names(scen_df_list[[i]])[startcol:(ncol(scen_df_list[[i]])-1)])
temp_df = data.frame(Scenario=scen_col, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Units=unit_col,
Year=year_col, Value=val_col)
#### annual flux data stored here
out_ann_df_list[[oind]] = rbind(out_ann_df_list[[oind]],temp_df)
# bar graph of annual components
# not include the total atmos
# all are negative except eco, which can be positive or negative
# get the net wood c stock rather than the gross gain
# convert this net wood gain to positive so it represents c retention with eco
scen_col2 = rep(scen_snames[s], length(val_col))
if (ann_sheets[oind] != "Total_Atmos_AnnGain_C_stock" & ann_sheets[oind] != "Man_Atmos_AnnGain_Harv2EnergyC" &
ann_sheets[oind] != "Man_Atmos_AnnGain_Slash2EnergyC") {
ann_comp_col = rep(bar_plot_labels[oind], length(val_col))
if (ann_sheets[oind] != "Eco_AnnGain_C_stock") {
if (ann_sheets[oind] == "Total_Wood_AnnGain_C_stock") {
# subtract the loss to atmosphere
lind = which(scen_sheets == "Wood_Atmos_AnnGain_C_stock")
temp_df <- readWorksheet(scen_wrkbk, lind, startRow = 1)
# remove the Xs added to the front of the year columns, and get the years as numbers only
yinds = which(substr(names(temp_df),1,1) == "X")
names(temp_df)[yinds] = substr(names(temp_df),2,5)[yinds]
# remove the years that are not to be plotted
# need to keep the extra change column because it is removed throughout the code
# i don't think it is used at all
remove_cols = which( as.integer(names(temp_df)) > last_year )
if (length(remove_cols) > 0) {
temp_df = temp_df[,-remove_cols]
}
# For all All_own cases: 1) All_own in specific region & landtype, 2) All_own, All_land & All_region,
# 3) All_own & All_land, 4) All_own & All_region
if (own_lab == "All_own") {
### 1) Extract data for All_own in specific region & landtype (only option for >1 row of reg_lab and lt_lab)
if (nrow(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) > 1) {
# aggregate the ownerships across the specific region and landtype
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Land_Type"] ==
lt_lab & temp_df[,"Region"] ==
reg_lab, startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
### 2) Extract data for All_own, All_land & All_region, 2) All_own & All_land, or 3) All_own & All_region
} else if (nrow(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) == 1) {
# get the correct single summary row from outputs (3 types possible)
val_col = Mg2MMT * unlist(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) - val_col
} else {
# Otherwise this land type does not exist in this region
val_col = 0
}
} else { # end All_own
###### Extract individual ownership cases ######
# (1) landcat, (2) All_region & All_land (not in outputs - must aggregate; exclude seagrass because not land), (3) single Land_Type & All_region
# (not in outputs - must aggregate), and (4) All_land & single region (not in outputs - must aggregate)
### (1) get landcat if it exists ###
if (nrow(temp_df[temp_df[,"Ownership"] == own_lab & temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] ==
reg_lab, startcol:(ncol(temp_df)-1)]) == 1) {
val_col = Mg2MMT * unlist(temp_df[temp_df[,"Ownership"] == own_lab & temp_df[,"Land_Type"] ==
lt_lab & temp_df[,"Region"] == reg_lab, startcol:(ncol(temp_df)-1)]) - val_col
} else {
### (2) get individual statewide ownership ###
# aggregate individual ownership across regions and Land_Types
if (lt_lab == "All_land" & reg_lab == "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)]) >=1) {
# sum specific ownership data across all Land_Types and regions
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else {
### (3) get individual statewide ownership-Land_Type ###
# aggregate current ownership & Land_Type across regions
if (lt_lab != "All_land" & reg_lab == "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Land_Type"] == lt_lab,
startcol:(ncol(temp_df)-1)]) >=1) {
# sum specific ownership data across all regions for specific Land_Type
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Land_Type"] == lt_lab,
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else { # end statewide Land_Type-ownership level
### (4) get regional individual ownership ###
# aggregate specific ownership within specific region across all landtypes
if (lt_lab == "All_land" & reg_lab != "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] == reg_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)]) >=1) {
# sum data across Land_Types in current ownership and region
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] == reg_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else {
# ownership does not exist in this land type and region
val_col = 0
} # end individual ownership does not exist for this land type and region
} # end regional individual ownership
} # end individual statewide ownership-Land_Type
} # end individual statewide ownership
} # end landcat level
ann_comp_col = rep("Net_Wood_Gain", length(val_col))
} # end if calc net wood stock gain
temp_df = data.frame(Scenario=scen_col2, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Component=ann_comp_col,
Units=unit_col, Year=year_col, Value=-val_col)
} else { # end if not eco gain
temp_df = data.frame(Scenario=scen_col2, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Component=ann_comp_col,
Units=unit_col, Year=year_col, Value=val_col)
} # end else eco gain
ann_comp_df = rbind(ann_comp_df, temp_df)
} # end if not total atmos gain
} else {
# do seagrass only for ocean and all region
if (lt_lab == "Seagrass") {
# all own
if (own_lab == "All_own") {
# >1 row of Seagrass and All_own would never exist in outputs
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) > 1) {
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
# 1 row of Seagrass, All_region and All_own exists
} else if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this land type does not exist in this region
val_col = 0
}
} else { # single ownership
# only one or zero rows of this ownership in this land type and region
# get landcat: Ocean, Seagrass, Other_fed
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] ==
own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this ownership does not exist in this land type and region
val_col = 0
}
} # end else extract a single ownership
scen_col = rep(scen_lnames[s], length(val_col))
reg_col = rep(reg_lab, length(val_col))
lt_col = rep(lt_lab, length(val_col))
own_col = rep(own_lab, length(val_col))
unit_col = rep(c_lab, length(val_col))
year_col = as.numeric(names(scen_df_list[[i]])[startcol:(ncol(scen_df_list[[i]])-1)])
temp_df = data.frame(Scenario=scen_col, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Units=unit_col,
Year=year_col, Value=val_col)
out_ann_df_list[[oind]] = rbind(out_ann_df_list[[oind]],temp_df)
} # end if region ocean and seagrass lt
} # end else ocean or seagrass
} # end get annual data
# get the cumulative data
if (scen_sheets[i] %in% cum_sheets) {
oind = which(cum_sheets == scen_sheets[i])
startcol = 5
# extract and convert the values for this region
if (reg_lab != "Ocean" & lt_lab != "Seagrass") {
# land
# For all All_own cases: 1) All_own in specific region & landtype, 2) All_own, All_land & All_region,
# 3) All_own & All_land, 4) All_own & All_region
if (own_lab == "All_own") {
### 1) Extract data for All_own in specific region & landtype (only option for >1 row of reg_lab and lt_lab)
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) > 1) {
# aggregate the ownerships across the specific region and landtype
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
### 2) Extract data for All_own, All_land & All_region, 2) All_own & All_land, or 3) All_own & All_region
} else if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1){
# get the correct single summary row from outputs (3 types possible)
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this land type does not exist in this region
val_col = 0
}
} else { # end All_own
###### Extract individual ownership cases ######
# (1) landcat, (2) All_region & All_land (not in outputs - must aggregate; exclude seagrass because not land), (3) single Land_Type & All_region
# (not in outputs - must aggregate), and (4) All_land & single region (not in outputs - must aggregate)
### (1) get landcat if it exists ###
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1){
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] ==
own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
### (2) get individual statewide ownership ###
# aggregate individual ownership across regions and Land_Types
if (lt_lab == "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all Land_Types and regions
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
### (3) get individual statewide ownership-Land_Type ###
# aggregate current ownership & Land_Type across regions
if (lt_lab != "All_land" & reg_lab == "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership data across all regions for specific Land_Type
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Land_Type"] == lt_lab,
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else { # end statewide Land_Type-ownership level
### (4) get regional individual ownership ###
# aggregate specific ownership within specific region across all landtypes
if (lt_lab == "All_land" & reg_lab != "All_region" & nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)]) >=1) {
# sum specific ownership and region across Land_Types
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab &
scen_df_list[[i]][,"Region"] == reg_lab &
scen_df_list[[i]][,"Region"] != "Ocean",
startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else {
# ownership does not exist in this land type and region
val_col = 0
} # end individual ownership does not exist for this land type and region
} # end regional individual ownership
} # end individual statewide ownership-Land_Type
} # end individual statewide ownership
} # end landcat level
scen_col = rep(scen_lnames[s], length(val_col))
reg_col = rep(reg_lab, length(val_col))
lt_col = rep(lt_lab, length(val_col))
own_col = rep(own_lab, length(val_col))
unit_col = rep(c_lab, length(val_col))
year_col = as.numeric(names(scen_df_list[[i]])[startcol:(ncol(scen_df_list[[i]])-1)])
temp_df = data.frame(Scenario=scen_col, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Units=unit_col, Year=year_col,
Value=val_col)
out_cum_df_list[[oind]] = rbind(out_cum_df_list[[oind]],temp_df)
# bar graph of cumulative components
# not include the total atmos
# all are negative except eco, which can be positive or negative
# get the net wood c stock rather than the gross gain
# convert this net wood gain to positive so it represents c retention with eco
scen_col2 = rep(scen_snames[s], length(val_col))
if (cum_sheets[oind] != "Total_Atmos_CumGain_C_stock" & cum_sheets[oind] != "Man_Atmos_CumGain_Harv2EnergyC" &
cum_sheets[oind] != "Man_Atmos_CumGain_Slash2EnergyC") {
cum_comp_col = rep(bar_plot_labels[oind], length(val_col))
if (cum_sheets[oind] != "Eco_CumGain_C_stock") {
if (cum_sheets[oind] == "Total_Wood_CumGain_C_stock") {
# subtract the loss to atmosphere
lind = which(scen_sheets == "Wood_Atmos_CumGain_C_stock")
temp_df <- readWorksheet(scen_wrkbk, lind, startRow = 1)
# remove the Xs added to the front of the year columns, and get the years as numbers only
yinds = which(substr(names(temp_df),1,1) == "X")
names(temp_df)[yinds] = substr(names(temp_df),2,5)[yinds]
# remove the years that are not to be plotted
# need to keep the extra change column because it is removed throughout the code
# i don't think it is used at all
remove_cols = which( as.integer(names(temp_df)) > last_year )
if (length(remove_cols) > 0) {
temp_df = temp_df[,-remove_cols]
}
# For all All_own cases: 1) All_own in specific region & landtype, 2) All_own, All_land & All_region,
# 3) All_own & All_land, 4) All_own & All_region
if (own_lab == "All_own") {
### 1) Extract data for All_own in specific region & landtype (only option for >1 row of reg_lab and lt_lab)
if (nrow(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) > 1) {
# aggregate the ownerships across the specific region and landtype
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else if (nrow(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) == 1) {
### 2) Extract data for All_own, All_land & All_region, 2) All_own & All_land, or 3) All_own & All_region
val_col = Mg2MMT * unlist(temp_df[temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] == reg_lab,
startcol:(ncol(temp_df)-1)]) - val_col
} else {
# this land type does not exist in this region
val_col = 0
}
} else { # end All_own
###### Extract individual ownership cases ######
# (1) landcat, (2) All_region & All_land (not in outputs - must aggregate), (3) single Land_Type & All_region
# (not in outputs - must aggregate), and (4) All_land & single region (not in outputs - must aggregate)
### (1) get landcat if it exists ###
if (nrow(temp_df[temp_df[,"Ownership"] == own_lab & temp_df[,"Land_Type"] == lt_lab & temp_df[,"Region"] ==
reg_lab, startcol:(ncol(temp_df)-1)]) == 1) {
val_col = Mg2MMT * unlist(temp_df[temp_df[,"Ownership"] == own_lab & temp_df[,"Land_Type"] ==
lt_lab & temp_df[,"Region"] == reg_lab, startcol:(ncol(temp_df)-1)]) - val_col
} else {
### (2) get individual statewide ownership ###
# aggregate individual ownership across regions and Land_Types
if (lt_lab == "All_land" & reg_lab == "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)]) >=1) {
# sum specific ownership data across all Land_Types and regions
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else {
### (3) get individual statewide ownership-Land_Type ###
# aggregate current ownership & Land_Type across regions
if (lt_lab != "All_land" & reg_lab == "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Land_Type"] == lt_lab,
startcol:(ncol(temp_df)-1)]) >=1) {
# sum specific ownership and landtype across all regions
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Land_Type"] == lt_lab,
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else { # end statewide Land_Type-ownership level
### (4) get regional individual ownership ###
# aggregate specific ownership within specific region across all landtypes
if (lt_lab == "All_land" & reg_lab != "All_region" & nrow(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] == reg_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)]) >=1) {
# sum specific ownership and region across Land_Types
val_col = Mg2MMT * unlist(apply(temp_df[temp_df[,"Ownership"] == own_lab &
temp_df[,"Region"] == reg_lab &
temp_df[,"Region"] != "Ocean",
startcol:(ncol(temp_df)-1)], 2, sum)) - val_col
} else {
# ownership does not exist in this land type and region
val_col = 0
} # end individual ownership does not exist for this land type and region
} # end regional individual ownership
} # end individual statewide ownership-Land_Type
} # end individual statewide ownership
} # end landcat level
cum_comp_col = rep("Net_Wood_Gain", length(val_col))
} # end if calc net wood stock gain
temp_df = data.frame(Scenario=scen_col2, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Component=cum_comp_col,
Units=unit_col, Year=year_col, Value=-val_col)
} else { # end if not eco gain
temp_df = data.frame(Scenario=scen_col2, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Component=cum_comp_col,
Units=unit_col, Year=year_col, Value=val_col)
} # end if eco gain
cum_comp_df = rbind(cum_comp_df, temp_df)
} # end if not total atmos gain
} else {
# do seagrass only for ocean and all_region
if (lt_lab == "Seagrass") {
# all own
if (own_lab == "All_own") {
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)]) > 1) {
val_col = Mg2MMT * unlist(apply(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)], 2, sum))
} else if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Land_Type"] == lt_lab & scen_df_list[[i]][,"Region"] ==
reg_lab, startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this land type does not exist in this region
val_col = 0
}
} else { # single ownership
# only one or zero rows of this ownership in this land type and region
if (nrow(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab, startcol:(ncol(scen_df_list[[i]])-1)]) == 1) {
val_col = Mg2MMT * unlist(scen_df_list[[i]][scen_df_list[[i]][,"Ownership"] == own_lab & scen_df_list[[i]][,"Land_Type"] ==
lt_lab & scen_df_list[[i]][,"Region"] == reg_lab,
startcol:(ncol(scen_df_list[[i]])-1)])
} else {
# this ownership does not exist in this land type and region
val_col = 0
}
} # end else extract a single ownership
scen_col = rep(scen_lnames[s], length(val_col))
reg_col = rep(reg_lab, length(val_col))
lt_col = rep(lt_lab, length(val_col))
own_col = rep(own_lab, length(val_col))
unit_col = rep(c_lab, length(val_col))
year_col = as.numeric(names(scen_df_list[[i]])[startcol:(ncol(scen_df_list[[i]])-1)])
temp_df = data.frame(Scenario=scen_col, Region=reg_col, Land_Type=lt_col, Ownership=own_col, Units=unit_col, Year=year_col,
Value=val_col)
out_cum_df_list[[oind]] = rbind(out_cum_df_list[[oind]],temp_df)
} # if ocean region and seagrass lt
} # end else ocean or seagrass
} # end get cumulative data
############### get the area data
# store the total area data for All_own & individual ownerships; it is needed for the density data
# also store the individual management areas for this reg, lt, own
# also store all individual land type data for this ownership and region if lt = "All_land"
if (scen_sheets[i] %in% area_sheets) {
oind = which(area_sheets == scen_sheets[i])
if (scen_sheets[i] == "Area") {
startcol = 5
} else {
startcol = 6
}
# need per management area in usable data frame for per area calcs
if (scen_sheets[i] == "Managed_area") {
temp_man_df = scen_df_list[[i]][, 2:(ncol(scen_df_list[[i]])-1)]
temp_man_df = temp_man_df[temp_man_df$Region != "All_region" & temp_man_df$Land_Type != "All_land" & temp_man_df$Ownership != "All_own",]
if (lt_lab == "All_land") {
temp_man_df = temp_man_df[temp_man_df$Region != "Ocean",]
if (nrow(temp_man_df) > 0) {
temp_df = aggregate(. ~ Region + Ownership + Management, data=temp_man_df[,-which(names(temp_man_df) == "Land_Type")], FUN=sum, na.rm = TRUE)
temp_df$Land_Type = "All_land"
temp_man_df = temp_df[,c("Region", "Land_Type", "Ownership", "Management", names(temp_man_df)[(startcol-1):ncol(temp_man_df)])]
}
} else {
temp_man_df = temp_man_df[temp_man_df$Land_Type == lt_lab,]
}
if (reg_lab == "All_region") {
if (nrow(temp_man_df) > 0) {
temp_df = aggregate(. ~ Land_Type + Ownership + Management, data=temp_man_df[,-which(names(temp_man_df) == "Region")], FUN=sum, na.rm = TRUE)
temp_df$Region = "All_region"
temp_man_df = temp_df[,c("Region", "Land_Type", "Ownership", "Management", names(temp_man_df)[(startcol-1):ncol(temp_man_df)])]
}
} else {
temp_man_df = temp_man_df[temp_man_df$Region == reg_lab,]
}
if (own_lab == "All_own") {
if (nrow(temp_man_df) > 0) {
temp_df = aggregate(. ~ Region + Land_Type + Management, data=temp_man_df[,-which(names(temp_man_df) == "Ownership")], FUN=sum, na.rm = TRUE)
temp_df$Ownership = "All_own"
temp_man_df = temp_df[,c("Region", "Land_Type", "Ownership", "Management", names(temp_man_df)[(startcol-1):ncol(temp_man_df)])]
}
} else {
temp_man_df = temp_man_df[temp_man_df$Ownership == own_lab,]
}
# do not add anything to man_df if there are no data
if (nrow(temp_man_df) > 0) {
# need to reshape temp_man_df so that there is only one value column, and order it
temp_man_df = melt(temp_man_df, variable.name = "Year", id.vars = names(temp_man_df)[1:4])
names(temp_man_df)[ncol(temp_man_df)] = "Value"
if (units_ha) { temp_man_df$Units = "ha"
} else { temp_man_df$Units = "ac" }
temp_man_df$Scenario = scen_lnames[s]
temp_man_df = temp_man_df[,c("Scenario", "Region", "Land_Type", "Ownership", "Management", "Year", "Units", "Value")]
temp_man_df = na.omit(temp_man_df[order(c(temp_man_df$Region, temp_man_df$Land_Type, temp_man_df$Ownership, temp_man_df$Management, temp_man_df$Year)),])