Skip to content
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

show which variables from templates are missing in checkProjectSummation #1628

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Imports:
data.table,
devtools,
dplyr,
edgeTransport (>= 1.5.4),
edgeTransport (>= 1.5.5),
flexdashboard,
GDPuc,
gdx (>= 1.53.0),
Expand Down Expand Up @@ -51,7 +51,7 @@ Imports:
nleqslv,
optparse,
piamenv (>= 0.4.0),
piamInterfaces (>= 0.15.2),
piamInterfaces (>= 0.17.0),
plotly,
purrr,
quitte (>= 0.3128.4),
Expand Down
2 changes: 1 addition & 1 deletion output.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ if (isTRUE(comp)) comp <- "comparison"

if (! exists("output")) {
modules <- gsub("\\.R$", "", grep("\\.R$", list.files(paste0("./scripts/output/", if (isFALSE(comp)) "single" else comp)), value = TRUE))
output <- chooseFromList(modules, type = "modules to be used for output generation", addAllPattern = FALSE)
output <- if (length(modules) == 1) modules else chooseFromList(modules, type = "modules to be used for output generation", addAllPattern = FALSE)
}

# Select output directories if not defined by readArgs
Expand Down
8 changes: 4 additions & 4 deletions scripts/output/export/xlsx_IIASA.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ projects <- list(
)

# add pure mapping from piamInterfaces
mappings <- setdiff(names(piamInterfaces::templateNames()), c(names(projects), "AR6_NGFS"))
mappings <- setdiff(names(piamInterfaces::mappingNames()), c(names(projects), "AR6_NGFS"))
projects <- c(projects,
do.call(c, lapply(mappings, function(x) stats::setNames(list(list(mapping = x)), x))))

Expand All @@ -65,16 +65,16 @@ lucode2::readArgs("outputdirs", "filename_prefix", "outputFilename", "model",
"mapping", "logFile", "removeFromScen", "addToScen", "iiasatemplate")

if (is.null(mapping)) {
mapping <- gms::chooseFromList(names(piamInterfaces::templateNames()), type = "mapping template")
mapping <- gms::chooseFromList(names(piamInterfaces::mappingNames()), type = "mapping")
}
if (length(mapping) == 0 || ! all(file.exists(mapping) | mapping %in% names(templateNames()))) {
if (length(mapping) == 0 || ! all(file.exists(mapping) | mapping %in% names(mappingNames()))) {
stop("mapping='", paste(mapping, collapse = ", "), "' not found.")
}
if (exists("iiasatemplate") && ! is.null(iiasatemplate) && ! file.exists(iiasatemplate)) {
stop("iiasatemplate=", iiasatemplate, " not found.")
}

# variables to be deleted although part of the template
# variables to be deleted although part of the mapping
temporarydelete <- NULL # example: c("GDP|MER", "GDP|PPP")

### define filenames
Expand Down
24 changes: 18 additions & 6 deletions scripts/output/single/checkProjectSummations.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ vars <- c("", "|Energy", "|Energy Demand|Transportation", "|Energy and Industria
gasvars <- expand.grid(gases, vars, stringsAsFactors = FALSE)
bunkervars <- unique(sort(c("Gross Emissions|CO2", paste0("Emissions|", gasvars$Var1, gasvars$Var2))))

for (template in c("AR6", "NAVIGATE")) {
message("\n### Check project summations for ", template)
for (mapping in c("AR6", "NAVIGATE")) {
message("\n### Check project summations for ", mapping)
mappingVariables <- mapping %>%
getMappingVariables(paste0("RT", if (any(grepl("^MAgPIE", levels(mifdata$model)))) "M")) %>%
unique() %>%
removePlus()
computedVariables <- unique(paste0(removePlus(mifdata$variable), " (", gsub("^$", "unitless", mifdata$unit), ")"))
missingVariables <- sort(setdiff(mappingVariables, computedVariables))
if (length(missingVariables) > 0) {
message("# The following ", length(missingVariables), " variables are expected in the piamInterfaces package ",
"for mapping ", mapping, ", but cannot be found in the reporting:\n- ",
paste(missingVariables, collapse = ",\n- "), "\n")
}

d <- generateIIASASubmission(mifdata, outputDirectory = NULL, logFile = NULL,
mapping = template, checkSummation = FALSE)
mapping = mapping, checkSummation = FALSE)
failvars <- d %>%
checkSummations(template = template, summationsFile = template, logFile = NULL, dataDumpFile = NULL,
checkSummations(template = mapping, summationsFile = mapping, logFile = NULL, dataDumpFile = NULL,
absDiff = absDiff, relDiff = relDiff) %>%
filter(abs(diff) >= absDiff, abs(reldiff) >= relDiff) %>%
df_variation() %>%
Expand All @@ -47,13 +59,13 @@ for (template in c("AR6", "NAVIGATE")) {
filter(! .data$variable %in% bunkervars) %>%
select(-"model", -"scenario")
if (nrow(failregi) > 0) {
message("For those ", template, " variables, the sum of regional values does not match the World value in 2050:")
message("For those ", mapping, " variables, the sum of regional values does not match the World value in 2050:")
failregi %>% piamInterfaces::niceround() %>% print(n = 1000)
} else {
message("Regional summation checks are fine.")
}

if (nrow(failvars) > 0 || nrow(failregi) > 0) stopmessage <- c(stopmessage, template)
if (nrow(failvars) > 0 || nrow(failregi) > 0 || length(missingVariables) > 0) stopmessage <- c(stopmessage, mapping)
}

if (length(stopmessage) > 0) {
Expand Down
Loading