diff --git a/.buildlibrary b/.buildlibrary index 3584a1f8..b1f905c1 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '20562120' +ValidationKey: '20753600' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.zenodo.json b/.zenodo.json index 32fe201d..854ce552 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "mrremind: MadRat REMIND Input Data Package", - "version": "0.108.0", + "version": "0.109.0", "description": "

The mrremind packages contains data preprocessing for the REMIND model.<\/p>", "creators": [ { diff --git a/DESCRIPTION b/DESCRIPTION index ee342515..45045603 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: mrremind Type: Package Title: MadRat REMIND Input Data Package -Version: 0.108.0 -Date: 2022-02-16 +Version: 0.109.0 +Date: 2022-02-17 Authors@R: c(person("Lavinia", "Baumstark", email = "lavinia@pik-potsdam.de", role = c("aut","cre")), person("Renato", "Rodrigues", role = "aut"), person("Antoine", "Levesque", role = "aut"), diff --git a/R/calcHistorical.R b/R/calcHistorical.R index d273b27b..5f43d70d 100644 --- a/R/calcHistorical.R +++ b/R/calcHistorical.R @@ -1,3 +1,4 @@ +#' Gather reference data from various sources. #' @importFrom magclass setNames getNames getSets add_columns #' @importFrom luscale rename_dimnames @@ -177,8 +178,6 @@ calcHistorical <- function() { ARIADNE_ReferenceScenarioPop <- add_dimension(ARIADNE_ReferenceScenarioPop, dim = 3.1, add = "model", nm = "ARIADNE") - IEA_ETP <- calcOutput("IEA_ETP", aggregate = F) - IEA_EVOutlook <- calcOutput("IEA_EVOutlook", aggregate = F) # Calculate Emission Reference Values @@ -235,8 +234,6 @@ calcHistorical <- function() { BP <- calcOutput("BP", aggregate = FALSE) BP <- add_dimension(BP, dim = 3.1, add = "model", nm = "BP") - WEO_2021 <- calcOutput("IEA_WEO_2021", subtype = "regional", aggregate = F) - # Steel Production ---- worldsteel <- readSource('worldsteel', convert = FALSE) %>% madrat_mule() %>% @@ -294,7 +291,7 @@ calcHistorical <- function() { LU_EDGAR_LU, LU_CEDS, LU_FAO_EmisLUC, LU_FAO_EmisAg, LU_PRIMAPhist, IRENAcap, eurostat, #emiMktES, emiMktETS, emiMktESOthers, EU_ReferenceScenario, emiEurostat, ARIADNE_ReferenceScenarioGdp, ARIADNE_ReferenceScenarioGdpCorona, ARIADNE_ReferenceScenarioPop, EEA_GHGSectoral, EEA_GHGTotal, EEA_GHGProjections, Emi_Reference, #, EEA_GHGES - IEA_ETP, IEA_EVOutlook, INNOPATHS, JRC_Industry, JRC_Transport, JRC_ResCom, AGEB_FE, UBA_emi, UNFCCC, BP, worldsteel, WEO_2021) + IEA_EVOutlook, INNOPATHS, JRC_Industry, JRC_Transport, JRC_ResCom, AGEB_FE, UBA_emi, UNFCCC, BP, worldsteel) y <- Reduce(union,lapply(varlist,getYears)) n <- Reduce(c,lapply(varlist,getNames)) diff --git a/R/calcIEA_ETP.R b/R/calcIEA_ETP.R index cf9ec883..e0359424 100644 --- a/R/calcIEA_ETP.R +++ b/R/calcIEA_ETP.R @@ -5,6 +5,8 @@ #' #' @author Falk Benke #' +#' @param aggregate Boolean indicating whether output data aggregation should be performed or not +#' @param isValidation indicates if result will be used in validation (as opposed to generating input data) #' @importFrom dplyr select mutate left_join #' @importFrom madrat toolGetMapping #' @importFrom magclass as.magpie @@ -12,30 +14,30 @@ #' @importFrom stats aggregate na.pass #' @export -calcIEA_ETP <- function() { - +calcIEA_ETP <- function(aggregate, isValidation = FALSE) { + mapping <- toolGetMapping("Mapping_IEA_ETP.csv", type = "reportingVariables") %>% filter(!is.na(!!sym("REMIND")), !!sym("REMIND") != "") %>% mutate(!!sym("Conversion") := as.numeric(!!sym("Conversion"))) %>% select("variable" = "IEA_ETP", "REMIND", "Conversion", "Unit_REMIND") - + mapping$variable <- trimws(mapping$variable) mapping$REMIND <- trimws(mapping$REMIND) - + x1 <- readSource("IEA_ETP", subtype = "industry") x2 <- readSource("IEA_ETP", subtype = "transport") x3 <- readSource("IEA_ETP", subtype = "buildings") x4 <- readSource("IEA_ETP", subtype = "summary") - - data <- mbind(x1,x2,x3,x4) - + + data <- mbind(x1, x2, x3, x4) + data <- as.data.frame(data) %>% as_tibble() %>% select( "region" = "Region", "scenario" = "Data1", "variable" = "Data2", "year" = "Year", "value" = "Value" ) - + x <- left_join( data, mapping, @@ -49,15 +51,27 @@ calcIEA_ETP <- function() { !!sym("year") := as.numeric(as.character(!!sym("year"))) ) %>% select("region", "year", "model", "variable" = "REMIND", "value") - + x <- aggregate(value ~ region + year + model + variable, x, sum, na.action = na.pass) %>% as.magpie() + # filter variables that are too imprecise on regional level + if (aggregate == "region") { + x <- x[, , c( + "Production|Industry|Cement (Mt/yr)", + "Production|Industry|Steel (Mt/yr" + ), pmatch = TRUE, invert = TRUE] + } + + if (isValidation) { + x <- add_dimension(x, dim = 3.1, add = "scenario", nm = "historical") + } + return(list( - x = x, + x = x, weight = NULL, unit = c("EJ/yr", "Mt CO2/yr", "Mt/yr", "bn pkm/yr", "bn tkm/yr"), description = "IEA ETP projections as REMIND variables" )) - + } diff --git a/R/calcIEA_WEO_2021.R b/R/calcIEA_WEO_2021.R index 75c78849..a24f09e4 100644 --- a/R/calcIEA_WEO_2021.R +++ b/R/calcIEA_WEO_2021.R @@ -2,7 +2,8 @@ #' #' @md #' @return A [`magpie`][magclass::magclass] object. -#' @param subtype Either "GLO" or "regional" +#' @param aggregate Boolean indicating whether output data aggregation should be performed or not +#' @param isValidation indicates if result will be used in validation (as opposed to generating input data) #' @author Falk Benke #' @importFrom dplyr select mutate left_join case_when #' @importFrom madrat toolGetMapping @@ -10,10 +11,10 @@ #' @importFrom rlang sym #' @export +calcIEA_WEO_2021 <- function(aggregate, isValidation = FALSE) { -calcIEA_WEO_2021 <- function(subtype = "GLO") { - if (!subtype %in% c("GLO", "regional")) { - stop("Not a valid subtype! Must be either \"regional\" or \"GLO\"") + if (!aggregate %in% c("global", "region")) { + aggregate <- "global" } mapping <- toolGetMapping("Mapping_IEA_WEO_2021.csv", type = "reportingVariables") %>% @@ -23,7 +24,7 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") { mapping$variable <- trimws(mapping$variable) - data <- readSource("IEA_WEO_2021", subtype = subtype) + data <- readSource("IEA_WEO_2021", subtype = aggregate) # copy over Stated Policies Scenario for 2010 - 2020 to other scenarios for (s in getNames(data, dim = 1)) { @@ -58,7 +59,7 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") { x <- as.magpie(x, spatial = 1, temporal = 2, data = 5) - if (subtype == "GLO") { + if (aggregate == "global") { x <- add_columns(x, "Cap|Electricity|Biomass|w/o CC (GW)", dim = 3.2) x[, , "Cap|Electricity|Biomass|w/o CC (GW)"] <- x[, , "Cap|Electricity|Biomass (GW)"] - x[, , "Cap|Electricity|Biomass|w/ CC (GW)"] @@ -75,8 +76,13 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") { x[, , "Cap|Electricity|Gas (GW)"] <- x[, , "Cap|Electricity|Gas|w/o CC (GW)"] + x[, , "Cap|Electricity|Gas|w/ CC (GW)"] } + if (isValidation) { + x <- add_dimension(x, dim = 3.1, add = "scenario", nm = "historical") + } + return(list( x = x, + weight = NULL, unit = c("GW", "EJ/yr", "Mt CO2/yr"), description = "IEA WEO 2021 values as REMIND variables" )) diff --git a/R/convertIEA_WEO_2021.R b/R/convertIEA_WEO_2021.R index e228b455..d30f8ee5 100644 --- a/R/convertIEA_WEO_2021.R +++ b/R/convertIEA_WEO_2021.R @@ -1,14 +1,14 @@ #' Disaggregates IEA WEO 2021 Data #' @param x MAgPIE object to be converted #' @return A [`magpie`][magclass::magclass] object. -#' @param subtype Either "GLO" or "regional" +#' @param subtype Either "global" or "region" #' @author Falk Benke #' @importFrom madrat getISOlist #' -convertIEA_WEO_2021 <- function(x, subtype = "GLO") { +convertIEA_WEO_2021 <- function(x, subtype = "global") { PE <- calcOutput("PE", aggregate = FALSE) - if (subtype == "GLO") { + if (subtype == "global") { # for now, we only have complete data on global level x.world <- x["World", , ] @@ -25,7 +25,7 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") { weight <- PE[, 2016, "PE (EJ/yr)"] x.world <- toolAggregate(x.world, rel = mapping_world, weight = weight) return(x.world) - } else if (subtype == "regional") { + } else if (subtype == "region") { .removeNaRegions <- function(x) { remove <- magpply(x, function(y) all(is.na(y)), MARGIN = 1) return(x[!remove, , ]) @@ -39,7 +39,7 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") { regions <- intersect(regions_in, getItems(x, dim = 1)) # iso countries in x - ctry <- toolCountry2isocode(getItems(x, dim = 1), warn = F) + ctry <- toolCountry2isocode(getItems(x, dim = 1), warn = FALSE) ctry <- ctry[!is.na(ctry)] # mapping of regions to iso countries other than in ctry (i.e. other regions) @@ -58,12 +58,12 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") { return(toolCountryFill(x2, fill = NA, verbosity = 2)) } - getItems(x1, dim = 1) <- toolCountry2isocode(getItems(x1, dim = 1), warn = F) + getItems(x1, dim = 1) <- toolCountry2isocode(getItems(x1, dim = 1), warn = FALSE) # combine the two objects x <- mbind(x1, x2) x <- toolCountryFill(x, fill = NA, verbosity = 2) - + return(x) } @@ -71,14 +71,14 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") { x.reg <- x[c( "Atlantic Basin", "East of Suez", "NonOPEC", "OPEC", "Japan and Korea", "Southeast Asia", "Other", "European Union", "World" - ), , , invert = T] + ), , , invert = TRUE] # remove all-na variables remove <- magpply(x.reg, function(y) all(is.na(y)), MARGIN = 3) x.reg <- x.reg[, , !remove] # remove 2040 as year, as source has no regional data for this year - x.reg <- x.reg[, 2040, , invert = T] + x.reg <- x.reg[, 2040, , invert = TRUE] regions <- c("Africa", "Asia Pacific", "Central and South America", "Europe", "Eurasia", "Middle East", "North America") @@ -117,13 +117,13 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") { x.regional <- mbind(x.regional, .disaggregate_regions(x_in = j, regions_in = regions)) } - x.regional <- x.regional[, , "dummy", invert = T] + x.regional <- x.regional[, , "dummy", invert = TRUE] Non28EUcountries <- c("ALA", "FRO", "GIB", "GGY", "IMN", "JEY") - x.regional[Non28EUcountries,,] <- 0 - + x.regional[Non28EUcountries, , ] <- 0 + return(x.regional) } else { - stop("Not a valid subtype! Must be either \"regional\" or \"GLO\"") + stop("Not a valid subtype! Must be either \"region\" or \"global\"") } } diff --git a/R/fullVALIDATIONREMIND.R b/R/fullVALIDATIONREMIND.R index 7508db17..061695d6 100644 --- a/R/fullVALIDATIONREMIND.R +++ b/R/fullVALIDATIONREMIND.R @@ -15,6 +15,22 @@ fullVALIDATIONREMIND <- function(rev = 0) { #-------------- historical data --------------------------------------------------------------------- - calcOutput("Historical", round = 5, file = "historical.mif", aggregate = "region+global+missingH12") + + valfile <- "historical.mif" + + calcOutput("Historical", round = 5, file = valfile, aggregate = "region+global+missingH12", + append = FALSE, na_warning = FALSE, try = TRUE) + + calcOutput(type = "IEA_ETP", aggregate = "global", file = valfile, + append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE) + + calcOutput(type = "IEA_ETP", aggregate = "region", file = valfile, + append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE) + + calcOutput(type = "IEA_WEO_2021", aggregate = "global", file = valfile, + append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE) + + calcOutput(type = "IEA_WEO_2021", aggregate = "region", file = valfile, + append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE) } diff --git a/README.md b/README.md index 9ed66cd1..c350b2b2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MadRat REMIND Input Data Package -R package **mrremind**, version **0.108.0** +R package **mrremind**, version **0.109.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrremind)](https://cran.r-project.org/package=mrremind) [![R build status](https://github.com/pik-piam/mrremind/workflows/check/badge.svg)](https://github.com/pik-piam/mrremind/actions) [![codecov](https://codecov.io/gh/pik-piam/mrremind/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrremind) [![r-universe](https://pik-piam.r-universe.dev/badges/mrremind)](https://pik-piam.r-universe.dev/ui#builds) @@ -38,7 +38,7 @@ In case of questions / problems please contact Lavinia Baumstark