Skip to content

Commit d13a356

Browse files
authored
Merge pull request #85 from PredictiveEcology/CBM4
CBM4 plotting functions added
2 parents 5f0bce4 + bb56827 commit d13a356

12 files changed

Lines changed: 312 additions & 22 deletions

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Description: Implementation of several components of the Carbon Budget Model of
55
Canadian Forest Service (v3).
66
URL:
77
https://github.com/PredictiveEcology/CBMutils
8-
Version: 2.5.3.9002
8+
Version: 2.5.4.9000
99
Authors@R: c(
1010
person("Céline", "Boisvenue", email = "celine.boisvenue@nrcan-rncan.gc.ca", role = c("aut", "cre")),
1111
person("Alex M", "Chubaty", email = "achubaty@for-cast.ca", role = c("aut"), comment = c(ORCID = "0000-0001-7146-8135")),
@@ -37,6 +37,7 @@ Imports:
3737
tidyterra,
3838
withr
3939
Suggests:
40+
CBM4r,
4041
covr,
4142
devtools,
4243
exactextractr,
@@ -48,7 +49,8 @@ Suggests:
4849
testthat
4950
Remotes:
5051
PredictiveEcology/reproducible@development,
51-
PredictiveEcology/SpaDES.core@development
52+
PredictiveEcology/SpaDES.core@development,
53+
PredictiveEcology/CBM4r@main
5254
VignetteBuilder: knitr, rmarkdown
5355
Encoding: UTF-8
5456
Roxygen: list(markdown = TRUE)

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export(boudewynSubsetTables)
1212
export(cTransfersAlluvial)
1313
export(calcC)
1414
export(calcRootC)
15+
export(cbm4MapNPP)
16+
export(cbm4MapTotalCarbon)
17+
export(cbm4PlotEmissionsProducts)
18+
export(cbm4PlotPoolProportions)
1519
export(convertAGB2pools)
1620
export(convertM3biom)
1721
export(cumPoolsCreate)

R/CBM-plots_mapNPP.R

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ mapNPP <- function(rastNPP, year = NULL) {
4646
}
4747

4848

49+
#' CBM4: `mapNPP`
50+
#'
51+
#' @template cbm4_results
52+
#' @param years integer. Year(s) of simulation results.
53+
#' @param yearStart integer. Simulation start year.
54+
#'
55+
#' @inherit mapNPP description return
56+
#' @export
57+
cbm4MapNPP <- function(cbm4_results, years = NULL, yearStart = 1){
58+
59+
if (length(find.package("CBM4r", quiet = TRUE)) == 0) stop("CBM4r package required")
60+
61+
rastNPP <- CBM4r::cbm4_results_raster(
62+
cbm4_results,
63+
timesteps = if (!is.null(years)) years - yearStart + 1,
64+
view_name = "spatial_composite_flux_indicators",
65+
view_column = "Ecosystem Indicators - Productivity - Net Primary Productivity (NPP)"
66+
)
67+
68+
if (is.null(years)) years <- as.numeric(names(rastNPP)) + yearStart - 1
69+
70+
names(rastNPP) <- years
71+
plotList <- lapply(rastNPP, function(rastYear) mapNPP(rastYear, year = names(rastYear)))
72+
names(plotList) <- years
73+
plotList
74+
}
75+
76+
4977
#' `simMapNPP`
5078
#'
5179
#' @template simCBM
@@ -59,12 +87,23 @@ simMapNPP <- function(simCBM, year = NULL, useCache = TRUE){
5987
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
6088
}
6189

62-
spadesCBMdbMapNPP(
63-
simCBM$spadesCBMdb,
64-
masterRaster = simCBM$masterRaster,
65-
year = year,
66-
useCache = useCache
67-
)
90+
if (!is.null(simCBM$CBM4data)){
91+
92+
cbm4MapNPP(
93+
simCBM$CBM4data,
94+
years = year,
95+
yearStart = SpaDES.core::start(simCBM)
96+
)[[1]]
97+
98+
}else{
99+
100+
spadesCBMdbMapNPP(
101+
simCBM$spadesCBMdb,
102+
masterRaster = simCBM$masterRaster,
103+
year = year,
104+
useCache = useCache
105+
)
106+
}
68107
}
69108

70109

R/CBM-plots_mapTotalCarbon.R

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@ mapTotalCarbon <- function(rastTC, year = NULL){
4444
}
4545

4646

