diff --git a/R/calcIEA_ETP.R b/R/calcIEA_ETP.R index a3ef5a5b..88619468 100644 --- a/R/calcIEA_ETP.R +++ b/R/calcIEA_ETP.R @@ -11,7 +11,6 @@ #' @export calcIEA_ETP <- function() { - mapping <- toolGetMapping("Mapping_IEA_ETP.csv", type = "reportingVariables", where = "mrremind") %>% filter(!is.na(!!sym("REMIND")), !!sym("REMIND") != "") %>% mutate("Conversion" = as.numeric(!!sym("Conversion"))) %>% @@ -52,11 +51,25 @@ calcIEA_ETP <- function() { x <- aggregate(value ~ region + year + model + variable, x, sum, na.action = na.pass) %>% as.magpie() + .customAggregate <- function(x, rel, to = NULL) { + out <- toolAggregate(x, rel = rel, to = to) + + # remove cement and industry production for all regions except for GLO + if ("GLO" %in% getItems(out, dim = 1)) { + out[setdiff(getItems(out, dim = 1), "GLO"), , + c("Production|Industry|Cement (Mt/yr)", "Production|Industry|Steel (Mt/yr)")] <- NA + + } else { + out <- out[, , c("Production|Industry|Cement (Mt/yr)", "Production|Industry|Steel (Mt/yr)"), invert = TRUE] + } + return(out) + } + return(list( x = x, weight = NULL, + aggregationFunction = .customAggregate, 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/filter_historical_mif.R b/R/filter_historical_mif.R deleted file mode 100644 index c86d3f27..00000000 --- a/R/filter_historical_mif.R +++ /dev/null @@ -1,71 +0,0 @@ -#' Filter improperly aggregated regional data from historical.mif -#' -#' Load a `historical.mif` file, remove designated data, write back to file. -#' -#' For all combinations of scenario, model, and variable (and all years), either -#' all regions listed in `exclude_regions` in the `filter_table` are removed, or -#' all regions _except_ those listed in `include_regions` in the `filter_table` -#' are removed. -#' Defining both `include_regions` and `exclude_regions` on the same row of -#' `filter_table` will throw an error. -#' Scenarios, models, and variables are matched precisely, not partially or via -#' regular expressions. -#' -#' @md -#' @param path Path to `historical.mif` (or any mif for that matter), defaults -#' to `historical.mif` in the currently configured -#' `getConfig('outputfolder')`. -#' @param filter_table Data frame with columns `scenario`, `model`, `variable`, -#' `include_regions`, and `exclude_regions`. Defaults to the built-in -#' `./inst/extdata/historical_mif_filter_table.csv` of the `mrremind` package. -#' -#' @importFrom quitte cartesian - -#' @export -filter_historical_mif <- function(path = NULL, filter_table = NULL) { - if (is.null(path)) - path <- 'historical.mif' - - if (is.null(filter_table)) - filter_table <- read.csv( - file = system.file('extdata', 'historical_mif_filter_table.csv', - package = 'mrremind', mustWork = TRUE), - colClasses = 'character') - - # combine scenario, model, and variable columns to string for indexing a - # magpie object - scenario_model_variable <- sapply( - seq_len(nrow(filter_table)), - function(i) { - Reduce(f = cartesian, - x = filter_table[i, c('scenario', 'model', 'variable')]) - }) - - # check for conflicting include/exclude definitions - include_and_exclude <- ( '' != filter_table[['include_regions']] - & '' != filter_table[['exclude_regions']]) - - if (any(include_and_exclude)) - stop('Both include and exclude regions defined for:', - paste(c('', scenario_model_variable[include_and_exclude]), - collapse = '\n')) - - h <- read.report(file = path, as.list = FALSE) - - # process each row of the filter table - for (i in seq_len(nrow(filter_table))) { - if ('' != filter_table[[i, 'include_regions']]) { - excludedRegions <- setdiff(getItems(h, dim = 'region'), - filter_table[[i, 'include_regions']]) - } else { - excludedRegions <- filter_table[[i, 'exclude_regions']] - } - - h_include <- h[, , scenario_model_variable[i]] - h_include[excludedRegions, , ] <- NA - - h <- mbind(h[, , scenario_model_variable[i], invert = TRUE], h_include) - } - - write.report(h, file = path) -} diff --git a/R/fullVALIDATIONREMIND.R b/R/fullVALIDATIONREMIND.R index c0974067..249fedc6 100644 --- a/R/fullVALIDATIONREMIND.R +++ b/R/fullVALIDATIONREMIND.R @@ -259,7 +259,4 @@ fullVALIDATIONREMIND <- function(rev = 0) { try = FALSE, years = years, writeArgs = list(scenario = "historical", model = "INDSTAT2") ) - - # filter variables that are too imprecise on regional level ---- - filter_historical_mif() } diff --git a/inst/extdata/historical_mif_filter_table.csv b/inst/extdata/historical_mif_filter_table.csv deleted file mode 100644 index 557c8b1f..00000000 --- a/inst/extdata/historical_mif_filter_table.csv +++ /dev/null @@ -1,7 +0,0 @@ -scenario,model,variable,include_regions,exclude_regions -historical,IEA ETP 2DS,Production|Industry|Cement (Mt/yr),GLO, -historical,IEA ETP 2DS,Production|Industry|Steel (Mt/yr),GLO, -historical,IEA ETP B2DS,Production|Industry|Cement (Mt/yr),GLO, -historical,IEA ETP B2DS,Production|Industry|Steel (Mt/yr),GLO, -historical,IEA ETP RTS,Production|Industry|Cement (Mt/yr),GLO, -historical,IEA ETP RTS,Production|Industry|Steel (Mt/yr),GLO,