-
Notifications
You must be signed in to change notification settings - Fork 47
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please bring back all the |
||
#' 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 | ||
|
@@ -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 | ||
|
@@ -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"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.