Skip to content

Commit

Permalink
Merge pull request #1661 from fbenke-pik/cs2
Browse files Browse the repository at this point in the history
move compareScenarios core framework to piamPlotComparison
  • Loading branch information
fbenke-pik authored Apr 30, 2024
2 parents 00b84cc + fc138d0 commit 0eb4cc1
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 3,099 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
origins in FE carriers for performance reasons [[#1659]](https://github.com/remindmodel/remind/pull/1659)
- **37_industry** make process-based steel production model the default over the ces-based model [[#1663]](https://github.com/remindmodel/remind/pull/1663)
- **core** another change of preference parameters and associated computation of interest rates/mark ups [[#1663]](https://github.com/remindmodel/remind/pull/1663)
- ***scripts*** adjust function calls after moving functionality from `remind2` [[#578]]](https://github.com/pik-piam/remind2/pull/578) to `piamPlotComparison` and `piamutils` [[#1661](https://github.com/remindmodel/remind/pull/1661)

## [3.3.0] - 2024-03-28

Expand Down
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Imports:
optparse,
piamenv (>= 0.4.0),
piamInterfaces (>= 0.17.4),
piamPlotComparison (>= 0.0.10),
piamutils,
plotly,
purrr,
quitte (>= 0.3128.4),
Expand Down
19 changes: 13 additions & 6 deletions scripts/cs2/profiles.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
{
"_TITLE_": "Profiles for compareScenarios2",
"_NOTE_": "Entries on the top level with a name starting with _ are ignored.",
"_DESCRIPTION_": "The top level entries identify the profiles for compareScenarios2 (cs2) when called via 'Rscript output.R'. The sub-entries define the arguments passed to remind2::compareScenarios2(). See the R help page of this function for the meaning of the arguments. The values of the sub-entries are strings, i.e., they start and end with a double quote. These strings will be evaluated as R-code. Use single quotes inside these expressions when quotes are needed. The expressions may contain a variable named '.'. It refers to the value of the variable before applying the profile, e.g., setting outputFile to paste0(., '-suffix') will add a suffix to the file name. For more details, see 'scripts/cs2/run_compareScenarios2.R' and 'output/comparison/compareScenarios2.R'.",
"_DESCRIPTION_": "The top level entries identify the profiles for compareScenarios2 (cs2) when called via 'Rscript output.R'. The sub-entries define the arguments passed to piamPlotComparison::compareScenarios(). See the R help page of this function for the meaning of the arguments. The values of the sub-entries are strings, i.e., they start and end with a double quote. These strings will be evaluated as R-code. Use single quotes inside these expressions when quotes are needed. The expressions may contain a variable named '.'. It refers to the value of the variable before applying the profile, e.g., setting outputFile to paste0(., '-suffix') will add a suffix to the file name. For more details, see 'scripts/cs2/run_compareScenarios2.R' and 'output/comparison/compareScenarios2.R'.",
"default": {},
"defaultHTML": {
"outputFormat": "'html'"
},
"H12": {
"projectLibrary": "'remind2'",
"reg": "c('CAZ', 'CHA', 'EUR', 'IND', 'JPN', 'LAM', 'MEA', 'NEU', 'OAS', 'REF', 'SSA', 'USA', 'World')"
},
"H12-short": {
"projectLibrary": "'remind2'",
"reg": "c('CAZ', 'CHA', 'EUR', 'IND', 'JPN', 'LAM', 'MEA', 'NEU', 'OAS', 'REF', 'SSA', 'USA', 'World')",
"yearsScen": "seq(2005, 2050, 5)",
"yearsHist": "c(seq(1990, 2024, 1), seq(2025, 2050, 5))",
"yearsBarPlot": "c(2020, 2030, 2050)"
},
"EU27": {
"projectLibrary": "'remind2'",
"reg": "c('DEU', 'ECE', 'ECS', 'ENC', 'ESC', 'ESW', 'EU27', 'EWN', 'FRA')",
"mainReg": "'EU27'"
},
"EU27-short": {
"projectLibrary": "'remind2'",
"reg": "c('DEU', 'ECE', 'ECS', 'ENC', 'ESC', 'ESW', 'EU27', 'EWN', 'FRA')",
"yearsScen": "seq(2005, 2050, 5)",
"yearsHist": "c(seq(1990, 2024, 1), seq(2025, 2050, 5))",
"yearsBarPlot": "c(2020, 2030, 2050)",
"mainReg": "'EU27'"
},
"AriadneDEU": {
"projectLibrary": "'remind2'",
"reg": "c('DEU', 'ECE', 'ECS', 'ENC', 'ESC', 'ESW', 'EU27', 'EWN', 'FRA')",
"yearsScen": "seq(2005, 2050, 5)",
"yearsHist": "c(seq(1990, 2024, 1), seq(2025, 2050, 5))",
"yearsBarPlot": "c(2020, 2030, 2050)",
"yearsBarPlot": "c(2020, 2030, 2045)",
"modelsHistExclude": "c('IEA ETP B2DS', 'IEA ETP 2DS', 'IEA ETP RTS', 'EDGE_SSP1', 'EDGE_SSP2', 'CEDS', 'IRENA', 'IEA WEO 2021 APS', 'IEA WEO 2021 SDS', 'IEA WEO 2021 SPS')",
"mainReg": "'DEU'"
},
"summary": {
"projectLibrary": "'remind2'",
"sections": "c(0,1,99)"
},
"REMIND-MAgPIE": {
"projectLibrary": "'remind2'",
"userSectionPath": "normalizePath('./scripts/cs2/magpie.Rmd')"
},
"MAgPIE": {
"sections": "0",
"userSectionPath": "normalizePath('./scripts/cs2/magpie.Rmd')"
},
"MAGICC7_AR6": {
"sections": "0",
"userSectionPath": "normalizePath('./scripts/cs2/MAGICC7_AR6.Rmd')"
},
"MAGICC7_AR6": {
"sections": "0",
"userSectionPath": "normalizePath('./scripts/cs2/MAGICC7_AR6.Rmd')"
},
"mySection": {
"sections": "0",
"userSectionPath": "normalizePath('./scripts/cs2/mySection.Rmd')"
Expand Down
39 changes: 19 additions & 20 deletions scripts/cs2/run_compareScenarios2.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# | AGPL-3.0, you are granted additional permissions described in the
# | REMIND License Exception, version 1.0 (see LICENSE file).
# | Contact: [email protected]
library(remind2)

library(piamPlotComparison)

if (!exists("source_include")) {
lucode2::readArgs("outputDirs", "outFileName", "profileName")
Expand All @@ -15,35 +16,33 @@ run_compareScenarios2 <- function(
outFileName,
profileName
) {

stopifnot(length(profileName) == 1 && is.character(profileName) && !is.na(profileName))
stopifnot(length(outFileName) == 1 && is.character(outFileName) && !is.na(outFileName))

# working directory is assumed to be the remind directory

# load cs2 profiles
profiles <- getCs2Profiles()
profiles <- piamPlotComparison::getCs2Profiles()

# Create temporary folder. This is necessary because each compareScenarios2
# run creates a folder named 'figure'. If multiple compareScenarios2 run in
# parallel they would interfere with the others' figure folder. So we create a
# temporary subfolder in which each compareScenarios2 creates its own figure
# folder.
system(paste0("mkdir ", outFileName))
system(paste0("mkdir ", outFileName))
outDir <- normalizePath(outFileName, mustWork = TRUE)

outputDirs <- unique(normalizePath(outputDirs, mustWork = TRUE))
mifPath <- getMifScenPath(outputDirs, mustWork = TRUE)
histPath <- getMifHistPath(outputDirs[1], mustWork = TRUE)
scenConfigPath <- getCfgScenPath(outputDirs, mustWork = TRUE)
defaultConfigPath <- getCfgDefaultPath(mustWork = TRUE)
mifPath <- remind2::getMifScenPath(outputDirs, mustWork = TRUE)
histPath <- remind2::getMifHistPath(outputDirs[1], mustWork = TRUE)
scenConfigPath <- remind2::getCfgScenPath(outputDirs, mustWork = TRUE)

# predefined arguments
args <- list(
mifScen = mifPath,
mifHist = histPath,
cfgScen = scenConfigPath,
cfgDefault = defaultConfigPath,
outputDir = outDir,
outputFile = outFileName,
outputFormat = "pdf"
Expand All @@ -53,38 +52,38 @@ run_compareScenarios2 <- function(
message("Applying profile ", profileName)
profile <- profiles[[profileName]]
# Evaluate entries of profile as R code.
profileEval <- lapply(
profileEval <- lapply(
names(profile),
function(nm) {
eval(
parse(text = profile[[nm]]),
parse(text = profile[[nm]]),
# Set variable . to predefined argument value.
# This allows refer to the predefined value in the profile expression.
list("." = args[[nm]]))
list("." = args[[nm]]))
}
)
args[names(profile)] <- profileEval

# Check outputFile ending.
expectedEnding <- paste0(".", tolower(args$outputFormat))
if (!endsWith(tolower(args$outputFile), expectedEnding)) {
args$outputFile <- paste0(args$outputFile, expectedEnding)
}

message("Will make following function call:")
message(" remind2::compareScenarios2(")
for (i in seq_along(args))
message(" piamPlotComparison::compareScenarios(")
for (i in seq_along(args))
message(" ", names(args)[i], " = ", capture.output(dput(args[[i]])), if (i < length(args)) ",")
message(" )")

message("Calling remind2::compareScenarios2()...\n")
do.call(compareScenarios2, args)
message("Calling piamPlotComparison::compareScenarios()...\n")
do.call(piamPlotComparison::compareScenarios, args)

message("Move outputFile and delete temporary folder.\n")
outputFilePath <- file.path(args$outputDir, args$outputFile)
system(paste0("mv ", outputFilePath, " ."))
system(paste0("rm -rf ", args$outputDir))

message("Done!\n")
}

Expand Down
13 changes: 7 additions & 6 deletions scripts/input/exoGAINSAirpollutants.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# | REMIND License Exception, version 1.0 (see LICENSE file).
# | Contact: [email protected]

# Only output messages to the log if it is the first run of exoGAINS to avoid repetition in the log.txt file

# Only output messages to the log if it is the first run of exoGAINS to avoid repetition in the log.txt file
firstIteration = FALSE
if (file.exists("log.txt")){
if(!any(grepl("ExoGAINS - log for first iteration...", readLines("log.txt")))){
Expand Down Expand Up @@ -50,7 +51,7 @@ message("- reportFE")
rem_in_mo <- mbind(rem_in_mo,reportFE(gdx)[,t,])

# delete "+" and "++" from variable names
rem_in_mo <- deletePlus(rem_in_mo)
rem_in_mo <- piamutils::deletePlus(rem_in_mo)

# for easier debugging use rem_in in the remainder of the script
rem_in <- rem_in_mo
Expand Down Expand Up @@ -129,7 +130,7 @@ ela[,,] <- tmp
##############################################################
RA_limited <- RA / (setYears(RA[,2015,] +1E-10))
RA_limited[RA_limited>5] <- 5

E <- ( ef_gains[,,ssp_scenario] / (setYears(ef_gains[,2015,ssp_scenario])+1E-10) + noef ) * setYears(emi_gains[,2015,ssp_scenario]) * ( RA_limited) ^ela

# Calcualte emissions using different formula: for emisions that have no ef_gains
Expand Down Expand Up @@ -214,17 +215,17 @@ writeGDX(out,file="pm_emiAPexsolve.gdx",period_with_y = FALSE)
# p<-ggplot(data=dat, aes(x=Year, y=Value)) + geom_line(aes(colour=Data2)) + facet_wrap(~Region,scales = "free_y")
# ggsave(filename=paste0("Regions_",sec,".png"),p,scale=1.5,width=32,height=18,unit="cm",dpi=150)
# }
#
#
# stop()
#
#
# # Add missing Int. Shipping sector from extra ECLIPSE file
# RA_ship <- setNames(rem_in["GLO",,"FE|Transport|Liquids (EJ/yr)"],NULL)
# # calculate shippin emission the same way as gains emissions
# ship_E <- (ship_ef/setYears(ship_ef[,2015,])) * setYears(ship_emi[,2015,]) * (RA_ship/setYears(RA_ship[,2015,]))
# E_rem <- add_columns(E_rem,addnm = getNames(ship_E,dim=1)) # filled with NA
# gases <- getNames(E_rem,dim=2)
# E_rem["GLO",,getNames(tmp)] <- ship_E[,,gases][,,ssp_scenario]
#
#
# # Add BC and NOx for missing Int. Aviation sector from extra file from Steve
# RA_avi <- collapseNames(time_interpolate(rem_in["GLO",,"Final Energy|Transportation|Liquids (EJ/yr)"][,,scenario], interpolated_year=getYears(ef_gains), integrate_interpolated_years=TRUE, extrapolation_type="constant"))
# # calculate aviation emission the same way as gains emissions
Expand Down
47 changes: 0 additions & 47 deletions scripts/output/comparison/Ariadne_output.R

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/output/comparison/compareScenarios2.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ startComp <- function(


# Load cs2 profiles.
profiles <- remind2::getCs2Profiles()
profiles <- piamPlotComparison::getCs2Profiles()

lucode2::readArgs("profileNames")

Expand Down
Loading

0 comments on commit 0eb4cc1

Please sign in to comment.