-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peatland rewetting automatically considered during SEALS downscaling #759
Changes from 3 commits
76e251d
2e53021
255fd28
4bb6050
85b6902
794e636
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,7 +14,7 @@ | |||||
# 1.00: first working version | ||||||
|
||||||
library(gms) | ||||||
library(gdx) | ||||||
library(gdx2) | ||||||
library(magpie4) | ||||||
library(filelock) | ||||||
|
||||||
|
@@ -63,9 +63,10 @@ if (length(cfg$seals_years) != 0) { | |||||
} | ||||||
|
||||||
# Restructure data to conform to SEALS | ||||||
sealsInput <- paste0("cell.land_0.5_SEALS_", title, ".nc") | ||||||
reportLandUseForSEALS( | ||||||
magCellLand = "cell.land_0.5_share.mz", | ||||||
outFile = paste0("cell.land_0.5_SEALS_", title, ".nc"), | ||||||
outFile = sealsInput, | ||||||
dir = outputdir, selectyears = rep_years | ||||||
) | ||||||
|
||||||
|
@@ -108,11 +109,13 @@ Sys.chmod(iniLock, mode = "0664") | |||||
# Prepare SEALS start script | ||||||
# -------------------------------- | ||||||
|
||||||
.setupSEALSrun <- function(title, dir, dirProject, dirSEALS, dirBaseFiles) { | ||||||
.setupSEALSrun <- function(cfg, sealsInput, dir, dirProject, dirSEALS, dirBaseFiles) { | ||||||
if (!dir.exists(file.path(dirProject, "scripts"))) { | ||||||
dir.create(file.path(dirProject, "scripts"), recursive = TRUE) | ||||||
} | ||||||
|
||||||
title <- cfg$title | ||||||
|
||||||
file.copy( | ||||||
from = list.files(file.path(dirSEALS, "seals"), full.names = TRUE), | ||||||
to = file.path(dirProject, "scripts"), | ||||||
|
@@ -124,11 +127,94 @@ Sys.chmod(iniLock, mode = "0664") | |||||
dir.create(file.path(dirProject, "inputs"), recursive = TRUE) | ||||||
} | ||||||
|
||||||
file.copy( | ||||||
from = file.path(dir, paste0("seals_scenario_config_", title, ".csv")), | ||||||
to = file.path(dirProject, "inputs", paste0("seals_scenario_config_", title, ".csv")), | ||||||
overwrite = TRUE | ||||||
rcp <- unlist(strsplit(cfg$input["cellular"], "_"))[6] | ||||||
rcp <- paste0("rcp", substr(rcp, nchar(rcp) - 1, nchar(rcp))) | ||||||
|
||||||
ssp <- tolower(cfg$gms$c09_pop_scenario) | ||||||
|
||||||
if (length(cfg$seals_years) != 0) { | ||||||
sealsYears <- cfg$seals_years[cfg$seals_years > 2020] | ||||||
sealsYears <- paste(sealsYears, collapse = " ") | ||||||
} else { | ||||||
sealsYears <- "2050" | ||||||
} | ||||||
|
||||||
scenarioType <- ifelse(grepl("default|bau|ssp\\d-ref", tolower(title)), "bau", "policy") | ||||||
|
||||||
if (cfg$gms$c22_protect_scenario == "none") { | ||||||
consv <- cfg$gms$c22_base_protect | ||||||
} else { | ||||||
consv <- cfg$gms$c22_protect_scenario | ||||||
} | ||||||
|
||||||
### Modify SEALS model coefficients based on scenario settings | ||||||
|
||||||
message("Updating SEALS model coefficients based on scenario settings") | ||||||
|
||||||
sealsCoeff <- c( | ||||||
"input/seals_global_coefficients.csv", | ||||||
"../input/seals_global_coefficients.csv", | ||||||
"../../input/seals_global_coefficients.csv" | ||||||
) | ||||||
sealsCoeff <- suppressWarnings(sealsCoeff[min(which(file.exists(sealsCoeff)))]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems rather hacky, is there no way of knowing where the file will actually be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach is pretty common in post-processing functions mostly in the Would you have an alternative suggestion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion would be to not run the code from different relative directories / setup up the whole system in a way such that that is not necessary. Not something that can be solved in this script though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a bit nicer this way:
Suggested change
This returns NULL if no file exists, so would need to change line 160 to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest the same for seals config below as well |
||||||
if (!is.na(sealsCoeff)) { | ||||||
sealsCoeff <- read.csv(sealsCoeff) | ||||||
consvRow <- which(sealsCoeff[, "spatial_regressor_name"] == "land_conservation") | ||||||
sealsCoeff[consvRow, "data_location"] <- sub( | ||||||
"WDPA", consv, sealsCoeff[consvRow, "data_location"] | ||||||
) | ||||||
|
||||||
peatlandPrice <- readGDX(file.path(dir, "fulldata.gdx"), "im_pollutant_prices") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to use a different criterion for activating peatlands in SEALS. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for spotting this and the suggestion! I've changed the code based on your suggestion. |
||||||
if (any(peatlandPrice[, as.numeric(sealsYears), "co2_c.peatland"] > 0)) { | ||||||
peatRow <- which(sealsCoeff[, "spatial_regressor_name"] == "peatland_rewetting") | ||||||
# SEALS rewetting coefficient | ||||||
rewetCoeff <- 10000 | ||||||
# disincentivise agricultural expansion | ||||||
sealsCoeff[peatRow, c("cropland", "grassland")] <- rewetCoeff | ||||||
# peatland rewetting incentive | ||||||
sealsCoeff[peatRow, c("forest", "othernat")] <- -rewetCoeff | ||||||
} | ||||||
|
||||||
sealsCoeffPath <- file.path( | ||||||
dirProject, "inputs", | ||||||
paste0("seals_global_coefficients_", title, ".csv") | ||||||
) | ||||||
|
||||||
write.csv(sealsCoeff, sealsCoeffPath, | ||||||
row.names = FALSE, na = "" | ||||||
) | ||||||
} else { | ||||||
stop("Could not find seals_global_coefficients.csv file") | ||||||
} | ||||||
|
||||||
|
||||||
### Create SEALS scenario definitions CSV | ||||||
|
||||||
message("Creating SEALS scenario definitions CSV") | ||||||
|
||||||
sealsConfig <- c( | ||||||
"input/seals_scenario_config.csv", | ||||||
"../input/seals_scenario_config.csv", | ||||||
"../../input/seals_scenario_config.csv" | ||||||
) | ||||||
sealsConfig <- suppressWarnings(sealsConfig[min(which(file.exists(sealsConfig)))]) | ||||||
if (!is.na(sealsConfig)) { | ||||||
sealsConfig <- read.csv(sealsConfig) | ||||||
sealsConfig[nrow(sealsConfig), "scenario_label"] <- title | ||||||
sealsConfig[nrow(sealsConfig), "scenario_type"] <- scenarioType | ||||||
sealsConfig[nrow(sealsConfig), "exogenous_label"] <- ssp | ||||||
sealsConfig[nrow(sealsConfig), "climate_label"] <- rcp | ||||||
sealsConfig[nrow(sealsConfig), "counterfactual_label"] <- title | ||||||
sealsConfig[nrow(sealsConfig), "comparison_counterfactual_labels"] <- ifelse(scenarioType == "bau", "", "bau") | ||||||
sealsConfig[, "coarse_projections_input_path"] <- normalizePath(file.path(dir, sealsInput)) | ||||||
sealsConfig[nrow(sealsConfig), "years"] <- sealsYears | ||||||
sealsConfig[nrow(sealsConfig), "calibration_parameters_source"] <- normalizePath(sealsCoeffPath) | ||||||
write.csv(sealsConfig, file.path(dirProject, "inputs", paste0("seals_scenario_config_", title, ".csv")), | ||||||
row.names = FALSE, na = "" | ||||||
) | ||||||
} else { | ||||||
stop("Could not find seals_scenario_config.csv file template") | ||||||
} | ||||||
|
||||||
main <- readLines(file.path(dirProject, "scripts", "run_test_standard.py")) | ||||||
|
||||||
|
@@ -200,19 +286,21 @@ Sys.chmod(iniLock, mode = "0664") | |||||
if (!is.null(lockOn)) { | ||||||
sealsLock <- file.path(dirProject, "seals.lock") | ||||||
|
||||||
.setupSEALSrun( | ||||||
cfg = cfg, | ||||||
sealsInput = sealsInput, | ||||||
dir = outputdir, | ||||||
dirProject = dirProject, | ||||||
dirSEALS = dirSEALS, | ||||||
dirBaseFiles = dirBaseFiles | ||||||
) | ||||||
|
||||||
if (!file.exists(sealsLock) || file.size(sealsLock) == 0) { | ||||||
message(paste( | ||||||
"Starting SEALS allocation with input data creation.\n", | ||||||
"Stitched SEALS allocation outputs will be written to", | ||||||
"'./output/seals/intermediate/stitched_lulc_simplified_scenarios'" | ||||||
)) | ||||||
.setupSEALSrun( | ||||||
title = title, | ||||||
dir = outputdir, | ||||||
dirProject = dirProject, | ||||||
dirSEALS = dirSEALS, | ||||||
dirBaseFiles = dirBaseFiles | ||||||
) | ||||||
|
||||||
id <- .submitSEALS( | ||||||
title = title, | ||||||
|
@@ -225,22 +313,14 @@ if (!is.null(lockOn)) { | |||||
|
||||||
writeLines(id, sealsLock) | ||||||
} else { | ||||||
id <- readLines(sealsLock) | ||||||
|
||||||
message(paste( | ||||||
"Starting SEALS allocation using existing input data.\n", | ||||||
"Stitched SEALS allocation outputs will be written to", | ||||||
"'./output/seals/intermediate/stitched_lulc_simplified_scenarios'" | ||||||
)) | ||||||
|
||||||
id <- readLines(sealsLock) | ||||||
|
||||||
.setupSEALSrun( | ||||||
title = title, | ||||||
dir = outputdir, | ||||||
dirProject = dirProject, | ||||||
dirSEALS = dirSEALS, | ||||||
dirBaseFiles = dirBaseFiles | ||||||
) | ||||||
|
||||||
.submitSEALS( | ||||||
title = title, | ||||||
dirProject = dirProject, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Pascal for these simplifications! I've reworked the parts of the code. Please double check.