Skip to content

Commit

Permalink
Merge pull request #524 from johannah-pik/EDGETrefactoring
Browse files Browse the repository at this point in the history
edgeTransport 2.0 release
  • Loading branch information
johannah-pik authored Jul 18, 2024
2 parents a0ab7bb + 00356c5 commit 65b82cf
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 732 deletions.
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '362724990'
ValidationKey: '36475351'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand All @@ -10,6 +10,7 @@ AcceptedNotes:
- unable to verify current time
- Imports includes .* non-default packages.
- installed size is
- 'Undefined global functions or variables:'
AutocreateReadme: yes
allowLinterWarnings: yes
enforceVersionUpdate: yes
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: 'mrremind: MadRat REMIND Input Data Package'
version: 0.182.10
date-released: '2024-07-15'
version: 0.183.1
date-released: '2024-07-17'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
- family-names: Baumstark
Expand Down
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrremind
Title: MadRat REMIND Input Data Package
Version: 0.182.10
Date: 2024-07-15
Version: 0.183.1
Date: 2024-07-17
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down Expand Up @@ -38,7 +38,7 @@ Description: The mrremind packages contains data preprocessing for the
License: LGPL-3 | file LICENSE
URL: https://github.com/pik-piam/mrremind
Depends:
edgeTransport (>= 0.17.0),
edgeTransport (>= 1.5.5),
madrat (>= 3.7.1),
magclass (>= 6.16.1),
mrcommons (>= 1.38.0),
Expand All @@ -64,7 +64,6 @@ Imports:
readxl,
reshape2,
rlang,
rmndt,
tibble,
tidyr,
tidyselect,
Expand Down
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ importFrom(assertr,not_na)
importFrom(assertr,verify)
importFrom(assertr,within_bounds)
importFrom(data.table,":=")
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
importFrom(data.table,fread)
importFrom(data.table,rbindlist)
importFrom(data.table,setDT)
importFrom(data.table,setcolorder)
importFrom(data.table,setkeyv)
importFrom(data.table,setnames)
importFrom(dplyr,across)
Expand Down Expand Up @@ -151,6 +148,8 @@ importFrom(ggplot2,scale_linetype_manual)
importFrom(ggplot2,scale_shape_manual)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_minimal)
importFrom(madrat,madratAttach)
importFrom(madrat,toolAggregate)
importFrom(magclass,matchDim)
importFrom(magrittr,"%>%")
importFrom(quitte,add_countrycode_)
Expand Down
79 changes: 0 additions & 79 deletions R/calcEDGETrData.R

This file was deleted.

