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

Implement explicit scenario choice across package. #633

Merged
merged 3 commits into from
Feb 21, 2025
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: '43925340'
ValidationKey: '44106600'
AcceptedWarnings:
- Invalid URL: .*
- '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: 'mrremind: MadRat REMIND Input Data Package'
version: 0.218.1
version: 0.219.0
date-released: '2025-02-21'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrremind
Title: MadRat REMIND Input Data Package
Version: 0.218.1
Version: 0.219.0
Date: 2025-02-21
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
Expand Down
19 changes: 7 additions & 12 deletions R/calcCO2Prices.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
calcCO2Prices <- function() {

# read data
x <- readSource("ExpertGuess", subtype = "co2prices")

# convert from $2005 to $2017

# Convert from $2005 to $2017
x <- GDPuc::toolConvertGDP(
gdp = x,
unit_in = "constant 2005 US$MER",
Expand All @@ -14,17 +11,15 @@ calcCO2Prices <- function() {

getNames(x) <- NULL

# read data used for weight
# Read data used for weight
ceds <- calcOutput("Emissions", datasource = "CEDS2024", aggregate = FALSE)
ceds <- ceds[, , "Emi|CO2|Energy and Industrial Processes (Mt CO2/yr)"]
# for years in the future, use last year available from CEDS
# For years in the future, use last year available from CEDS
ceds <- ceds[, pmin(getYears(x), max(getYears(ceds))), ]
getYears(ceds) <- getYears(x)

return(list(
Copy link
Contributor

@fbenke-pik fbenke-pik Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agreed as a team to not use that rule and switched off that linter. Therefore, I don't think it makes sense to go through legacy code to change it. Probably not worth changing it back now, either. So this is just a remark for future changes.

x = x,
weight = ceds,
unit = "US$2017/t CO2",
description = "CO2 prices in 2010, 2015 and 2020"
))
list(x = x,
weight = ceds,
unit = "US$2017/t CO2",
description = "CO2 prices in 2010, 2015 and 2020")
}
138 changes: 60 additions & 78 deletions R/calcCapTarget.R
Original file line number Diff line number Diff line change
@@ -1,104 +1,87 @@
#' Capacity targets from two sources
#'
#' @description The capacity targets (GW) at regional level are produced from two different databases-
#' UNFCCC_NDC database, an update of the Rogelj 2017 paper (see readme in inputdata), and REN21 Global Renewables report
#' The UNFCCC_NDC capacity targets are further broken down to conditional and unconditional targets.
#' @author Aman Malik, Oliver Richters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please bring back all the @author tags. They are useful to evaluate who to ask for help and if the function is rather new or legacy code.

#' UNFCCC_NDC database, an update of the Rogelj 2017 paper (see readme in inputdata), and REN21 Global Renewables
#' report. The UNFCCC_NDC capacity targets are further broken down to conditional and unconditional targets.
#'
#' @param sources Database source
#' @importFrom dplyr filter


#'
calcCapTarget <- function(sources) {

convertNAto0 <- function(x) {
x[is.na(x)] <- 0
return(x)
if (! sources %in% c("REN21", "UNFCCC_NDC", "UNFCCC_NDC+REN21+CHN_NUC", "NewClimate")) {
stop("Unknown 'sources' argument.")
}

REN21data <- readSource("REN21", subtype = "Capacity")

if (sources == "REN21") { # only REN21


return(list(x = REN21data,
if (sources == "REN21") {
return(list(x = readSource("REN21", subtype = "Capacity"),
weight = NULL,
unit = "GW",
description = "Capacity targets from REN 21(2017) database")
)

} else if (sources == "NewClimate") {
description = "Capacity targets from REN 21(2017) database"))
}

if (sources == "NewClimate") {
capCond <- readSource("NewClimate", subtype = "Capacity_2025_cond")
capUncond <- readSource("NewClimate", subtype = "Capacity_2025_uncond")

x <- mbind(capCond, capUncond)

return(list(x = x,
weight = NULL,
unit = "GW",
description = "Capacity targets combined from NewClimate Database for Current Policy Scenarios")
)


} else { # import NDC capacity target
listCapacitiesNDC <- list(
"2018_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2018_cond"),
"2018_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2018_uncond"),
"2021_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2021_cond"),
"2021_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2021_uncond"),
"2022_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2022_cond"),
"2022_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2022_uncond"),
"2023_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2023_cond"),
"2023_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2023_uncond"),
"2024_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2024_cond"),
"2024_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2024_uncond")
)

