Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:magpiemodel/magpie into f_fixOpti
Browse files Browse the repository at this point in the history
# Conflicts:
#	main.gms
  • Loading branch information
flohump committed Nov 14, 2024
2 parents 3a4ddd8 + d785b7f commit 77cb4d5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]

### changed
- **38_factor_costs** updated use of USDA cost shares
- **38_factor_costs** updated use of USDA cost shares
- **inputdata** changed GDP base year from 2005USD to 2017USD
- **config** changed default input data to use 2017USD
- **module_documentation** all references to USD05 changed to USD17
- **scripts** REMIND coupling reads data in US$2017, not US$2005
- **config** updated input data to rev4.114
- **config** SHAPE scenarios start year of dietary shift changed to 2025
- **extra/disaggregation** Peatland now considered in disaggregation of land pools
- **core** number of age-classes doubled from 150 to 300 years for better match of growth curves with potential natural vegetation.
- **35_natveg** revised age-class initialization of secondary forest
- **modules** update of scaling factors in several modules


### added
- **62_material** added switch to turn off future material demand for bioplastic
- **config** added SSP1-POP-GDP SSP2-POP-GDP and SSP5-POP-GDP
Expand All @@ -27,9 +27,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
-

### fixed
- **11_costs** changed equation to fix bug in total water cost calculation
- **29_cropland** treecover age-class growth was not working properly because ac_sub was erroneously not fixed
- **scripts** script/output/extra/resubmit.R
- **11_costs** changed equation to fix bug in total water cost calculation
- **29_cropland** treecover age-class growth was not working properly because ac_sub was erroneously not fixed
- **scripts** script/output/extra/resubmit.R
- **28_ageclass** bugfix input data unit and code clean-up. Renamed `feb21` realization to `oct24`
- **70_livestock** bugfix scaling.gms file in wrong folder
- **10_land** Simplified land transition matrix for improved feasibility
Expand Down
104 changes: 61 additions & 43 deletions scripts/output/extra/disaggregation.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ if (length(map_file) > 1) {
}

.dissagLandConsv <- function(gdx, cfg, map_file, wdpa_hr_file, consv_prio_hr_file) {
land_consv_lr <- readGDX(gdx, "p22_conservation_area", react = "silent")
land_consv_lr <- readGDX(gdx, "pm_land_conservation", react = "silent")
land_consv_lr <- dimSums(land_consv_lr, dim=3.2)
wdpa_hr <- read.magpie(wdpa_hr_file)
map <- readRDS(map_file)

Expand Down Expand Up @@ -275,6 +276,8 @@ if (cfg$gms$urban == "exo_nov21") {
# Prepare land conservation data
# ----------------------------------------

message("Disaggregating conservation land")

land_consv_hr <- NULL
if (file.exists(wdpa_hr_file)) {
land_consv_hr <- .dissagLandConsv(gdx, cfg, map_file, wdpa_hr_file, consv_prio_hr_file)
Expand All @@ -300,6 +303,30 @@ avl_cropland_hr <- file.path(outputdir, "avl_cropland_0.5.mz") # available cropl
marginal_land <- cfg$gms$c29_marginal_land # marginal land scenario
snv_pol_fader <- readGDX(gdx, "i29_snv_scenario_fader")


# --------------------------------
# Disaggregate peatland
# --------------------------------

message("Disaggregating peatland")

# check for peatland version
if (cfg$gms$peatland == "v2") {
peat_lr <- PeatlandArea(gdx, level = "cell", sum = FALSE)
peat_ini_hr <- read.magpie(peatland_v2_hr_file)
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "rewetted", dim = "d3", fill = 0)
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "unused", dim = "d3", fill = 0)
peat_hr <- suppressWarnings(luscale::interpolate2(peat_lr, peat_ini_hr, map_file))
peat_hr <- peat_hr[, getYears(peat_hr, as.integer = T) >= cfg$gms$s58_fix_peatland, ]
} else if (cfg$gms$peatland == "on") {
peat_lr <- PeatlandArea(gdx, level = "cell", sum = TRUE)
peat_ini_hr <- mbind(setNames(read.magpie(peatland_on_intact_hr_file), "intact"), setNames(read.magpie(peatland_on_degrad_hr_file), "degrad"))
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "rewet", dim = "d3", fill = 0)
peat_hr <- suppressWarnings(luscale::interpolate2(peat_lr, peat_ini_hr, map_file))
peat_hr <- peat_hr[, getYears(peat_hr, as.integer = T) >= cfg$gms$s58_fix_peatland, ]
}
peat_hr <- .fixCoords(peat_hr)