190 changes: 82 additions & 108 deletions R/calcEDGETransport.R
Original file line number Diff line number Diff line change
@@ -1,132 +1,106 @@
#' @title Prepare EDGETransport inputs
#'
#' @return magpie object of EDGEtransport iterative inputs
#' @author Alois Dirnaichner, Marianna Rottoli
#' @author Johanna Hoppe
#' @seealso \code{\link{readSource}}
#' @param subtype refer to the code for a list of allowed subtypes.
#'
#' @param subtype REMIND/iterative EDGE-T input data subtypes
#' @return REMIND/iterative EDGE-T input data for all scenario combinations
#' @examples
#' \dontrun{ a <- calcOutput(type="EDGETransport", subtype="logit_exponent", aggregate=F)
#' \dontrun{ a <- calcOutput(type = "EDGETransport", subtype = "CAPEXandNonFuelOPEX", aggregate = F)
#' }
#'
#'@importFrom data.table as.data.table
calcEDGETransport <- function(subtype = "logit_exponent") {
value <- i.value <- NULL

if (subtype %in% c("logit_exponent", "ptab4W")) {
conv = FALSE
} else {
conv = TRUE
}
calcEDGETransport <- function(subtype) {

weightInt <- calcOutput("GDP", aggregate = F)
get_weight <- function(data, weightInt) {
year_inter = getYears(data)
## define weight for intensive entries (for weighted average)
weightInt <- time_interpolate(
weightInt,
year_inter,
extrapolation_type="constant")[,, getNames(data, dim=1)]
## create an empty object that has the same dimensions as data
weight <- new.magpie(cells_and_regions = getRegions(data), years = getYears(data), names = getNames(data), fill = 0)
## use the GPD for each SSP in data to fill up the empty weight (it needs as many repetitions as SSP* is called in data)
regions <- getRegions(weightInt)
years <- getYears(weightInt)
for (gdpscen in getNames(weightInt)) {
mselect(weight, region=regions, year=years, data=gdpscen) <- weightInt[regions, years, gdpscen]
}
getSets(weight, fulldim=F)[3] <- getSets(data, fulldim=F)[3]
return(weight)
}

if(subtype == "pm_trp_demand"){
fe_dem <- as.data.table(as.quitte(readSource("EDGETransport", "fe_demand_tech")))[
, c("model", "scenario", "variable", "unit") := NULL
]
fe2es <- as.data.table(as.quitte(readSource("EDGETransport", "fe2es")))[
, c("model", "scenario", "variable", "unit") := NULL
]
es_dem <- fe_dem[fe2es, on=c("period", "region", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs")][
, sum(value * i.value), by=c("period", "region", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_in")
]
data <- as.magpie(es_dem, spatial=2, temporal=1, datacol=7)
}else{
data <- readSource("EDGETransport", subtype, convert = conv)
}
x <- readSource("EDGETransport", subtype)

switch(subtype,
"logit_exponent" = {
weight = NULL
unit = "[-]"
description = "Logit exponent values for transport alternatives"
"f35_esCapCost" = {
weight <- readSource("EDGETransport", subtype = "weightESdemand")
#Rule out numerical errors after disaggregating very small numbers
weight[weight < 1e-5] <- 0
#check whether weightsum is zero for some cases
#if so, the values should just be aggregated equally in order to prevent zeros in the results
weight <- magpie2dt(weight)
regMap <- toolGetMapping(getConfig("regionmapping"), type = "regional", where = "mappingfolder")
setnames(regMap, "CountryCode", "all_regi")
weight <- merge(weight, regMap[, c("all_regi", "RegionCode")], by = "all_regi")
weight[, weightSum := sum(value), by = c("RegionCode", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs", "tall")]
weight[weightSum == 0, value := 1]
weight[, c("RegionCode", "weightSum") := NULL]
weight <- as.magpie(weight)
unit = "2005US$/(p|t)km"
description = "Capital cost (purchase) per energy service demand on CES level."
},
"harmonized_intensities" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport: [EJ/Mpkm]; freight transport: [EJ/Mtkm]"
description = "Energy intensity for transport modes and fuel, harmonized on IEA balances"
"f35_fe2es" = {
weight = readSource("EDGETransport", "f35_demByTech")
weight[weight < 1e-5] <- 0
#check whether weightsum is zero for some cases
#if so, the values should just be aggregated equally in order to prevent zeros in the results
weight <- magpie2dt(weight)
regMap <- toolGetMapping(getConfig("regionmapping"), type = "regional", where = "mappingfolder")
setnames(regMap, "CountryCode", "all_regi")
weight <- merge(weight, regMap[, c("all_regi", "RegionCode")], by = "all_regi")
weight[, weightSum := sum(value), by = c("RegionCode", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_enty", "all_in", "all_teEs", "tall")]
weight[weightSum == 0, value := 1]
weight[, c("RegionCode", "weightSum", "all_enty", "all_in") := NULL]
weight <- as.magpie(weight)
unit = "trn (p|t)km/Twa"
description = "Energy efficiency on CES level."
},
"value_time" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport: [2005US$/pkm]; freight transport: [2005US$/tkm]"
description = "Value of time for passenger transport modes"
},
"pref" = {
weight = get_weight(data, weightInt)
unit = "LDVs 4wheelers: inconvenience cost [2005US$/pkm]; all other modes: [-] share weight, a dimensionless parameter reflecting consumer preferences"
description = "Inconvenience cost reflecting availability of infrastructure"
},
"ptab4W" = {
"f35_demByTech" = {
weight = NULL
unit = "[-]"
description = "LDVs 4wheelers: factors for the inconvenience costs."
},
"price_nonmot" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport: [2005US$/pkm]; freight transport: [2005US$/tkm]"
description = "Price for non motorized transport modes (Walking; Cycling)"
unit = "TWa"
description = "Final energy demand on CES level."
},
"UCD_NEC_iso" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport: [2005US$/pkm]; freight transport: [2005US$/tkm]"
description = "Non energy costs for all motorized transport modes, both total and purchase for LDVs"
"f29_trpdemand" = {
weight = NULL
unit = "trillion pkm/trillion tkm"
description = "Energy service demand on CES level."
},
"loadFactor" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport: [pass/veh]; freight transport: [ton/veh]"
description = "Load factor for all motorized transport modes"
"CAPEXandNonFuelOPEX" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "2005US$/(p|t)km"
description = "Capital cost (purchase) and non-fuel operational costs on technology level."
},
"annual_mileage" = {
weight = get_weight(data, weightInt)
unit = "km/veh/year"
description = "Annual mileage for selected transport modes"
"scenSpecPrefTrends" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "-"
description = "Scenario specific preference trends on technology level."
},
"esCapCost" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport [2005US$/pkm]; freight transport: [2005US$/tkm]"
description = "Transport ES demand trajectories for the CES tree."
"scenSpecLoadFactor" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "-"
description = "Scenario specific load factor on technology level."
},
"fe2es" = {
weight = get_weight(data, weightInt)
unit = "Passenger transport [trn pkm/Twa], freight transport [trn tkm/Twa]"
description = "Energy efficiency of CES level nodes for Transport."
"scenSpecEnIntensity" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "MJ/vehkm"
description = "Scenario specific energy intensity on technology level."
},
"pm_trp_demand" = {
weight = NULL
unit = "Passenger transport [trn pkm], freight transport [trn tkm]"
description = "CES level transport demand."
"initialIncoCosts" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "2005US$/(p|t)km"
description = "Initial inconvenience cost values."
},
"fe_demand_tech" = {
weight = NULL
unit = "TWa"
description = "FE demand divided by technologies for different ES on the CES level."
"annualMileage" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "vehkm/yr"
description = "Annual vehicle km traveled."
},
"pm_fe_demand_EDGETbased" = {
weight = NULL
unit = "EJ"
description = "UE demand divided by technologies for different ES on the CES level."
})
"timeValueCosts" = {
gdp <- calcOutput("GDP", aggregate = FALSE)[,,"gdp_SSP2"]
weight = gdp |> time_interpolate(getYears(x))
unit = "2005US$/(p|t)km"
description = "Value of time cost equivalent."
}
)

return(list(x = data,
return(list(x = x,
weight = weight,
unit = unit,
description = description))
Expand Down
2 changes: 1 addition & 1 deletion R/calcEU_ReferenceScenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ calcEU_ReferenceScenario <- function() {

weights <- x
weights[, , ] <- NA
weights[, , "Price|Secondary Energy|Electricity (US$2005/GJ)"] <- 1
weights[, , "Price|Secondary Energy|Electricity (EUR2013/GJ)"] <- 1

return(list(
x = x, weight = weights, mixed_aggregation = TRUE,
Expand Down
9 changes: 9 additions & 0 deletions R/calcIO.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
# read in data and convert from ktoe to EJ
data <- readSource("IEA", subtype = ieaSubtype) * 4.1868e-5

# Correct transport reporting issue in IEA data for NONBIODIES.MARBUNK in RUS
# FE is reported in 1990 and 2010 but not in the years in between. This cause problems in the harmonization of EDGE-Transport
# and the IEA data in 2005 as there is no MARBUNK demand at all for REF regions.
data["RUS", seq(1990, 2010, 1), "NONBIODIES.MARBUNK"] <-
data["RUS", c(1990, 2010),"NONBIODIES.MARBUNK"]|> time_interpolate(seq(1990, 2010, 1))
#Adjust totals
data["RUS", seq(1991, 2009, 1),"TOTAL.MARBUNK"] <-
data["RUS", seq(1991, 2009, 1),"TOTAL.MARBUNK"] + data["RUS", seq(1991, 2009, 1), "NONBIODIES.MARBUNK"]

ieamatch <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "")

# add total buildings electricity demand (feelb = feelcb + feelhpb + feelrhb)
Expand Down
Loading

0 comments on commit 65b82cf

Please sign in to comment.