listYears <- lapply(listCapacitiesNDC, getItems, dim = "year") %>% unlist() %>% unique() %>% sort()
listRegions <- lapply(listCapacitiesNDC, getItems, dim = "region") %>% unlist() %>% unique() %>% sort()

# expand all magpies to listYears
expandMagpieYears <- function(x) {
y <- new.magpie(cells_and_regions = listRegions, years = listYears, names = getNames(x))
for (year in getItems(x, dim = "year")) {
y[, year, ] <- x[, year, ]
}
return(y)
}

lapply(listCapacitiesNDC, expandMagpieYears) %>% mbind() %>% convertNAto0() -> NDC
}

if (sources == "UNFCCC_NDC") { # if no other source
description <- "Capacity targets from Nationally Determined Contributions (NDC)"
return(list(x = NDC, weight = NULL, unit = "GW", description = description))
listCapacitiesNDC <- list(
"2018_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2018_cond"),
"2018_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2018_uncond"),
"2021_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2021_cond"),
"2021_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2021_uncond"),
"2022_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2022_cond"),
"2022_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2022_uncond"),
"2023_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2023_cond"),
"2023_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2023_uncond"),
"2024_cond" = readSource("UNFCCC_NDC", subtype = "Capacity_2024_cond"),
"2024_uncond" = readSource("UNFCCC_NDC", subtype = "Capacity_2024_uncond")
)
listYears <- lapply(listCapacitiesNDC, getItems, dim = "year") %>% unlist() %>% unique() %>% sort()
NDC <- purrr::map(listCapacitiesNDC,
~ add_columns(.x, listYears[!listYears %in% getItems(.x, dim = "year")], 2)) %>%
mbind()
NDC <- NDC[, sort(getYears(NDC)), ]
NDC[is.na(NDC)] <- 0

if (sources == "UNFCCC_NDC") {
return(list(x = NDC,
weight = NULL,
unit = "GW",
description = "Capacity targets from Nationally Determined Contributions (NDC)"))
}

