Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration with piamPlotComparison for compareScenarios #247

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3069465'
ValidationKey: '3092076'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -36,7 +35,7 @@ jobs:
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9

Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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: 'edgeTransport: Prepare EDGE Transport Data for the REMIND model'
version: 1.5.5
date-released: '2024-03-21'
version: 1.5.6
date-released: '2024-04-08'
abstract: EDGE-T is a fork of the GCAM transport module https://jgcri.github.io/gcam-doc/energy.html#transportation
with a high level of detail in its representation of technological and modal options.
It is a partial equilibrium model with a nested multinomial logit structure and
Expand Down
8 changes: 3 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: edgeTransport
Title: Prepare EDGE Transport Data for the REMIND model
Version: 1.5.5
Version: 1.5.6
Authors@R: c(
person("Alois", "Dirnaichner", email = "[email protected]", role = c("aut", "cre")),
person("Marianna", "Rottoli", email = "[email protected]", role = "aut"),
Expand All @@ -15,7 +15,7 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Date: 2024-03-21
Date: 2024-04-08
Config/testthat/edition: 3
Imports:
rmndt,
Expand All @@ -32,9 +32,7 @@ Imports:
gdxrrw,
gdxdt,
remind2,
rlang,
yaml,
ymlthis
piamPlotComparison
Suggests:
testthat,
knitr,
Expand Down
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ importFrom(quitte,as.quitte)
importFrom(quitte,write.mif)
importFrom(readxl,read_excel)
importFrom(remind2,toolRegionSubsets)
importFrom(rlang,parse_expr)
importFrom(rmarkdown,render)
importFrom(rmndt,aggregate_dt)
importFrom(rmndt,approx_dt)
Expand All @@ -74,10 +73,5 @@ importFrom(rootSolve,multiroot)
importFrom(stats,complete.cases)
importFrom(stats,na.omit)
importFrom(utils,read.csv)
importFrom(yaml,yaml.load)
importFrom(ymlthis,as_yml)
importFrom(ymlthis,use_rmarkdown)
importFrom(ymlthis,yml_params_code)
importFrom(ymlthis,yml_replace)
importFrom(zoo,na.approx)
importFrom(zoo,na.spline)
162 changes: 17 additions & 145 deletions R/compareScenarios_EDGET.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#' Render CompareScenarios EDGE Transport
#'
#' Renders the *.Rmd-files associated to CompareScenarios EDGE TRansport. In the Rmds,
#' scenario- and historical .mif-files are loaded. Then plots are created from
#' this data. The result may be rendered to PDF or HTML. Alternatively one can
#' choose Rmd as output format and obtain a copy of the *.Rmd-files.
#' A wrapper for piamPlotComparison::compareScenarios
#'
#' @param mifScen \code{character(n)}, optionally named. Paths to scenario mifs.
#' If the vector has names, those are used to refer to the scenarios in the
Expand All @@ -15,149 +12,24 @@
#' and intermediary files are created.
#' @param outputFormat \code{character(1)}, not case-sensitive. \code{"html"},
#' \code{"pdf"}, or \code{"rmd"}.
#' @param ... YAML parameters, see below.
#' @importFrom yaml yaml.load
#' @importFrom rlang parse_expr
#' @importFrom ymlthis yml_params_code yml_replace as_yml use_rmarkdown
#' @importFrom rmarkdown render
#' @return The value returned by \code{\link[rmarkdown:render]{render()}}.
#' @section YAML Parameters:
#' \describe{
#' \item{\code{yearsScen}}{
#' \code{numeric(n)}.
#' Default: \code{c(seq(2005, 2060, 5), seq(2070, 2100, 10))}.
#' Years to show for scenario data.}
#' \item{\code{yearsHist}}{
#' \code{numeric(n)}.
#' Default: \code{c(seq(1990, 2020, 1), seq(2025, 2100, 5))}.
#' Years to show for historical data.}
#' \item{\code{yearsBarPlot}}{
#' \code{numeric(n)}.
#' Default: \code{c(2010, 2030, 2050, 2100)}.
#' Years to show in bar plots of scenario data.}
#' \item{\code{reg}}{
#' \code{NULL} or \code{character(n)}.
#' Default: \code{NULL}.
#' Regions to show. \code{NULL} means all.}
#' \item{\code{modelsHistExclude}}{
#' \code{character(n) or NULL}.
#' Default: \code{c()}.
#' Models in historical data to exclude.}
#' \item{\code{sections}}{
#' \code{character(n)}.
#' Default: \code{"all"}.
#' Names of sections to include. A subset of
#' \code{c("01_energy_demand", "02_energy_services", "03_stock_and_sales", "04_costs_and_shareweight_trends")}
#' or \code{"all"} for all available sections.}
#' \item{\code{userSectionPath}}{
#' \code{NULL} or \code{character(n)}.
#' Default: \code{NULL}.
#' Path to a *.Rmd-file that may be included as additional section.}
#' \item{\code{mainReg}}{
#' \code{character(1)}.
#' Default: \code{"World"}.
#' A region for which larger plots are shown.}
#' \item{\code{figWidth, figHeight}}{
#' \code{numeric(1)}.
#' Default: \code{15} and \code{10}, respectively.
#' Size of plots in inches.}
#' \item{\code{warning}}{
#' \code{logical(1)}.
#' Default: \code{TRUE}.
#' Show warnings in output?}
#' }
#' @author Christof Schoetz, Johanna Hoppe
#' @examples
#' \dontrun{
#' compareScenarios2(
#' mifScen = c("path/to/Base.mif", "path/to/NDC.mif"),
#' mifHist = "path/to/historical.mif",
#' outputFile = "CompareScenarios2Example1",
#' userSectionPath = "path/to/myPlots.Rmd")
#' compareScenarios2(
#' mifScen = c(ScenarioName1 = "path/to/scen1.mif", ScenarioName2 = "path/to/scen2.mif"),
#' mifHist = "path/to/historical.mif",
#' outputFile = "CompareScenarios2Example2",
#' figWidth = 18, figHeight = 10)
#' }
#' @export
compareScenarios_EDGET <- function(
mifScen, mifHist,
outputDir = getwd(),
outputFile = "CompareScenarios_EDGE-Transport",
outputFormat = "PDF",
...
) {
yamlParams <- c(
list(
mifScen = normalizePath(mifScen, mustWork = TRUE),
mifScenNames = names(mifScen),
mifHist = normalizePath(mifHist, mustWork = TRUE)),
list(...))
mifScen, mifHist,
outputDir = getwd(),
outputFile = "CompareScenarios_EDGE-Transport",
outputFormat = "PDF") {

# convert relative to absolute paths
if ("userSectionPath" %in% names(yamlParams)) {
yamlParams$userSectionPath <- normalizePath(yamlParams$userSectionPath,
mustWork = TRUE)
}

outputFormat <- tolower(outputFormat)
if (outputFormat == "pdf") outputFormat <- "pdf_document"
if (outputFormat == "html") outputFormat <- "html_document"
if (identical(tolower(outputFormat), "rmd")) {
return(.compareScenarios2Rmd(yamlParams, outputDir, outputFile))
}
# copy the template directory from the package to the outputDir because rmarkdown writes to the folder
# containing the template.
templateInOutputDir <- file.path(outputDir, "compareScenarios_Transport", "csEDGET_main.Rmd")
file.copy(system.file("Rmd/compareScenarios_Transport/", package = "edgeTransport"),
outputDir, recursive = TRUE)
render(
templateInOutputDir,
intermediates_dir = outputDir,
output_dir = outputDir,
output_file = outputFile,
output_format = outputFormat,
params = yamlParams,
envir = new.env())
unlink(file.path(outputDir, "compareScenarios_Transport"), recursive = TRUE)
}

# Copies the CompareScenarios2-Rmds to the specified location and modifies
# their YAML header according to \code{yamlParams}.
.compareScenarios2Rmd <- function(yamlParams, outputDir, outputFile) {
pathMain <- system.file("Rmd/compareScenarios_Transport/csEDGET_main.Rmd", package = "edgeTransport")
linesMain <- readLines(pathMain)
delimiters <- grep("^(---|\\.\\.\\.)\\s*$", linesMain)
headerMain <- linesMain[(delimiters[1]):(delimiters[2])]
yml <- yaml.load(
headerMain,
handlers = list(r = function(x) yml_params_code(!!parse_expr(x))))
baseYaml <- as_yml(yml)
newYamlParams <- baseYaml$params
newYamlParams[names(yamlParams)] <- yamlParams
if (!is.null(names(yamlParams$mifScen))) {
newYamlParams$mifScenNames <- names(yamlParams$mifScen)
}
newYaml <- yml_replace(
baseYaml,
params = newYamlParams,
date = format(Sys.Date()))
pathDir <- file.path(outputDir, paste0(outputFile, "_Rmd"))
if (!dir.exists(pathDir)) dir.create(pathDir)
dirFiles <- dir(
system.file("Rmd/compareScenarios_Transport", package = "edgeTransport"),
full.names = TRUE)
rmdDirFiles <- grep(
dirFiles,
pattern = "csEDGET_main\\.Rmd$",
invert = TRUE, value = TRUE)
file.copy(rmdDirFiles, pathDir)
use_rmarkdown(
newYaml,
path = file.path(pathDir, "cs2_main.Rmd"),
template = system.file(
"Rmd/compareScenarios_Transport/csEDGET_main.Rmd",
package = "edgeTransport"),
include_yaml = FALSE)
piamPlotComparison::compareScenarios(
projectLibrary = "edgeTransport",
mifScen = mifScen,
mifHist = mifHist,
outputFormat = outputFormat,
outputFile = outputFile,
sections = "all",
docTitle = "Edge Transport Compare Scenarios",
outputDir = outputDir,
reg = c("OAS", "MEA", "SSA", "LAM", "REF", "CAZ", "CHA", "IND", "JPN", "USA", "NEU", "EUR", "World"),
yearsHist = c(seq(2010, 2020, 1), seq(2025, 2100, 5))
)
}
2 changes: 0 additions & 2 deletions R/generateEDGEdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ toolGenerateEDGEdata <- function(input_folder, output_folder, cache_folder = NUL
if(nrow(nas) > 0) {
print("NAs found in FV vintage shares.")
nas
browser()
}
shares$FV_shares = vintages[["shares"]]$FV_shares
prices = vintages[["prices"]]
Expand All @@ -474,7 +473,6 @@ toolGenerateEDGEdata <- function(input_folder, output_folder, cache_folder = NUL
if(nrow(nas) > 0) {
print("NAs found in final demand output.")
nas
browser()
}

num_veh_stations = toolVehicleStations(
Expand Down
8 changes: 0 additions & 8 deletions R/incotrend.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ toolPreftrend <- function(SWS, ptab, calibdem, incocost, years, GDP_POP_MER,
nas <- FVtarget[is.na(sw)]
if(nrow(nas) > 0){
print("Warning: NAs in SWs found.")
browser()
}

setnames(FVtarget, "sw", "value")
Expand Down Expand Up @@ -221,31 +220,27 @@ toolPreftrend <- function(SWS, ptab, calibdem, incocost, years, GDP_POP_MER,
nas <- S3target[is.na(sw)]
if(nrow(nas) > 0){
print("Warning: NAs in SWs found.")
browser()
}

S2target[, sw := sw/max(sw),
by = c("region", "year", "subsector_L3")]
nas <- S2target[is.na(sw)]
if(nrow(nas) > 0){
print("Warning: NAs in SWs found.")
browser()
}

S1target[, sw := sw/max(sw),
by = c("region", "year", "subsector_L2")]
nas <- S1target[is.na(sw)]
if(nrow(nas) > 0){
print("Warning: NAs in SWs found.")
browser()
}

VStarget[, sw := sw/max(sw),
by = c("region", "year", "subsector_L1")]
nas <- VStarget[is.na(sw)]
if(nrow(nas) > 0){
print("Warning: NAs in SWs found.")
browser()
}


Expand Down Expand Up @@ -332,7 +327,6 @@ Hybrid Electric,Liquids")
nas <- FVtarget[logit_type != "pchar" & is.na(value)]
if(nrow(nas) > 0){
print(sprintf("NAs found in FV shareweight trends for %s scenario.", tech_scen))
browser()
}
## level S2: Bus vs LDV
S2target[, regioncat := ifelse(region %in% richregions, "rich", "poor")]
Expand All @@ -350,7 +344,6 @@ Hybrid Electric,Liquids")
nas <- S2target[is.na(sw)]
if(nrow(nas) > 0){
print(sprintf("NAs found in S2 shareweight trends for %s scenario.", tech_scen))
browser()
}

## level S3: all other mode shares
Expand All @@ -369,7 +362,6 @@ Hybrid Electric,Liquids")
nas <- S3target[is.na(sw)]
if(nrow(nas) > 0){
print(sprintf("NAs found in S3 shareweight trends for %s scenario.", tech_scen))
browser()
}

}else{
Expand Down
2 changes: 0 additions & 2 deletions R/logit.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ toolCalculateLogitSW <- function(prices,
nas <- df[is.na(share)]
if(nrow(nas) > 0) {
print("NAs found in SWs.")
browser()
}
MJ_km <- merge(df, mj_km_data, by=intersect(names(df),names(mj_km_data)), all = FALSE)

Expand Down Expand Up @@ -237,7 +236,6 @@ toolCalculateLogitSW <- function(prices,
if(nrow(nas) > 0){
print("NAs found in FV shares.")
nas
browser()
}
# VS1
VS1_all <- X2Xcalc(prices = FV,
Expand Down
10 changes: 4 additions & 6 deletions R/reportEDGET.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,12 @@ toolReportEDGET <- function(output_folder = ".",

vars <- varlist[[aggrname]]
#access the first element in vars


if (length(unique(datatable[variable %in% vars]$variable)) < length(vars)){
browser()
print(paste0("Missing variables to aggregate data to ", aggrname))
}
else if (length(unique(datatable[variable %in% vars]$variable)) > length(vars)) {
browser()
print(paste0('duplicates from: ', aggrname, 'not summed up'))
}
else {
Expand Down Expand Up @@ -992,11 +990,11 @@ toolReportEDGET <- function(output_folder = ".",

#Calculate useful energy
UE <- toMIF[grepl("FE", variable) & grepl("Electric|Liquids|Hydrogen", variable)] #select only FE for electricity, liquids and hydrogen


#create new column named technology and assign values based on variables in UE.varialbe: if the variable contains "Electricity" then technology is "Electric", if the variable contains "Liquids" then technology is "Liquids", if the variable contains "Hydrogen" then technology is "Hydrogen"
UE[, technology := ifelse(grepl("Electricity", variable), "Electric", ifelse(grepl("Liquids", variable), "Liquids", ifelse(grepl("Hydrogen", variable), "Hydrogen", NA)))]

UE <- merge(UE, Mapp_UE, technology) #merge with efficiencies
UE[, value:= value*UE_efficiency][, c("variable", "technology", "UE_efficiency"):= list(gsub("FE","UE", variable), NULL, NULL)] #calculate useful energy

Expand Down
Loading