Skip to content

Commit

Permalink
reduce disaggregation.R memory footprint with rm
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-sauer committed Feb 21, 2024
1 parent 80383c3 commit 4eaa922
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions scripts/output/extra/disaggregation.R
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ gc()
land_split_hr <- land_hr[, getYears(area_shr_hr), ]
area_hr <- area_shr_hr * dimSums(land_split_hr, dim = 3)

rm(area_shr_hr)
gc()

# replace crop in land_hr in with crop_kfo_rf, crop_kfo_ir, crop_kbe_rf
# and crop_kbe_ir
kbe <- c("betr", "begr")
Expand Down Expand Up @@ -409,6 +412,8 @@ land_split_hr <- land_split_hr[, , "crop", invert = TRUE]
# combine land_split_hr with crop_hr.
land_split_hr <- mbind(crop_hr, fallow, land_split_hr)

rm(crop_kfo_rf, crop_kfo_ir, crop_kbe_rf, crop_kbe_ir, crop_hr, fallow, area_hr)

# split "forestry" into timber plantations, pre-scribed afforestation (NPi/NDC) and endogenous afforestation (CO2 price driven)
message("Disaggregating forestry")
farea <- dimSums(landForestry(gdx, level = "cell"), dim = "ac")
Expand All @@ -430,6 +435,9 @@ land_split_hr <- land_split_hr[, , "forestry", invert = TRUE]
# combine land_split_hr with farea_hr
land_split_hr <- mbind(land_split_hr, farea_hr)

rm(farea, farea_shr, farea_hr)
gc()

# Write output
.writeDisagg(land_split_hr, land_hr_split_file,
comment = "unit: Mha per grid-cell",
Expand All @@ -439,6 +447,7 @@ land_split_hr <- mbind(land_split_hr, farea_hr)
comment = "unit: grid-cell land area fraction",
message = "Write cropsplit land area share"
)
rm(land_split_hr)
gc()

# --------------------------------
Expand Down Expand Up @@ -508,6 +517,9 @@ land_bii_hr <- interpolateAvlCroplandWeighted(
snv_pol_fader = snv_pol_fader,
unit = "share"
)

rm(land_consv_hr, urban_land_hr)

land_bii_hr <- .fixCoords(land_bii_hr)

# Add primary and secondaray other land
Expand All @@ -518,12 +530,14 @@ land_bii_hr <- land_bii_hr * side_layers_hr[, , c("forested", "nonforested")]

# Sum over land classes
bii_hr <- dimSums(land_bii_hr * bii_hr, dim = 3, na.rm = TRUE)
rm(land_bii_hr)

# Write output
.writeDisagg(bii_hr, bii_hr_out_file,
comment = "unitless",
message = "Write output BII at 0.5°"
)
rm(bii_hr)
gc()


Expand Down Expand Up @@ -561,6 +575,8 @@ out <- peat_hr / dimSums(land_hr[,getYears(peat_hr),], dim = 3)
out[is.nan(out)] <- 0
out[is.infinite(out)] <- 0

rm(land_hr, peat_hr)

.writeDisagg(out, peatland_hr_share_out_file,
comment = "unit: grid-cell land area fraction",
message = "Write outputs peatland share")
Expand Down

0 comments on commit 4eaa922

Please sign in to comment.