11
22utils :: globalVariables(c(
3- " ..aboveGroundColumns"
3+ " sw" , " AG" , " SoftwoodAG" , " HardwoodAG" , " SoftwoodAGB" , " HardwoodAGB" ,
4+ " SoftwoodRootB" , " HardwoodRootB" , " SoftwoodRootProp" , " HardwoodRootProp"
45))
56
67# ' `calcRootC`
78# '
89# ' `calcRootC` calculates the mass of carbon in roots pools from above ground pools
910# '
10- # ' @param aboveGroundC data.table with mass of carbon (tonnes/ha) in the Merch, Foliage and Other pools
11- # ' @param sw_hw a boolean vector indicating if the cohort is softwood (0) or hardwood (1)
11+ # ' @param aboveGroundC data.table of above ground biomass with the mass of carbon (tonnes/ha) in each pool.
12+ # ' Columns can either be `sw` (Softwood = TRUE, hardwood = FALSE), `Merch`, `Foliage` and `Other`,
13+ # ' or `SoftwoodMerch`, `HardwoodMerch`, `SoftwoodFoliage`, `HardwoodFoliage`, `SoftwoodOther`, and `HardwoodOther`.
1214# ' @param a_sw "a" value for softwood root biomass
1315# ' @param b_sw "b" value for softwood root biomass
1416# ' @param a_hw "a" value for hardwood root biomass
@@ -27,47 +29,65 @@ utils::globalVariables(c(
2729# ' @returns data.table with mass of carbon (tonnes/ha) in coarseRoots and fineRoots pools.
2830# ' @export
2931# '
30- calcRootC <- function (aboveGroundC , sw_hw ,
32+ calcRootC <- function (aboveGroundC ,
3133 a_sw = 0.222 , b_sw = 1 ,
3234 a_hw = 1.576 , b_hw = 0.615 ,
3335 a_frp = 0.072 , b_frp = 0.354 , c_frp = - 0.060212 ,
3436 biomassToCarbonRate = 0.5 ){
3537
36- aboveGroundColumns <- c(" Merch" , " Foliage" , " Other" )
38+ # Choose column set
39+ AGcols <- list (
40+ c(" SoftwoodMerch" , " HardwoodMerch" , " SoftwoodFoliage" , " HardwoodFoliage" , " SoftwoodOther" , " HardwoodOther" ),
41+ c(" sw" , " Merch" , " Foliage" , " Other" )
42+ )
3743
38- if (! all(aboveGroundColumns %in% names(aboveGroundC ))) {
39- stop(" aboveGroundC needs the columns: " , paste(aboveGroundColumns , collapse = " , " ))
40- }
41- aboveGroundC <- as.data.table(aboveGroundC )
44+ whichCols <- which(sapply(AGcols , function (cols ) all(tolower(cols ) %in% tolower(names(aboveGroundC )))))
45+ if (length(whichCols ) == 0 ) stop(
46+ " aboveGroundC needs one of these column sets:\n - " ,
47+ paste(sapply(AGcols , function (AGcol ) paste(shQuote(AGcol ), collapse = " , " )),
48+ collapse = " \n - " ))
49+
50+ AGcols <- AGcols [[whichCols [[1 ]]]]
51+ aboveGroundC <- data.table :: as.data.table(aboveGroundC )
52+ data.table :: setnames(aboveGroundC , tolower(AGcols ), AGcols , skip_absent = TRUE )
53+ aboveGroundC <- aboveGroundC [, .SD , .SDcols = AGcols ]
4254
4355 # Calculate the total above ground mass of carbon
44- totAGC <- rowSums( aboveGroundC [, ..aboveGroundColumns ])
56+ if ( whichCols == 1 ){
4557
46- # Convert Mg/ha of Carbon to Mg/ha of biomass
47- totAGB <- totAGC / biomassToCarbonRate
58+ aboveGroundC [, SoftwoodAG : = rowSums( aboveGroundC [, .( SoftwoodMerch , SoftwoodFoliage , SoftwoodOther )])]
59+ aboveGroundC [, HardwoodAG : = rowSums( aboveGroundC [, .( HardwoodMerch , HardwoodFoliage , HardwoodOther )])]
4860
49- # Calculate root total biomass
50- if (! all(sw_hw %in% c(1 ,0 ))) {
51- stop(" sw_hw needs to be a boolean vector" )
61+ }else {
62+
63+ if (! is.logical(aboveGroundC $ sw )) stop(" aboveGroundC 'sw' column must be logical" )
64+
65+ aboveGroundC [, AG : = rowSums(aboveGroundC [, .(Merch , Foliage , Other )])]
66+ aboveGroundC [, SoftwoodAG : = data.table :: fifelse( sw , AG , 0 )]
67+ aboveGroundC [, HardwoodAG : = data.table :: fifelse(! sw , AG , 0 )]
68+ aboveGroundC [, AG : = NULL ]
5269 }
5370
54- rootTotBiom <- ifelse(sw_hw == 0 ,
55- a_sw * totAGB ^ b_sw ,
56- a_hw * totAGB ^ b_hw )
71+ # Convert Mg/ha of Carbon to Mg/ha of biomass
72+ aboveGroundC [, SoftwoodAGB : = SoftwoodAG / biomassToCarbonRate ]
73+ aboveGroundC [, HardwoodAGB : = HardwoodAG / biomassToCarbonRate ]
74+
75+ # Calculate root total biomass
76+ aboveGroundC [, SoftwoodRootB : = a_sw * SoftwoodAGB ^ b_sw ]
77+ aboveGroundC [, HardwoodRootB : = a_hw * HardwoodAGB ^ b_hw ]
5778
5879 # Calculate the proportion of fine roots
59- fineRootProp <- a_frp + b_frp * exp(c_frp * rootTotBiom )
80+ aboveGroundC [, SoftwoodRootProp : = a_frp + b_frp * exp(c_frp * SoftwoodRootB )]
81+ aboveGroundC [, HardwoodRootProp : = a_frp + b_frp * exp(c_frp * HardwoodRootB )]
6082
83+ # Calculate tonnes/ha of carbon
84+ aboveGroundC [, SoftwoodCoarseRoots : = biomassToCarbonRate * SoftwoodRootB * (1 - SoftwoodRootProp )]
85+ aboveGroundC [, SoftwoodFineRoots : = biomassToCarbonRate * SoftwoodRootB * SoftwoodRootProp ]
86+ aboveGroundC [, HardwoodCoarseRoots : = biomassToCarbonRate * HardwoodRootB * (1 - HardwoodRootProp )]
87+ aboveGroundC [, HardwoodFineRoots : = biomassToCarbonRate * HardwoodRootB * HardwoodRootProp ]
6188
62- # Calculate the proportion of fine roots
63- rootBiom <- data.table(
64- coarseRoots = rootTotBiom * (1 - fineRootProp ),
65- fineRoots = rootTotBiom * fineRootProp
66- )
89+ return (aboveGroundC [, .(SoftwoodCoarseRoots , HardwoodCoarseRoots , SoftwoodFineRoots , HardwoodFineRoots )])
90+ }
6791
68- # Reconvert into tonnes/ha of carbon
69- rootC <- rootBiom * biomassToCarbonRate
7092
71- return (rootC )
7293
73- }
0 commit comments