@@ -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
79116simPlotPoolProportions <- 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
0 commit comments