Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
4 changes: 4 additions & 0 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ aglu.DIGITS_RES_ENERGY <- 4
aglu.DIGITS_WATER_CONTENT <- 2


# Adding a switch here to use the beforeadjustfile

aglu.USE_BEFORE_ADJUST_FOREST_FILE <- TRUE

#Land leaf names used in the data system for different land types
aglu.PASTURE_NODE_NAMES <- "Pasture"
aglu.FOREST_NODE_NAMES <- "Forest"
Expand Down
50 changes: 34 additions & 16 deletions R/zchunk_LB120.LC_GIS_R_LTgis_Yh_GLU.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,42 @@ module_aglu_LB120.LC_GIS_R_LTgis_Yh_GLU <- function(command, ...) {
mutate(year = as.integer(year)) ->
L120.LC_bm2_R_LT_Yh_GLU


# adding a switch in L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust ----
# The file is used if "TRUE"
# If not, forest yield could be wrong #' module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU
# The file is an output from
# It need to be set to not "TRUE" if breaking out regions
# Then add L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust.csv from output to AGLU/LDS
# For regions with zero forest land but positive production
# Areas can be added in that csv
# scale forest to avoid negative unmanaged forest area which caused issue for yield in Pakistan and African regions
# L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust, pulled from L123.LC_bm2_R_MgdFor_Yh_GLU before managed forest scaling, was used here.
L120.LC_bm2_R_LT_Yh_GLU %>%
left_join(L120.LC_bm2_R_LT_Yh_GLU %>%
spread(Land_Type, value, fill = 0) %>%
left_join(L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust %>% select(-Land_Type),
by = c("GCAM_region_ID", "GLU", "year")) %>%
mutate(nonForScaler =
if_else((Forest - MgdFor) < 0 & Forest > 0,
1 + (Forest - MgdFor)/(Grassland + Shrubland + Pasture), 1),
ForScaler = if_else((Forest - MgdFor) < 0 & Forest > 0, MgdFor/Forest ,1)) %>%
select(GCAM_region_ID, GLU, year, nonForScaler, ForScaler),
by = c("GCAM_region_ID", "GLU", "year") ) %>%
mutate(value = if_else(Land_Type %in% c("Grassland", "Shrubland" , "Pasture"),
value * nonForScaler,
if_else(Land_Type == "Forest", value * ForScaler, value) )) %>%
select(-nonForScaler, -ForScaler) ->
L120.LC_bm2_R_LT_Yh_GLU
if (aglu.USE_BEFORE_ADJUST_FOREST_FILE) {
# scale forest to avoid negative unmanaged forest area which caused issue for yield in Pakistan and African regions
# L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust, pulled from L123.LC_bm2_R_MgdFor_Yh_GLU before managed forest scaling, was used here.

L120.LC_bm2_R_LT_Yh_GLU %>%
left_join(L120.LC_bm2_R_LT_Yh_GLU %>%
spread(Land_Type, value, fill = 0) %>%
left_join(L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust %>% select(-Land_Type),
by = c("GCAM_region_ID", "GLU", "year")) %>%
mutate(nonForScaler =
if_else((Forest - MgdFor) < 0 & Forest > 0,
1 + (Forest - MgdFor)/(Grassland + Shrubland + Pasture), 1),
ForScaler = if_else((Forest - MgdFor) < 0 & Forest > 0, MgdFor/Forest ,1)) %>%
select(GCAM_region_ID, GLU, year, nonForScaler, ForScaler),
by = c("GCAM_region_ID", "GLU", "year") ) %>%
#If the forest scaler exceeds a limit,or if the nonForscaler goes negative then don't do this adjustment. It will lead to cropland errors downstream.
# These errors happen in years other than calibration years in just in one basin, namely NileR.
mutate(ForScaler= if_else(is.nan(ForScaler)|is.na(ForScaler) | ForScaler >= 20 | nonForScaler <1 ,1,ForScaler),
nonForScaler= if_else(is.nan(nonForScaler)|is.na(nonForScaler)| ForScaler >= 20| nonForScaler <1,1,nonForScaler),
value = if_else(Land_Type %in% c("Grassland", "Shrubland" , "Pasture"),
value * nonForScaler,
if_else(Land_Type == "Forest", value * ForScaler, value) )) %>%
select(-nonForScaler, -ForScaler) ->
L120.LC_bm2_R_LT_Yh_GLU
}

# Subset the land types that are not further modified
L120.LC_bm2_R_UrbanLand_Yh_GLU <- filter(L120.LC_bm2_R_LT_Yh_GLU, Land_Type == "UrbanLand")
Expand Down
33 changes: 25 additions & 8 deletions R/zchunk_LB123.LC_R_MgdPastFor_Yh_GLU.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU <- function(command, ...) {
"L123.LC_bm2_R_MgdPast_Yh_GLU",
"L123.For_Prod_bm3_R_Y_GLU",
"L123.For_Yield_m3m2_R_GLU",
"L123.LC_bm2_R_MgdFor_Yh_GLU"))
"L123.LC_bm2_R_MgdFor_Yh_GLU",
"L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust"))
} else if(command == driver.MAKE) {

all_data <- list(...)[[1]]
Expand Down Expand Up @@ -106,9 +107,10 @@ module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU <- function(command, ...) {
mutate(MgdPast = value / pasture_yield) %>%
select(-value, -pasture_yield) %>%
# Match in total pasture land
left_join_error_no_match(L123.LC_bm2_R_Past_Y_GLU,
left_join(L123.LC_bm2_R_Past_Y_GLU,
by = c("GCAM_region_ID", "Land_Type", "GLU", "year")) %>%
# Calculate the percentage of managed to total pasture land
mutate(value= if_else(is.na(value),0,value)) %>%
mutate(frac = MgdPast / value) %>%
replace_na(list(frac = 0)) %>%
# Apply maximum percentage of any region/GLUs pasture that is allowed to be in production (managed)
Expand Down Expand Up @@ -235,15 +237,17 @@ module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU <- function(command, ...) {
# Scale managed forest land based on population in pre-aglu years
mutate(MgdFor = MgdFor * PopRatio) %>%
select(-PopRatio) %>%
filter(year %in% aglu.LAND_COVER_YEARS) ->
L123.LC_bm2_R_MgdFor_Yh_GLU
filter(year %in% aglu.LAND_COVER_YEARS) ->L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust


# Adjust managed forest land: where managed forest is greater than
# assumed maximum percentage of total forest, reduce the managed forest land.
L123.LC_bm2_R_MgdFor_Yh_GLU %>%
L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust %>%
# Match in total forest land
left_join_error_no_match(filter(L120.LC_bm2_R_LT_Yh_GLU, Land_Type == "Forest", year %in% aglu.LAND_COVER_YEARS),
left_join(filter(L120.LC_bm2_R_LT_Yh_GLU, Land_Type == "Forest", year %in% aglu.LAND_COVER_YEARS),
by = c("GCAM_region_ID", "Land_Type", "GLU", "year")) %>%

mutate(value= if_else(is.na(value),0,value)) %>%
# Calculate the fraction of managed to total forest land
mutate(frac = MgdFor / value) %>%
select(-value, -MgdFor) %>%
Expand All @@ -252,8 +256,9 @@ module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU <- function(command, ...) {
# Apply maximum percentage of any region/GLUs forest that is allowed to be in production (managed)
mutate(frac = replace(frac, frac > aglu.MAX_MGDFOR_FRAC, aglu.MAX_MGDFOR_FRAC)) %>%
# Match in total forest land again to calculate the adjusted managed forest land
left_join_error_no_match(filter(L120.LC_bm2_R_LT_Yh_GLU, Land_Type == "Forest", year %in% aglu.LAND_COVER_YEARS),
left_join(filter(L120.LC_bm2_R_LT_Yh_GLU, Land_Type == "Forest", year %in% aglu.LAND_COVER_YEARS),
by = c("GCAM_region_ID", "Land_Type", "GLU", "year")) %>%
mutate(value= if_else(is.na(value),0,value)) %>%
# Recalculate managed forest land, adjusted by assumed maximum portion that can be managed
mutate(value = value * frac) %>%
select(-frac) ->
Expand Down Expand Up @@ -349,7 +354,19 @@ module_aglu_LB123.LC_R_MgdPastFor_Yh_GLU <- function(command, ...) {
same_precursors_as("L123.LC_bm2_R_MgdFor_Yh_GLU") ->
L123.For_Yield_m3m2_R_GLU

return_data(L123.ag_Prod_Mt_R_Past_Y_GLU, L123.ag_Yield_kgm2_R_Past_Y_GLU, L123.LC_bm2_R_MgdPast_Yh_GLU, L123.For_Prod_bm3_R_Y_GLU, L123.For_Yield_m3m2_R_GLU, L123.LC_bm2_R_MgdFor_Yh_GLU)
L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust %>%
na.omit() %>%
#L123.LC_bm2_R_MgdFor_Yh_GLU %>%
add_title("Managed forest land cover by GCAM region / historical year / GLU") %>%
add_units("bm2") %>%
add_comments("This is required managed forest area, with right yield, which might be adjusted later if total forest is not enough") %>%
add_legacy_name("L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust") %>%
same_precursors_as("L123.For_Prod_bm3_R_Y_GLU_beforeadjust") %>%
add_precursors("L101.Pop_thous_R_Yh") ->
L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust

return_data(L123.ag_Prod_Mt_R_Past_Y_GLU, L123.ag_Yield_kgm2_R_Past_Y_GLU, L123.LC_bm2_R_MgdPast_Yh_GLU, L123.For_Prod_bm3_R_Y_GLU, L123.For_Yield_m3m2_R_GLU, L123.LC_bm2_R_MgdFor_Yh_GLU,
L123.LC_bm2_R_MgdFor_Yh_GLU_beforeadjust)
} else {
stop("Unknown command")
}
Expand Down
Loading