Skip to content

Commit

Permalink
Implement explicit scenario choice across package.
Browse files Browse the repository at this point in the history
Introduce a scenario argument to calcCapital, calcFeDemand, calcFeDemandBuildings, calcFloorspace, calcTheil, and calcEmiTarget. The larger picture is the attempt to step away from a default set of scenarios. The choice of scenarios should be explicit. In the case of the input data pipeline, it is made in fullREMIND and then passed on to the different calcOutput functions. Hopefully this will help us control/drop/introduce new scenarios more easily in the future.
Move SSP2_NAV_all duplication to mrindustry.
Set NAs in CO2 prices from ExpertGuess to 0.
Drop unused sources argument in calcEmiTarget.
Drop unused scenario_proj argument in calcFE.
General refactoring.
  • Loading branch information
johanneskoch94 committed Feb 19, 2025
1 parent 00311a6 commit aa97c52
Show file tree
Hide file tree
Showing 41 changed files with 885 additions and 1,056 deletions.
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(
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")
}
130 changes: 59 additions & 71 deletions R/calcCapTarget.R
Original file line number Diff line number Diff line change
@@ -1,86 +1,75 @@
#' 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
#'
#' @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")) {
stop("Unknown 'sources' argument.")
}

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

if (sources == "REN21") { # only REN21
description <- "Capacity targets from REN 21(2017) database"
return(list(x = REN21data, weight = NULL, unit = "GW", description = description))
# end REN21

} 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 == "REN21") {
return(list(x = readSource("REN21", subtype = "Capacity"),
weight = NULL,
unit = "GW",
description = "Capacity targets from REN 21(2017) database"))
}

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(length(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 @@ -101,8 +90,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 @@ -122,28 +110,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,
weight = GDP2015[country.target.regs, , , invert = TRUE])
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("SSP2", scenario)), 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, scenario = 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

0 comments on commit aa97c52

Please sign in to comment.