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

Flexibilization of the currency unit in accordance with mrdrivers #24

Merged
merged 5 commits into from
Oct 30, 2024
Merged
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: '1561560'
ValidationKey: '1602080'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
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: 'mrtransport: Input data generation for the EDGE-Transport model'
version: 0.7.8
date-released: '2024-10-24'
version: 0.8.0
date-released: '2024-10-30'
abstract: The mrtransport package contains data preprocessing for the EDGE-Transport
model.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: mrtransport
Title: Input data generation for the EDGE-Transport model
Version: 0.7.8
Version: 0.8.0
Authors@R: c(
person("Johanna", "Hoppe", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0009-0004-6753-5090")),
Expand All @@ -26,4 +26,4 @@ Imports:
magrittr,
quitte,
data.table
Date: 2024-10-24
Date: 2024-10-30
60 changes: 31 additions & 29 deletions R/calcEdgeTransportSAinputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
#' @importFrom madrat readSource calcOutput
#' @importFrom magclass time_interpolate

calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRUE) { # nolint: cyclocomp_linter
calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2", IEAharm = TRUE) { # nolint: cyclocomp_linter

temporal <- spatial <- present <- period <- region <- technology <-
univocalName <- gdppc <- speed <- altTech <- variable <- value <-
regionCode12 <- multiplier <- time_interpolate <- setNames <-
capex <- untilPrice <- untilPrice2 <- purchasePriceSubsidy <-
purchasePriceSubsidy2 <- NULL

monUnit <- gsub(".*?(\\d{4}).*", "US$\\1", mrdrivers::toolGetUnitDollar())

lowResYears <- data.table(temporal = "all", period = c(
1990,
seq(2005, 2060, by = 5),
Expand Down Expand Up @@ -171,7 +173,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU

},
"annualMileage" = {
unit <- "vehkm/veh/yr"
unit <- "vehkm/veh yr"
description <- "Annual mileage on technology level. Sources: TRACCS, UCD"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(highResYears)
weight <- weight[, , paste0("gdp_", SSPscen)]
Expand All @@ -197,7 +199,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU

# Add annual mileage of zero for active modes
activeModes <- completeDataSet[univocalName %in% c("Cycle", "Walk")]
activeModes[, unit := "vehkm/veh/yr"][, variable := "Annual mileage"][, value := 0][, check := NULL]
activeModes[, unit := "vehkm/veh yr"][, variable := "Annual mileage"][, value := 0][, check := NULL]
annualMileage <- rbind(annualMileage, activeModes)

annualMileage <- annualMileage[, c("region", "period", "univocalName", "technology",
Expand Down Expand Up @@ -345,17 +347,17 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- loadFactor
},
"CAPEXtrackedFleet" = {
unit <- "US$2017/veh"
unit <- paste0(monUnit, "/veh")
description <- "CAPEX for vehicle types that feature fleet tracking (cars, trucks and busses).
Sources: UCD, PSI"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(highResYears)
weight <- weight[, , paste0("gdp_", SSPscen)]

# read PSI CAPEX
CAPEXPSI <- toolPreparePSI(readSource("PSI", "CAPEX"))
# read UCD CAPEX given in US$2017/vkt and US$2017/veh
# read UCD CAPEX given in US$/vehkm and US$/veh
CAPEXUCD <- toolPrepareUCD(readSource("UCD", "CAPEX"), "CAPEX")
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$2017/vehkm
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$/vehkm
CAPEXcombinedUCD <- toolPrepareUCD(readSource("UCD", "CAPEXandNonFuelOPEX"), "CAPEXandNonFuelOPEX")
# Operating subsidies for Freight Rail, Passenger Rail, HSR (+ Bus not used here) are partially attributed to CAPEX
# otherwise negative values in OPEX
Expand Down Expand Up @@ -385,7 +387,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
# PSI CAPEX for 4 Wheelers feature only purchase costs - take other capital costs from UCD for EUR regions
CAPEXraw <- rbind(PSIpurchaseCosts, CAPEXUCD4W, CAPEXcombinedUCD, data$operatingSubsidyUCD)

GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = "constant 2017 US$MER") |> time_interpolate(highResYears)
GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = mrdrivers::toolGetUnitDollar()) |> time_interpolate(highResYears)
GDPpcMERmag <- GDPpcMERmag[, , paste0("gdppc_", SSPscen)]

GDPpcMER <- magpie2dt(GDPpcMERmag, yearcol = "period", regioncol = "region", valcol = "gdppc")[, variable := NULL]
Expand Down Expand Up @@ -424,15 +426,15 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- CAPEX
},
"nonFuelOPEXtrackedFleet" = {
unit <- "US$2017/veh/yr"
unit <- paste0(monUnit, "/veh yr")
description <- "Non-fuel OPEX on technology level for vehicle types that feature fleet tracking
(cars, trucks, busses). Sources: UCD, PSI"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(highResYears)
weight <- weight[, , paste0("gdp_", SSPscen)]

nonFuelOPEXUCD <- toolPrepareUCD(readSource("UCD", "nonFuelOPEX"), "nonFuelOPEX")
nonFuelOPEXUCD <- nonFuelOPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_4W]
# For trucks and busses UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$2017/vehkm
# For trucks and busses UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$/vehkm
nonFuelOPEXcombinedUCD <- toolPrepareUCD(readSource("UCD", "CAPEXandNonFuelOPEX"), "CAPEXandNonFuelOPEX")
nonFuelOPEXcombinedUCD <- nonFuelOPEXcombinedUCD[univocalName %in% filterEntries$trn_freight_road
| univocalName == "Bus"]
Expand Down Expand Up @@ -483,17 +485,17 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- nonFuelOPEX
},
"CAPEXother" = {
unit <- "US$2017/vehkm"
unit <- paste0(monUnit, "/vehkm")
description <- "CAPEX (purchase costs) for vehicle types that do not feature fleet tracking
(all other than cars, trucks and busses). Sources: UCD"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(lowResYears)
weight <- weight[, , paste0("gdp_", SSPscen)]

# read UCD CAPEX given in US$2017/vkt and US$2017/veh
# read UCD CAPEX given in US$/vehkm and US$/veh
# non fuel OPEX include Domestic Aviation, International Aviation, Moped, Motorcycle (50-250cc), Motorcycle (>250cc) (+4W not used here)
CAPEXUCD <- toolPrepareUCD(readSource("UCD", "CAPEX"), "CAPEX")
CAPEXUCD <- CAPEXUCD[!univocalName %in% filterEntries$trn_pass_road_LDV_4W]
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$2017/vehkm
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$/vehkm
# combined CAPEX and OPEX include Domestic Ship, Freight Rail, HSR, International Ship, Passenger Rail
CAPEXcombinedUCD <- toolPrepareUCD(readSource("UCD", "CAPEXandNonFuelOPEX"), "CAPEXandNonFuelOPEX")
CAPEXcombinedUCD <- CAPEXcombinedUCD[!(univocalName %in% filterEntries$trn_freight_road | univocalName == "Bus")]
Expand All @@ -507,7 +509,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
c("region", "univocalName", "technology",
"variable", "unit"), extrapolate = TRUE)

# Data for two wheelers is given in US$2017/veh and needs to be converted to US$2017/vehkm
# Data for two wheelers is given in US$/veh and needs to be converted to US$/vehkm
# with the help of annuity and annual mileage

# UCD applied interest rate of 10% and uniform vehicle lifetime of 15 yrs
Expand All @@ -527,12 +529,12 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
CAPEXUCD <- merge.data.table(data$CAPEXUCD, AMUCD2W, by = c("region", "univocalName", "technology", "period"),
all.x = TRUE)
CAPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, value := (value * annuityFactor)/ annualMileage]
CAPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, unit := "US$2017/vehkm"][, annualMileage := NULL]
CAPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, unit := paste0(monUnit, "/vehkm")][, annualMileage := NULL]

