Skip to content

Commit

Permalink
Merge pull request #312 from ahagen-pik/fixWarnings
Browse files Browse the repository at this point in the history
Fix warnings lucode2::buildLibrary()
  • Loading branch information
ahagen-pik authored Dec 13, 2024
2 parents 8dca393 + 17d00a6 commit e9b554e
Show file tree
Hide file tree
Showing 37 changed files with 564 additions and 188 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '42568470'
ValidationKey: '42588540'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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: 2.12.1
version: 2.12.2
date-released: '2024-12-13'
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.
Expand Down
2 changes: 1 addition & 1 deletion 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: 2.12.1
Version: 2.12.2
Authors@R: c(
person("Johanna", "Hoppe", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0009-0004-6753-5090")),
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ importFrom(gdxdt,writegdx)
importFrom(gdxdt,writegdx.parameter)
importFrom(gdxrrw,rgdx)
importFrom(madrat,calcOutput)
importFrom(madrat,getConfig)
importFrom(madrat,readSource)
importFrom(magclass,getComment)
importFrom(magclass,lowpass)
importFrom(magclass,time_interpolate)
importFrom(magrittr,`%>%`)
importFrom(reporttransport,reportEdgeTransport)
importFrom(reporttransport,reportToREMINDcapitalCosts)
Expand All @@ -47,5 +51,6 @@ importFrom(rmndt,approx_dt)
importFrom(rmndt,magpie2dt)
importFrom(rootSolve,multiroot)
importFrom(stats,approxfun)
importFrom(stats,setNames)
importFrom(utils,capture.output)
importFrom(zoo,na.approx)
23 changes: 15 additions & 8 deletions R/iterativeEDGETransport.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' reportToREMINDenergyEfficiency reportToREMINDfinalEnergyShares
#' @import data.table
#' @export
#'


iterativeEdgeTransport <- function() {
Expand All @@ -19,6 +20,7 @@ iterativeEdgeTransport <- function() {
#############################################################
## Settings
#############################################################
cfg <- NULL

# Set paths to folders
edgeTransportFolder <- "EDGE-T"
Expand All @@ -31,7 +33,7 @@ iterativeEdgeTransport <- function() {

# Set gdx
gdx <- "input.gdx"
if(file.exists("fulldata.gdx"))
if (file.exists("fulldata.gdx"))
gdx <- "fulldata.gdx"

# Load config
Expand All @@ -48,7 +50,12 @@ iterativeEdgeTransport <- function() {
#############################################################
## Load input data
#############################################################
inputFolder = paste0("./")
inputFolder <- paste0("./")

# bind variables locally to prevent NSE notes in R CMD CHECK
period <- value <- unit <- univocalName <- iteration <- type <- variable <- . <- NULL


# share of electricity in Hybrid electric vehicles
hybridElecShare <- 0.4
numberOfRegions <- length(readGDX(gdx, "all_regi"))
Expand All @@ -70,7 +77,7 @@ iterativeEdgeTransport <- function() {
genModelPar <- inputs$genModelPar
scenModelPar <- inputs$scenModelPar
RDSinputs <- inputs$RDSfiles



# Data from previous REMIND iteration
Expand Down Expand Up @@ -101,8 +108,8 @@ iterativeEdgeTransport <- function() {
REMINDfuelCostIterations <- rbind(REMINDfuelCostIterations, copy(REMINDfuelCost)[, iteration := iterationNumber])
storeData(file.path(".", edgeTransportFolder), REMINDfuelCostIterations = REMINDfuelCostIterations)
averagePricesOverIterations <- TRUE
if(averagePricesOverIterations) {
if(max(unique(REMINDfuelCostIterations$iteration)) >= 20 &
if (averagePricesOverIterations) {
if (max(unique(REMINDfuelCostIterations$iteration)) >= 20 &&
max(unique(REMINDfuelCostIterations$iteration)) <= 30) {
## apply moving avg
byCols <- names(REMINDfuelCostIterations)
Expand Down Expand Up @@ -247,15 +254,15 @@ iterativeEdgeTransport <- function() {

## CapCosts
writegdx.parameter("p35_esCapCost.gdx", f35_esCapCost, "p35_esCapCost",
valcol="value", uelcols = c("tall", "all_regi", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs"))
valcol = "value", uelcols = c("tall", "all_regi", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs"))

## Intensities
writegdx.parameter("p35_fe2es.gdx", f35_fe2es, "p35_fe2es",
valcol="value", uelcols = c("tall", "all_regi", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs"))
valcol = "value", uelcols = c("tall", "all_regi", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs"))

## Shares: demand can represent the shares since it is normalized
writegdx.parameter("p35_shFeCes.gdx", f35_shFeCes, "p35_shFeCes",
valcol="value",
valcol = "value",
uelcols = c("tall", "all_regi", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_enty", "all_in", "all_teEs"))

print(paste("---", Sys.time(), "End of the EDGE-T iterative model run."))
Expand Down
64 changes: 46 additions & 18 deletions R/supportFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,36 @@
#' @export

toolNormalizePreferences <- function(preferenceTab) {
# bind variables locally to prevent NSE notes in R CMD CHECK
level <- value <- NULL

# S1S: logit level: distances (e.g. short-medium, long)
preferenceTab[level == "S1S", max := max(value), by = c("region", "period", "sector")]
preferenceTab[level == "S1S" & max != 0, value := value/max(value), by = c("region", "period", "sector")] # S1S: logit level: distances (e.g. short-medium, long)
preferenceTab[level == "S2S1", max := max(value), by = c("region", "period", "sector", "subsectorL1")]
preferenceTab[level == "S2S1" & max != 0, value := value/max(value), by = c("region", "period", "sector", "subsectorL1")] # S2S1: logit level: modes/categories (e.g. walk, road, rail)
preferenceTab[level == "S1S" & max != 0, value := value / max(value), by = c("region", "period", "sector")]

# S2S1: logit level: modes/categories (e.g. walk, road, rail)
preferenceTab[level == "S3S2", max := max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2")]
preferenceTab[level == "S3S2" & max != 0, value := value/max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2")] # S3S2: logit level: modes/technologies (e.g. LDV, bus, Liquids)
preferenceTab[level == "VS3", max := max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3")]
preferenceTab[level == "VS3" & max != 0, value := value/max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3")] # VS3: logit level: modes/technologies (e.g. cars, Liquids)
preferenceTab[level == "FV", max := max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3", "vehicleType")]
preferenceTab[level == "FV" & max != 0, value := value/max(value), by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3", "vehicleType")] # FV: logit level: vehicle type (e.g. large car, moped)
preferenceTab[level == "S2S1", max := max(value), by = c("region", "period", "sector", "subsectorL1")]
preferenceTab[level == "S2S1" & max != 0, value := value / max(value),
by = c("region", "period", "sector", "subsectorL1")]

# S3S2: logit level: modes/technologies (e.g. LDV, bus, Liquids)
preferenceTab[level == "S3S2" & max != 0, value := value / max(value),
by = c("region", "period", "sector", "subsectorL1", "subsectorL2")]

# VS3: logit level: modes/technologies (e.g. cars, Liquids)
preferenceTab[level == "VS3", max := max(value),
by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3")]
preferenceTab[level == "VS3" & max != 0, value := value / max(value),
by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3")]


# FV: logit level: vehicle type (e.g. large car, moped)
preferenceTab[level == "FV", max := max(value),
by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3", "vehicleType")]
preferenceTab[level == "FV" & max != 0, value := value / max(value),
by = c("region", "period", "sector", "subsectorL1", "subsectorL2", "subsectorL3", "vehicleType")]

preferenceTab[, max := NULL]

if (anyNA(preferenceTab)) stop("Something went wrong with the normalization of the preference trends. Please check toolNormalizePreferences()")
Expand All @@ -34,10 +54,11 @@ toolNormalizePreferences <- function(preferenceTab) {
#' @export

toolLoadDecisionTree <- function(regionAggregation = "iso") {
# bind variables locally to prevent NSE notes in R CMD CHECK
spatial <- present <- region <- sector <- subsectorL1 <- subsectorL2 <- subsectorL3 <- vehicleType <- technology <- univocalName <- NULL

# decisionTree.csv contains all possible branches of the decision tree
decisionTree <- fread(system.file("extdata/helpersDecisionTree.csv",
package = "edgeTransport", mustWork = TRUE), header = TRUE)
decisionTree <- fread(system.file("extdata/helpersDecisionTree.csv", package = "edgeTransport", mustWork = TRUE), header = TRUE)
decisionTree[, spatial := "all"]
# Not all countries feature the same branches of the decision tree - Some vehicleTypes and modes are not
# available in certain countries
Expand Down Expand Up @@ -99,7 +120,7 @@ toolLoadDecisionTree <- function(regionAggregation = "iso") {

getFilterEntriesUnivocalName <- function(categories, decisionTree) {

findEntries <- function(category, dt){
findEntries <- function(category, dt) {
test <- dt[, lapply(.SD, function(x) grepl(category, x))]
entries <- unique(dt[rowSums(test) > 0]$univocalName)
}
Expand Down Expand Up @@ -130,10 +151,10 @@ calculateShares <- function(totPrice, lambda, pref = NULL) {
if (sum(totPrice) == 0) {
share <- 1 # e.g. for active modes with totPrice of zero on FV level
} else {
share <- pref * totPrice ^ lambda / (sum(pref * totPrice ^ lambda))
share <- pref * totPrice^lambda / (sum(pref * totPrice^lambda))
}
} else {
share <- totPrice ^ lambda / (sum(totPrice ^ lambda))
share <- totPrice^lambda / (sum(totPrice^lambda))
}

return(share)
Expand All @@ -152,6 +173,7 @@ calculateShares <- function(totPrice, lambda, pref = NULL) {
#' @export

toolTraverseDecisionTree <- function(data, upperLevel, decisionTree) {
share <- value <- . <- NULL

decisionGroups <- names(decisionTree[, -c("region", "univocalName")])
indexUpperLevel <- which(match(decisionGroups, upperLevel) == 1)
Expand Down Expand Up @@ -182,15 +204,17 @@ toolTraverseDecisionTree <- function(data, upperLevel, decisionTree) {
#' @export

toolApplyMixedTimeRes <- function(data, helpers, idcols = NULL) {
# bind variables locally to prevent NSE notes in R CMD CHECK
period <- level <- test <- univocalName <- . <- NULL

highRes <- unique(helpers$dtTimeRes$period)
highResUnivocalNames <- copy(helpers$dtTimeRes)
highResUnivocalNames <- highResUnivocalNames[, .(test = all(highRes %in% period)), by = univocalName]
highResUnivocalNames <- highResUnivocalNames[test == TRUE]$univocalName

if (c("level") %in% names(data)){
#in this data format there is no univocalName
#this needs to change temporarily
if (c("level") %in% names(data)) {
# in this data format there is no univocalName
# this needs to change temporarily
dataFV <- merge(data[level == "FV"], helpers$decisionTree, by = intersect(names(data), names(helpers$decisionTree)))
dataHighRes <- dataFV[univocalName %in% highResUnivocalNames][, univocalName := NULL]
dataLowRes <- rbind(dataFV[!(univocalName %in% highResUnivocalNames)][, univocalName := NULL], data[!level == "FV"])
Expand Down Expand Up @@ -227,6 +251,8 @@ return(data)
#' @export

toolCheckAllLevelsComplete <- function(data, decisionTree, name) {
# bind variables locally to prevent NSE notes in R CMD CHECK
level <- univocalName <- technology <- NULL

decisionFV <- copy(decisionTree)
decisionFV[, univocalName := NULL][, level := "FV"]
Expand Down Expand Up @@ -272,15 +298,17 @@ toolCheckAllLevelsComplete <- function(data, decisionTree, name) {
#' @export

toolOrderandCheck <- function(data, decisionTree, yrs = NULL, checkCompleteness = FALSE, fleetVars = FALSE) {
# bind variables locally to prevent NSE notes in R CMD CHECK
period <- subsectorL3 <- allyears <- NULL

data <- merge(data, decisionTree, by = c(intersect(names(data), names(decisionTree))))
allCols <- c(names(decisionTree), "variable", "unit", "period", "value")
isComplete <- all(names(data) %in% allCols) && all(allCols %in% names(data))
if (isComplete == FALSE) stop(paste0(deparse(substitute(data), " misses columns or has additional columns")))
if (anyNA(data) == TRUE) stop(paste0(deparse(substitute(data), " contains NAs")))
data <- data[, ..allCols]
data <- data[, allCols, with = FALSE]

if (checkCompleteness == TRUE){
if (checkCompleteness == TRUE) {
data <- data[period %in% yrs]
if (fleetVars == TRUE) decisionTree <- decisionTree[grepl("Bus.*|.*4W|.*freight_road.*", subsectorL3)]
allTimesteps <- data.table(period = yrs)[, allyears := "All"]
Expand Down
30 changes: 20 additions & 10 deletions R/supportFunctionsIterative.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
#' @param demScenario demand scenario
#' @param transportPolScenario Transport policy scenario
#' @import data.table
#' @importFrom stats setNames
#' @export

# Loads the csv input files chooses the correct scenario and
# converts the files into RDS local files
csv2RDS <- function(filename, inputPath, SSPscenario, demScenario, transportPolScenario) {
if (filename == "scenSpecPrefTrends") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen", "sector",
"subsectorL1", "subsectorL2", "subsectorL3", "vehicleType", "technology",
"level", "variable", "unit", "value")
else if (filename == "initialIncoCosts") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen", "univocalName","technology","variable","unit","type","value")
else if (filename == "timeValueCosts") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen", "univocalName", "variable", "unit", "value")
csv2RDS <- function(filename, inputPath, SSPscenario, demScenario, transportPolScenario) {
# bind variables locally to prevent NSE notes in R CMD CHECK
SSPscen <- demScen <- transportPolScen <- NULL

if (filename == "scenSpecPrefTrends") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen",
"sector", "subsectorL1", "subsectorL2", "subsectorL3",
"vehicleType", "technology", "level", "variable", "unit", "value")
else if (filename == "initialIncoCosts") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen",
"univocalName", "technology", "variable", "unit", "type", "value")
else if (filename == "timeValueCosts") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen",
"univocalName", "variable", "unit", "value")
else if (filename == "f29_trpdemand") colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen", "all_in", "value")
else colNames <- c("period", "region", "SSPscen", "demScen", "transportPolScen",
"univocalName", "technology", "variable", "unit", "value")
Expand Down Expand Up @@ -53,6 +59,8 @@ toolLoadRDSinputs <- function(edgeTransportFolder, inputFiles) {
#' @export

toolLoadIterativeInputs <- function(edgeTransportFolder, inputFolder, inputFiles, numberOfRegions, SSPscenario, transportPolScenario, demScenario) {
# bind variables locally to prevent NSE notes in R CMD CHECK
transportPolScen <- all_in <- period <- value <- unit <- sector <- variable <- . <- univocalName <- test <- SSPscen <- NULL

# Model input parameters from the package
## Exponents discrete choice model
Expand All @@ -65,8 +73,9 @@ toolLoadIterativeInputs <- function(edgeTransportFolder, inputFolder, inputFiles
annuityCalc <- fread(system.file("extdata/genParAnnuityCalc.csv", package = "edgeTransport", mustWork = TRUE), header = TRUE)
# Interest Rate and vehicle service life for annuity calculation
# NOTE: right now there is only "default". If we add scenario specific annuity parameters, we can shift annuityCalc to the scenPar's
if (gsub("ICEban", "", transportPolScenario) %in% annuityCalc$transportPolScen){
annuityCalc <- annuityCalc[transportPolScen == gsub("ICEban", "", transportPolScenario)][, transportPolScen := NULL]} else {
if (gsub("ICEban", "", transportPolScenario) %in% annuityCalc$transportPolScen) {
annuityCalc <- annuityCalc[transportPolScen == gsub("ICEban", "", transportPolScenario)][, transportPolScen := NULL]
} else {
annuityCalc <- annuityCalc[transportPolScen == "default"][, transportPolScen := NULL]
}

Expand Down Expand Up @@ -99,8 +108,9 @@ toolLoadIterativeInputs <- function(edgeTransportFolder, inputFolder, inputFiles

# Input from REMIND input data
# In the first iteration input data needs to be loaded
if (!dir.exists(file.path(edgeTransportFolder))) {
print("Loading csv data from input folder and creating RDS files...")}
if (!dir.exists(file.path(edgeTransportFolder))) {
print("Loading csv data from input folder and creating RDS files...")
}
RDSfiles <- list()
for (filename in inputFiles) {
if (length(list.files(file.path(".", edgeTransportFolder), paste0(filename, ".RDS"), recursive = TRUE, full.names = TRUE)) < 1) {
Expand Down
Loading

0 comments on commit e9b554e

Please sign in to comment.