-
Notifications
You must be signed in to change notification settings - Fork 2
/
figures.R
677 lines (590 loc) · 31.1 KB
/
figures.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
## Figure for RIA runs -------------------------------------------------------
Rversion <- gsub(".+(4..).+", "\\1", R.version.string)
rlib <- file.path("R", Rversion)
options(repos = c(CRAN = "https://cloud.r-project.org"))
if (!dir.exists(rlib)) dir.create(rlib, recursive = TRUE); .libPaths(rlib, include.site = FALSE)
### In this section, only load the minimum of packages (Require, SpaDES.install) so all packages can be installed with
# correct version numbering. If we load a package too early and it is an older version that what may be required by
# a module, then we get an inconsistency
if (!require("remotes")) {
install.packages("remotes")
}
remotes::install_github("PredictiveEcology/Require@f2c791eb05fb0ad99b278619198ef925f85cbb9d")
library(Require)
Require(c("PredictiveEcology/SpaDES.project@transition"), require = FALSE)
# library(Require)
# Require("magrittr") # this is needed to use "%>%" below
# Require("SpaDES.core")
#install_github("PredictiveEcology/CBMutils@development")
#load_all("~/GitHub/PredictiveEcology/CBMutils")
#Require("PredictiveEcology/CBMutils (>= 0.0.6)")
install.packages("devtools")
library("devtools")
devtools::load_all("C:/Celine/github/CBMutils")
Require(c("SpaDES.core (>=1.1.0)", "SpaDES.tools (>= 1.0.0)",
"googledrive", 'RCurl', 'XML', "stars", "shiny", "data.table"),
require = FALSE, # call `require` only on this package (same as `library`)
verbose = 1)
library(SpaDES.core)
options("reproducible.useRequire" = TRUE)
library(data.table)
library(raster)
library(ggplot2)
# read-in all the results from the paper simulations
RIAfriRuns <- loadSimList("C:/Celine/github/spadesCBM_RIA/outputs/FRI/RIAfriRuns.qs")
RIApresentDayRuns <- loadSimList("C:/Celine/github/spadesCBM_RIA/outputs/presentDay/RIApresentDayRuns.qs")
RIAharvest1Runs <- loadSimList("C:/Celine/github/spadesCBM_RIA/outputs/harvest1/RIAharvest1Runs.qs")
RIAharvest2Runs <- loadSimList("C:/Celine/github/spadesCBM_RIA/outputs/harvest2/RIAharvest2Runs.qs")
#----------------------------------------------------------------------------
# - raster plotting function for paper -----------------------------------------
# the CBMutils::spatialPlot function plot rasters directly, but does not return
# rasters. Here I modify it to return rasters for more flexibility in presenting
# results
spatialRaster <- function(pixelkeep, cbmPools, poolsToPlot, years, masterRaster) {
cbmPools[is.na(cbmPools)] <- 0
colnames(cbmPools)[c(1,3,4)] <- c("simYear", "pixelGroup", "age")
# totalCarbon
if ("totalCarbon" %in% poolsToPlot) {
totalCarbon <- apply(cbmPools[, SoftwoodMerch:HardwoodBranchSnag], 1, "sum")
cbmPools <- cbind(cbmPools, totalCarbon)CBM_spec
}
## Add AG and BG options here
if ("aboveGround" %in% poolsToPlot) {
colsAG <- c("SoftwoodMerch", "SoftwoodFoliage", "SoftwoodOther",
"HardwoodMerch", "HardwoodFoliage", "HardwoodOther",
"SoftwoodStemSnag", "SoftwoodBranchSnag",
"HardwoodStemSnag", "HardwoodBranchSnag",
"AboveGroundVeryFastSoil", "AboveGroundFastSoil",
"AboveGroundSlowSoil")
aboveGround <- apply(cbmPools[, ..colsAG], 1, "sum")
cbmPools <- cbind(cbmPools, aboveGround)
}
## belowGround
if ("belowGround" %in% poolsToPlot) {
colsBG <- c("SoftwoodCoarseRoots", "SoftwoodFineRoots",
"HardwoodCoarseRoots", "HardwoodFineRoots",
"BelowGroundVeryFastSoil",
"BelowGroundFastSoil", "MediumSoil",
"BelowGroundSlowSoil")
belowGround <- apply(cbmPools[, ..colsBG], 1, "sum")
cbmPools <- cbind(cbmPools, belowGround)
}
if (any(!poolsToPlot %in% colnames(cbmPools))) {
stop("The carbon pool you specified for plotting is not contained in the pool definitions")
}
cbmPools <- as.data.table( cbmPools)
#Build rasters for every year and pool
carbonStacks <- vector(mode = "list", length = length(poolsToPlot))
names(carbonStacks) <- poolsToPlot
for (pool in poolsToPlot) {
carbonStacks[[pool]] <- lapply(years, FUN = function(x, poolsDT = cbmPools,
var = pool,
pixelKeep = pixelkeep) {
poolsDT <- poolsDT[order(pixelGroup)] %>% #order by stand index
.[simYear == x, .(pixelGroup, "var" = get(pool))] #subset by year
#subset pixelKeep
colsToKeep <- c("pixelIndex", paste0("pixelGroup", x))
pixelKeep <- pixelKeep[, colsToKeep, with = FALSE] %>%
setnames(., c("pixelIndex", "pixelGroup"))
# with=FALSE tells data.table colsToKeep isn't a column name until here it
# is ok...then in 1993 - an extra line gets added from the merge below
# Keep <- poolsDT[pixelKeep, on = c("pixelGroup")]
pixelKeep <- merge(pixelKeep, poolsDT, by = "pixelGroup", all.x = TRUE) %>% #join with pixelKeep
.[order(pixelIndex)] #order by rowOrder for raster prep
#pixelKeep <- pixelKeep[poolsDT, on = c("pixelGroup")] %>% #join with pixelKeep
pixels <- getValues(masterRaster)
plotMaster <- raster(masterRaster)
plotMaster[] <- 0
plotMaster[pixelKeep$pixelIndex] <- pixelKeep$var
# masterRaster[masterRaster == 0] <- NA #Species has zeroes instead of NA. Revisit if masterRaster changes
# masterRaster[!is.na(masterRaster)] <- pixelKeep$var
#name will begin with x if no character assigned
return(plotMaster)
})
}
names(carbonStacks) <- paste0(poolsToPlot)
unlist(carbonStacks)
return(carbonStacks)
}
# END raster plotting function for paper -----------------------------------------
## make rasters, plot rasters and save plots ----------------------
FRIresultRasters <- spatialRaster(
pixelkeep = RIAfriRuns$pixelKeep,
cbmPools = RIAfriRuns$cbmPools,
poolsToPlot = c("totalCarbon","aboveGround"),
years = c(2020, 2540),
masterRaster = RIAfriRuns$masterRaster)
presentDayResultRasters <- spatialRaster(
pixelkeep = RIApresentDayRuns$pixelKeep,
cbmPools = RIApresentDayRuns$cbmPools,
poolsToPlot = c("totalCarbon","aboveGround"),
years = c(1985, 2015),
masterRaster = RIApresentDayRuns$masterRaster)
harv1baseResultRasters <- spatialRaster(
pixelkeep = RIAharvest1Runs$pixelKeep,
cbmPools = RIAharvest1Runs$cbmPools,
poolsToPlot = c("totalCarbon","aboveGround"),
years = c(2020, 2099),
masterRaster = RIAharvest1Runs$masterRaster)
harv2lessResultRasters <- spatialRaster(
pixelkeep = RIAharvest2Runs$pixelKeep,
cbmPools = RIAharvest2Runs$cbmPools,
poolsToPlot = c("totalCarbon","aboveGround"),
years = c(2020, 2099),
masterRaster = RIAharvest2Runs$masterRaster)
## Total C raster figures
## start + present day end
dev()
clearPlot()
Plot(FRIresultRasters$totalCarbon[[1]], title = "a) C-holding capacity scenario
Total Carbon t/ha C")
Plot(presentDayResultRasters$totalCarbon[[2]], title = "b) Present day scenario (start - 1985)
Total Carbon t/ha C")
Plot(harv1baseResultRasters$totalCarbon[[1]], title = "c) Base harvest scenario (start - 2020)
Total Carbon t/ha C")
Plot(harv2lessResultRasters$totalCarbon[[1]], title = "d) Less harvest scenario (start - 2020)
Total Carbon t/ha C")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/totCstartFor3presenDayEnd", type = "png")
# all end of sims
clearPlot()
Plot(FRIresultRasters$totalCarbon[[2]], title = "a) C-holding capacity scenario")
Plot(presentDayResultRasters$totalCarbon[[2]], title = "b) Present day scenario")
Plot(harv1baseResultRasters$totalCarbon[[2]], title = "c) Base harvest scenario")
Plot(harv2lessResultRasters$totalCarbon[[2]], title = "d) Less harvest scenario")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/figure2", type = "png")
#### ALL END SIMS PLAYING WITH COLOURS FOR 3RD SUBMISSION
# all end of sims
dev()
clearPlot()
Plot(FRIresultRasters$totalCarbon[[2]],
col = c("blue","yellow","red"),
speedup = 0.0005,
title = "a) C-holding capacity scenario (405.5 MtC)")
Plot(presentDayResultRasters$totalCarbon[[2]],
col = c("blue","yellow","red"),
speedup = 0.0005,
title = "b) Present day scenario (477.2 MtC)")
Plot(harv1baseResultRasters$totalCarbon[[2]],
col = c("blue","yellow","red"),
speedup = 0.0005,
title = "c) Base harvest scenario (367.3 MtC)")
Plot(harv2lessResultRasters$totalCarbon[[2]],
col = c("blue","yellow","red"),
speedup = 0.0005,
title = "d) Less harvest scenario (400.9 MtC)")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/figure3_sub3speedupLow", type = "tiff")
#### Trying the differences between present day, harv1, harv2 and c-hold
theNAs <- is.na(RIAfriRuns$masterRaster[])
presentDayDiff <- presentDayResultRasters$totalCarbon[[2]]-FRIresultRasters$totalCarbon[[2]]
presentDayDiff[theNAs] <- NA
harv1diff <- harv1baseResultRasters$totalCarbon[[2]]-FRIresultRasters$totalCarbon[[2]]
harv1diff[theNAs] <- NA
harv2diff <- harv2lessResultRasters$totalCarbon[[2]]-FRIresultRasters$totalCarbon[[2]]
harv2diff[theNAs] <- NA
clearPlot()
Plot(presentDayDiff,
cols = c("PRGn"),
title = "a) Present Day minus C-holding capacity",
arr = c(3,1))
Plot(harv1diff,
cols = c("PRGn"),
title = "b) Base harvest minus C-holding capacity")
Plot(harv2diff,
cols = c("PRGn"),
title = "c) Less harvest minus C-holding capacity")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/NewFigure2_submission3", type = "tiff")
## aboveGround raster figures
## start + present day end
clearPlot()
Plot(FRIresultRasters$aboveGround[[1]], title = "a) Carrying capacity scenario (year 2020)
Above Ground Carbon t/ha C")
Plot(presentDayResultRasters$aboveGround[[2]], title = "b) Present scenario (year 2015)
Above Ground Carbon t/ha C")
Plot(harv1baseResultRasters$aboveGround[[1]], title = "c) Base harvest scenario (year 2020)
Above Ground Carbon t/ha C")
Plot(harv2lessResultRasters$aboveGround[[1]], title = "d) Less harvest scenario (year 2020)
Above Ground Carbon t/ha C")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/AGstartFor3presenDayEnd", type = "png")
# all end of sims
clearPlot()
Plot(FRIresultRasters$aboveGround[[2]], title = "a) Carrying capacity scenario (year 2540)
Above Ground Carbon t/ha C")
Plot(presentDayResultRasters$aboveGround[[2]], title = "b) Present scenario (year 2015)
Above Ground Carbon t/ha C")
Plot(harv1baseResultRasters$aboveGround[[2]], title = "c) Base harvest scenario (year 2099)
Above Ground Carbon t/ha C")
Plot(harv2lessResultRasters$aboveGround[[2]], title = "d) Less harvest scenario (year 2099)
Above Ground Carbon t/ha C")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/AGend", type = "png")
## Total carbon histograms
FRIresultRasters$totalCarbon[[2]][theNAs] <- NA
presentDayResultRasters$totalCarbon[[2]][theNAs] <- NA
harv1baseResultRasters$totalCarbon[[2]][theNAs] <- NA
harv2lessResultRasters$totalCarbon[[2]][theNAs] <- NA
cHoldVals <- values(FRIresultRasters$totalCarbon[[2]])
presentDayVals <- values(presentDayResultRasters$totalCarbon[[2]])
harv1Vals <- values(harv1baseResultRasters$totalCarbon[[2]])
harv2Vals <- values(harv2lessResultRasters$totalCarbon[[2]])
tChaValues <- c(cHoldVals[!is.na(cHoldVals)],
presentDayVals[!is.na(presentDayVals)],
harv1Vals[!is.na(harv1Vals)],
harv2Vals[!is.na(harv2Vals)])
groups <- c(rep("C-holding capacity", length(cHoldVals[!is.na(cHoldVals)])),
rep("Present Day", length(presentDayVals[!is.na(presentDayVals)])),
rep("Base Harvest", length(harv1Vals[!is.na(harv1Vals)])),
rep("Less Harvest", length(harv2Vals[!is.na(harv2Vals)])))
#preDf <- cbind(tChaValues,groups)
df <- data.frame(tChaValues,groups)
dev()
clearPlot()
#ggplot(df, aes(x=tChaValues, fill = groups, stat = "count")) + geom_histogram()
ggplot(df, aes(tChaValues, colour = groups)) +
geom_density(lwd = 1.2, linetype = 1, adjust = 2.0) +
labs(title="Distribution of C across the landscape",x="tC/ha per pixel", y = "Density") +
theme(plot.title=element_text(hjust=0.5))
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/rasters/Figure2", type = "tiff")
cHoldTothist <- qplot(cHoldVals[!is.na(cHoldVals)], geom = "density",
main = "a) Carrying capacity scenario",
xlab = "t/ha per pixel")
presentDayTothist <- qplot(presentDayVals[!is.na(presentDayVals)], geom = "density",
main = "b) Present Day (year 2015)",
xlab = "t/ha per pixel")
Plot(cHoldTothist)
Plot(), presentDayTothist)
## make rasters, plot rasters and save plots ----------------------
## plot age class distributions and save plots ----------------------
### Age class distributions
#
# FRIageDist2540hist <- hist(RIAfriRuns$spatialDT$ages, plot=FALSE)
# FRIageDist2020hist <- hist(RIAfriRuns$allPixDT[!is.na(ages),]$ages, plot=FALSE)
# presentDayAgeDist2015hist <- hist(RIApresentDayRuns$spatialDT$ages, plot=FALSE)
# presentDayAgeDist1985hist <- hist(RIApresentDayRuns$allPixDT[!is.na(ages),]$ages, plot=FALSE)
# harv1ageDist2099hist <- hist(RIAharvest1Runs$spatialDT$ages, plot=FALSE)
# harv1ageDist2020hist <- hist(RIAharvest1Runs$allPixDT[!is.na(ages),]$ages, plot=FALSE)
# harv2ageDist2099hist <- hist(RIAharvest2Runs$spatialDT$ages, plot=FALSE)
# harv2ageDist2020hist <- hist(RIAharvest2Runs$allPixDT[!is.na(ages),]$ages, plot=FALSE)
#
# # end 4 scenarios
# clearPlot()
# Plot(FRIageDist2540hist,presentDayAgeDist2015hist,harv1ageDist2099hist,harv2ageDist2099hist)
# Plot(FRIageDist2540hist, title = "a) Carrying capacity scenario year 2540",
# xlab = "Ages")
# Plot(presentDayAgeDist2015hist, title = "b) Present scenario year 2015",
# xlab = "Ages", addTo = TRUE)
# Plot(harv1ageDist2099hist, title = "c) Base harvest scenario year 2099",
# xlab = "Ages", addTo = TRUE)
# Plot(harv2ageDist2099hist, title = "d) Less harvest scenario year 2099",
# xlab = "Ages", addTo = TRUE)
#
# savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/endSimsAgeClassDists", type = "png")
# all scenario start
#
# main = "a) Carrying capacity scenario year 2540",
# xlab = "Ages", plot = FALSE)
# FRIageDist2020hist <- hist(RIAfriRuns$allPixDT[!is.na(ages),]$ages,
# main = "a) Carrying capacity scenario year 2020",
# xlab = "Ages")
# presentDayAgeDist2015hist <- hist(RIApresentDayRuns$spatialDT$ages,
# main = "b) Present scenario year 2015",
# xlab = "Ages")
# presentDayAgeDist1985hist <- hist(RIApresentDayRuns$allPixDT[!is.na(ages),]$ages,
# main = "b) Present scenario year 1985",
# xlab = "Ages")
# harv1ageDist2099hist <- hist(RIAharvest1Runs$spatialDT$ages,
# main = "c) Base harvest scenario year 2099",
# xlab = "Ages")
# harv1ageDist2020hist <- hist(RIAharvest1Runs$allPixDT[!is.na(ages),]$ages,
# main = "c) Base harvest scenario year 2020",
# xlab = "Ages")
# harv2ageDist2099hist <- hist(RIAharvest2Runs$spatialDT$ages,
# main = "d) Less harvest scenario year 2099",
# xlab = "Ages")
# harv2ageDist2020hist <- hist(RIAharvest2Runs$allPixDT[!is.na(ages),]$ages,
# main = "d) Less harvest scenario year 2020",
# xlab = "Ages")
FRIageDist2540hist <- qplot(RIAfriRuns$spatialDT$ages, geom = "histogram",
main = "a) Carrying capacity scenario year 2540",
xlab = "Ages",
ylab = "Frequency")
FRIageDist2020hist <- qplot(RIAfriRuns$allPixDT[!is.na(ages),]$ages, geom = "histogram",
main = "c) Initialized landscape year 2020",
xlab = "Ages")
clearPlot()
Plot(FRIageDist2540hist, presentDayAgeDist1985hist)
, FRIageDist2020hist)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsFRI", type = "png")
presentDayAgeDist2015hist <- qplot(RIApresentDayRuns$spatialDT$ages, geom = "histogram",
main = "b) Present Day landscape (2015)",
xlab = "Ages",
ylab = "Frequency")
presentDayAgeDist1985hist <- qplot(RIApresentDayRuns$allPixDT[!is.na(ages),]$ages,
geom = "histogram",
main = "b) Initialized scenario year 1985",
xlab = "Ages")
clearPlot()
Plot(presentDayAgeDist1985hist, presentDayAgeDist2015hist)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsPresentDay", type = "png")
harv1ageDist2099hist <- qplot(RIAharvest1Runs$spatialDT$ages, geom = "histogram",
main = "c) Base harvest scenario year 2099",
xlab = "Ages")
harv1ageDist2020hist <- qplot(RIAharvest1Runs$allPixDT[!is.na(ages),]$ages,
geom = "histogram",
main = "c) Base harvest scenario year 2020",
xlab = "Ages",
ylab = "Frequency")
clearPlot()
Plot(harv1ageDist2020hist, harv1ageDist2099hist)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsHarv1", type = "png")
harv2ageDist2099hist <- qplot(RIAharvest2Runs$spatialDT$ages, geom = "histogram",
main = "d) Less harvest scenario year 2099",
xlab = "Ages")
harv2ageDist2020hist <- qplot(RIAharvest2Runs$allPixDT[!is.na(ages),]$ages,
geom = "histogram",
main = "d) Less harvest scenario year 2020",
xlab = "Ages")
clearPlot()
Plot(harv2ageDist2020hist, harv2ageDist2099hist)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsHarv2", type = "png")
clearPlot()
Plot(harv1ageDist2099hist, harv2ageDist2099hist)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsBothHarv", type = "png")
clearPlot()
Plot(FRIageDist2540hist)
Plot(presentDayAgeDist2015hist)
Plot(harv1ageDist2099hist, addTo = TRUE)
Plot(harv2ageDist2099hist, addTo = TRUE)
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/AgeClassDistsEndAll", type = "png")
# for discussion Figure 3
FRIageDist2540hist <- qplot(RIAfriRuns$spatialDT$ages, geom = "histogram",
xlab = "Ages",
ylab = "Frequency")
FRIageDist2020hist <- qplot(RIAfriRuns$allPixDT[!is.na(ages),]$ages, geom = "histogram",
xlab = "Ages",
ylab = "Frequency")
# presentDayAgeDist1985hist <- qplot(RIApresentDayRuns$allPixDT[!is.na(ages),]$ages,
# geom = "histogram",
# xlab = "Ages")
presentDayAgeDist2015hist <- qplot(RIApresentDayRuns$spatialDT$ages, geom = "histogram",
#main = "b) Present Day landscape (2015)",
xlab = "Ages",
ylab = "Frequency")
dev()
clearPlot()
Plot(FRIageDist2540hist, title = "a) C-holding capacity - 520 year simulation")
Plot(presentDayAgeDist2015hist, addTo = TRUE, title = "b) Present Day year 2015")
Plot(FRIageDist2020hist, addTo = TRUE, title = "c) Initialized landscape year 2020")
savePlot(filename = "C:/Celine/github/spadesCBM_RIA/results/ageClassDists/figure3", type = "png")
## Note that the harv1 and harv2 are bi-modal
## END plot age class distributions and save plots ----------------------
## function to sum carbon for totalCarbon or aboveGround or belowGround -------------
calcC <- function(cbmPools, poolToSum, masterRaster){
#targetPool <- poolToSum
# year <- time(RIApresentDayRuns)
# cbmPools <- RIApresentDayRuns$cbmPools
# masterRaster <- RIApresentDayRuns$masterRaster
# calculate total carbon by pixelGroup
if ("totalCarbon" %in% poolToSum) {
targetPool <- apply(cbmPools[, SoftwoodMerch:HardwoodBranchSnag], 1, "sum")
cbmPools <- cbind(cbmPools, targetPool)
}
## Add AG and BG options here
if ("aboveGround" %in% poolToSum) {
colsAG <- c("SoftwoodMerch", "SoftwoodFoliage", "SoftwoodOther",
"HardwoodMerch", "HardwoodFoliage", "HardwoodOther",
"SoftwoodStemSnag", "SoftwoodBranchSnag",
"HardwoodStemSnag", "HardwoodBranchSnag",
"AboveGroundVeryFastSoil", "AboveGroundFastSoil",
"AboveGroundSlowSoil")
targetPool <- apply(cbmPools[, ..colsAG], 1, "sum")
cbmPools <- cbind(cbmPools, targetPool)
}
## belowGround
if ("belowGround" %in% poolToSum) {
colsBG <- c("SoftwoodCoarseRoots", "SoftwoodFineRoots",
"HardwoodCoarseRoots", "HardwoodFineRoots",
"BelowGroundVeryFastSoil",
"BelowGroundFastSoil", "MediumSoil",
"BelowGroundSlowSoil")
targetPool <- apply(cbmPools[, ..colsBG], 1, "sum")
cbmPools <- cbind(cbmPools, targetPool)
}
sumColsOnly <- cbmPools[,.(simYear,pixelCount, pixelGroup, targetPool)]
## check that all is good
sumColsOnly[,sum(pixelCount), by=simYear]
# simYear V1
# 1: 1985 3112425
# 2: 1990 3112425
# 3: 1995 3112425
# 4: 2000 3112425
# 5: 2005 3112425
# 6: 2010 3112425
# 7: 2013 3112425
# 8: 2014 3112425
# 9: 2015 3112425
resInHa <- res(masterRaster)[1]*res(masterRaster)[2]/10000
sumColsOnly[, absCarbon := (pixelCount*resInHa*targetPool)]
landscapeCarbon <- sumColsOnly[,sum(absCarbon)/1000000, by = simYear]
return(landscapeCarbon)
}
## END function to sum carbon----------------------------------------------
## Same function just for summing total carbon -----------------
calcTotalC <- function(cbmPools, masterRaster){
# year <- time(RIApresentDayRuns)
# cbmPools <- RIApresentDayRuns$cbmPools
# masterRaster <- RIApresentDayRuns$masterRaster
# calculate total carbon by pixelGroup
totalCarbon <- apply(cbmPools[, SoftwoodMerch:HardwoodBranchSnag], 1, "sum")
cbmPools <- cbind(cbmPools, totalCarbon)
totColsOnly <- cbmPools[,.(simYear,pixelCount, pixelGroup, totalCarbon)]
## check that all is good
totColsOnly[,sum(pixelCount), by=simYear]
# simYear V1
# 1: 1985 3112425
# 2: 1990 3112425
# 3: 1995 3112425
# 4: 2000 3112425
# 5: 2005 3112425
# 6: 2010 3112425
# 7: 2013 3112425
# 8: 2014 3112425
# 9: 2015 3112425
resInHa <- res(masterRaster)[1]*res(masterRaster)[2]/10000
totColsOnly[, absCarbon := (pixelCount*resInHa*totalCarbon)]
landscapeCarbon <- totColsOnly[,sum(absCarbon)/1000000, by = simYear]
return(landscapeCarbon)
}
## END Same function just for summing total carbon -----------------
# these are in Megatonnes of C
presentDayTotalC <- calcTotalC(cbmPools = RIApresentDayRuns$cbmPools,
masterRaster = RIApresentDayRuns$masterRaster)
presentDayAboveGroundC <- calcC(cbmPools = RIApresentDayRuns$cbmPools,
poolToSum = "aboveGround",
masterRaster = RIApresentDayRuns$masterRaster)
presentDayCresults <- as.data.table(cbind(presentDayTotalC, presentDayAboveGroundC$V1))
setnames(presentDayCresults, names(presentDayCresults),
c("simYear", "TotalC", "AGC"))
presentDayCresults[, scenario := "presentDay"]
#write.csv(presentDayCresults, file = "C:/Celine/github/spadesCBM_RIA/results/presentDayAbsC.csv")
FRITotalC <- calcTotalC(cbmPools = RIAfriRuns$cbmPools,
masterRaster = RIAfriRuns$masterRaster)
FRIAGC <- calcC(cbmPools = RIAfriRuns$cbmPools,
poolToSum = "aboveGround",
masterRaster = RIAfriRuns$masterRaster)
FRICresults <- as.data.table(cbind(FRITotalC,FRIAGC$V1))
setnames(FRICresults, names(FRICresults),
c("simYear", "TotalC", "AGC"))
FRICresults[, scenario := "FRI"]
#write.csv(FRICresults, file = "C:/Celine/github/spadesCBM_RIA/results/FRIabsC.csv")
harv1TotalC <- calcTotalC(cbmPools = RIAharvest1Runs$cbmPools,
masterRaster = RIAharvest1Runs$masterRaster)
harv1AGC <- calcC(cbmPools = RIAharvest1Runs$cbmPools,
poolToSum = "aboveGround",
masterRaster = RIAharvest1Runs$masterRaster)
harv1Cresults <- as.data.table(cbind(harv1TotalC,harv1AGC$V1))
setnames(harv1Cresults, names(harv1Cresults),
c("simYear", "TotalC", "AGC"))
harv1Cresults[, scenario := "harvBase"]
#write.csv(harv1Cresults, file = "C:/Celine/github/spadesCBM_RIA/results/harv1absC.csv")
harv2TotalC <- calcTotalC(cbmPools = RIAharvest2Runs$cbmPools,
masterRaster = RIAharvest2Runs$masterRaster)
harv2AGC <- calcC(cbmPools = RIAharvest2Runs$cbmPools,
poolToSum = "aboveGround",
masterRaster = RIAharvest2Runs$masterRaster)
harv2Cresults <- as.data.table(cbind(harv2TotalC,harv2AGC$V1))
setnames(harv2Cresults, names(harv2Cresults),
c("simYear", "TotalC", "AGC"))
harv2Cresults[, scenario := "harvLess"]
#write.csv(harv2Cresults, file = "C:/Celine/github/spadesCBM_RIA/results/harv2absC.csv")
allSimsC <- as.data.table(rbind(FRICresults, presentDayCresults, harv1Cresults, harv2Cresults))
write.csv(allSimsC, file = "C:/Celine/github/spadesCBM_RIA/results/allSimsC.csv")
#spinupSims
calcSpinupC <- function(spinup, level3DT, pixelKeep, masterRaster){
# calculate total C
spinup <- as.data.table(cbind(level3DT$pixelGroup, spinup))
setnames(spinup,"V1", "pixelGroup")
totalCarbon <- apply(spinup[, SoftwoodMerch:HardwoodBranchSnag], 1, "sum")
spinUp <- cbind(spinup, totalCarbon)
# get the number of pixels per pixelGoup
pixelCount <- pixelKeep[, .N, by = pixelGroup0]
setnames(pixelCount, "pixelGroup0", "pixelGroup")
allspinup <- spinUp[pixelCount, on = "pixelGroup"]
totColsOnly <- allspinup[,.(pixelGroup, totalCarbon, N)]
# get the resolution in ha of each pixel
resInHa <- res(masterRaster)[1]*res(masterRaster)[2]/10000
totColsOnly[, absCarbon := (N*resInHa*totalCarbon)]
# this is in Mega tonnes of carbon
landscapeCarbon <- totColsOnly[,sum(absCarbon)/1000000]
return(landscapeCarbon)
}
presentDaySpinupC <- calcSpinupC(spinup = RIApresentDayRuns$spinupResult,
level3DT = RIApresentDayRuns$level3DT,
pixelKeep = RIApresentDayRuns$pixelKeep,
masterRaster = RIApresentDayRuns$masterRaster)
FRISpinupC <- calcSpinupC(spinup = RIAfriRuns$spinupResult,
level3DT = RIAfriRuns$level3DT,
pixelKeep = RIAfriRuns$pixelKeep,
masterRaster = RIAfriRuns$masterRaster)
harv1SpinupC <- calcSpinupC(spinup = RIAharvest1Runs$spinupResult,
level3DT = RIAharvest1Runs$level3DT,
pixelKeep = RIAharvest1Runs$pixelKeep,
masterRaster = RIAharvest1Runs$masterRaster)
harv2SpinupC <- calcSpinupC(spinup = RIAharvest2Runs$spinupResult,
level3DT = RIAharvest2Runs$level3DT,
pixelKeep = RIAharvest2Runs$pixelKeep,
masterRaster = RIAharvest2Runs$masterRaster)
analyseNPP <- function(NPPDT, cbmPools, masterRaster){
#NPPDT <- RIApresentDayRuns$NPP
# adding the pixelCount for each pixelGroup for each year
#cbmPools <- RIApresentDayRuns$cbmPools
#masterRaster <- RIApresentDayRuns$masterRaster
cbmPools <- cbmPools[,.(simYear, pixelCount, pixelGroup)]
dt1 <- merge.data.table(NPPDT, cbmPools,
by = c("pixelGroup", "simYear"), all.x = TRUE)
avgNPPbyHabyYr <- dt1[, .(avgNPPha = mean(NPP)), by = simYear]
# calculate the absolute carbon update of the landscape for each year
resInHa <- res(masterRaster)[1]*res(masterRaster)[2]/10000
dt1[, absNPP := (pixelCount*resInHa*NPP), by = simYear]
# this is in Mega tonnes of carbon
absNPPbyYr <- dt1[, .(absNPP = sum(absNPP)/1000000), by = simYear]
NPPtable <- as.data.table(cbind(avgNPPbyHabyYr,absNPP=absNPPbyYr$absNPP))
return(NPPtable)
}
FRINPP <- analyseNPP(RIAfriRuns$NPP, RIAfriRuns$cbmPools, RIAfriRuns$masterRaster)
FRINPP[, scenario := "FRI"]
presentDayNPP <- analyseNPP(RIApresentDayRuns$NPP, RIApresentDayRuns$cbmPools, RIApresentDayRuns$masterRaster)
presentDayNPP[, scenario := "presentDay"]
harv1NPP <- analyseNPP(RIAharvest1Runs$NPP, RIAharvest1Runs$cbmPools, RIAharvest1Runs$masterRaster)
harv1NPP[, scenario := "harvBase"]
harv2NPP <- analyseNPP(RIAharvest2Runs$NPP, RIAharvest2Runs$cbmPools, RIAharvest2Runs$masterRaster)
harv2NPP[, scenario := "harvLess"]
simsNPP <- as.data.table(rbind(FRINPP, presentDayNPP, harv1NPP, harv2NPP))
write.csv(simsNPP, file = "C:/Celine/github/spadesCBM_RIA/results/simsNPP.csv")
# below not performed yet
avgNPPabs <- mean(absNPPbyYr$absNPP)
maxNPPbyYr <- max(absNPPbyYr$absNPP)
minNPPbyYr <- min(absNPPbyYr$absNPP)
avgNPPha <- mean(avgNPPbyHabyYr$avgNPPha)
maxNPPha <- max(avgNPPbyHabyYr$avgNPPha)
minNPPha <- min(avgNPPbyHabyYr$avgNPPha)
## products-------------------------------------------------------------
# Units: products and emissions are in tonnes (absolute tonnes, not per ha)
presentDayProducts <- as.data.table(RIApresentDayRuns$emissionsProducts)
presentDayProducts <- presentDayProdcuts[,.(simYear, Products)]
presentDayProducts[, scenario := "presentDay"]
harv1Products <- as.data.table(RIAharvest1Runs$emissionsProducts)
harv1Products <- harv1Products[,.(simYear,Products)]
harv1Products[, scenario := "base"]
harv2Products <- as.data.table(RIAharvest2Runs$emissionsProducts)
harv2Products <- harv2Products[,.(simYear,Products)]
harv2Products[, scenario := "less"]
RIAproducts <- as.data.table(rbind(presentDayProducts, harv1Products, harv2Products))
# No need for this, it is already in tonnes (not per ha not per pixels)
# totNoPixels <- 3112425
# resInHa <- res(RIAharvest1Runs$masterRaster)[1]*res(RIAharvest1Runs$masterRaster)[2]/10000
# totalAreaHa <- totNoPixels*resInHa
# RIAproducts[, abs := (Products*totalAreaHa)]
# With the specific gravity around 1.5, solid wood "substance", or
# lignocellulose as it is commonly called today, weighs around 1500 kg/m3
# 1000kg/tonne
RIAproducts[, m3 := ((Products*1000)/1500)]
write.csv(RIAproducts, file = "C:/Celine/github/spadesCBM_RIA/results/RIAproducts.csv")