diff --git a/.buildlibrary b/.buildlibrary index f394e15..8128f65 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '1019439' +ValidationKey: '1201560' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' @@ -6,3 +6,4 @@ AcceptedWarnings: AcceptedNotes: 'Undefined global functions or variables:' allowLinterWarnings: yes enforceVersionUpdate: no +skipCoverage: no diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index f6ea5d4..d85a316 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -23,14 +23,14 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: | - any::lucode2 - any::covr - any::madrat - any::magclass - any::citation - any::gms - any::goxygen - any::GDPuc + lucode2 + covr + madrat + magclass + citation + gms + goxygen + GDPuc # piam packages also available on CRAN (madrat, magclass, citation, # gms, goxygen, GDPuc) will usually have an outdated binary version # available; by using extra-packages we get the newest version @@ -63,6 +63,6 @@ jobs: shell: Rscript {0} run: | nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps")) - if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE) + if(length(nonDummyTests) > 0 && !lucode2:::loadBuildLibraryConfig()[["skipCoverage"]]) covr::codecov(quiet = FALSE) env: NOT_CRAN: "true" diff --git a/CITATION.cff b/CITATION.cff index 593037b..e0a85ef 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'reporttransport: Reporting package for edgeTransport' -version: 0.5.1 -date-released: '2024-09-23' +version: 0.6.0 +date-released: '2024-10-30' abstract: This package contains edgeTransport-specific routines to report model results. The main functionality is to generate transport reporting variables in MIF format from a given edgeTransport model run folder or REMIND input data. diff --git a/DESCRIPTION b/DESCRIPTION index 3db5ee6..904708a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: reporttransport Title: Reporting package for edgeTransport -Version: 0.5.1 -Date: 2024-09-23 +Version: 0.6.0 +Date: 2024-10-30 Authors@R: person("Johanna", "Hoppe", , "johanna.hoppe@pik-potsdam.de", role = c("aut", "cre")) Description: This package contains edgeTransport-specific routines to diff --git a/R/aggregateVariables.R b/R/aggregateVariables.R index e37ed3d..1b76eba 100644 --- a/R/aggregateVariables.R +++ b/R/aggregateVariables.R @@ -75,6 +75,7 @@ aggregateVariables <- function(vars, mapAggregation, weight = NULL) { # Test for duplicated entries to prevent double counting in the aggregation test <- copy(vars) test[, value := NULL] + if (anyDuplicated(test)) stop("Variables for aggregation contain duplicates. Check reportEdgeTransport() to prevent double counting") @@ -96,6 +97,14 @@ aggregateVariables <- function(vars, mapAggregation, weight = NULL) { setnames(weight, "value", "weight") } + # 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 (raw)", "Purchase Price", "Load factor (raw)") + + aggregatedvars <- createVariableEntry(vars[variable %in% exclude | grepl(".*Iteration.*", variable)], aggrOrder) + vars <- vars[!(variable %in% exclude | grepl(".*Iteration.*", variable))] + # Aggregate each level of the decision tree -------------------------------------------------------------------- for (i in seq(0, length(aggrOrder) - 1)) { aggrvars <- copy(vars) @@ -135,7 +144,7 @@ aggregateVariables <- function(vars, mapAggregation, weight = NULL) { } } - exclude <- c("Sales", "Vintages", "Stock", "Load factor") + exclude <- c("Sales", "Vintages", "Stock") varsForFurtherAggregation <- vars[!variable %in% exclude] # Aggregate Pass with bunkers -------------------------------------------------------------------- @@ -283,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 c32fec2..01292bd 100644 --- a/R/convertToMIF.R +++ b/R/convertToMIF.R @@ -22,6 +22,7 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo applyReportingNames <- function(vars, mapNames) { rename <- function(columns, mapNames) { + dt <- data.table(name = c(columns)) dt[, rownum := .I] dt <- merge(dt, mapNames, by = "name", allow.cartesian = TRUE, all.x = TRUE) @@ -29,6 +30,7 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo setkey(dt, "rownum") dt <- dt[!is.na(reportName), name := reportName] dt[grepl(".*tmp", name), name := NA] + dt[name == "", name := NA] dt[!is.na(name), name := paste0("|", name)] return(dt$name) @@ -45,42 +47,46 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo return(varsNew) } - # Use ES demand as weight to aggregate over modes------------------------------------------- - varsToMIFext <- vars$ext[!names(vars$ext) %in% c("GDPppp", "population")] - noAggregationvars <- vars$ext[c("GDPppp","population")] - varsToMIFext <- rbindlist(varsToMIFext, fill = TRUE, use.names = TRUE) - varsToMIFint <- rbindlist(vars$int, fill = TRUE, use.names = TRUE) - - # Prepare vars that are not aggregated over modes - if (!is.null(noAggregationvars$GDPppp)){ - noAggregationvars$GDPppp[, variable := "GDP|PPP"][, value := value * 1e-3][, unit := "billion constant 2017 Int$PPP"] - noAggregationvars$population[, variable := "Population"][, unit := "million"] - noAggregationvars <- rbindlist(noAggregationvars, fill = TRUE, use.names = TRUE) - } - - # Apply variable naming convention---------------------------------------------------------- - varsToMIFext <- applyReportingNames(varsToMIFext, helpers$reportingNames) - varsToMIFint[, fuel := NA] - varsToMIFint <- applyReportingNames(varsToMIFint, helpers$reportingNames) + 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(varsToMIFext[, c("region")])[, aggrReg := "World"] - worldDataExt <- as.data.table(aggregate_map(varsToMIFext, mapWorld, by = "region")) - if (!is.null(noAggregationvars$GDPppp)) worldDatanoAggregationvars <- as.data.table(aggregate_map(noAggregationvars, mapWorld, by = "region")) - weight <- copy(varsToMIFext[variable == "ES"]) - weight[, c("variable", "unit", "fuel") := NULL] + mapWorld <- unique(vars$ext[[1]][, c("region")])[, aggrReg := "World"] + worldDataExt <- lapply(vars$ext, function(x, mapWorld) as.data.table(aggregate_map(x, mapWorld, by = "region")), mapWorld) + + weight <- copy(vars$ext$fleetESdemand) + weight[, c("variable", "unit") := NULL] setnames(weight, "value", "weight") - weightedInt <- merge(weight,varsToMIFint, by = intersect(names(varsToMIFint), names(weight)), all.x = TRUE) - byCols <- names(weightedInt) - byCols <- byCols[!byCols %in% c("region", "value", "weight")] - weightedInt[, sum := sum(weight), by = eval(byCols)] - weightedInt[sum == 0, weight := 1, by = eval(byCols)][, sum := NULL] - worldDataInt <- weightedInt[, .(value = sum(value * (weight / sum(weight)))), by = eval(byCols)] - worldDataInt[, region := "World"] - varsToMIFint <- rbind(varsToMIFint, worldDataInt) - varsToMIFext <- rbind(varsToMIFext, worldDataExt) - if (!is.null(noAggregationvars$GDPppp)) noAggregationvars <- rbind(noAggregationvars, worldDatanoAggregationvars) + + #split shareweights + if (!is.null(vars$int$scenScpecPrefTrends)) { + Preferences <- list( + PrefrenceFV = vars$int$scenScpecPrefTrends[variable == "Preference|FV"], + PrefrenceS1S = vars$int$scenScpecPrefTrends[variable == "Preference|S1S"], + PrefrenceS2S1 = vars$int$scenScpecPrefTrends[variable == "Preference|S2S1"], + PrefrenceS3S2 = vars$int$scenScpecPrefTrends[variable == "Preference|S3S2"], + PrefrenceVS3 = vars$int$scenScpecPrefTrends[variable == "Preference|VS3"] + ) + vars$int <- vars$int[!names(vars$int) == "scenScpecPrefTrends"] + vars$int <- append(vars$int, Preferences) + } + + worldDataInt <- lapply(vars$int, function(x, weight) { + byCols <- names(x) + #sharweights include empty columns + emptyColumns <- names(x)[sapply(x, function(x) all(is.na(x) | x == ""))] + byCols <- byCols[!byCols %in% c("value") & byCols %in% names(weight) & !byCols %in% emptyColumns] + weight <- weight[, .(weight = sum(weight)), by = eval(byCols)] + weightedInt <- merge(x, weight, by = intersect(names(x), names(weight)), all.x = TRUE) + byCols <- names(weightedInt) + byCols <- byCols[!byCols %in% c("region", "value", "weight")] + weightedInt[, sum := sum(weight), by = eval(byCols)] + weightedInt[sum == 0, weight := 1, by = eval(byCols)][, sum := NULL] + worldDataInt <- weightedInt[, .(value = sum(value * (weight / sum(weight)))), by = eval(byCols)] + worldDataInt[, region := "World"] + return(worldDataInt)}, weight) ## Additional regions ## if regionSubsetList != NULL -> gdx provides 21 region resolution @@ -99,33 +105,52 @@ convertToMIF <- function(vars, GDPMER, helpers, scenario, model, gdx, isTranspo tmp <- data.table(region = regionSubsetList[[i]], aggrReg = namesReg[i]) regSubsetMap <- rbind(regSubsetMap, tmp) } - regSubsetDataExt <- as.data.table(aggregate_map(varsToMIFext[region %in% unique(regSubsetMap$region)], - regSubsetMap, by = "region")) - if (!is.null(noAggregationvars$GDPppp)) regSubsetDataNoAggregationVars <- as.data.table(aggregate_map(noAggregationvars[region %in% unique(regSubsetMap$region)], - regSubsetMap, by = "region")) - weightedInt <- merge(varsToMIFint, regSubsetMap, by = intersect(names(varsToMIFint), names(regSubsetMap)), all.y = TRUE) - weightedInt <- merge(weight, weightedInt, by = intersect(names(weightedInt), names(weight)), all.y = TRUE) - byCols <- names(weightedInt) - byCols <- byCols[!byCols %in% c("region", "value", "weight")] - weightedInt[, sum := sum(weight), by = eval(byCols)] - weightedInt[sum == 0, weight := 1, by = eval(byCols)][, sum := NULL] - regSubsetDataInt <- weightedInt[, .(value = sum(value * (weight / sum(weight)))), by = eval(byCols)] - setnames(regSubsetDataInt, "aggrReg", "region") - - regSubsetDataExt <- as.data.table(aggregate_map(varsToMIFext[region %in% unique(regSubsetMap$region)], - regSubsetMap, by = "region")) - - varsToMIFint <- rbind(varsToMIFint, regSubsetDataInt) - varsToMIFext <- rbind(varsToMIFext, regSubsetDataExt) - if (!is.null(noAggregationvars$GDPppp)) noAggregationvars <- rbind(regSubsetDataNoAggregationVars, noAggregationvars) + regSubsetDataExt <- lapply(vars$ext, function(x, regSubsetMap) { + as.data.table(aggregate_map(x[region %in% unique(regSubsetMap$region)], + regSubsetMap, by = "region"))}, regSubsetMap) + regSubsetDataInt <- lapply(vars$int, function(x, regSubsetMap, weight) { + weightedInt <- merge(x, regSubsetMap, by = intersect(names(x), names(regSubsetMap)), all.y = TRUE) + byCols <- names(x) + #sharweights include empty columns + emptyColumns <- names(x)[sapply(x, function(x) all(is.na(x) | x == ""))] + byCols <- byCols[!byCols %in% c("value") & byCols %in% names(weight) & !byCols %in% emptyColumns] + weight <- weight[, .(weight = sum(weight)), by = eval(byCols)] + weightedInt <- merge(weight, weightedInt, by = intersect(names(weightedInt), names(weight)), all.y = TRUE) + byCols <- names(weightedInt) + byCols <- byCols[!byCols %in% c("region", "value", "weight")] + weightedInt[, sum := sum(weight), by = eval(byCols)] + weightedInt[sum == 0, weight := 1, by = eval(byCols)][, sum := NULL] + regSubsetDataInt <- weightedInt[, .(value = sum(value * (weight / sum(weight)))), by = eval(byCols)] + setnames(regSubsetDataInt, "aggrReg", "region") + return(regSubsetDataInt)}, regSubsetMap, weight) + + 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(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(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)) } + # Apply variable naming convention---------------------------------------------------------- + varsToMIFext <- applyReportingNames(varsToMIFext, helpers$reportingNames) + varsToMIFint[, fuel := NA] + varsToMIFint <- applyReportingNames(varsToMIFint, helpers$reportingNames) + # Aggregate variables----------------------------------------------------------------------- toMIFext <- aggregateVariables(varsToMIFext, helpers$reportingAggregation) weight <- varsToMIFext[variable == "ES"] toMIFint <- aggregateVariables(varsToMIFint, helpers$reportingAggregation, weight) - if (!is.null(noAggregationvars$GDPppp)) { + if (!is.null(noAggregationvars)) { toMIF <- rbind(toMIFint, toMIFext, noAggregationvars) } else {toMIF <- rbind(toMIFint, toMIFext)} diff --git a/R/reportAnalyticsVarSet.R b/R/reportAnalyticsVarSet.R index 2c89dfc..1fff7f9 100644 --- a/R/reportAnalyticsVarSet.R +++ b/R/reportAnalyticsVarSet.R @@ -13,19 +13,52 @@ reportAnalyticsVarSet <- function(data, timeResReporting) { updatedEndogenousCosts <- list() policyMask <- list() rawEndogenousCost <- list() + allCostsFV <- list() + allCostsVS3 <- list() + allCostsS3S2 <- list() + allCostsS2S1 <- list() + allCostsS1S <- list() - for (i in seq_along(data$endogenousCostsIterations)) { - updatedEndogenousCosts[[i]] <- data$endogenousCostsIterations[[i]]$updatedEndogenousCosts - policyMask[[i]] <- data$endogenousCostsIterations[[i]]$policyMask - rawEndogenousCost[[i]] <- data$endogenousCostsIterations[[i]]$rawEndogenousCost + endogenousCostsData <- data[names(data)[grepl("endogenousCostsIteration.*", names(data))]] + for (i in 1:length(endogenousCostsData)) { + updatedEndogenousCosts[[i]] <- endogenousCostsData[[i]]$updatedEndogenousCosts + policyMask[[i]] <- endogenousCostsData[[i]]$policyMask + rawEndogenousCost[[i]] <- endogenousCostsData[[i]]$rawEndogenousCosts } updatedEndogenousCosts <- rbindlist(updatedEndogenousCosts) policyMask <- rbindlist(policyMask) rawEndogenousCost <- rbindlist(rawEndogenousCost) - fleetVehNumbersIterations <- rbindlist(data$fleetVehNumbersIterations) + fleetVehNumbersIterations <- rbindlist(data[names(data)[grepl("fleetVehNumbersIteration[0-9]+", names(data))]]) + costsDiscreteChoiceData <- data[names(data)[grepl("costsDiscreteChoiceIteration[0-9]+", names(data))]] + cols <- names(data$helpers$decisionTree) + for (i in 1:length(costsDiscreteChoiceData)) { + lapply(costsDiscreteChoiceData[[i]], function(dt, cols){ + colsdt <- names(dt) + missingCols <- cols[!cols %in% intersect(colsdt, cols)] + if (!length(missingCols) == 0) dt[, eval(missingCols) := ""] + }, cols) + allCostsFV[[i]] <- costsDiscreteChoiceData[[i]]$allCostsFV + allCostsVS3[[i]] <- costsDiscreteChoiceData[[i]]$allCostsVS3 + allCostsS3S2[[i]] <- costsDiscreteChoiceData[[i]]$allCostsS3S2 + allCostsS2S1[[i]] <- costsDiscreteChoiceData[[i]]$allCostsS2S1 + allCostsS1S[[i]] <- costsDiscreteChoiceData[[i]]$allCostsS1S + } - analyticsData <- list(updatedEndogenousCosts, policyMask, rawEndogenousCost, fleetVehNumbersIterations) + allCostsFV <- rbindlist(allCostsFV) + allCostsVS3 <- rbindlist(allCostsVS3) + allCostsS3S2 <- rbindlist(allCostsS3S2) + allCostsS2S1 <- rbindlist(allCostsS2S1) + allCostsS1S <- rbindlist(allCostsS1S) + analyticsData <- list(updatedEndogenousCosts = updatedEndogenousCosts, + policyMask = policyMask, + rawEndogenousCost = rawEndogenousCost, + fleetVehNumbersIterations = fleetVehNumbersIterations, + allCostsFV = allCostsFV, + allCostsVS3 = allCostsVS3, + allCostsS3S2 = allCostsS3S2, + allCostsS2S1 = allCostsS2S1, + allCostsS1S = allCostsS1S) return(analyticsData) } diff --git a/R/reportEdgeTransport.R b/R/reportEdgeTransport.R index b11fae0..daf4390 100644 --- a/R/reportEdgeTransport.R +++ b/R/reportEdgeTransport.R @@ -42,78 +42,82 @@ reportEdgeTransport <- function(folderPath = file.path(".", "EDGE-T"), data = NU isTransportExtendedReported = FALSE, isAnalyticsReported = FALSE, isREMINDinputReported = FALSE, isStored = TRUE, ...) { - # If you want to change timeResReporting to timesteps outside the modeleled timesteps, - # please add an interpolation step - timeResReporting <- c(seq(2005, 2060, by = 5), seq(2070, 2110, by = 10), 2130, 2150) + applyReportingTimeRes <- function(item, timeRes) { + if (typeof(item) %in% c("character", "double") | "decisionTree" %in% names(item)) return(item) + else if (is.data.table(item) & ("period" %chin% colnames(item))) item <- item[period %in% timeRes] + else if (typeof(item) == "list" & !is.data.table(item)) { + item <- lapply(item, applyReportingTimeRes, timeRes) + } + return(item) + } ######################################################################### ## Load data for reporting if data is not supplied in function call ######################################################################### + # collect data supplied in the function args <- list(...) if (is.null(data)) data <- list() data <- append(data, args) - # load files needed for all + # Load necessary data that is not yet available + ## Define which files are needed for the configuration of switches + filesToLoad <- c("hybridElecShare", + "helpers", + "combinedCAPEXandOPEX", + "scenSpecEnIntensity", + "scenSpecLoadFactor", + "fleetSizeAndComposition", + "ESdemandFVsalesLevel") + if (isREMINDinputReported) { + add <- c("annualMileage", "timeValueCosts", "scenSpecPrefTrends", "initialIncoCosts") + filesToLoad <- c(filesToLoad, add[!add %in% filesToLoad]) + } + if (isTransportReported) { + add <- c("upfrontCAPEXtrackedFleet") + filesToLoad <- c(filesToLoad, add[!add %in% filesToLoad]) + + if (isTransportExtendedReported) { + add <- c("population", "GDPppp", "GDPpcPPP", "GDPpcMER","GDPMER", "annualMileage", "energyIntensityRaw", + "loadFactorRaw", "CAPEXother", "nonFuelOPEXother", "nonFuelOPEXtrackedFleet", "subsidies", + "timeValueCosts", "scenSpecPrefTrends", "initialIncoCosts") + filesToLoad <- c(filesToLoad, add[!add %in% filesToLoad]) + } + if (isAnalyticsReported) { + add <- c("fleetVehNumbersIteration[0-9]+", "endogenousCostsIteration[0-9]+", "costsDiscreteChoiceIteration[0-9]+") + filesToLoad <- c(filesToLoad, add[!add %in% filesToLoad]) + } + } + filesToLoad <- c(filesToLoad[!filesToLoad %in% names(data)]) + + # Load data if (is.null(data$SSPscen)) { cfg <- readRDS(file.path(folderPath, "cfg.RDS")) data <- append(data, cfg[names(cfg) %in% c("SSPscen", "transportPolScen", "demScen")]) } if (is.null(data$scenarioName)) data$scenarioName <- paste0(data$transportPolScen, " ", data$SSPscen) if (is.null(data$modelName)) data$modelName <- "EDGE-T" - if (is.null(data$hybridElecShare)) data$hybridElecShare <- readRDS(file.path(folderPath, "1_InputDataRaw", "hybridElecShare.RDS")) - if (is.null(data$helpers)) data$helpers <- readRDS(file.path(folderPath, "1_InputDataRaw", "helpers.RDS")) - if (is.null(data$combinedCAPEXandOPEX)) data$combinedCAPEXandOPEX <- readRDS(file.path(folderPath, "2_InputDataPolicy", "combinedCAPEXandOPEX.RDS")) - if (is.null(data$scenSpecEnIntensity)) data$scenSpecEnIntensity <- readRDS(file.path(folderPath, "2_InputDataPolicy", "scenSpecEnIntensity.RDS")) - if (is.null(data$scenSpecLoadFactor)) data$scenSpecLoadFactor <- readRDS(file.path(folderPath, "2_InputDataPolicy", "scenSpecLoadFactor.RDS")) - if (is.null(data$fleetSizeAndComposition)) data$fleetSizeAndComposition <- readRDS(file.path(folderPath, "4_Output", "fleetSizeAndComposition.RDS")) - if (is.null(data$ESdemandFVsalesLevel)) data$ESdemandFVsalesLevel <- readRDS(file.path(folderPath, "4_Output", "ESdemandFVsalesLevel.RDS")) - - # load files for standard and extended transport reporting - if (isTransportReported) { - if (is.null(data$upfrontCAPEXtrackedFleet) & length(list.files(folderPath, "upfrontCAPEXtrackedFleet.RDS", recursive = TRUE, full.names = TRUE)) > 0) - data$upfrontCAPEXtrackedFleet <- readRDS(file.path(folderPath, "2_InputDataPolicy", "upfrontCAPEXtrackedFleet.RDS")) - if (is.null(data$population) & length(list.files(folderPath, "population.RDS", recursive = TRUE, full.names = TRUE)) > 0) - data$population <- readRDS(file.path(folderPath, "1_InputDataRaw", "population.RDS")) - if (is.null(data$GDPppp) & length(list.files(folderPath, "GDPppp.RDS", recursive = TRUE, full.names = TRUE)) > 0) - data$GDPppp <- readRDS(file.path(folderPath, "1_InputDataRaw", "GDPppp.RDS")) - if (is.null(data$gdxPath)) { - gdxPath <- list.files(path = folderPath, pattern = "\\.gdx$", full.names = TRUE) - # Check if any files were found - if (length(gdxPath) > 1) { - gdxPath <- gdxPath[1] - cat("More than one gdx file found. The following one was chosen\n") - cat(gdxPath, sep = "\n") - } else if (length(gdxPath) == 0) { - stop("No gdx files found in the specified directory.\n") - } - data$gdxPath <- gdxPath - } - } - if (isAnalyticsReported) { - # load files for analytic purposes - if (is.null(data$fleetVehNumbersIterations)) { - fleetFilesIterations <- list.files(path = file.path(folderPath, "4_Output"), - pattern = "fleetVehNumbersIteration.*", full.names = TRUE) - if (length(fleetFilesIterations) > 0) { - data$fleetVehNumbersIterations <- lapply(fleetFilesIterations, readRDS) - } - } - if (is.null(data$endogenousCostsIterations)) { - endogenousCostFilesIterations <- list.files(path = file.path(folderPath, "4_Output"), - pattern = "endogenousCostsIteration.*", - full.names = TRUE) - if (length(endogenousCostFilesIterations) > 0) { - data$endogenousCostsIterations <- lapply(endogenousCostFilesIterations, readRDS) - } + + if (is.null(data$gdxPath)) { + gdxPath <- list.files(path = folderPath, pattern = "\\.gdx$", full.names = TRUE) + # Check if any files were found + if (length(gdxPath) > 1) { + gdxPath <- gdxPath[1] + cat("More than one gdx file found. The following one was chosen\n") + cat(gdxPath, sep = "\n") + } else if (length(gdxPath) == 0) { + stop("No gdx files found in the specified directory.\n") } + data$gdxPath <- gdxPath } - if (isREMINDinputReported) { - # load files for REMIND input data only reporting - if (is.null(data$annualMileage)) data$annualMileage <- readRDS(file.path(folderPath, "1_InputDataRaw", "annualMileage.RDS")) - if (is.null(data$timeValueCosts)) data$timeValueCosts <- readRDS(file.path(folderPath, "1_InputDataRaw", "timeValueCosts.RDS")) - if (is.null(data$scenSpecPrefTrends)) data$scenSpecPrefTrends <- readRDS(file.path(folderPath, "2_InputDataPolicy", "scenSpecPrefTrends.RDS")) - if (is.null(data$initialIncoCosts)) data$initialIncoCosts <- readRDS(file.path(folderPath, "2_InputDataPolicy", "initialIncoCosts.RDS")) + if (length(filesToLoad) > 0) { + filePaths <- list.files(folderPath, recursive = TRUE, full.names = TRUE) + pathFilesToLoad <- unlist(lapply(filesToLoad, function(x) {filePaths[grepl(paste0(x, ".RDS"), filePaths)]})) + itemNames <- basename(pathFilesToLoad) + itemNames <- sub("\\.RDS$", "", itemNames) + addFiles <- lapply(pathFilesToLoad, readRDS) + names(addFiles) <- itemNames + data <- c(data, addFiles) } ######################################################################### ## Report output variables @@ -123,22 +127,62 @@ reportEdgeTransport <- function(folderPath = file.path(".", "EDGE-T"), data = NU reporting <- baseVarSet outputVars <- baseVarSet + ######################################################################### + ## Report REMIND input data + ######################################################################### + if (isREMINDinputReported) { # nolint: object_name_linter + + timeResReporting <- c(seq(1900,1985,5), + seq(1990, 2060, by = 5), + seq(2070, 2110, by = 10), + 2130, 2150) + REMINDinputData <- reportREMINDinputVarSet(fleetESdemand = baseVarSet$ext$fleetESdemand, # nolint: object_name_linter + fleetFEdemand = baseVarSet$ext$fleetFEdemand, + fleetEnergyIntensity = baseVarSet$int$fleetEnergyIntensity, + fleetCapCosts = baseVarSet$int$fleetCost[variable == "Capital costs"], + combinedCAPEXandOPEX = data$combinedCAPEXandOPEX, + scenSpecLoadFactor = data$scenSpecLoadFactor, + scenSpecPrefTrends = data$scenSpecPrefTrends, + scenSpecEnIntensity = data$scenSpecEnIntensity, + initialIncoCosts = data$initialIncoCosts, + annualMileage = data$annualMileage, + timeValueCosts = data$timeValueCosts, + hybridElecShare = data$hybridElecShare, + demScen = data$demScen, + SSPscen = data$SSPscen, + transportPolScen = data$transportPolScen, + timeResReporting = timeResReporting, + helpers = data$helpers) + + reporting <- REMINDinputData + if (isStored) storeData(outputFolder = folderPath, REMINDinputData = REMINDinputData) + } + ######################################################################### + ## Report transport variables + ######################################################################### + # If you want to change timeResReporting to timesteps outside the modeleled timesteps, + # please add an interpolation step + timeResReporting <- c(seq(2005, 2060, by = 5), seq(2070, 2110, by = 10), 2130, 2150) + # Apply time resolution that should be reported + data <- lapply(data, applyReportingTimeRes, timeResReporting) + baseVarSet <- lapply(baseVarSet, applyReportingTimeRes, timeResReporting) + if (isTransportReported) { transportVarSet <- reportTransportVarSet(data = data, - baseVarSet = baseVarSet, - timeResReporting = timeResReporting) + baseVarSet = baseVarSet) outputVars <- transportVarSet + if (isTransportExtendedReported) { extendedTransportVarSet <- reportExtendedTransportVarSet(data = data, - baseVarSet = baseVarSet, - timeResReporting = timeResReporting) + baseVarSet = baseVarSet) + outputVars$ext <- append(outputVars$ext, extendedTransportVarSet$ext) outputVars$int <- append(outputVars$int, extendedTransportVarSet$int) } if (isAnalyticsReported) { - if (!is.null(data$endogenousCostsIterations)) { + if (!is.null(data$endogenousCostsIteration1)) { analyticsVarSet <- reportAnalyticsVarSet(data = data, timeResReporting = timeResReporting) - outputVars$analytic <- analyticsVarSet + outputVars$int <- c(outputVars$int, analyticsVarSet) } else { message("Analytics data not stored in the run folder. Analytics reporting is skipped.") } @@ -161,31 +205,5 @@ reportEdgeTransport <- function(folderPath = file.path(".", "EDGE-T"), data = NU if (isStored) write.mif(reporting, file.path(folderPath, "Transport.mif")) } - ######################################################################### - ## Report REMIND input data - ######################################################################### - if (isREMINDinputReported) { # nolint: object_name_linter - REMINDinputData <- reportREMINDinputVarSet(fleetESdemand = baseVarSet$ext$fleetESdemand, # nolint: object_name_linter - fleetFEdemand = baseVarSet$ext$fleetFEdemand, - fleetEnergyIntensity = baseVarSet$int$fleetEnergyIntensity, - fleetCapCosts = baseVarSet$int$fleetCost[variable == "Capital costs"], - combinedCAPEXandOPEX = data$combinedCAPEXandOPEX, - scenSpecLoadFactor = data$scenSpecLoadFactor, - scenSpecPrefTrends = data$scenSpecPrefTrends, - scenSpecEnIntensity = data$scenSpecEnIntensity, - initialIncoCosts = data$initialIncoCosts, - annualMileage = data$annualMileage, - timeValueCosts = data$timeValueCosts, - hybridElecShare = data$hybridElecShare, - demScen = data$demScen, - SSPscen = data$SSPscen, - transportPolScen = data$transportPolScen, - timeResReporting = timeResReporting, - helpers = data$helpers) - - reporting <- REMINDinputData - if (isStored) storeData(outputFolder = folderPath, REMINDinputData = REMINDinputData) - } - return(reporting) } diff --git a/R/reportExtendedTransportVarSet.R b/R/reportExtendedTransportVarSet.R index 5705e68..3526f35 100644 --- a/R/reportExtendedTransportVarSet.R +++ b/R/reportExtendedTransportVarSet.R @@ -14,15 +14,11 @@ reportExtendedTransportVarSet <- function(data, baseVarSet, timeResReporting) { constrYear <- variable <- period <- . <- value <- NULL - # Switch from mixed time resolution to the reporting time resolution for all vars------------ - loadFactor <- copy(data$scenSpecLoadFactor)[period %in% timeResReporting] - fleetFEdemand <- baseVarSet$ext$fleetFEdemand[period %in% timeResReporting] - fleetCost <- baseVarSet$int$fleetCost[period %in% timeResReporting] - population <- data$population[period %in% timeResReporting] - GDPppp <- data$GDPppp[period %in% timeResReporting] + outputVarsExt <- list() + outputVarsInt <- list() # Report useful energy----------------------------------------------------------------------- - fleetUEdemand <- reportUE(FEdemand = fleetFEdemand, + fleetUEdemand <- reportUE(FEdemand = baseVarSet$ext$fleetFEdemand, helpers = data$helpers) # Report vintages (stock without sales)------------------------------------------------------- @@ -30,17 +26,64 @@ reportExtendedTransportVarSet <- function(data, baseVarSet, timeResReporting) { vintages[, variable := "Vintages"][, constrYear := NULL] cols <- names(vintages) vintages <- vintages[, .(value = sum(value)), by = eval(cols[!cols %in% c("value", "constrYear")])] - vintages <- approx_dt(vintages, timeResReporting, "period", "value", extrapolate = TRUE) - loadFactor <- merge(loadFactor, data$helpers$decisionTree, - by = intersect(names(loadFactor), names(data$helpers$decisionTree))) + vintages <- approx_dt(vintages, unique(fleetUEdemand$period), "period", "value", extrapolate = TRUE) + + # Report annualized TCO per pkm/tkm + aggregatedCAPEX <- data$combinedCAPEXandOPEX[grepl(".*Capital.*", variable)] + aggregatedCAPEX <- aggregatedCAPEX[, .(value = sum(value)), by = c("region", "period", "univocalName", "technology", "unit")] + aggregatedCAPEX[, variable := "Capital costs"] + combinedCAPEXandOPEX <- rbind(data$combinedCAPEXandOPEX[!grepl(".*Capital.*", variable)], + aggregatedCAPEX) + combinedCAPEXandOPEX <- merge(combinedCAPEXandOPEX, data$helpers$decisionTree, + by = intersect(names(data$combinedCAPEXandOPEX), + names(data$helpers$decisionTree))) + combinedCAPEXandOPEX[, variable := paste0("TCO sales ", variable)] + + 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("timeValueCosts", "annualMileage", "scenSpecLoadFactor", + "loadFactorRaw", "scenSpecEnIntensity", "energyIntensityRaw") + inputData <- inputData[inputData %in% names(data)] + + 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) + outputVarsInt <- c(outputVarsInt, inputData) + } + + # Split extensive and intensive variables --------------------------------------------------- - outputVarsExt <- list(fleetUEdemand = fleetUEdemand, - vintages = vintages, - population = population, - GDPppp = GDPppp) - outputVarsInt <- list(loadFactor = loadFactor, - fleetCost = fleetCost) + 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/reportLiquidsAndGasesComposition.R b/R/reportLiquidsAndGasesComposition.R index 4e77365..c518285 100644 --- a/R/reportLiquidsAndGasesComposition.R +++ b/R/reportLiquidsAndGasesComposition.R @@ -3,7 +3,6 @@ #' @param dtFE Final energy data for liquids and gases #' @param gdxPath Path to REMIND gdx, which contains the share of the various production routes #' for liquid and gaseous energy carriers -#' @param timeResReporting Time resolution for variable reporting #' @param helpers List of helpers #' #' @returns Final energy for liquids and gases split into fossil|bio|hydrogen @@ -13,13 +12,13 @@ #' @import data.table #' @export -reportLiquidsAndGasesComposition <- function(dtFE, gdxPath, timeResReporting, helpers) { +reportLiquidsAndGasesComposition <- function(dtFE, gdxPath, helpers) { all <- value <- Fossil <- Biomass <- Hydrogen <- variable <- type <- from <- bioToSynShareOverall <- synToBioShareOverall <- fuel <- technology <- univocalName <- share <- emiSectors <- period <- to <- from <- sumbio <- sumsyn <- . <- region <- unit <- NULL - calcSplit <- function(REMINDsegment, dataREMIND, splitOverall, timeResReporting) { # nolint: object_name_linter + calcSplit <- function(REMINDsegment, dataREMIND, splitOverall) { # nolint: object_name_linter # Final energy carrier types liquids and gases consist of the following secondary energy carrier types in REMIND mixedCarrierTypes <- c("seliqfos", "seliqbio", "seliqsyn", "segafos", "segabio", "segasyn") @@ -78,8 +77,6 @@ reportLiquidsAndGasesComposition <- function(dtFE, gdxPath, timeResReporting, he shares <- sharesLiquids } - # apply low time resolution - shares <- approx_dt(shares, timeResReporting, "period", "value", extrapolate = TRUE) shares[, sum := sum(value), by = c("region", "period", "technology")] if (anyNA(shares) | nrow(shares[(sum < 0.9999 | sum > 1.0001) & sum != 0])) { @@ -152,12 +149,13 @@ reportLiquidsAndGasesComposition <- function(dtFE, gdxPath, timeResReporting, he splitTransportOverall <- list(liqBioToSyn = liqBioToSyn, gasesBioToSyn = gasesBioToSyn) REMINDsegments <- c("LDVs", "nonLDVs", "bunker") # nolint: object_name_linter - splitShares <- sapply(REMINDsegments, calcSplit, demFeSector, splitTransportOverall, timeResReporting, # nolint: undesirable_function_linter + splitShares <- sapply(REMINDsegments, calcSplit, demFeSector, splitTransportOverall, # nolint: undesirable_function_linter simplify = FALSE, USE.NAMES = TRUE) # Make sure that only Liquids are supplied dtFE <- copy(dtFE) dtFE <- dtFE[technology %in% c("Liquids", "Gases")] + splitShares <- lapply(splitShares, approx_dt, xdata = unique(dtFE$period), xcol = "period", ycol = "value", extrapolate = TRUE) splittedCarriers <- rbindlist(lapply(REMINDsegments, applySplit, dtFE, splitShares, helpers)) splitShares[["LDVs"]][, variable := paste0(variable, "Transport|LDV")] splitShares[["nonLDVs"]][, variable := paste0(variable, "Transport|Other")] diff --git a/R/reportREMINDinputVarSet.R b/R/reportREMINDinputVarSet.R index c0fa008..280a11e 100644 --- a/R/reportREMINDinputVarSet.R +++ b/R/reportREMINDinputVarSet.R @@ -44,11 +44,6 @@ reportREMINDinputVarSet <- function(fleetESdemand, DEM_scenario <- GDP_scenario <- EDGE_scenario <- value <- sumES <- variable <- univocalName <- ESdemand <- NULL - timeResReporting <- c(seq(1900,1985,5), - seq(1990, 2060, by = 5), - seq(2070, 2110, by = 10), - 2130, 2150) - ## Input data for transport module GAMS code---------------------------------------------------------------------------- # See needed inputs in REMIND/modules/35_transport/edge_esm/datainput.gms diff --git a/R/reportTransportVarSet.R b/R/reportTransportVarSet.R index 74e6bc3..0a8ae57 100644 --- a/R/reportTransportVarSet.R +++ b/R/reportTransportVarSet.R @@ -2,34 +2,22 @@ #' #' @param data List that contains the model results to report the detailed transport variable set #' @param baseVarSet Basic output variable set -#' @param timeResReporting Timesteps to be reported #' #' @returns Detailed transport output variable set #' @author Johanna Hoppe #' @import data.table #' @export -reportTransportVarSet <- function(data, baseVarSet, timeResReporting) { +reportTransportVarSet <- function(data, baseVarSet) { fuel <- variable <- value <- constrYear <- period <- technology <- . <- ESdemand <- unit <- NULL - # Switch from mixed time resolution to the reporting time resolution for all vars------------------------ - data$ESdemandFVsalesLevel <- data$ESdemandFVsalesLevel[period %in% timeResReporting] - data$fleetSizeAndComposition <- lapply(data$fleetSizeAndComposition, - FUN = function(x) x <- x[period %in% timeResReporting]) - data$upfrontCAPEXtrackedFleet <- data$upfrontCAPEXtrackedFleet[period %in% timeResReporting] - fleetFEdemand <- baseVarSet$ext$fleetFEdemand[period %in% timeResReporting] - fleetESdemand <- baseVarSet$ext$fleetESdemand[period %in% timeResReporting] - fleetCost <- baseVarSet$int$fleetCost[period %in% timeResReporting] - fleetEnergyIntensity <- baseVarSet$int$fleetEnergyIntensity[period %in% timeResReporting] - # Report liquids and gases split---------------------------------------------------------------------- - varsFEcomposition <- fleetFEdemand[technology %in% c("Liquids", "Gases")] + varsFEcomposition <- baseVarSet$ext$fleetFEdemand[technology %in% c("Liquids", "Gases")] mixedCarrierSplit <- reportLiquidsAndGasesComposition(dtFE = varsFEcomposition, gdxPath = data$gdxPath, - timeResReporting = timeResReporting, helpers = data$helpers) - fleetFEdemandsplittedCarriers <- copy(fleetFEdemand[!technology %in% c("Liquids", "Gases")]) + fleetFEdemandsplittedCarriers <- copy(baseVarSet$ext$fleetFEdemand[!technology %in% c("Liquids", "Gases")]) fleetFEdemandsplittedCarriers[, fuel := NA] fleetFEdemandsplittedCarriers <- rbind(fleetFEdemandsplittedCarriers, mixedCarrierSplit$splittedCarriers) @@ -56,16 +44,17 @@ reportTransportVarSet <- function(data, baseVarSet, timeResReporting) { # Report vehicle sales----------------------------------------------------------------------------------- sales <- copy(data$fleetSizeAndComposition$fleetVehNumbersConstrYears[period == constrYear]) sales[, variable := "Sales"][, constrYear := NULL] - sales <- approx_dt(sales, timeResReporting, "period", "value", extrapolate = TRUE) + sales <- approx_dt(sales, unique(fleetEmissions$period), "period", "value", extrapolate = TRUE) # Report yearly investment costs------------------------------------------------------------------------- - fleetES <- copy(fleetESdemand) + fleetES <- copy(baseVarSet$ext$fleetESdemand) fleetES[, c("variable", "unit") := NULL] setnames(fleetES, "value", "ESdemand") - fleetYrlCosts <- merge(fleetCost, fleetES, - by = intersect(names(fleetCost), names(fleetES))) + fleetYrlCosts <- merge(baseVarSet$int$fleetCost, fleetES, + by = intersect(names(baseVarSet$int$fleetCost), names(fleetES))) fleetYrlCosts[, value := value * ESdemand][, unit := "billion US$2017/yr"][, ESdemand := NULL] fleetYrlCosts[variable == "Capital costs", variable := "Annualized fleet investments"] + fleetYrlCosts[variable == "Operating costs (total non-fuel)", variable := "Operating costs fleet (total non-fuel)"] fleetYrlCosts[variable == "Fuel costs", @@ -80,20 +69,23 @@ reportTransportVarSet <- function(data, baseVarSet, timeResReporting) { # Report upfront capital cost for vehicle sales if (!is.null(data$upfrontCAPEXtrackedFleet)) { - data$upfrontCAPEXtrackedFleet <- copy(data$upfrontCAPEXtrackedFleet) - data$upfrontCAPEXtrackedFleet <- merge(data$upfrontCAPEXtrackedFleet, data$helpers$decisionTree, + upfrontCAPEXtrackedFleet <- data$upfrontCAPEXtrackedFleet + upfrontCAPEXtrackedFleet <- upfrontCAPEXtrackedFleet[, .(value = sum(value)), by = c("region", "period", "univocalName", "technology", "unit")] + upfrontCAPEXtrackedFleet[, variable := "Purchase Price"] + upfrontCAPEXtrackedFleet <- merge(upfrontCAPEXtrackedFleet, data$helpers$decisionTree, by = intersect(names(data$upfrontCAPEXtrackedFleet), names(data$helpers$decisionTree))) } + # Split extensive and intensive variables --------------------------------------------------- outputVarsExt <- list(FEsplittedCarriers = fleetFEdemandsplittedCarriers, - fleetESdemand = fleetESdemand, + fleetESdemand = baseVarSet$ext$fleetESdemand, fleetEmissions = fleetEmissions, sales = sales, stock = data$fleetSizeAndComposition$fleetVehNumbers, fleetYrlCosts = fleetYrlCosts) - outputVarsInt <- list(upfrontCAPEXtrackedFleet = data$upfrontCAPEXtrackedFleet, - fleetEnergyIntensity = 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/R/storeData.R b/R/storeData.R index 46b3862..50f8f6e 100644 --- a/R/storeData.R +++ b/R/storeData.R @@ -28,6 +28,9 @@ storeData <- function(outputFolder, varsList = NULL, ...) { "timeValueCosts", "subsidies", "GDPppp", + "GDPMER", + "GDPpcMER", + "GDPpcPPP", "population", "f29_trpdemand", "helpers")) subfolder <- "1_InputDataRaw" @@ -42,6 +45,7 @@ storeData <- function(outputFolder, varsList = NULL, ...) { if (varName %in% c("histPrefs")) subfolder <- "3_Calibration" if (varName %in% c("fleetSizeAndComposition", "vehSalesAndModeShares", + "costsDiscreteChoiceIterations", "fleetVehNumbersIterations", "endogenousCostsIterations", "endogenousCosts", @@ -120,6 +124,13 @@ storeData <- function(outputFolder, varsList = NULL, ...) { } vars <- vars[!names(vars) %in% c("endogenousCostsIterations")] } + if (!is.null(vars$costsDiscreteChoiceIterations)) { + for (i in seq_along(vars$costsDiscreteChoiceIterations)) { + saveRDS(vars$costsDiscreteChoiceIterations[[i]], file.path(outputFolder, "4_Output", + paste0("costsDiscreteChoiceIteration", i, ".RDS"))) + } + vars <- vars[!names(vars) %in% c("endogenousCostsIterations")] + } # store REMIND inputdata if provided if (!is.null(vars$REMINDinputData)) { diff --git a/R/supportFunctions.R b/R/supportFunctions.R index 3855356..727cbf2 100644 --- a/R/supportFunctions.R +++ b/R/supportFunctions.R @@ -83,3 +83,6 @@ checkForNAsAndDups <- function(dt, varname, codePosition) { } } + + + diff --git a/README.md b/README.md index 0b2bb28..a3cb277 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Reporting package for edgeTransport -R package **reporttransport**, version **0.5.1** +R package **reporttransport**, version **0.6.0** [![CRAN status](https://www.r-pkg.org/badges/version/reporttransport)](https://cran.r-project.org/package=reporttransport) [![R build status](https://github.com/pik-piam/reporttransport/workflows/check/badge.svg)](https://github.com/pik-piam/reporttransport/actions) [![codecov](https://codecov.io/gh/pik-piam/reporttransport/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/reporttransport) [![r-universe](https://pik-piam.r-universe.dev/badges/reporttransport)](https://pik-piam.r-universe.dev/builds) @@ -41,7 +41,7 @@ In case of questions / problems please contact Johanna Hoppe . +Hoppe J (2024). _reporttransport: Reporting package for edgeTransport_. R package version 0.6.0, . A BibTeX entry for LaTeX users is @@ -50,7 +50,7 @@ A BibTeX entry for LaTeX users is title = {reporttransport: Reporting package for edgeTransport}, author = {Johanna Hoppe}, year = {2024}, - note = {R package version 0.5.1}, + note = {R package version 0.6.0}, url = {https://github.com/pik-piam/reporttransport}, } ``` diff --git a/inst/compareScenarios/cs_06_input_parameters.Rmd b/inst/compareScenarios/cs_06_input_parameters.Rmd new file mode 100644 index 0000000..848a26f --- /dev/null +++ b/inst/compareScenarios/cs_06_input_parameters.Rmd @@ -0,0 +1,587 @@ +# Model input parameters +## Raw input data +```{r} +library(dplyr) +library(ggplot2) + +showTransportInputMultiLinePlots <- function(data, vars, scales = "free_y", nrowNum = 1, mainReg = getOption("mip.mainReg")) { + + # Validate function arguments + stopifnot(is.character(vars)) + stopifnot(is.character(scales) && length(scales) == 1) + stopifnot(is.character(mainReg) && length(mainReg) == 1) + + # Data preprocessing + d <- data %>% + filter(variable %in% vars) %>% + droplevels() + + dMainScen <- d %>% + filter(region == mainReg, scenario != "historical") %>% + droplevels() + + dMainHist <- d %>% + filter(region == mainReg, scenario == "historical") %>% + droplevels() + + dRegiScen <- d %>% + filter(region != mainReg, scenario != "historical") %>% + droplevels() + + dRegiHist <- d %>% + filter(region != mainReg, scenario == "historical") %>% + droplevels() + + regions <- levels(dRegiScen$region) + + # Check if there is any data to plot + if (nrow(dMainScen) == 0) { + warning("Nothing to plot.", call. = FALSE) + return(invisible(NULL)) + } + + label <- paste0("(", paste0(levels(d$unit), collapse = ","), ")") + + p2 <- dRegiScen %>% + ggplot(aes(x = period, y = value, color = region)) + + geom_line(aes(linetype = scenario)) + + geom_point(data = dRegiHist, aes(shape = model)) + + geom_line(data = dRegiHist, aes(group = paste0(model, region)), alpha = 0.5) + + facet_wrap(~ variable, scales = scales, nrow = nrowNum) + + theme_minimal() + + scale_color_manual(values = plotstyle(regions)) + + expand_limits(y = 0) + + ylab(label) + + print(p2) + cat("\n\n") +} +``` + +### Load factor +#### Pass other +```{r} +vars <- as.character(unique(data$variable)) +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) %>% + mutate(variable = gsub("(Load factor raw|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 5)) +``` + +#### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +#### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight Trucks +```{r} +itemsSplit <- items[grepl(".*Freight.*", .) & grepl(".*Truck.*", .)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +### Energy intensity +#### Pass other +```{r} +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)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 5)) +``` + +#### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +#### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight Trucks +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +### Annual mileage +#### Cars +```{r} +items <- vars[grepl(".*Annual mileage.*", vars)] +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +#### Busses +```{r} +itemsSplit <- items[grepl(".*Bus.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|", "", variable)) +itemsSplit <- unique(plotData$variable) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit) / 6)) +``` + +#### Trucks +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### Purchase Price Cars, Trucks and Busses +#### Cars +```{r} +items <- vars[grepl(".*Purchase Price.*", vars)] +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Busses +```{r} +itemsSplit <- items[grepl(".*Bus.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Trucks +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### Time value costs +#### Pass other +```{r} +items <- vars[grepl("Time value costs\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items) & !grepl(".*Four Wheelers.*", items) & !grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("(Time value costs|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +#### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +## Scenario specific input data +### Annualized TCO for new sales +#### Capital costs +##### Pass other +```{r} +items <- vars[grepl("TCO sales Capital costs\\|.*", 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("(TCO sales Capital costs|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +##### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight Trucks +```{r} +itemsSplit<- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Operating costs (non-fuel) +##### Pass other +```{r} +items <- vars[grepl("TCO sales Operating costs \\(total non-fuel\\)\\|.*", 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("(TCO sales Operating costs \\(total non-fuel\\)|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +##### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% + filter(variable %in% itemsSplit) %>% + mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight Trucks +```{r} +itemsSplit<- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Fuel costs +##### Pass other +```{r} +items <- vars[grepl("TCO sales Fuel costs\\|.*", 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("(TCO sales Fuel costs|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +##### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +##### Freight Trucks +```{r} +itemsSplit<- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### Scenario specific load factor +#### Pass other +```{r} +vars <- as.character(unique(data$variable)) +items <- vars[grepl("Load factor\\|.*", 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("(Load factor|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +#### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight Trucks +```{r} +itemsSplit<- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### Scenario specific energy intensity +#### Pass other +```{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 sales|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +#### Pass Four Wheelers +```{r} +itemsSplit <- items[grepl(".*Four Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Four Wheelers\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +#### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight other +```{r} +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(\\|Transport|\\|Pass|\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight Trucks +```{r} +itemsSplit<- items[grepl(".*Freight.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Road\\|[A-Za-z]+\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### Preference trends +#### sector-Subsector 1 +##### Pass +```{r} +items <- vars[grepl("Preference\\|S1S\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|S1S|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Freight +```{r} +items <- vars[grepl("Preference\\|S1S\\|.*", vars)] +itemsSplit <- items[grepl(".*Freight.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|S1S|\\|Transport|\\|Freight\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +#### Subsector 1 - Subsector 2 +```{r} +items <- vars[grepl("Preference\\|S2S1\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items) & grepl(".*Bus.*|.*LDV.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|S2S1|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit)/5)) +``` + +#### Subsector 2 - Subsector 3 +```{r} +items <- vars[grepl("Preference\\|S3S2\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items) & grepl(".*LDV.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|S3S2|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit)/5)) +``` + +#### Subsector 3 - Vehicle +##### Pass +```{r} +items <- vars[grepl("Preference\\|VS3\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items)& grepl(".*LDV.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|LDV\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Freight +```{r} +items <- vars[grepl("Preference\\|VS3\\|.*", vars)] +itemsSplit <- items[grepl(".*Freight.*", items)& grepl(".*Road.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Road\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +#### Vehicle - Fuel +#### Pass other +```{r} +items <- vars[grepl("Preference\\|FV\\|.*", vars)] +itemsSplit <- items[grepl(".*Pass.*", items) & !grepl(".*Four Wheelers.*", items) & !grepl(".*Two Wheelers.*", items)& !grepl(".*(Walk|Cycle).*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|FV|\\|Transport|\\|Pass\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Pass Two Wheelers +```{r} +itemsSplit <- items[grepl(".*Two Wheelers.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("^.*\\|Two Wheelers\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = 2) +``` + +#### Freight other +```{r} +items <- vars[grepl("Preference\\|FV\\|.*", vars)] +itemsSplit <- items[grepl(".*Freight.*", items) & !grepl(".*Road.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("(Preference\\|FV|\\|Transport|\\|Freight\\||\\|Bunkers)", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/5)) +``` + +##### Freight Trucks +```{r} +itemsSplit<- items[grepl("Preference\\|FV\\|.*", items) & grepl(".*Truck.*", items)] +plotData <- data %>% filter(variable %in% itemsSplit) %>% mutate(variable = gsub("Preference\\|FV\\|Transport\\|Freight\\|Road\\|", "", variable)) +itemsSplit <- as.character(unique(plotData$variable)) +showTransportInputMultiLinePlots(plotData, itemsSplit, nrowNum = ceiling(length(itemsSplit )/6)) +``` + +### GDP +```{r} +items <- vars[grepl(".*GDP.*", vars)] +showMultiLinePlots(data, items, nrowNum = 2) +``` diff --git a/inst/compareScenarios/cs_08_transportRemindInputfiles.Rmd b/inst/compareScenarios/cs_08_transportRemindInputfiles.Rmd new file mode 100644 index 0000000..77278eb --- /dev/null +++ b/inst/compareScenarios/cs_08_transportRemindInputfiles.Rmd @@ -0,0 +1,186 @@ +--- +title: "Untitled" +author: "Johanna Hoppe" +date: "2024-09-05" +output: html_document +--- + +```{r} +library(ggplot2) +library(dplyr) + +timeResReporting <- c(seq(2005, 2060, by = 5), seq(2070, 2100, by = 10)) +axisScaling = "free_y" #fixed +paths = params$mifScen +paths <- gsub("Transport.mif", "5_REMINDinputData", paths) + +files <- list(f29_trpdemand = "f29_trpdemand", + f35_demByTech = "f35_demByTech", + f35_esCapCost = "f35_esCapCost", + f35_fe2es = "f35_fe2es") + +# Load files from all paths +loaded_files <- lapply(files, function(file_name) { + lapply(paths, function(path) { + # Load the data using read.csv + file_path <- file.path(path, paste0(file_name, ".csv")) + if (file.exists(file_path)) read.csv(file_path) + }) +}) + +# Clean the loaded data by applying the required transformations +loaded_files <- lapply(loaded_files, function(file_list) { + lapply(file_list, function(df) { + # If the column V1 exists, remove it + if ("V1" %in% colnames(df)) df <- df %>% select(-V1) + return(df) + }) +}) + +# Change scenario name to mifScenNames if applicable +if (!is.null(params$mifScenNames)) { + loaded_files <- lapply(loaded_files, function(file_list) { + for (i in 1:length(params$mifScenNames)) { + file_list[[i]] <- file_list[[i]] %>% mutate(EDGE_scenario = params$mifScenNames[i]) + } + return(file_list) + }) +} + +# Combine the corresponding datasets across paths +combined_files <- setNames( + lapply(files, function(file_name) { + bind_rows(loaded_files[[file_name]]) + }), + files # This will set the names of the resulting list to the filenames +) + +# Select relevant data for different files, based on "tall" column being in "timeResReporting" +f29_trpdemand <- combined_files[["f29_trpdemand"]] %>% filter(tall %in% timeResReporting) +f35_fe2es <- combined_files[["f35_fe2es"]] %>% filter(tall %in% timeResReporting) +f35_esCapCost <- combined_files[["f35_esCapCost"]] %>% filter(tall %in% timeResReporting) +f35_demByTech <- combined_files[["f35_demByTech"]] %>% filter(tall %in% timeResReporting) +``` + +# REMIND input data rom EDGE-T standalone + +## Energy service demand on CES node level +```{r} +unique_all_in <- unique(f29_trpdemand$all_in) + +# Loop through each unique value of all_teEs and create a plot +for (allin in unique_all_in) { + + # Filter data for the current technology/service + filtered_data <- f29_trpdemand %>% filter(all_in == allin) + + # Create the ggplot for the current allin + p <- ggplot(filtered_data, aes(x = tall, y = value, color = EDGE_scenario)) + + geom_line() + + facet_wrap(~ all_regi, scales = axisScaling) + # Facet by region/country + labs( + title = paste("Energy service demand for:", allin), + x = "Time", + y = "Value", + color = "EDGE Scenario" + ) + + theme_minimal() + + theme(legend.position = "bottom") + + # Print the plot on a new page + print(p) + cat("\n\n") +} + +``` + + +## Energy efficiency per energy service technology +```{r} +unique_teEs <- unique(f35_fe2es$all_teEs) + +# Loop through each unique value of all_teEs and create a plot +for (teEs in unique_teEs) { + + # Filter data for the current technology/service (all_teEs) + filtered_data <- f35_fe2es %>% filter(all_teEs == teEs) + + # Create the ggplot for the current teEs + p <- ggplot(filtered_data, aes(x = tall, y = value, color = EDGE_scenario)) + + geom_line() + + facet_wrap(~ all_regi, scales = axisScaling) + # Facet by region/country + labs( + title = paste("Energy efficiency for:", teEs), + x = "Time", + y = "Value", + color = "EDGE Scenario" + ) + + theme_minimal() + + theme(legend.position = "bottom") + + # Print the plot (or save it if you want to store the plots) + print(p) + cat("\n\n") +} +``` + + +## Capital cost per energy service technology +```{r} +unique_teEs <- unique(f35_esCapCost$all_teEs) + +# Loop through each unique value of all_teEs and create a plot +for (teEs in unique_teEs) { + + # Filter data for the current technology/service (all_teEs) + filtered_data <- f35_esCapCost %>% filter(all_teEs == teEs) + + # Create the ggplot for the current teEs + p <- ggplot(filtered_data, aes(x = tall, y = value, color = EDGE_scenario)) + + geom_line() + + facet_wrap(~ all_regi, scales = axisScaling) + # Facet by region/country + labs( + title = paste("Capital cost for:", teEs), + x = "Time", + y = "Value", + color = "EDGE Scenario" + ) + + theme_minimal() + + theme(legend.position = "bottom") + + # Print the plot (or save it if you want to store the plots) + print(p) + cat("\n\n") +} + +``` + +## Final energy demand per energy service technology +```{r} +unique_teEs <- unique(f35_demByTech$all_teEs) + +# Loop through each unique value of all_teEs and create a plot +for (teEs in unique_teEs) { + + # Filter data for the current technology/service (all_teEs) + filtered_data <- f35_demByTech %>% filter(all_teEs == teEs) + + # Create the ggplot for the current teEs + p <- ggplot(filtered_data, aes(x = tall, y = value, color = EDGE_scenario)) + + geom_line() + + facet_wrap(~ all_regi, scales = axisScaling) + # Facet by region/country + labs( + title = paste("Final energy for:", teEs), + x = "Time", + y = "Value", + color = "EDGE Scenario" + ) + + theme_minimal() + + theme(legend.position = "bottom") + + # Print the plot (or save it if you want to store the plots) + print(p) + cat("\n\n") +} + +``` diff --git a/man/reportLiquidsAndGasesComposition.Rd b/man/reportLiquidsAndGasesComposition.Rd index 882e5ff..762542e 100644 --- a/man/reportLiquidsAndGasesComposition.Rd +++ b/man/reportLiquidsAndGasesComposition.Rd @@ -4,7 +4,7 @@ \alias{reportLiquidsAndGasesComposition} \title{Report the split of liquids and gases into fossil|bio|hydrogen} \usage{ -reportLiquidsAndGasesComposition(dtFE, gdxPath, timeResReporting, helpers) +reportLiquidsAndGasesComposition(dtFE, gdxPath, helpers) } \arguments{ \item{dtFE}{Final energy data for liquids and gases} @@ -12,8 +12,6 @@ reportLiquidsAndGasesComposition(dtFE, gdxPath, timeResReporting, helpers) \item{gdxPath}{Path to REMIND gdx, which contains the share of the various production routes for liquid and gaseous energy carriers} -\item{timeResReporting}{Time resolution for variable reporting} - \item{helpers}{List of helpers} } \value{ diff --git a/man/reportTransportVarSet.Rd b/man/reportTransportVarSet.Rd index 315dae7..8280074 100644 --- a/man/reportTransportVarSet.Rd +++ b/man/reportTransportVarSet.Rd @@ -4,14 +4,12 @@ \alias{reportTransportVarSet} \title{Report detailed transport variable set} \usage{ -reportTransportVarSet(data, baseVarSet, timeResReporting) +reportTransportVarSet(data, baseVarSet) } \arguments{ \item{data}{List that contains the model results to report the detailed transport variable set} \item{baseVarSet}{Basic output variable set} - -\item{timeResReporting}{Timesteps to be reported} } \value{ Detailed transport output variable set