47+
#' CBM4: `mapTotalCarbon`
48+
#'
49+
#' @template cbm4_results
50+
#' @param years integer. Year(s) of simulation results.
51+
#' @param yearStart integer. Simulation start year.
52+
#'
53+
#' @inherit mapTotalCarbon description return
54+
#' @export
55+
cbm4MapTotalCarbon <- function(cbm4_results, years = NULL, yearStart = 1){
56+
57+
if (length(find.package("CBM4r", quiet = TRUE)) == 0) stop("CBM4r package required")
58+
59+
rastTC <- CBM4r::cbm4_results_raster(
60+
cbm4_results,
61+
timesteps = if (!is.null(years)) years - yearStart + 1,
62+
view_name = "spatial_composite_pool_indicators",
63+
view_column = "Total Ecosystem"
64+
)
65+
66+
if (is.null(years)) years <- as.numeric(names(rastTC)) + yearStart - 1
67+
68+
names(rastTC) <- years
69+
plotList <- lapply(rastTC, function(rastYear) mapTotalCarbon(rastYear, year = names(rastYear)))
70+
names(plotList) <- years
71+
plotList
72+
}
73+
74+
4775
#' `simMapTotalCarbon`
4876
#'
4977
#' @template simCBM
@@ -57,12 +85,23 @@ simMapTotalCarbon <- function(simCBM, year = NULL, useCache = TRUE){
5785
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
5886
}
5987

60-
spadesCBMdbMapTotalCarbon(
61-
simCBM$spadesCBMdb,
62-
masterRaster = simCBM$masterRaster,
63-
year = year,
64-
useCache = useCache
65-
)
88+
if (!is.null(simCBM$CBM4data)){
89+
90+
cbm4MapTotalCarbon(
91+
simCBM$CBM4data,
92+
years = year,
93+
yearStart = SpaDES.core::start(simCBM)
94+
)[[1]]
95+
96+
}else{
97+
98+
spadesCBMdbMapTotalCarbon(
99+
simCBM$spadesCBMdb,
100+
masterRaster = simCBM$masterRaster,
101+
year = year,
102+
useCache = useCache
103+
)
104+
}
66105
}
67106

68107

R/CBM-plots_plotEmissionsProducts.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,52 @@ plotEmissionsProducts <- function(emissionsProducts) {
3939
}
4040

4141

