Skip to content

Commit

Permalink
Merge pull request #205 from fbenke-pik/global_mif
Browse files Browse the repository at this point in the history
Generate global and regional mif
  • Loading branch information
fbenke-pik authored Feb 17, 2022
2 parents fe64e79 + 99282fc commit 03cebc2
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '20562120'
ValidationKey: '20753600'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mrremind: MadRat REMIND Input Data Package",
"version": "0.108.0",
"version": "0.109.0",
"description": "<p>The mrremind packages contains data preprocessing for the REMIND model.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrremind
Type: Package
Title: MadRat REMIND Input Data Package
Version: 0.108.0
Date: 2022-02-16
Version: 0.109.0
Date: 2022-02-17
Authors@R: c(person("Lavinia", "Baumstark", email = "lavinia@pik-potsdam.de", role = c("aut","cre")),
person("Renato", "Rodrigues", role = "aut"),
person("Antoine", "Levesque", role = "aut"),
Expand Down
7 changes: 2 additions & 5 deletions R/calcHistorical.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' Gather reference data from various sources.
#' @importFrom magclass setNames getNames getSets add_columns
#' @importFrom luscale rename_dimnames

Expand Down Expand Up @@ -177,8 +178,6 @@ calcHistorical <- function() {
ARIADNE_ReferenceScenarioPop <- add_dimension(ARIADNE_ReferenceScenarioPop,
dim = 3.1, add = "model", nm = "ARIADNE")

IEA_ETP <- calcOutput("IEA_ETP", aggregate = F)

IEA_EVOutlook <- calcOutput("IEA_EVOutlook", aggregate = F)

# Calculate Emission Reference Values
Expand Down Expand Up @@ -235,8 +234,6 @@ calcHistorical <- function() {
BP <- calcOutput("BP", aggregate = FALSE)
BP <- add_dimension(BP, dim = 3.1, add = "model", nm = "BP")

WEO_2021 <- calcOutput("IEA_WEO_2021", subtype = "regional", aggregate = F)

# Steel Production ----
worldsteel <- readSource('worldsteel', convert = FALSE) %>%
madrat_mule() %>%
Expand Down Expand Up @@ -294,7 +291,7 @@ calcHistorical <- function() {
LU_EDGAR_LU, LU_CEDS, LU_FAO_EmisLUC, LU_FAO_EmisAg, LU_PRIMAPhist, IRENAcap, eurostat, #emiMktES, emiMktETS, emiMktESOthers,
EU_ReferenceScenario, emiEurostat, ARIADNE_ReferenceScenarioGdp, ARIADNE_ReferenceScenarioGdpCorona,
ARIADNE_ReferenceScenarioPop, EEA_GHGSectoral, EEA_GHGTotal, EEA_GHGProjections, Emi_Reference, #, EEA_GHGES
IEA_ETP, IEA_EVOutlook, INNOPATHS, JRC_Industry, JRC_Transport, JRC_ResCom, AGEB_FE, UBA_emi, UNFCCC, BP, worldsteel, WEO_2021)
IEA_EVOutlook, INNOPATHS, JRC_Industry, JRC_Transport, JRC_ResCom, AGEB_FE, UBA_emi, UNFCCC, BP, worldsteel)

y <- Reduce(union,lapply(varlist,getYears))
n <- Reduce(c,lapply(varlist,getNames))
Expand Down
36 changes: 25 additions & 11 deletions R/calcIEA_ETP.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,39 @@
#'
#' @author Falk Benke
#'
#' @param aggregate Boolean indicating whether output data aggregation should be performed or not
#' @param isValidation indicates if result will be used in validation (as opposed to generating input data)
#' @importFrom dplyr select mutate left_join
#' @importFrom madrat toolGetMapping
#' @importFrom magclass as.magpie
#' @importFrom rlang sym
#' @importFrom stats aggregate na.pass
#' @export

calcIEA_ETP <- function() {
calcIEA_ETP <- function(aggregate, isValidation = FALSE) {

mapping <- toolGetMapping("Mapping_IEA_ETP.csv", type = "reportingVariables") %>%
filter(!is.na(!!sym("REMIND")), !!sym("REMIND") != "") %>%
mutate(!!sym("Conversion") := as.numeric(!!sym("Conversion"))) %>%
select("variable" = "IEA_ETP", "REMIND", "Conversion", "Unit_REMIND")

mapping$variable <- trimws(mapping$variable)
mapping$REMIND <- trimws(mapping$REMIND)

x1 <- readSource("IEA_ETP", subtype = "industry")
x2 <- readSource("IEA_ETP", subtype = "transport")
x3 <- readSource("IEA_ETP", subtype = "buildings")
x4 <- readSource("IEA_ETP", subtype = "summary")
data <- mbind(x1,x2,x3,x4)

data <- mbind(x1, x2, x3, x4)

data <- as.data.frame(data) %>%
as_tibble() %>%
select(
"region" = "Region", "scenario" = "Data1", "variable" = "Data2",
"year" = "Year", "value" = "Value"
)

x <- left_join(
data,
mapping,
Expand All @@ -49,15 +51,27 @@ calcIEA_ETP <- function() {
!!sym("year") := as.numeric(as.character(!!sym("year")))
) %>%
select("region", "year", "model", "variable" = "REMIND", "value")

x <- aggregate(value ~ region + year + model + variable, x, sum, na.action = na.pass) %>%
as.magpie()

# filter variables that are too imprecise on regional level
if (aggregate == "region") {
x <- x[, , c(
"Production|Industry|Cement (Mt/yr)",
"Production|Industry|Steel (Mt/yr"
), pmatch = TRUE, invert = TRUE]
}

if (isValidation) {
x <- add_dimension(x, dim = 3.1, add = "scenario", nm = "historical")
}

return(list(
x = x,
x = x,
weight = NULL,
unit = c("EJ/yr", "Mt CO2/yr", "Mt/yr", "bn pkm/yr", "bn tkm/yr"),
description = "IEA ETP projections as REMIND variables"
))

}
18 changes: 12 additions & 6 deletions R/calcIEA_WEO_2021.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#'
#' @md
#' @return A [`magpie`][magclass::magclass] object.
#' @param subtype Either "GLO" or "regional"
#' @param aggregate Boolean indicating whether output data aggregation should be performed or not
#' @param isValidation indicates if result will be used in validation (as opposed to generating input data)
#' @author Falk Benke
#' @importFrom dplyr select mutate left_join case_when
#' @importFrom madrat toolGetMapping
#' @importFrom magclass as.magpie
#' @importFrom rlang sym
#' @export

calcIEA_WEO_2021 <- function(aggregate, isValidation = FALSE) {

calcIEA_WEO_2021 <- function(subtype = "GLO") {
if (!subtype %in% c("GLO", "regional")) {
stop("Not a valid subtype! Must be either \"regional\" or \"GLO\"")
if (!aggregate %in% c("global", "region")) {
aggregate <- "global"
}

mapping <- toolGetMapping("Mapping_IEA_WEO_2021.csv", type = "reportingVariables") %>%
Expand All @@ -23,7 +24,7 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") {

mapping$variable <- trimws(mapping$variable)

data <- readSource("IEA_WEO_2021", subtype = subtype)
data <- readSource("IEA_WEO_2021", subtype = aggregate)

# copy over Stated Policies Scenario for 2010 - 2020 to other scenarios
for (s in getNames(data, dim = 1)) {
Expand Down Expand Up @@ -58,7 +59,7 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") {

x <- as.magpie(x, spatial = 1, temporal = 2, data = 5)

if (subtype == "GLO") {
if (aggregate == "global") {
x <- add_columns(x, "Cap|Electricity|Biomass|w/o CC (GW)", dim = 3.2)
x[, , "Cap|Electricity|Biomass|w/o CC (GW)"] <- x[, , "Cap|Electricity|Biomass (GW)"] - x[, , "Cap|Electricity|Biomass|w/ CC (GW)"]

Expand All @@ -75,8 +76,13 @@ calcIEA_WEO_2021 <- function(subtype = "GLO") {
x[, , "Cap|Electricity|Gas (GW)"] <- x[, , "Cap|Electricity|Gas|w/o CC (GW)"] + x[, , "Cap|Electricity|Gas|w/ CC (GW)"]
}

if (isValidation) {
x <- add_dimension(x, dim = 3.1, add = "scenario", nm = "historical")
}

return(list(
x = x,
weight = NULL,
unit = c("GW", "EJ/yr", "Mt CO2/yr"),
description = "IEA WEO 2021 values as REMIND variables"
))
Expand Down
26 changes: 13 additions & 13 deletions R/convertIEA_WEO_2021.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Disaggregates IEA WEO 2021 Data
#' @param x MAgPIE object to be converted
#' @return A [`magpie`][magclass::magclass] object.
#' @param subtype Either "GLO" or "regional"
#' @param subtype Either "global" or "region"
#' @author Falk Benke
#' @importFrom madrat getISOlist
#'

convertIEA_WEO_2021 <- function(x, subtype = "GLO") {
convertIEA_WEO_2021 <- function(x, subtype = "global") {
PE <- calcOutput("PE", aggregate = FALSE)
if (subtype == "GLO") {
if (subtype == "global") {

# for now, we only have complete data on global level
x.world <- x["World", , ]
Expand All @@ -25,7 +25,7 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") {
weight <- PE[, 2016, "PE (EJ/yr)"]
x.world <- toolAggregate(x.world, rel = mapping_world, weight = weight)
return(x.world)
} else if (subtype == "regional") {
} else if (subtype == "region") {
.removeNaRegions <- function(x) {
remove <- magpply(x, function(y) all(is.na(y)), MARGIN = 1)
return(x[!remove, , ])
Expand All @@ -39,7 +39,7 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") {
regions <- intersect(regions_in, getItems(x, dim = 1))

# iso countries in x
ctry <- toolCountry2isocode(getItems(x, dim = 1), warn = F)
ctry <- toolCountry2isocode(getItems(x, dim = 1), warn = FALSE)
ctry <- ctry[!is.na(ctry)]

# mapping of regions to iso countries other than in ctry (i.e. other regions)
Expand All @@ -58,27 +58,27 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") {
return(toolCountryFill(x2, fill = NA, verbosity = 2))
}

getItems(x1, dim = 1) <- toolCountry2isocode(getItems(x1, dim = 1), warn = F)
getItems(x1, dim = 1) <- toolCountry2isocode(getItems(x1, dim = 1), warn = FALSE)

# combine the two objects
x <- mbind(x1, x2)
x <- toolCountryFill(x, fill = NA, verbosity = 2)

return(x)
}

# exclude all regions we don't want to disaggregate due to redundancies or lack of accuracy
x.reg <- x[c(
"Atlantic Basin", "East of Suez", "NonOPEC", "OPEC", "Japan and Korea",
"Southeast Asia", "Other", "European Union", "World"
), , , invert = T]
), , , invert = TRUE]

# remove all-na variables
remove <- magpply(x.reg, function(y) all(is.na(y)), MARGIN = 3)
x.reg <- x.reg[, , !remove]

# remove 2040 as year, as source has no regional data for this year
x.reg <- x.reg[, 2040, , invert = T]
x.reg <- x.reg[, 2040, , invert = TRUE]

regions <- c("Africa", "Asia Pacific", "Central and South America", "Europe",
"Eurasia", "Middle East", "North America")
Expand Down Expand Up @@ -117,13 +117,13 @@ convertIEA_WEO_2021 <- function(x, subtype = "GLO") {
x.regional <- mbind(x.regional, .disaggregate_regions(x_in = j, regions_in = regions))
}

x.regional <- x.regional[, , "dummy", invert = T]
x.regional <- x.regional[, , "dummy", invert = TRUE]

Non28EUcountries <- c("ALA", "FRO", "GIB", "GGY", "IMN", "JEY")
x.regional[Non28EUcountries,,] <- 0
x.regional[Non28EUcountries, , ] <- 0

return(x.regional)
} else {
stop("Not a valid subtype! Must be either \"regional\" or \"GLO\"")
stop("Not a valid subtype! Must be either \"region\" or \"global\"")
}
}
18 changes: 17 additions & 1 deletion R/fullVALIDATIONREMIND.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
fullVALIDATIONREMIND <- function(rev = 0) {

#-------------- historical data ---------------------------------------------------------------------
calcOutput("Historical", round = 5, file = "historical.mif", aggregate = "region+global+missingH12")

valfile <- "historical.mif"

calcOutput("Historical", round = 5, file = valfile, aggregate = "region+global+missingH12",
append = FALSE, na_warning = FALSE, try = TRUE)

calcOutput(type = "IEA_ETP", aggregate = "global", file = valfile,
append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE)

calcOutput(type = "IEA_ETP", aggregate = "region", file = valfile,
append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE)

calcOutput(type = "IEA_WEO_2021", aggregate = "global", file = valfile,
append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE)

calcOutput(type = "IEA_WEO_2021", aggregate = "region", file = valfile,
append = TRUE, na_warning = FALSE, try = TRUE, isValidation = TRUE)

}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRat REMIND Input Data Package

R package **mrremind**, version **0.108.0**
R package **mrremind**, version **0.109.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrremind)](https://cran.r-project.org/package=mrremind) [![R build status](https://github.com/pik-piam/mrremind/workflows/check/badge.svg)](https://github.com/pik-piam/mrremind/actions) [![codecov](https://codecov.io/gh/pik-piam/mrremind/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrremind) [![r-universe](https://pik-piam.r-universe.dev/badges/mrremind)](https://pik-piam.r-universe.dev/ui#builds)

Expand Down Expand Up @@ -38,7 +38,7 @@ In case of questions / problems please contact Lavinia Baumstark <lavinia@pik-po

To cite package **mrremind** in publications use:

Baumstark L, Rodrigues R, Levesque A, Oeser J, Bertram C, Mouratiadou I, Malik A, Schreyer F, Soergel B, Rottoli M, Mishra A, Dirnaichner A, Pehl M, Giannousakis A, Klein D, Strefler J, Feldhaus L, Brecha R, Rauner S, Dietrich J, Bi S, Benke F, Weigmann P, Richters O, Krekeler R (2022). _mrremind: MadRat REMIND Input Data Package_. R package version 0.108.0.
Baumstark L, Rodrigues R, Levesque A, Oeser J, Bertram C, Mouratiadou I, Malik A, Schreyer F, Soergel B, Rottoli M, Mishra A, Dirnaichner A, Pehl M, Giannousakis A, Klein D, Strefler J, Feldhaus L, Brecha R, Rauner S, Dietrich J, Bi S, Benke F, Weigmann P, Richters O, Krekeler R (2022). _mrremind: MadRat REMIND Input Data Package_. R package version 0.109.0.

A BibTeX entry for LaTeX users is

Expand All @@ -47,6 +47,6 @@ A BibTeX entry for LaTeX users is
title = {mrremind: MadRat REMIND Input Data Package},
author = {Lavinia Baumstark and Renato Rodrigues and Antoine Levesque and Julian Oeser and Christoph Bertram and Ioanna Mouratiadou and Aman Malik and Felix Schreyer and Bjoern Soergel and Marianna Rottoli and Abhijeet Mishra and Alois Dirnaichner and Michaja Pehl and Anastasis Giannousakis and David Klein and Jessica Strefler and Lukas Feldhaus and Regina Brecha and Sebastian Rauner and Jan Philipp Dietrich and Stephen Bi and Falk Benke and Pascal Weigmann and Oliver Richters and Robin Krekeler},
year = {2022},
note = {R package version 0.108.0},
note = {R package version 0.109.0},
}
```
11 changes: 11 additions & 0 deletions man/calcHistorical.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/calcIEA_ETP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/calcIEA_WEO_2021.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/convertIEA_WEO_2021.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03cebc2

Please sign in to comment.