From 915b5a47f08849c1322d30c1fcd3a6e31ef16ddb Mon Sep 17 00:00:00 2001 From: Falk Benke Date: Mon, 8 Jan 2024 17:18:24 +0100 Subject: [PATCH] remove subtypes FE_for_Eff and UE_for_Eff from calcFEdemand --- R/calcEnergyEffPaths.R | 44 --------------------------- R/calcFEdemand.R | 69 ++++++++++-------------------------------- R/fullREMIND.R | 1 - 3 files changed, 16 insertions(+), 98 deletions(-) delete mode 100644 R/calcEnergyEffPaths.R diff --git a/R/calcEnergyEffPaths.R b/R/calcEnergyEffPaths.R deleted file mode 100644 index 6f93b672..00000000 --- a/R/calcEnergyEffPaths.R +++ /dev/null @@ -1,44 +0,0 @@ -#' Efficiency paths for CES variables beyond calibration CES nest -#' -#' Returns the Efficiency paths for Macro variables -#' -#' @author Antoine Levesque -#' @importFrom quitte removeColNa interpolate_missing_periods_ inline.data.frame getVars - -calcEnergyEffPaths <- function() { - #----- Functions ------------------ - - #----- READ-IN DATA ------------------ - fe = calcOutput("FEdemand",subtype = "FE_for_Eff",aggregate = F) - ue = calcOutput("FEdemand",subtype = "UE_for_Eff",aggregate = F) - #----- PARAMETERS ------------------ - t0 = 2005 - - #----- PROCESS DATA ------------------ - - ue_names = getNames(ue) - fe = fe[,,ue_names] - - output = ue/fe - - # Fill NAs and Os, and replace with 50% efficiency for fully empty entries - output = output %>% as.quitte() %>% - mutate(value = ifelse((is.na(.data$value) | .data$value == 0), NA, - .data$value)) %>% - group_by(!!!syms(c('scenario', 'region', 'item'))) %>% - mutate(tmp = all(is.na(.data$value)), - value = ifelse(.data$tmp, 0.5, .data$value)) %>% # , value = ifelse(all(is.na(value)),0.5,value) overwrites entries if !all(is.na) - select(-'tmp') %>% ungroup() %>% - interpolate_missing_periods_(list(period = getYears(output,T)),expand.values = T) %>% - removeColNa() %>% - select('scenario', 'region', 'period', 'item', 'value') %>% - as.magpie(spatial = 2, temporal = 3, data = 5) - - - weight_fe = fe - weight_fe[weight_fe == 0 ] <- 1e-6 - - return(list(x=output,weight=weight_fe, - unit = "energy efficiency paths", - description = "Efficiency pathways for CES variables outside the calibration CES nest")) -} diff --git a/R/calcFEdemand.R b/R/calcFEdemand.R index 3ca678e2..d254d66f 100644 --- a/R/calcFEdemand.R +++ b/R/calcFEdemand.R @@ -2,9 +2,6 @@ #' #' Returns the Edge data at the Remind level #' -#' @param subtype Final energy (FE) or Useful/Final -#' Energy items from EDGEv3 corresponding to REMIND FE items (UE_for_Eff, -#' FE_for_Eff) #' @param use_ODYM_RECC per-capita pathways for `SDP_xx` scenarios? (Defaults #' to `FALSE`.) #' @@ -27,11 +24,7 @@ #' @importFrom zoo na.fill #' #' @author Michaja Pehl, Robin Hasse, Falk Benke -calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { - - if (!subtype %in% c("FE", "FE_for_Eff", "UE_for_Eff")) { - stop(paste0("Unsupported subtype: ", subtype )) - } +calcFEdemand <- function(use_ODYM_RECC = FALSE) { # Functions ------------------ @@ -347,7 +340,7 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { buildings <- mselect(buildings, rcp = "fixed", collapseNames = TRUE) # fix issue with trains in transport trajectories: they seem to be 0 for t > 2100 - if (subtype == "FE" && all(mselect(stationary, year = "y2105", scenario = "SSP2", item = "feelt") == 0)) { + if (all(mselect(stationary, year = "y2105", scenario = "SSP2", item = "feelt") == 0)) { stationary[, seq(2105, 2150, 5), "feelt"] <- time_interpolate(stationary[, 2100, "feelt"], seq(2105, 2150, 5)) } @@ -359,15 +352,12 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { extrapolation_type = "constant") data <- mbind(stationary, buildings) - y <- getYears(data) # TODO # nolint - unit <- "EJ" # TODO # nolint - if (subtype == "FE") { - feIndustry <- calcOutput("FeDemandIndustry", warnNA = FALSE, aggregate = FALSE) - data <- mbind(data[ , , getNames(feIndustry), invert = TRUE], feIndustry) - } + feIndustry <- calcOutput("FeDemandIndustry", warnNA = FALSE, aggregate = FALSE) + + data <- mbind(data[ , , getNames(feIndustry), invert = TRUE], feIndustry) # TODO - # SAME FOR ALL ---- + # Prepare Mapping ---- mapping = toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", where = "mappingfolder") @@ -380,22 +370,6 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { mutate(REMINDitems_out = "feelb") ) - years <- getYears(data) # TODO # nolint - - if (subtype %in% c("FE_for_Eff", "UE_for_Eff")) { - - # select items from EDGE v3, which is based on the distinct UE and FE - mapping = mapping[grepl("^.*_fe$", mapping$EDGEitems), ] - - # replace the FE input with UE inputs, but let the output names as in REMIND - if (subtype %in% c("UE_for_Eff")){ - mapping$EDGEitems = gsub("_fe$", "_ue", mapping$EDGEitems) - } - - # reduce data set to relevant items - data = data[ , , unique(mapping$EDGEitems)] - } - mapping <- mapping %>% select("EDGEitems", "REMINDitems_out", "weight_Fedemand") %>% na.omit() %>% @@ -406,16 +380,14 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { stop("Not all EDGE items are in the mapping") } - remindVars <- unique(mapping$REMINDitems_out) - # Apply Mapping ---- remind <- new.magpie(cells_and_regions = getItems(data, dim = 1), years = getYears(data), - names = cartesian(getNames(data, dim = "scenario"), remindVars), + names = cartesian(getNames(data, dim = "scenario"), unique(mapping$REMINDitems_out)), sets = getSets(data)) - for (v in remindVars) { + for (v in unique(mapping$REMINDitems_out)) { w <- mapping %>% filter(.data$REMINDitems_out == v) %>% @@ -430,13 +402,14 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { remind[, , getNames(tmp)] <- tmp } - # Prepare Output ---- - # change the scenario names for consistency with REMIND sets getNames(remind) <- gsub("^SSP", "gdp_SSP", getNames(remind)) getNames(remind) <- gsub("SDP", "gdp_SDP", getNames(remind)) - # FE only ---- + years <- getYears(data) + y <- getYears(data) + unit <- "EJ" + subtype <- "FE" if ('FE' == subtype) { @@ -1761,23 +1734,13 @@ calcFEdemand <- function(subtype = "FE", use_ODYM_RECC = FALSE) { ) } - # SAME FOR ALL ---- - - description <- ifelse( - subtype %in% c("FE_for_Eff", "UE_for_Eff"), - "demand pathways for useful/final energy in buildings and industry corresponding to the final energy items in REMIND", - "demand pathways for final energy in buildings and industry in the original file" - ) + ########################### - outputStructure <- switch(subtype, - FE = "^gdp_(SSP[1-5].*|SDP.*)\\.(fe|ue)", - FE_for_Eff = "^gdp_(SSP[1-5]|SDP).*\\.fe.*(b|s)$", - UE_for_Eff = "^gdp_(SSP[1-5]|SDP).*\\.fe.*(b|s)$", - "^gdp_(SSP[1-5].*|SDP.*)\\.fe..s\\.ue.*b\\.te_ue.*b$") + # Prepare Output ---- return(list(x = remind, weight = NULL, unit = unit, - description = description, - structure.data = outputStructure)) + description = "demand pathways for final energy in buildings and industry in the original file", + structure.data = "^gdp_(SSP[1-5].*|SDP.*)\\.(fe|ue)")) } diff --git a/R/fullREMIND.R b/R/fullREMIND.R index fa80fabb..5539ddad 100644 --- a/R/fullREMIND.R +++ b/R/fullREMIND.R @@ -47,7 +47,6 @@ fullREMIND <- function() { calcOutput("FeDemandBuildings", subtype = "FE_buildings", round = 8, file = "f_fedemand_build.cs4r") calcOutput("FeDemandBuildings", subtype = "UE_buildings", round = 8, file = "f36_uedemand_build.cs4r") calcOutput("ChemicalFeedstocksShare", round = 2, file = "p37_chemicals_feedstock_share.cs4r") - calcOutput("EnergyEffPaths", round = 6, file = "f29_efficiency_growth.cs4r") calcOutput("Floorspace", onlyTotal = TRUE, round = 1, file = "p36_floorspace_scen.cs4r") calcOutput("Floorspace", round = 1, file = "f36_floorspace_scen.cs4r") calcOutput("WeightNash", round = 6, file = "p80_eoWeights_fix.cs4r")