Skip to content

Commit

Permalink
Merge pull request #529 from fbenke-pik/weo
Browse files Browse the repository at this point in the history
update IEA WEO reference data to 2023 data
  • Loading branch information
fbenke-pik authored Aug 30, 2024
2 parents c02a561 + b68ad77 commit 8728c5b
Show file tree
Hide file tree
Showing 15 changed files with 732 additions and 599 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '37105140'
ValidationKey: '37153004'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
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: 'mrremind: MadRat REMIND Input Data Package'
version: 0.186.0
date-released: '2024-08-14'
version: 0.186.1
date-released: '2024-08-29'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
- family-names: Baumstark
Expand Down
4 changes: 2 additions & 2 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.186.0
Date: 2024-08-14
Version: 0.186.1
Date: 2024-08-29
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
47 changes: 1 addition & 46 deletions R/calcFeDemandIndustry.R
Original file line number Diff line number Diff line change
Expand Up @@ -950,53 +950,8 @@ calcFeDemandIndustry <- function(use_ODYM_RECC = FALSE) {
industry_subsectors_en <- industry_subsectors_en %>%
mutate(pf = sub("^feelwlth_", "feel_", .data$pf))

### extend to 2020 using WEO 2021 data ----
IEA_WEO_2021_ref_year <- 2020L
last_industry_subsectors_en_year <- max(industry_subsectors_en$year)
if (IEA_WEO_2021_ref_year > last_industry_subsectors_en_year) {
industry_subsectors_en <- bind_rows(
industry_subsectors_en,

inner_join(
industry_subsectors_en %>%
filter(2010 == .data$year),

readSource(type = "IEA_WorldEnergyOutlook", subtype = "2021-region",
convert = TRUE) %>%
magclass_to_tibble(c("iso3c", "year", "scenario", "variable", "industry.FE")) %>%
filter(.data$year %in% c(2010, IEA_WEO_2021_ref_year),
"Stated Policies Scenario" == .data$scenario,
"Energy-Total-Industry (EJ)" == .data$variable) %>%
left_join(region_mapping_21, "iso3c") %>%
group_by(!!!syms(c("region", "year"))) %>%
summarise(industry.FE = sum(.data$industry.FE, na.rm = TRUE),
.groups = "drop_last") %>%
summarise(change = last(.data$industry.FE, order_by = .data$year)
/ first(.data$industry.FE, order_by = .data$year),
.groups = "drop"),

"region"
) %>%
mutate(value = .data$value * .data$change,
year = IEA_WEO_2021_ref_year) %>%
select(-"change")
) %>%
pivot_wider(names_from = "year") %>%
mutate(`2015` = (!!sym("2010")
+ !!sym("2015")
+ !!sym(as.character(IEA_WEO_2021_ref_year))
)
/ 3) %>%
pivot_longer(matches("^[0-9]*$"), names_to = "year",
names_transform = list(year = as.integer)) %>%
interpolate_missing_periods_(periods = list(year = seq_range(range(.$year)))) %>%
assert(not_na, everything()) %>%
verify(is.finite(.data$value))

FE_alpha_mod <- 0.9 # found by fiddling around
} else {
FE_alpha_mod <- 1
}
FE_alpha_mod <- 1

### calculate 1993-2020 industry subsector FE shares ----
industry_subsectors_en_shares <- industry_subsectors_en %>%
Expand Down
128 changes: 54 additions & 74 deletions R/calcIEA_WorldEnergyOutlook.R
Original file line number Diff line number Diff line change
@@ -1,61 +1,46 @@
#' Calculate REMIND variables from IEA World Energy Outlook data.
#'
#' @param version of the WEO data, either 'default' (full paid version) or
#' 'latest' (free, restricted, up-to-date dataset)
#' @author Falk Benke
#' @export

calcIEA_WorldEnergyOutlook <- function(version = "latest") { # nolint
calcIEA_WorldEnergyOutlook <- function() { # nolint

if (!(version %in% c("default", "latest"))) {
stop("Invalid parameter `version`. Must be either 'default' or 'latest'")
}

refYear <- if (version == "default") 2021 else 2023

dataGlo <- readSource("IEA_WorldEnergyOutlook", subtype = paste0(refYear, "-global"))
dataReg <- readSource("IEA_WorldEnergyOutlook", subtype = paste0(refYear, "-region"))
dataGlo <- readSource("IEA_WorldEnergyOutlook", convert = FALSE)["World", , ]
magclass::getItems(dataGlo, dim = 1) <- "GLO"
dataReg <- readSource("IEA_WorldEnergyOutlook", convert = TRUE)

.mapToRemind <- function(data) {
if (refYear == 2021) {
# copy over Stated Policies Scenario for 2010 - 2020 to other scenarios
for (s in getNames(data, dim = 1)) {
data[, c("y2010", "y2019", "y2020"), s] <-
data[, c("y2010", "y2019", "y2020"), "Stated Policies Scenario"][, , getNames(data[, , s], dim = 2)]
}

# copy over Stated Policies Scenario for 2010 - 2022 to other scenarios
for (s in magclass::getNames(data, dim = 1)) {
data[, c("y2010", "y2015", "y2021", "y2022"), s] <-
data[, c("y2010", "y2015", "y2021", "y2022"), "Stated Policies Scenario"][, , magclass::getNames(data[, , s], dim = 2)]
}

# rename scenarios
scens <- c(
"Stated Policies Scenario" = "SPS",
"Announced pledges scenario" = "APS",
"Announced Pledges Scenario" = "APS",
"Sustainable Development Scenario" = "SDS",
"Net Zero Emissions by 2050 Scenario" = "Net2050"
)

getNames(data, dim = 1) <- paste0("IEA WEO ", refYear, " ", scens[getNames(data, dim = 1)])
getSets(data)[3] <- "model"
magclass::getNames(data, dim = 1) <- paste0("IEA WEO 2023 ", scens[magclass::getNames(data, dim = 1)])
magclass::getSets(data)[3] <- "model"

map <- toolGetMapping(
"Mapping_IEA_WEO_2021_complete.csv",
type = "reportingVariables", where = "mrremind"
) %>%
map <- toolGetMapping("Mapping_IEA_WEO_complete.csv", type = "reportingVariables", where = "mrremind") %>%
dplyr::filter(!is.na(.data$REMIND), .data$REMIND != "") %>%
dplyr::mutate(
"from" = paste0(trimws(.data$WEO), " (", .data$Unit_WEO, ")"),
"to" = paste0(trimws(.data$REMIND), " (", .data$Unit_REMIND, ")"),
"conversion" = as.numeric(.data$Conversion)
) %>%
dplyr::select("from", "to", "conversion")
dplyr::mutate("conversion" = as.numeric(.data$Conversion)) %>%
dplyr::select("from" = "Variable", "to" = "REMIND", "conversion")

for (var in intersect(getNames(data, dim = 2), unique(map$from))) {
conv <- map[map$from == var, "conversion"]

# if there is more than one conversion factor, it means that one source variable
# is converted two more than one target variable using a different conversion
# is converted two more than one target variable using a different conversion factor
if (length(unique(conv)) > 1) {

# create unique "from" variables for each mapping entry
# create unique "from" variables for each mapping entry by appending numbers
map[map$from == var, "from"] <- paste0(map[map$from == var, "from"], " ", seq(1, nrow(map[map$from == var, ])))

# duplicate "from" data with for each mapping entry
Expand All @@ -81,67 +66,62 @@ calcIEA_WorldEnergyOutlook <- function(version = "latest") { # nolint
dataGlo <- .mapToRemind(dataGlo)
dataReg <- .mapToRemind(dataReg)

# do additional calculations on full dataset for 2021 (won't work for incomplete 2023 data)
if (refYear == 2021) {
.calcAdditionalVars <- function(x) {
# correct PE|Nuclear and PE
# PE Nuclear is usually reported in direct equivalents, values from IEA are
# roughly 3 times higher than the REMIND ones
x[, , "PE (EJ/yr)"] <- x[, , "PE (EJ/yr)"] - x[, , "PE|Nuclear (EJ/yr)"]
x[, , "PE|Nuclear (EJ/yr)"] <- x[, , "PE|Nuclear (EJ/yr)"] / 3
x[, , "PE (EJ/yr)"] <- x[, , "PE (EJ/yr)"] + x[, , "PE|Nuclear (EJ/yr)"]
.calcAdditionalVars <- function(x) {
# correct PE|Nuclear and PE
# PE Nuclear is usually reported in direct equivalents, values from IEA are
# roughly 3 times higher than the REMIND ones
x[, , "PE (EJ/yr)"] <- x[, , "PE (EJ/yr)"] - x[, , "PE|Nuclear (EJ/yr)"]
x[, , "PE|Nuclear (EJ/yr)"] <- x[, , "PE|Nuclear (EJ/yr)"] / 3
x[, , "PE (EJ/yr)"] <- x[, , "PE (EJ/yr)"] + x[, , "PE|Nuclear (EJ/yr)"]

return(x)
}
return(x)
}

dataGlo <- .calcAdditionalVars(dataGlo)
dataReg <- .calcAdditionalVars(dataReg)
dataGlo <- .calcAdditionalVars(dataGlo)
dataReg <- .calcAdditionalVars(dataReg)

dataGlo <- add_columns(dataGlo, "Cap|Electricity|Biomass|w/o CC (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Biomass|w/o CC (GW)"] <-
dataGlo[, , "Cap|Electricity|Biomass (GW)"] - dataGlo[, , "Cap|Electricity|Biomass|w/ CC (GW)"]
dataGlo <- magclass::add_columns(dataGlo, "Cap|Electricity|Biomass|w/o CC (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Biomass|w/o CC (GW)"] <-
dataGlo[, , "Cap|Electricity|Biomass (GW)"] - dataGlo[, , "Cap|Electricity|Biomass|w/ CC (GW)"]

dataGlo <- add_columns(dataGlo, "Cap|Electricity|Coal (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Coal (GW)"] <-
dataGlo[, , "Cap|Electricity|Coal|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Coal|w/ CC (GW)"]
dataGlo <- magclass::add_columns(dataGlo, "Cap|Electricity|Coal (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Coal (GW)"] <-
dataGlo[, , "Cap|Electricity|Coal|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Coal|w/ CC (GW)"]

dataGlo <- add_columns(dataGlo, "Cap|Electricity|Solar (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Solar (GW)"] <-
dataGlo[, , "Cap|Electricity|Solar|CSP (GW)"] + dataGlo[, , "Cap|Electricity|Solar|PV (GW)"]
dataGlo <- magclass::add_columns(dataGlo, "Cap|Electricity|Solar (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Solar (GW)"] <-
dataGlo[, , "Cap|Electricity|Solar|CSP (GW)"] + dataGlo[, , "Cap|Electricity|Solar|PV (GW)"]

dataGlo <- add_columns(dataGlo, "Cap|Electricity|Fossil (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Fossil (GW)"] <-
dataGlo[, , "Cap|Electricity|Fossil|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Fossil|w/ CC (GW)"]
dataGlo <- magclass::add_columns(dataGlo, "Cap|Electricity|Fossil (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Fossil (GW)"] <-
dataGlo[, , "Cap|Electricity|Fossil|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Fossil|w/ CC (GW)"]

dataGlo <- add_columns(dataGlo, "Cap|Electricity|Gas (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Gas (GW)"] <-
dataGlo[, , "Cap|Electricity|Gas|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Gas|w/ CC (GW)"]
dataGlo <- magclass::add_columns(dataGlo, "Cap|Electricity|Gas (GW)", dim = 3.2)
dataGlo[, , "Cap|Electricity|Gas (GW)"] <-
dataGlo[, , "Cap|Electricity|Gas|w/o CC (GW)"] + dataGlo[, , "Cap|Electricity|Gas|w/ CC (GW)"]

dataGlo <- add_columns(dataGlo, "SE|Electricity|Solar (EJ/yr)", dim = 3.2)
dataGlo[, , "SE|Electricity|Solar (EJ/yr)"] <-
dataGlo[, , "SE|Electricity|Solar|PV (EJ/yr)"] + dataGlo[, , "SE|Electricity|Solar|CSP (EJ/yr)"]
}
dataGlo <- magclass::add_columns(dataGlo, "SE|Electricity|Solar (EJ/yr)", dim = 3.2)
dataGlo[, , "SE|Electricity|Solar (EJ/yr)"] <-
dataGlo[, , "SE|Electricity|Solar|PV (EJ/yr)"] + dataGlo[, , "SE|Electricity|Solar|CSP (EJ/yr)"]

# includes values from the original source for global region instead of calculating
# it as the sum of all countries (as countries are incomplete)
.customAggregate <- function(x, rel, to = NULL, glo) {
x <- toolAggregate(x, rel = rel, to = to)

if ("GLO" %in% getItems(x, dim = 1)) {
if ("GLO" %in% magclass::getItems(x, dim = 1)) {
x <- x["GLO", , , invert = TRUE]

glo <- dimSums(glo, dim = 1, na.rm = FALSE)

out <- new.magpie(
cells_and_regions = union(getItems(x, dim = 1), "GLO"),
years = union(getYears(x), getYears(glo)),
names = union(getNames(x), getNames(glo)),
out <- magclass::new.magpie(
cells_and_regions = union(magclass::getItems(x, dim = 1), "GLO"),
years = union(magclass::getYears(x), magclass::getYears(glo)),
names = union(magclass::getNames(x), magclass::getNames(glo)),
fill = NA,
sets = names(dimnames(x))
)

out[getItems(x, dim = 1), getYears(x), getNames(x)] <- x
out["GLO", getYears(glo), getNames(glo)] <- glo
out[magclass::getItems(x, dim = 1), magclass::getYears(x), magclass::getNames(x)] <- x
out["GLO", magclass::getYears(glo), magclass::getNames(glo)] <- glo

return(out)
} else {
Expand All @@ -155,6 +135,6 @@ calcIEA_WorldEnergyOutlook <- function(version = "latest") { # nolint
unit = c("GW", "EJ/yr", "Mt CO2/yr"),
aggregationFunction = .customAggregate,
aggregationArguments = list(glo = dataGlo),
description = "IEA World Energy Outlook values as REMIND variables"
description = "IEA World Energy Outlook 2023 values as REMIND variables"
))
}
Loading

0 comments on commit 8728c5b

Please sign in to comment.