42+
#' CBM4: `plotEmissionsProducts`
43+
#'
44+
#' @template cbm4_results
45+
#' @param years integer. Year(s) of simulation results.
46+
#' @param yearStart integer. Simulation start year.
47+
#'
48+
#' @inherit mapTotalCarbon description return
49+
#' @export
50+
cbm4PlotEmissionsProducts <- function(cbm4_results, years = NULL, yearStart = 1){
51+
52+
if (length(find.package("CBM4r", quiet = TRUE)) == 0) stop("CBM4r package required")
53+
54+
timesteps <- if (!is.null(years)) years - yearStart + 1
55+
56+
cbm4_results <- CBM4r::cbm4_results_processor(cbm4_results)
57+
58+
cbm4_totals <- merge(
59+
CBM4r::cbm4_results_totals(
60+
cbm4_results,
61+
timesteps = timesteps,
62+
view_name = "composite_flux_indicators",
63+
view_columns = c(
64+
"CH4" = "Emissions - Emissions By Gas - Total CH4",
65+
"CO" = "Emissions - Emissions By Gas - Total CO",
66+
"CO2" = "Emissions - Emissions By Gas - Total CO2"
67+
)),
68+
CBM4r::cbm4_results_totals(
69+
cbm4_results,
70+
timesteps = timesteps,
71+
view_name = "composite_disturbance_indicators",
72+
view_columns = c(
73+
"Products" = "Ecosystem Transfers - Ecosystem to Forest Products - Total Harvest (Biomass + Snags)"
74+
)),
75+
all = TRUE)[, .(
76+
year = timestep + yearStart - 1,
77+
Products = data.table::fcoalesce(Products, 0),
78+
Emissions = CO2 + CH4 + CO,
79+
CO2 = CO2,
80+
CH4 = CH4,
81+
CO = CO
82+
)]
83+
84+
plotEmissionsProducts(cbm4_totals)
85+
}
86+
87+
4288
#' `simPlotEmissionsProducts`
4389
#'
4490
#' @template simCBM
@@ -49,10 +95,19 @@ plotEmissionsProducts <- function(emissionsProducts) {
4995
simPlotEmissionsProducts <- function(simCBM, years = NULL, useCache = TRUE){
5096

5197
if ("emissionsProducts" %in% names(simCBM)){
98+
5299
emissionsProducts <- simCBM$emissionsProducts
53100
if (!is.null(years)) emissionsProducts <- subset(emissionsProducts, year %in% years)
54101
plotEmissionsProducts(emissionsProducts)
55102

103+
}else if (!is.null(simCBM$CBM4data)){
104+
105+
cbm4PlotEmissionsProducts(
106+
simCBM$CBM4data,
107+
years = years,
108+
yearStart = SpaDES.core::start(simCBM)
109+
)
110+
56111
}else{
57112

58113
if (is.null(years)) years <- SpaDES.core::start(simCBM):SpaDES.core::end(simCBM)

R/CBM-plots_plotPoolProportions.R

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plotPoolProportions <- function(pools){
1818

1919
if (!"year" %in% names(pools)) stop("pools requires column 'year'")
2020

21-
if (!identical(names(pools), c("year", "Soil", "BGlive", "AGlive", "Snags"))){
21+
if (!all(c("Soil", "BGlive", "AGlive", "Snags") %in% names(pools))){
2222

2323
if (!is.data.table(pools)) pools <- as.data.table(pools)
2424
pools <- pools[, .(
@@ -32,7 +32,8 @@ plotPoolProportions <- function(pools){
3232
}
3333

3434
poolsSum <- data.table::melt.data.table(
35-
pools, id.vars = "year", variable.name = "pool", value.name = "carbon")
35+
pools, id.vars = "year", measure.vars = c("Soil", "BGlive", "AGlive", "Snags"),
36+
variable.name = "pool", value.name = "carbon")
3637
poolsSum[, proportion := carbon / sum(carbon), by = "year"]
3738

3839
startYear <- min(poolsSum$year[poolsSum$year != 0])
@@ -69,6 +70,42 @@ plotPoolProportions <- function(pools){
6970
}
7071

7172

73+
#' CBM4: `plotPoolProportions`
74+
#'
75+
#' @template cbm4_results
76+
#' @param years integer. Year(s) of simulation results.
77+
#' @param yearStart integer. Simulation start year.
78+
#'
79+
#' @inherit plotPoolProportions description return
80+
#' @export
81+
cbm4PlotPoolProportions <- function(cbm4_results, years = NULL, yearStart = 1){
82+
83+
if (length(find.package("CBM4r", quiet = TRUE)) == 0) stop("CBM4r package required")
84+
85+
timesteps <- if (!is.null(years)) years - yearStart + 1
86+
87+
cbm4_results <- CBM4r::cbm4_results_processor(cbm4_results)
88+
89+
cbm4_totals <- CBM4r::cbm4_results_totals(
90+
cbm4_results,
91+
timesteps = timesteps,
92+
view_name = "pool_indicators")[, .(
93+
year = timestep + yearStart - 1,
94+
Soil = sum(AboveGroundVeryFastSoil, BelowGroundVeryFastSoil,
95+
AboveGroundFastSoil, BelowGroundFastSoil,
96+
AboveGroundSlowSoil, BelowGroundSlowSoil, MediumSoil),
97+
BGlive = sum(SoftwoodCoarseRoots, SoftwoodFineRoots,
98+
HardwoodCoarseRoots, HardwoodFineRoots),
99+
AGlive = sum(SoftwoodMerch, SoftwoodFoliage, SoftwoodOther,
100+
HardwoodMerch, HardwoodFoliage, HardwoodOther),
101+
Snags = sum(SoftwoodStemSnag, SoftwoodBranchSnag,
102+
HardwoodStemSnag, HardwoodBranchSnag)
103+
), by = "timestep"]
104+
105+
plotPoolProportions(cbm4_totals)
106+
}
107+
108+
72109
#' `simPlotPoolProportions`
73110
#'
74111
#' @template simCBM
@@ -78,13 +115,24 @@ plotPoolProportions <- function(pools){
78115
#' @export
79116
simPlotPoolProportions <- function(simCBM, years = NULL, useCache = TRUE){
80117

81-
if (is.null(years)) years <- c(0, SpaDES.core::start(simCBM):SpaDES.core::end(simCBM))
118+
if (!is.null(simCBM$CBM4data)){
82119

83-
spadesCBMdbPlotPoolProportions(
84-
simCBM$spadesCBMdb,
85-
years = years,
86-
useCache = useCache
87-
)
120+
cbm4PlotPoolProportions(
121+
simCBM$CBM4data,
122+
years = years,
123+
yearStart = SpaDES.core::start(simCBM)
124+
)
125+
126+
}else{
127+
128+
if (is.null(years)) years <- c(0, SpaDES.core::start(simCBM):SpaDES.core::end(simCBM))
129+
130+
spadesCBMdbPlotPoolProportions(
131+
simCBM$spadesCBMdb,
132+
years = years,
133+
useCache = useCache
134+
)
135+
}
88136
}
89137

90138

R/CBMutils-package.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@ utils::globalVariables(c(
6464
"StemSnag", "BranchSnag", "CO2", "CH4", "CO", "NO2", "Products"
6565
))
6666

67+
# CBM4 variables
68+
utils::globalVariables(c(
69+
"timestep", "timesteps"
70+
))
71+
72+
73+
6774

man-roxygen/cbm4_results.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#' @param cbm4_results character or `SQLResultsProcessor`.
2+
#' Path to CBM4 spatial datasets directory
3+
#' or a `SQLResultsProcessor` object created with \code{\link[CBM4r]{cbm4_results_processor}}.
4+
NULL

man/cbm4MapNPP.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbm4MapTotalCarbon.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)