# merge.data.table data
CAPEXraw <- rbind(CAPEXUCD, data$CAPEXcombinedUCD, data$operatingSubsidyUCD)

GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = "constant 2017 US$MER") |> time_interpolate(lowResYears)
GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = mrdrivers::toolGetUnitDollar()) |> time_interpolate(lowResYears)
GDPpcMERmag <- GDPpcMERmag[, , paste0("gdppc_", SSPscen)]

GDPpcMER <- magpie2dt(GDPpcMERmag, yearcol = "period", regioncol = "region", valcol = "gdppc")[, variable := NULL]
Expand Down Expand Up @@ -578,16 +580,16 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- CAPEX
},
"nonFuelOPEXother" = {
unit <- "US$2017/vehkm"
unit <- paste0(monUnit, "/vehkm")
description <- "Non fuel OPEX on technology level for vehicle types that do not feature fleet tracking
(other than cars, trucks, busses). Sources: UCD, PSI"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(lowResYears)
weight <- weight[, , paste0("gdp_", SSPscen)]
# read UCD non fuel given in US$2017/vkt and US$2017/veh/yr
# read UCD non fuel given in US$/vehkm and US$/veh/yr
# non fuel OPEX include Domestic Aviation, International Aviation, Moped, Motorcycle (50-250cc), Motorcycle (>250cc) (+4W not used here)
nonFuelOPEXUCD <- toolPrepareUCD(readSource("UCD", "nonFuelOPEX"), "nonFuelOPEX")
nonFuelOPEXUCD <- nonFuelOPEXUCD[!univocalName %in% filterEntries$trn_pass_road_LDV_4W]
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$2017/vehkm
# For some modes UCD offers only a combined value for CAPEX and non-fuel OPEX given in US$/vehkm
# combined CAPEX and OPEX include Domestic Ship, Freight Rail, HSR, International Ship, Passenger Rail
nonFuelOPEXcombinedUCD <- toolPrepareUCD(readSource("UCD", "CAPEXandNonFuelOPEX"), "CAPEXandNonFuelOPEX")
nonFuelOPEXcombinedUCD <- nonFuelOPEXcombinedUCD[!univocalName %in% c(filterEntries$trn_freight_road,
Expand All @@ -604,7 +606,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
data <- lapply(data, approx_dt, lowResYears, "period", "value",
c("region", "univocalName", "technology", "variable", "unit"), extrapolate = TRUE)

# Data for two wheelers is given in US$2017/veh and needs to be converted to US$2017/vehkm
# Data for two wheelers is given in US$/veh and needs to be converted to US$/vehkm
# with the help of annual mileage
AMUCD2W <- toolPrepareUCD(readSource("UCD", "annualMileage"), "annualMileage")
AMUCD2W <- AMUCD2W[univocalName %in% filterEntries$trn_pass_road_LDV_2W]
Expand All @@ -616,7 +618,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
"univocalName", "technology"),
all.x = TRUE)
nonFuelOPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, value := value / annualMileage]
nonFuelOPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, unit := "US$2017/vehkm"][, annualMileage
nonFuelOPEXUCD[univocalName %in% filterEntries$trn_pass_road_LDV_2W, unit := paste0(monUnit, "/vehkm")][, annualMileage
:= NULL]

# merge.data.table data
Expand Down Expand Up @@ -758,7 +760,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- VOT
},
"timeValueCosts" = {
unit <- "US$2017/pkm"
unit <- paste0(monUnit, "/pkm")
description <- "Time value costs for passenger transport modes.
Sources: GCAM"
weight <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) |> time_interpolate(highResYears)
Expand All @@ -776,7 +778,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
valcol = "multiplier")[, c("variable", "unit") := NULL]
setkey(valueOfTimeMultiplier, region, period, univocalName, technology)

GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = "constant 2017 US$MER") |> time_interpolate(highResYears)
GDPpcMERmag <- calcOutput("GDPpc", aggregate = FALSE, unit = mrdrivers::toolGetUnitDollar()) |> time_interpolate(highResYears)
GDPpcMERmag <- GDPpcMERmag[, , paste0("gdppc_", SSPscen)]

GDPpcMER <- magpie2dt(GDPpcMERmag, yearcol = "period", regioncol = "region", valcol = "gdppc")[, variable := NULL]
Expand All @@ -787,11 +789,11 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
timeValueCosts <- merge(timeValueCosts, GDPpcMER)
weeksPerYear <- 50
hoursPerWeek <- 40
timeValueCosts[, value := gdppc ## [US$2017/person/year]
* multiplier ## [US$2017/person/year]
/ (hoursPerWeek * weeksPerYear) / ## [US$2017/h]
speed] ## [US$2017/pkm]
timeValueCosts[, variable := "Time value costs"][, unit := "US$2017/pkm"]
timeValueCosts[, value := gdppc ## [US$/person/year]
* multiplier ## [US$/person/year]
/ (hoursPerWeek * weeksPerYear) / ## [US$/h]
speed] ## [US$/pkm]
timeValueCosts[, variable := "Time value costs"][, unit := paste0(monUnit, "/pkm")]
timeValueCosts <- timeValueCosts[, c("region", "univocalName", "technology",
"variable", "unit", "period", "value")]
timeValueCosts <- timeValueCosts[(univocalName %in% highResUnivocalNames
Expand Down Expand Up @@ -820,7 +822,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU
quitteobj <- timeValueCosts
},
"PurchasePriceSubsidies" = {
unit <- "US$2017/veh"
unit <- paste0(monUnit, "/veh")
description <- "Subsidies for individuals purchasing alternative technology LDVs.
If untilPrice is not NA, then the subsidies only apply if the purchase price (CAPEX)
is smaller than or equal to the untilPrice."
Expand All @@ -844,7 +846,7 @@ calcEdgeTransportSAinputs <- function(subtype, SSPscen = "SSP2EU", IEAharm = TRU

# merge subsidies with CAPEX data to obtain purchase price and calculate subsidies
data <- merge(data, CAPEXapprox, all.y = TRUE, by = intersect(names(data), names(CAPEXapprox)))
data$unit <- "US$2017/veh"
data$unit <- paste0(monUnit, "/veh")

# calculate subsidies based on purchase price and untilPrice values
calculateSubsidies <- function(period, price, subsidy, untilPrice, subsidy2,
Expand Down
6 changes: 3 additions & 3 deletions R/convertPSI.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ convertPSI <- function(x, subtype) {
subtype,
"CAPEX" = {

# PSI CAPEX need to be transformed from EUR 2017 to USD 2017
# PSI CAPEX need to be transformed from EUR 2017 to USD
x <- GDPuc::toolConvertGDP(
gdp = x,
unit_in = "constant 2017 EUR",
unit_out = mrdrivers::toolGetUnitDollar(),
replace_NAs = "with_USA"
)

getItems(x, dim = "unit") <- "US$2017/veh" # nolint: object_usage_linter
monUnit <- gsub(".*?(\\d{4}).*", "US$\\1", mrdrivers::toolGetUnitDollar())
getItems(x, dim = "unit") <- paste0(monUnit, "/veh") # nolint: object_usage_linter
},
"energyIntensity" = {
#PSI energy intensity needs to be transformed to MJ/vehkm
Expand Down
4 changes: 2 additions & 2 deletions R/convertUCD.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ convertUCD <- function(x, subtype) {

x <- toolAggregate(x, rel = UCD2iso)

# convert US$2005 to US$20217
x <- GDPuc::toolConvertGDP(
gdp = x,
unit_in = "constant 2005 US$MER",
unit_out = mrdrivers::toolGetUnitDollar(),
replace_NAs = "with_USA"
)

magclass::getNames(x) <- gsub("US\\$2005", "US$2017", magclass::getNames(x))
monUnit <- gsub(".*?(\\d{4}).*", "US$\\1", mrdrivers::toolGetUnitDollar())
magclass::getNames(x) <- gsub("US\\$2005", monUnit, magclass::getNames(x))

} else {
x <- toolAggregate(x, rel = UCD2iso)
Expand Down
2 changes: 1 addition & 1 deletion R/readTRACCS.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ readTRACCS <- function(subtype = c("fuelEnDensity", "roadFuelConsumption", "ener
}))
mpobj <- data[
, .(country_name, TRACCS_category, TRACCS_vehicle_type,
TRACCS_technology, variable = "Annual mileage", unit = "vehkm/veh/yr", period, value)] %>%
TRACCS_technology, variable = "Annual mileage", unit = "vehkm/veh yr", period, value)] %>%
as.magpie(spatial = "country_name", temporal = "period")
return(mpobj)
},
Expand Down
4 changes: 2 additions & 2 deletions R/readUCD.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ readUCD <- function(subtype = c(
"annualMileage" = {
dt <- UCD[variable == "annual travel per vehicle"]
dt[, variable := "Annual mileage"]
dt[, unit := "vehkm/veh/yr"]
dt[, unit := "vehkm/veh yr"]
},
"nonMotorizedDemand" = {
dt <- UCD[variable == "service output"]
Expand All @@ -68,7 +68,7 @@ readUCD <- function(subtype = c(
"Operating costs (tolls)", "Operating costs (total non-fuel)")]

dt[unit == "2005$/vkt", unit := "US$2005/vehkm"]
dt[unit == "2005$/veh/yr", unit := "US$2005/veh/yr"]
dt[unit == "2005$/veh/yr", unit := "US$2005/veh yr"]
},
"CAPEXandNonFuelOPEX" = {
dt <- UCD[variable %in% c("CAPEX and non-fuel OPEX")]
Expand Down
3 changes: 2 additions & 1 deletion R/toolAdjustAnnualMileage.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ toolAdjustAnnualMileage <- function(dt, completeData, filter, ariadneAdjustments
# Non-motorized modes do not get an annual mileage (no fleet tracking possible for walking/
# not planned for non-motorized cycling)
completeData <- completeData[!univocalName %in% c("Cycle", "Walk")]
mileageUnit <- unique(dt$unit)
dt <- merge.data.table(completeData, dt, all = TRUE)
# For some regions an annual mileage is provided for certain vehicle types, but no demand.
# These values need to be deleted
dt <- dt[!is.na(check)]
# update variable and unit for introduced NAs
dt[, unit := "vehkm/veh/yr"][, variable := "Annual mileage"][, check := NULL]
dt[, unit := mileageUnit][, variable := "Annual mileage"][, check := NULL]

# Average first within regions over technologies -> e.g. BEV gets the same value as other technologies
# Take min over technologies for BEV
Expand Down
2 changes: 1 addition & 1 deletion R/toolAdjustCAPEXother.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ toolAdjustCAPEXother <- function(dt, ISOcountries, yrs, completeData, GDPpcMER,
missingMoped[is.na(value), value := twoW250][, twoW250 := NULL]

missing2W <- rbind(missing50, missing250, missingMoped)
missing2W[, unit := "US$2017/vehkm"][, variable := "Capital costs (total)"]
missing2W[, unit := unique(dt[!(is.na(value))]$unit)][, variable := "Capital costs (total)"]

dt <- rbind(dt[!(is.na(value) & univocalName %in% filter$trn_pass_road_LDV_2W)], missing2W)
dt[, check := NULL]
Expand Down
Loading
Loading