if (sources == "UNFCCC_NDC+REN21+CHN_NUC") { # Additional CHN nuclear target and EV target

if (sources == "UNFCCC_NDC+REN21+CHN_NUC") {
# used to extend non NDC-data to data structure of NDC targets
extend2Dim <- function(x, dimNames) {
listx <- rep(list(x), length(dimNames))
for (i in seq(length(dimNames))) {
for (i in seq_along(dimNames)) {
getNames(listx[[i]]) <- paste(dimNames[[i]], getNames(x), sep = ".")
}
return(mbind(listx))
}

REN21data <- readSource("REN21", subtype = "Capacity")
REN21 <- extend2Dim(REN21data, names(listCapacitiesNDC))

# names of all technologies in REN21 and NDC database + apCarElT
c(getNames(NDC), getNames(REN21), paste(names(listCapacitiesNDC), "apCarElT", sep = ".")) %>%
unique() %>% sort() -> techNames
techNames <- c(getNames(NDC), getNames(REN21), paste(names(listCapacitiesNDC), "apCarElT", sep = ".")) %>%
unique() %>%
sort()

x <- new.magpie(getItems(REN21, dim = "region"), getYears(REN21), techNames)
# China's nuclear target
common_tech <- intersect(
getNames(REN21) %>% unlist() %>% unique(),
getNames(NDC) %>% unlist() %>% unique())
common_tech <- intersect(getNames(REN21) %>% unlist() %>% unique(),
getNames(NDC) %>% unlist() %>% unique())
# for common technologies, take bigger value
x[, listYears, common_tech] <- pmax(REN21[, listYears, common_tech], NDC[, , common_tech])
# for tech. in REN21 but not in NDC, take REN21 values
x[, , setdiff(getNames(REN21), common_tech)] <- REN21[, , setdiff(getNames(REN21), common_tech)]
x[, , setdiff(getNames(REN21), common_tech)] <- REN21[, , setdiff(getNames(REN21), common_tech)]
# for tech. in NDC but not in REN21, take NDC values
x[, getYears(NDC), setdiff(getNames(NDC), common_tech)] <- NDC[, , setdiff(getNames(NDC), common_tech)]
# additional nuclear policy for CHN. The target is actually 2020 in 58 GW in 2020, but setting this leads to an
Expand All @@ -119,8 +102,7 @@ calcCapTarget <- function(sources) {
# hydrogen capacity targets from national/EU hydrogen strategies

# Region targets
reg.map <- toolGetMapping("regionmappingH12.csv", type = "regional",
where = "mappingfolder") # get H12 regionmapping
reg.map <- toolGetMapping("regionmappingH12.csv", type = "regional", where = "mappingfolder")
H2Target.reg <- new.magpie(unique(reg.map$RegionCode), getYears(x), "elh2", fill = 0)
# Electrolyzer capacity target from the EU Hydrogen Strategy
# https://ec.europa.eu/energy/sites/ener/files/hydrogen_strategy.pdf
Expand All @@ -140,28 +122,28 @@ calcCapTarget <- function(sources) {
H2Target <- new.magpie(unique(reg.map$RegionCode), getYears(x), "elh2", fill = 0)
H2Target["EUR", , "elh2"] <- H2Target.reg["EUR", , "elh2"] - H2Target.CountryAgg["EUR", , "elh2"]

# # SE VRE Production in 2015 to be used as weight for disaggregation EU target to iso countries
# SEHistVRE <- dimSums(calcOutput("FE", aggregate = FALSE)[,"y2015",c("SE|Electricity|Solar (EJ/yr)",
# "SE|Electricity|Wind (EJ/yr)")],
# dim = 3)

# GDP 2015 to be used as weight for disaggregation of EU target to iso coutries
GDP2015 <- calcOutput("GDPPast", aggregate = FALSE)[, "y2015", ]

# regionmapping without countries that already have a country target
CountryCode <- NULL
reg.map.reduced <- reg.map %>%
filter(!CountryCode %in% country.target.regs)
reg.map.reduced <- reg.map %>% dplyr::filter(!.data$CountryCode %in% country.target.regs)
# disaggregate EU target to iso-countries
H2Target.disagg <- toolAggregate(H2Target, reg.map.reduced,
from = "RegionCode", to = "CountryCode", dim = 1,
H2Target.disagg <- toolAggregate(H2Target,
reg.map.reduced,
from = "RegionCode",
to = "CountryCode",
dim = 1,
weight = GDP2015[country.target.regs, , , invert = TRUE])
# bind country target together with disaggregation of EU targets to other countries
H2Target.out <- magpiesort(mbind(H2Target.country[country.target.regs, , ], H2Target.disagg))
x <- mbind(x, extend2Dim(H2Target.out, names(listCapacitiesNDC)))
### Hydrogen Target End

description <- "Capacity targets combined from REN 21(2017), NDC database, special case for China nuclear and EV"
return(list(x = convertNAto0(x), weight = NULL, unit = "GW", description = description))
} # end sources = UNFCCC_NDC+REN21+CHN_NUC
x[is.na(x)] <- 0

return(list(x = x,
weight = NULL,
unit = "GW",
description = glue::glue("Capacity targets combined from REN 21(2017), NDC database, special case \\
for China nuclear and EV")))
}
}
30 changes: 17 additions & 13 deletions R/calcCapital.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
#' that of Japan in 2010, at speeds that vary across scenarios. The final capital stocks are the product
#' of the capital intensities and the gdp scenarios from [mrdrivers].
#'
#' @param scenario GDP and pop scenarios. Passed to [mrdrivers::calcGDP()].
#' @export
#' @seealso \itemize{
#' \item See the vignette \code{vignette("scenarios", "mrdrivers")} for information on the GDP scenarios.
#' \item [readPWT()] for information on the PWT version used.
#' }
#' @inherit madrat::calcOutput return
calcCapital <- function() {

calcCapital <- function(scenario) {
# Get capital intensities (capital over GDP) from PWT
kPWT <- readSource("PWT")[, , "rkna"]
gdpPWT <- readSource("PWT")[, , "rgdpna"]
kIntPWT <- kPWT / setNames(gdpPWT, NULL)

# Get GDP from mrdrivers (which differs from GDP in PWT)
gdp <- calcOutput("GDP",
scenario = c("SSPs", "SDPs"),
naming = "scenario",
aggregate = FALSE,
years = seq(1995, 2150, 5))
# Get GDP from mrdrivers (which differs from GDP in PWT). Make sure SSP2 is always returned.
gdp <- calcOutput("GDP", scenario = unique(c(scenario, "SSP2")), aggregate = FALSE, years = seq(1995, 2150, 5))

# Define reference capital intensity, and the convergence time in years, of the countries capital intensities towards
# that reference, for the different GDP scenarios. The convergence assumptions should follow the SSP narratives.
# Convergence starts after 2010.
kIntRef <- kIntPWT["JPN", 2010, ] %>% as.numeric()
convTime <- c("SSP1" = 150, "SSP2" = 250, "SSP3" = 150, "SSP4" = 300, "SSP5" = 150,
"SDP" = 150, "SDP_EI" = 150, "SDP_RC" = 150, "SDP_MC" = 150)
convTime <- c("SSP1" = 150, "SSP2" = 250, "SSP3" = 150, "SSP4" = 300, "SSP5" = 150)

# If required, add any assumption on convergence times for non SSP scenarios. By default use the SSP2 convTime.
if (any(! getNames(gdp) %in% names(convTime))) {
scens <- getNames(gdp)[! getNames(gdp) %in% names(convTime)]
message(glue::glue("Adding {paste(scens, collapse = ', ')} assumptions as copies of SSP2."))
addConvTime <- purrr::map(scens, ~`names<-`(convTime["SSP2"], .x)) %>% purrr::list_c()
convTime <- c(convTime, addConvTime)
}


# Create kInt magpie object with the same dimension as gdp, and assign the PWT capital intensities for the
# historic years until 2010.
Expand Down Expand Up @@ -60,9 +64,9 @@ calcCapital <- function() {

# Use regional average for countries missing data
## To get regional aggregate values for all countries: first aggregate, using only non-missing countries and
## partrel = TRUE, then disaggregate again.
## partrel = TRUE, then disaggregate again. (Faster than toolFillWithRegionAvg).
nmc <- getItems(kInt, dim = 1)[!is.nan(kInt[, 2010, "SSP2"])]
map <- toolGetMapping("regionmappingH12.csv")
map <- toolGetMapping("regionmappingH12.csv", where = "mappingfolder", type = "regional")
kIntReg <- toolAggregate(kInt[nmc, , ], rel = map, weight = gdp[nmc, , ], partrel = TRUE) %>%
toolAggregate(rel = map, from = "RegionCode", to = "CountryCode")
mc <- setdiff(getItems(kInt, dim = 1), nmc)
Expand All @@ -73,7 +77,7 @@ calcCapital <- function() {

# Add industry energy efficiency capital stocks (passing 2015 SSP2 capital stock as "kap")
kap <- k[, 2015, "SSP2"] %>% tibble::as_tibble() %>% dplyr::select("iso3c", "kap" = "value")
EEK <- calcOutput("Industry_EEK", kap = kap, aggregate = FALSE, years = getYears(k))
EEK <- calcOutput("Industry_EEK", kap = kap, scenarios = scenario, aggregate = FALSE, years = getYears(k))

# Modify names to differentiate the macroeconomic capital "kap" from EEK capital stocks.
getNames(k) <- paste0(getNames(k), ".kap")
Expand Down
Loading
Loading