@@ -7,8 +7,9 @@ utils::globalVariables(c(
77# '
88# ' `calcRootC` calculates the mass of carbon in roots pools from above ground pools
99# '
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)
10+ # ' @param aboveGroundC data.table of above ground biomass with the mass of carbon (tonnes/ha) in each pool.
11+ # ' Columns can either be `sw` (Softwood = TRUE, hardwood = FALSE), `Merch`, `Foliage` and `Other`,
12+ # ' or `SoftwoodMerch`, `HardwoodMerch`, `SoftwoodFoliage`, `HardwoodFoliage`, `SoftwoodOther`, and `HardwoodOther`.
1213# ' @param a_sw "a" value for softwood root biomass
1314# ' @param b_sw "b" value for softwood root biomass
1415# ' @param a_hw "a" value for hardwood root biomass
@@ -27,47 +28,65 @@ utils::globalVariables(c(
2728# ' @returns data.table with mass of carbon (tonnes/ha) in coarseRoots and fineRoots pools.
2829# ' @export
2930# '
30- calcRootC <- function (aboveGroundC , sw_hw ,
31+ calcRootC <- function (aboveGroundC ,
3132 a_sw = 0.222 , b_sw = 1 ,
3233 a_hw = 1.576 , b_hw = 0.615 ,
3334 a_frp = 0.072 , b_frp = 0.354 , c_frp = - 0.060212 ,
3435 biomassToCarbonRate = 0.5 ){
3536
36- aboveGroundColumns <- c(" Merch" , " Foliage" , " Other" )
37+ # Choose column set
38+ AGcols <- list (
39+ c(" SoftwoodMerch" , " HardwoodMerch" , " SoftwoodFoliage" , " HardwoodFoliage" , " SoftwoodOther" , " HardwoodOther" ),
40+ c(" sw" , " Merch" , " Foliage" , " Other" )
41+ )
3742
38- if (! all(aboveGroundColumns %in% names(aboveGroundC ))) {
39- stop(" aboveGroundC needs the columns: " , paste(aboveGroundColumns , collapse = " , " ))
40- }
41- aboveGroundC <- as.data.table(aboveGroundC )
43+ whichCols <- which(sapply(AGcols , function (cols ) all(tolower(cols ) %in% tolower(names(aboveGroundC )))))
44+ if (length(whichCols ) == 0 ) stop(
45+ " aboveGroundC needs one of these column sets:\n - " ,
46+ paste(sapply(AGcols , function (AGcol ) paste(shQuote(AGcol ), collapse = " , " )),
47+ collapse = " \n - " ))
48+
49+ AGcols <- AGcols [[whichCols [[1 ]]]]
50+ aboveGroundC <- data.table :: as.data.table(aboveGroundC )
51+ data.table :: setnames(aboveGroundC , tolower(AGcols ), AGcols , skip_absent = TRUE )
52+ aboveGroundC <- aboveGroundC [, .SD , .SDcols = AGcols ]
4253
4354 # Calculate the total above ground mass of carbon
44- totAGC <- rowSums( aboveGroundC [, ..aboveGroundColumns ])
55+ if ( whichCols == 1 ){
4556
46- # Convert Mg/ha of Carbon to Mg/ha of biomass
47- totAGB <- totAGC / biomassToCarbonRate
57+ aboveGroundC [, SoftwoodAG : = rowSums( aboveGroundC [, .( SoftwoodMerch , SoftwoodFoliage , SoftwoodOther )])]
58+ aboveGroundC [, HardwoodAG : = rowSums( aboveGroundC [, .( HardwoodMerch , HardwoodFoliage , HardwoodOther )])]
4859
49- # Calculate root total biomass
50- if (! all(sw_hw %in% c(1 ,0 ))) {
51- stop(" sw_hw needs to be a boolean vector" )
60+ }else {
61+
62+ if (! is.logical(aboveGroundC $ sw )) stop(" aboveGroundC 'sw' column must be logical" )
63+
64+ aboveGroundC [, AG : = rowSums(aboveGroundC [, .(Merch , Foliage , Other )])]
65+ aboveGroundC [, SoftwoodAG : = data.table :: fifelse( sw , AG , 0 )]
66+ aboveGroundC [, HardwoodAG : = data.table :: fifelse(! sw , AG , 0 )]
67+ aboveGroundC [, AG : = NULL ]
5268 }
5369
54- rootTotBiom <- ifelse(sw_hw == 0 ,
55- a_sw * totAGB ^ b_sw ,
56- a_hw * totAGB ^ b_hw )
70+ # Convert Mg/ha of Carbon to Mg/ha of biomass
71+ aboveGroundC [, SoftwoodAGB : = SoftwoodAG / biomassToCarbonRate ]
72+ aboveGroundC [, HardwoodAGB : = HardwoodAG / biomassToCarbonRate ]
73+
74+ # Calculate root total biomass
75+ aboveGroundC [, SoftwoodRootB : = a_sw * SoftwoodAGB ^ b_sw ]
76+ aboveGroundC [, HardwoodRootB : = a_hw * HardwoodAGB ^ b_hw ]
5777
5878 # Calculate the proportion of fine roots
59- fineRootProp <- a_frp + b_frp * exp(c_frp * rootTotBiom )
79+ aboveGroundC [, SoftwoodRootProp : = a_frp + b_frp * exp(c_frp * SoftwoodRootB )]
80+ aboveGroundC [, HardwoodRootProp : = a_frp + b_frp * exp(c_frp * HardwoodRootB )]
6081
82+ # Calculate tonnes/ha of carbon
83+ aboveGroundC [, SoftwoodCoarseRoots : = biomassToCarbonRate * SoftwoodRootB * (1 - SoftwoodRootProp )]
84+ aboveGroundC [, SoftwoodFineRoots : = biomassToCarbonRate * SoftwoodRootB * SoftwoodRootProp ]
85+ aboveGroundC [, HardwoodCoarseRoots : = biomassToCarbonRate * HardwoodRootB * (1 - HardwoodRootProp )]
86+ aboveGroundC [, HardwoodFineRoots : = biomassToCarbonRate * HardwoodRootB * HardwoodRootProp ]
6187
62- # Calculate the proportion of fine roots
63- rootBiom <- data.table(
64- coarseRoots = rootTotBiom * (1 - fineRootProp ),
65- fineRoots = rootTotBiom * fineRootProp
66- )
88+ return (aboveGroundC [, .(SoftwoodCoarseRoots , HardwoodCoarseRoots , SoftwoodFineRoots , HardwoodFineRoots )])
89+ }
6790
68- # Reconvert into tonnes/ha of carbon
69- rootC <- rootBiom * biomassToCarbonRate
7091
71- return (rootC )
7292
73- }
0 commit comments