# ============================================
# Start disaggregation
# ============================================
Expand All @@ -319,6 +346,7 @@ land_hr <- interpolateAvlCroplandWeighted(
marginal_land = marginal_land,
urban_land_hr = urban_land_hr,
land_consv_hr = land_consv_hr,
peat_hr = peat_hr,
snv_pol_shr = snv_pol_shr,
snv_pol_fader = snv_pol_fader
)
Expand All @@ -335,6 +363,28 @@ land_hr <- .fixCoords(land_hr)
)
gc()

# -----------------------------------
# Write peatland outputs
# -----------------------------------

# Write output
.writeDisagg(peat_hr, peatland_hr_out_file,
comment = "unit: Mha per grid-cell",
message = "Write outputs peatland Mha"
)
gc()

out <- peat_hr / dimSums(land_hr[, getYears(peat_hr), ], dim = 3)
out[is.nan(out)] <- 0
out[is.infinite(out)] <- 0

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


# ---------------------------------
# Split land pools
# ---------------------------------
Expand Down Expand Up @@ -474,6 +524,10 @@ if (grepl("grass", cfg$gms$past)) {
"past", "manpast",
gsub("range", "rangeland", getNames(land_lr))
)
getNames(land_consv_hr) <- gsub(
"past", "manpast",
gsub("range", "rangeland", getNames(land_consv_hr))
)
} else {
# Disaggregate pasture
land_ini_lr <- mbind(
Expand All @@ -485,6 +539,11 @@ if (grepl("grass", cfg$gms$past)) {
land_lr[, , c("past"), invert = TRUE],
collapseNames(land_lr[, , "past"]) * side_layers_lr[, , c("manpast", "rangeland")]
)

land_consv_hr <- mbind(
land_consv_hr[, , c("past"), invert = TRUE],
collapseNames(land_consv_hr[, , "past"]) * side_layers_hr[, , c("manpast", "rangeland")]
)
}

# Sort and rename
Expand All @@ -504,6 +563,7 @@ land_bii_hr <- interpolateAvlCroplandWeighted(
marginal_land = marginal_land,
urban_land_hr = urban_land_hr,
land_consv_hr = land_consv_hr,
peat_hr = peat_hr,
snv_pol_shr = snv_pol_shr,
snv_pol_fader = snv_pol_fader,
unit = "share"
Expand Down Expand Up @@ -532,46 +592,4 @@ rm(bii_hr)
gc()


# --------------------------------
# Disaggregate peatland
# --------------------------------

message("Disaggregating peatland")

# check for peatland version
if (cfg$gms$peatland == "v2") {
peat_lr <- PeatlandArea(gdx, level = "cell", sum = FALSE)
peat_ini_hr <- read.magpie(peatland_v2_hr_file)
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "rewetted", dim = "d3", fill = 0)
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "unused", dim = "d3", fill = 0)
peat_hr <- suppressWarnings(luscale::interpolate2(peat_lr, peat_ini_hr, map_file))
peat_hr <- peat_hr[, getYears(peat_hr, as.integer = T) >= cfg$gms$s58_fix_peatland, ]
} else if (cfg$gms$peatland == "on") {
peat_lr <- PeatlandArea(gdx, level = "cell", sum = TRUE)
peat_ini_hr <- mbind(setNames(read.magpie(peatland_on_intact_hr_file), "intact"), setNames(read.magpie(peatland_on_degrad_hr_file), "degrad"))
peat_ini_hr <- add_columns(peat_ini_hr, addnm = "rewet", dim = "d3", fill = 0)
peat_hr <- suppressWarnings(luscale::interpolate2(peat_lr, peat_ini_hr, map_file))
peat_hr <- peat_hr[, getYears(peat_hr, as.integer = T) >= cfg$gms$s58_fix_peatland, ]
}
peat_hr <- .fixCoords(peat_hr)

# Write output
.writeDisagg(peat_hr, peatland_hr_out_file,
comment = "unit: Mha per grid-cell",
message = "Write outputs peatland Mha"
)
gc()

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"
)
gc()

message("Finished disaggregation")

0 comments on commit 77cb4d5

Please sign in to comment.