From 2bb86ce0020901f0d9e5cd4afd1ce5da9233b6e5 Mon Sep 17 00:00:00 2001 From: johannah-pik <89136160+johannah-pik@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:09:42 +0100 Subject: [PATCH] fix reporting after REMIND run tests --- R/aggregateVariables.R | 6 +- R/convertToMIF.R | 10 ++- R/reportExtendedTransportVarSet.R | 65 +++++++++++-------- R/reportTransportVarSet.R | 4 +- .../cs_06_input_parameters.Rmd | 8 +-- 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/R/aggregateVariables.R b/R/aggregateVariables.R index 7ea77a1..1b76eba 100644 --- a/R/aggregateVariables.R +++ b/R/aggregateVariables.R @@ -100,7 +100,7 @@ aggregateVariables <- function(vars, mapAggregation, weight = NULL) { # Exclude vars that are not aggregated and create solely the variable entry exclude <- c("Load factor", "Load factor raw", "Preference|FV", "Preference|S1S", "Preference|S2S1", "Preference|S3S2", "Preference|VS3", "TCO sales Operating costs (total non-fuel)", "TCO sales Fuel costs", "TCO sales Capital costs", "Time value costs", - "Annual mileage", "Energy intensity sales", "Energy intensity sales raw", "Purchase Price") + "Annual mileage", "Energy intensity sales", "Energy intensity (raw)", "Purchase Price", "Load factor (raw)") aggregatedvars <- createVariableEntry(vars[variable %in% exclude | grepl(".*Iteration.*", variable)], aggrOrder) vars <- vars[!(variable %in% exclude | grepl(".*Iteration.*", variable))] @@ -292,8 +292,8 @@ aggregateVariables <- function(vars, mapAggregation, weight = NULL) { aggregatedvars <- rbind(aggregatedvars, aggrvars) if (anyNA(aggregatedvars)) stop("Output variable contains NAs. - Please check reportAndAggregatedMIF()") + Please check aggregateVariables()") if (anyDuplicated(aggregatedvars[, c("region", "period", "variable")])) stop("Output variable contains Duplicates. - Please check reportAndAggregatedMIF()") + Please check aggregateVariables()") return(aggregatedvars) } diff --git a/R/convertToMIF.R b/R/convertToMIF.R index 6fdbb41..01292bd 100644 --- a/R/convertToMIF.R +++ b/R/convertToMIF.R @@ -47,6 +47,10 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo return(varsNew) } + noAggregationvars <- rbindlist(vars$int[c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE) + varsToMIFint <- rbindlist(vars$int[!names(vars$int) %in% c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE) + varsToMIFext <- rbindlist(vars$ext[!names(vars$ext) %in% c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE) + # Regional aggregation---------------------------------------------------------------------- ## Aggregation to world is always supplied mapWorld <- unique(vars$ext[[1]][, c("region")])[, aggrReg := "World"] @@ -120,18 +124,18 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo setnames(regSubsetDataInt, "aggrReg", "region") return(regSubsetDataInt)}, regSubsetMap, weight) - noAggregationvars <- rbind(rbindlist(vars$int[c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE), + noAggregationvars <- rbind(noAggregationvars, rbindlist(regSubsetDataInt[c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE), rbindlist(worldDataInt[c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE), rbindlist(vars$ext[c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE), rbindlist(regSubsetDataExt[c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE), rbindlist(worldDataExt[c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE)) - varsToMIFint <- rbind(rbindlist(vars$int[!names(vars$int) %in% c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE), + varsToMIFint <- rbind(varsToMIFint, rbindlist(regSubsetDataInt[!names(regSubsetDataInt) %in% c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE), rbindlist(worldDataInt[!names(worldDataInt) %in% c("GDPpcPPP", "GDPpcMER")], fill = TRUE, use.names = TRUE)) - varsToMIFext <- rbind(rbindlist(vars$ext[!names(vars$ext) %in% c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE), + varsToMIFext <- rbind(varsToMIFext, rbindlist(regSubsetDataExt[!names(regSubsetDataExt) %in% c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE), rbindlist(worldDataExt[!names(worldDataExt) %in% c("GDPppp", "population", "GDPMER")], fill = TRUE, use.names = TRUE)) } diff --git a/R/reportExtendedTransportVarSet.R b/R/reportExtendedTransportVarSet.R index 3c99334..3526f35 100644 --- a/R/reportExtendedTransportVarSet.R +++ b/R/reportExtendedTransportVarSet.R @@ -14,6 +14,9 @@ reportExtendedTransportVarSet <- function(data, baseVarSet, timeResReporting) { constrYear <- variable <- period <- . <- value <- NULL + outputVarsExt <- list() + outputVarsInt <- list() + # Report useful energy----------------------------------------------------------------------- fleetUEdemand <- reportUE(FEdemand = baseVarSet$ext$fleetFEdemand, helpers = data$helpers) @@ -36,41 +39,51 @@ reportExtendedTransportVarSet <- function(data, baseVarSet, timeResReporting) { names(data$helpers$decisionTree))) combinedCAPEXandOPEX[, variable := paste0("TCO sales ", variable)] - data$GDPppp[, variable := "GDP|PPP"][, value := value * 1e-3][, unit := "billion constant 2017 Int$PPP"] - data$population[, variable := "Population"][, unit := "million"] - data$GDPpcPPP[, variable := "GDPpc|PPP"][, value := value * 1e-3][, unit := "billion constant 2017 Int$PPP"] - data$GDPMER[, variable := "GDP|MER"][, value := value * 1e-3][, unit := "billion constant 2017 Int$MER"] - data$GDPpcMER[, variable := "GDPpc|MER"][, value := value * 1e-3][, unit := "billion constant 2017 Int$MER"] + if (!is.null(data$GDPppp)) { + data$GDPppp[grepl("mil\\..*", unit), value := value * 1e-3][, unit := gsub("mil\\.", "billion", unit)] + outputVarsExt <- c(outputVarsExt, list(GDPppp = data$GDPppp)) + } + if (!is.null(data$GDPpcPPP)) { + data$GDPpcPPP[grepl("mil\\..*", unit), value := value * 1e-3][, unit := gsub("mil\\.", "billion", unit)] + outputVarsInt <- c(outputVarsInt, list(GDPpcPPP = data$GDPpcPPP)) + } + if (!is.null(data$GDPMER)) { + data$GDPMER[grepl("mil\\..*", unit), value := value * 1e-3][, unit := gsub("mil\\.", "billion", unit)] + outputVarsExt <- c(outputVarsExt, list(GDPMER = data$GDPMER)) + } + if (!is.null(data$GDPpcMER)) { + data$GDPpcMER[grepl("mil\\..*", unit), value := value * 1e-3][, unit := gsub("mil\\.", "billion", unit)] + outputVarsInt <- c(outputVarsInt, list(GDPpcMER = data$GDPpcMER)) + } + if (!is.null(data$population)) { + outputVarsExt <- c(outputVarsExt, list(population = data$population)) + } # Report transport input data if available - inputData <- c("CAPEXother", "nonFuelOPEXtrackedFleet", "subsidies", "timeValueCosts", "annualMileage", "scenSpecLoadFactor", + inputData <- c("timeValueCosts", "annualMileage", "scenSpecLoadFactor", "loadFactorRaw", "scenSpecEnIntensity", "energyIntensityRaw") inputData <- inputData[inputData %in% names(data)] - inputData <- lapply(copy(data[inputData]), function(item, decisionTree) {item <- merge(item, decisionTree, + + if (!is.null(inputData)) { + inputData <- lapply(copy(data[inputData]), function(item, decisionTree) {item <- merge(item, decisionTree, by = intersect(names(item), names(decisionTree)), allow.cartesian = TRUE)}, data$helpers$decisionTree) - inputData$loadFactorRaw[, variable := paste0(variable, " raw")] - inputData$energyIntensityRaw[, variable := paste0(variable, " raw")] + outputVarsInt <- c(outputVarsInt, inputData) + } + + # Split extensive and intensive variables --------------------------------------------------- - outputVarsExt <- list(fleetUEdemand = fleetUEdemand, - vintages = vintages, - GDPppp = data$GDPppp, - GDPMER = data$GDPMER, - population = data$population) - outputVarsInt <- list(scenScpecPrefTrends = data$scenSpecPrefTrends[, level := NULL], - combinedCAPEXandOPEX = combinedCAPEXandOPEX, - CAPEXother = inputData$CAPEXother, - nonFuelOPEXtrackedFleet = inputData$nonFuelOPEXtrackedFleet, - timeValueCosts = inputData$timeValueCosts, - annualMileage = inputData$annualMileage, - loadFactor = inputData$scenSpecLoadFactor, - loadFactorRaw = inputData$loadFactorRaw, - energyIntensity = inputData$scenSpecEnIntensity, - energyIntensityRaw = inputData$energyIntensityRaw, - GDPpcPPP = data$GDPpcPPP, - GDPpcMER = data$GDPpcMER) + outputVarsExt <- c(outputVarsExt, + list(fleetUEdemand = fleetUEdemand, + vintages = vintages) + ) + + outputVarsInt <- c(outputVarsInt, + list(scenScpecPrefTrends = data$scenSpecPrefTrends[, level := NULL], + combinedCAPEXandOPEX = combinedCAPEXandOPEX) + ) outputVars <- list(ext = outputVarsExt, int = outputVarsInt) return(outputVars) diff --git a/R/reportTransportVarSet.R b/R/reportTransportVarSet.R index ea51045..0a8ae57 100644 --- a/R/reportTransportVarSet.R +++ b/R/reportTransportVarSet.R @@ -84,8 +84,8 @@ reportTransportVarSet <- function(data, baseVarSet) { sales = sales, stock = data$fleetSizeAndComposition$fleetVehNumbers, fleetYrlCosts = fleetYrlCosts) - outputVarsInt <- list(upfrontCAPEXtrackedFleet = upfrontCAPEXtrackedFleet, - fleetEnergyIntensity = baseVarSet$int$fleetEnergyIntensity) + outputVarsInt <- list(fleetEnergyIntensity = baseVarSet$int$fleetEnergyIntensity) + if (!is.null(data$upfrontCAPEXtrackedFleet)) outputVarsInt <- c(outputVarsInt, list(upfrontCAPEXtrackedFleet = upfrontCAPEXtrackedFleet)) outputVars <- list(ext = outputVarsExt, int = outputVarsInt) diff --git a/inst/compareScenarios/cs_06_input_parameters.Rmd b/inst/compareScenarios/cs_06_input_parameters.Rmd index 058521a..848a26f 100644 --- a/inst/compareScenarios/cs_06_input_parameters.Rmd +++ b/inst/compareScenarios/cs_06_input_parameters.Rmd @@ -62,7 +62,7 @@ showTransportInputMultiLinePlots <- function(data, vars, scales = "free_y", nrow #### Pass other ```{r} vars <- as.character(unique(data$variable)) -items <- vars[grepl("Load factor raw\\|.*", vars)] +items <- vars[grepl("Load factor (raw)\\|.*", vars)] itemsSplit <- items[grepl(".*Pass.*", items) & !grepl(".*Four Wheelers.*|.*Two Wheelers.*|.*Non-motorized.*", items)] plotData <- data %>% filter(variable %in% itemsSplit) %>% @@ -114,11 +114,11 @@ showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length( ### Energy intensity #### Pass other ```{r} -items <- vars[grepl("Energy intensity sales raw\\|.*", vars)] +items <- vars[grepl("Energy intensity (raw)\\|.*", vars)] itemsSplit <- items[grepl(".*Pass.*", items) & !grepl(".*Four Wheelers.*|.*Two Wheelers.*|.*Non-motorized.*", items)] plotData <- data %>% filter(variable %in% itemsSplit) %>% - mutate(variable = gsub("(Energy intensity raw|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) + mutate(variable = gsub("(Energy intensity (raw)|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) itemsSplit <- unique(plotData$variable) showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 5)) ``` @@ -451,7 +451,7 @@ showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length( ```{r} items <- vars[grepl("Energy intensity sales\\|.*", vars)] itemsSplit <- items[grepl(".*Pass.*", items) & !grepl(".*Four Wheelers.*", items) & !grepl(".*Two Wheelers.*", items)& !grepl(".*Non-motorized.*", items)] -plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Energy intensity|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Energy intensity sales|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) itemsSplit <- as.character(unique(plotData$variable)) showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) ```