From 13ace17f3ac19c5bd4271486d9a9ddd6a09826dd Mon Sep 17 00:00:00 2001 From: orichters Date: Fri, 22 Sep 2023 15:27:04 +0200 Subject: [PATCH] fix path_gdx_bau checks, some more realizations need it --- scripts/start/readCheckScenarioConfig.R | 35 +++++++++++++------ .../test_01-readCheckScenarioConfig.R | 4 +-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/scripts/start/readCheckScenarioConfig.R b/scripts/start/readCheckScenarioConfig.R index cd238694e..0f1808000 100644 --- a/scripts/start/readCheckScenarioConfig.R +++ b/scripts/start/readCheckScenarioConfig.R @@ -83,21 +83,34 @@ readCheckScenarioConfig <- function(filename, remindPath = ".", testmode = FALSE } if ("path_gdx_bau" %in% names(scenConf)) { # fix if bau given despite not needed - NDC45 <- if ("carbonprice" %in% names(scenConf)) scenConf$carbonprice %in% "NDC" else FALSE - NDC46 <- if ("carbonpriceRegi" %in% names(scenConf)) scenConf$carbonpriceRegi %in% "NDC" else FALSE - noNDCbutBAU <- ! is.na(scenConf$path_gdx_bau) & ! (NDC45 | NDC46) - if (sum(noNDCbutBAU) > 0) { - msg <- paste0("In ", sum(noNDCbutBAU), " scenarios, neither 'carbonprice' nor 'carbonpriceRegi' is set to 'NDC', but 'path_gdx_bau' is not empty.\n", + needBau <- list(welfare = "ineqLognormal", + carbonprice = c("NDC", "diffPriceSameCost"), + carbonpriceRegi = "NDC", + emicapregi = "AbilityToPay") + scenNeedsBau <- rep(FALSE, nrow(scenConf)) + for (n in names(needBau)) { + if (n %in% names(scenConf)) { + if (n == "welfare" && "cm_emiscen" %in% names(scenConf)) { + scenNeedsBAU <- scenNeedsBau | (scenConf[[n]] %in% needBau[[n]] & scenConf[["cm_emiscen"]] != 1) + } else { + scenNeedsBau <- scenNeedsBau | scenConf[[n]] %in% needBau[[n]] + } + } + } + BAUbutNotNeeded <- ! is.na(scenConf$path_gdx_bau) & ! (scenNeedsBau) + if (sum(BAUbutNotNeeded) > 0) { + msg <- paste0("In ", sum(BAUbutNotNeeded), " scenarios, 'path_gdx_bau' is not empty although no realization is selected that needs it.\n", "To avoid unnecessary dependencies to other runs, setting 'path_gdx_bau' to NA.") message(msg) - scenConf$path_gdx_bau[noNDCbutBAU] <- NA + scenConf$path_gdx_bau[BAUbutNotNeeded] <- NA } # fail if bau not given but needed - noBAUbutNDC <- is.na(scenConf$path_gdx_bau) & (NDC45 | NDC46) - if (sum(noBAUbutNDC) > 0) { - pathgdxerrors <- pathgdxerrors + sum(noBAUbutNDC) - warning("In ", sum(noBAUbutNDC), " scenarios, 'carbonprice' or 'carbonpriceRegi' is set to 'NDC' ", - "which requires a reference gdx in 'path_gdx_bau', but it is empty.") + noBAUbutNeeded <- is.na(scenConf$path_gdx_bau) & (scenNeedsBau) + if (sum(noBAUbutNeeded) > 0) { + pathgdxerrors <- pathgdxerrors + sum(noBAUbutNeeded) + warning("In ", sum(noBAUbutNeeded), " scenarios, a reference gdx in 'path_gdx_bau' is needed, but it is empty. ", + "These realizations need it: ", + paste0(names(needBau), ": ", sapply(needBau, paste, collapse = ", "), ".", collapse = " ")) } } # make sure every path gdx column exists diff --git a/tests/testthat/test_01-readCheckScenarioConfig.R b/tests/testthat/test_01-readCheckScenarioConfig.R index 37c254402..dbefbe69d 100644 --- a/tests/testthat/test_01-readCheckScenarioConfig.R +++ b/tests/testthat/test_01-readCheckScenarioConfig.R @@ -34,10 +34,10 @@ test_that("readCheckScenarioConfig fails on error-loaden config", { expect_match(w, "contain whitespaces", all = FALSE, fixed = TRUE) expect_match(w, "scenario names indicated in copyConfigFrom column were not found", all = FALSE, fixed = TRUE) expect_match(w, "specify in copyConfigFrom column a scenario name defined below in the file", all = FALSE, fixed = TRUE) - expect_match(w, "which requires a reference gdx", all = FALSE, fixed = TRUE) + expect_match(w, "a reference gdx in 'path_gdx_bau'", all = FALSE, fixed = TRUE) expect_match(w, "Do not use 'NA' as scenario name", all = FALSE, fixed = TRUE) expect_match(m, "no column path_gdx_refpolicycost for policy cost comparison found, using path_gdx_ref instead", all = FALSE, fixed = TRUE) - expect_match(m, "In 1 scenarios, neither 'carbonprice'", all = FALSE, fixed = TRUE) + expect_match(m, "is not empty although no realization is selected that needs it", all = FALSE, fixed = TRUE) copiedFromPBS <- c("c_budgetCO2", "path_gdx", "path_gdx_ref") expect_identical(unlist(scenConf["PBS", copiedFromPBS]), unlist(scenConf["PBScopy", copiedFromPBS]))