Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pik-piam/mrcommons into eurostat
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Dec 3, 2024
2 parents eb38c3c + 234b8c1 commit 5ee42ae
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '29377174'
ValidationKey: '29459326'
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: 'mrcommons: MadRat commons Input Data Library'
version: 1.46.6
date-released: '2024-11-12'
version: 1.46.9
date-released: '2024-11-27'
abstract: Provides useful functions and a common structure to all the input data required
to run models like MAgPIE and REMIND of model input data.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrcommons
Type: Package
Title: MadRat commons Input Data Library
Version: 1.46.6
Date: 2024-11-12
Version: 1.46.9
Date: 2024-11-27
Authors@R: c(person("Benjamin Leon", "Bodirsky", email = "bodirsky@pik-potsdam.de", role = "aut"),
person("Kristine", "Karstens", role = "aut"),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(readHoulton2018)
export(readJRC_IDEES)
export(toolCalcIEAfromStructureMappingPEFE)
export(toolPregnant)
export(toolSplitBiomass)
import(GDPuc)
import(madrat)
import(magclass)
Expand Down
165 changes: 69 additions & 96 deletions R/calcIOEdgeBuildings.R
Original file line number Diff line number Diff line change
@@ -1,116 +1,89 @@
#' calcIOEdgeBuildings
#'
#' Calculates buildings-related energy flows from the IEA energy balances. 'output_EDGE_buildings' is a key input
#' to EDGE-Buildings providing the historic final energy demand from buildings. 'output_EDGE' does the same for
#' Calculates buildings-related energy flows from the IEA energy balances.
#' 'output_EDGE_buildings' is a key input to EDGE-Buildings providing the
#' historic final energy demand from buildings. 'output_EDGE' does the same for
#' buildings and industry together.
#'
#' @param subtype Data subtype. See default argument for possible values.
#' @param ieaVersion Release version of IEA data, either 'default'
#' (vetted and used in REMIND) or 'latest'.
#' @return IEA data as MAgPIE object aggregated to country level
#' @returns IEA data as MAgPIE object aggregated to country level
#'
#' @author Pascal Sauer, Anastasis Giannousakis, Robin Hasse
#'
#' @author Pascal Sauer, Anastasis Giannousakis
#' @examples
#' \dontrun{
#' a <- calcOutput("IOEdgeBuildings", subtype = "output_EDGE_buildings")
#' }
#'
#' @seealso \code{\link{calcOutput}}
#' @importFrom dplyr %>% all_of
#'
#' @importFrom dplyr %>% .data all_of filter select
#' @importFrom tidyr unite
calcIOEdgeBuildings <- function(subtype = c("output_EDGE", "output_EDGE_buildings"), ieaVersion = "default") {
#' @importFrom madrat readSource toolGetMapping toolAggregate calcOutput
#' @importFrom utils read.csv2
#' @importFrom magclass as.magpie getNames mselect

calcIOEdgeBuildings <- function(subtype = c("output_EDGE", "output_EDGE_buildings"),
ieaVersion = c("default", "latest")) {

subtype <- match.arg(subtype)
switch(subtype,
output_EDGE = {
mapping <- toolGetMapping(type = "sectoral",
name = "structuremappingIO_outputs.csv",
where = "mrcommons", returnPathOnly = TRUE)
target <- "EDGEitems"
},
output_EDGE_buildings = {
mapping <- toolGetMapping(type = "sectoral",
name = "structuremappingIO_outputs.csv",
where = "mrcommons", returnPathOnly = TRUE)
target <- "EDGE_buildings"
})

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

ieaSubtype <- if (ieaVersion == "default") "EnergyBalances" else "EnergyBalances-latest"

# read in data and convert from ktoe to EJ
data <- readSource("IEA", subtype = ieaSubtype) * 4.1868e-5

ieamatch <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "")

# delete NAs rows
ieamatch <- ieamatch[c("iea_product", "iea_flows", target, "Weight")] %>%
ieaVersion <- match.arg(ieaVersion)



# READ -----------------------------------------------------------------------

# convert from ktoe to EJ
data <- switch(ieaVersion,
default = readSource("IEA", subtype = "EnergyBalances"),
latest = readSource("IEA", subtype = "EnergyBalances-latest")
) * 4.1868e-5



# AGGREGATE ------------------------------------------------------------------

target <- switch(subtype,
output_EDGE = "EDGEitems",
output_EDGE_buildings = "EDGE_buildings"
)

mapping <- toolGetMapping(type = "sectoral",
name = "structuremappingIO_outputs.csv",
where = "mrcommons", returnPathOnly = TRUE) %>%
read.csv2(stringsAsFactors = FALSE, na.strings = "") %>%
select(all_of(c("iea_product", "iea_flows", target, "Weight"))) %>%
na.omit() %>%
unite("target", all_of(target), sep = ".") %>%
unite("product.flow", c("iea_product", "iea_flows"), sep = ".", remove = FALSE) %>%
filter(!!sym("product.flow") %in% getNames(data))

magpieNames <- ieamatch[["target"]] %>% unique()

# in case we include IEA categories in the output, iea categories in `ieamatch` got renamed
ieapname <- "iea_product"
ieafname <- "iea_flows"

reminditems <- do.call(mbind,
lapply(magpieNames, function(item) {
testdf <- ieamatch[ieamatch$target == item, c(ieapname, ieafname, "Weight")]
prfl <- paste(testdf[, ieapname], testdf[, ieafname], sep = ".")
vec <- as.numeric(ieamatch[rownames(testdf), "Weight"])
names(vec) <- prfl
tmp <- data[, , prfl] * as.magpie(vec)
tmp <- dimSums(tmp, dim = 3, na.rm = TRUE)
getNames(tmp) <- item
return(tmp)
}))

# Split residential Biomass into traditional and modern biomass depending upon the income per capita
if (subtype == "output_EDGE") {
nBiotrad <- "feresbiotrad"
nBiomod <- "feresbiomod"
nBioshare <- "feresbioshare"
} else if (subtype == "output_EDGE_buildings") {
nBiotrad <- "biotrad"
nBiomod <- "biomod"
nBioshare <- "bioshare"
}
# Read-in data to compute income per capita
gdp <- calcOutput("GDPPast", aggregate = FALSE)
pop <- calcOutput("PopulationPast", aggregate = FALSE)
gdppop <- gdp[, intersect(getYears(gdp), getYears(pop)), ] / pop[, intersect(getYears(gdp), getYears(pop)), ]
# Create a lambda which is 1 for income per capita <= 10000, and 0 above 15000
# the multiplication by gdppop was necessary to avoid error from vector length.
lambda <- pmin(gdppop * 0 + 1, pmax(0 * gdppop, (15000 - gdppop) / (15000 - 10000)))
lambda <- time_interpolate(lambda, getYears(reminditems), extrapolation_type = "constant")

# Split Bioshare (residential PRIMSBIO) between traditional and modern biomass according to lambda
bioshareTrad <- setNames(reminditems[, , nBioshare] * lambda, nBiotrad)
bioshareMod <- setNames(reminditems[, , nBioshare] - bioshareTrad, nBiomod)

# In case biomod and biotrad do not exist yet in the data set, create dummy items
if (!any(nBiomod %in% getNames(reminditems))) {
reminditems <- mbind(reminditems,
setNames(reminditems[, , nBioshare] * 0, nBiomod))
}
if (!any(nBiotrad %in% getNames(reminditems))) {
reminditems <- mbind(reminditems,
setNames(reminditems[, , nBioshare] * 0, nBiotrad))
}

# Add the values from bioshare to the other modern and traditional biomass
reminditems[, , nBiotrad] <- reminditems[, , nBiotrad] + bioshareTrad
reminditems[, , nBiomod] <- reminditems[, , nBiomod] + bioshareMod

# Remove the bioshare item
reminditems <- reminditems[, , nBioshare, invert = TRUE]

return(list(x = reminditems, weight = NULL, unit = "EJ",
description = paste("Historic final energy demand from buildings (and industry)",
"based on the 2022 IEA World Energy Balances")))
filter(.data[["product.flow"]] %in% getNames(data),
.data[["Weight"]] != 0) %>%
mutate(Weight = as.numeric(.data[["Weight"]]))

weight <- as.magpie(mapping[, c("iea_product", "iea_flows", "Weight")])

data <- toolAggregate(data[, , mapping[["product.flow"]]] * weight,
rel = mapping, from = "product.flow", to = "target", dim = 3)
getSets(data)[3] <- "d3"



# SPLIT BIOMASS --------------------------------------------------------------

gdppop <- calcOutput("GDPpc", average2020 = FALSE, aggregate = FALSE) %>%
mselect(variable = "gdppc_SSP2", collapseNames = TRUE)

data <- switch(subtype,
output_EDGE = toolSplitBiomass(data, gdppop, split = "feresbioshare",
into = c("feresbiotrad", "feresbiomod")),
output_EDGE_buildings = toolSplitBiomass(data, gdppop, split = "bioshare")
)

return(list(x = data,
weight = NULL,
unit = "EJ/yr",
description = paste("Historic FE demand from buildings",
"(and industry) based on IEA Energy Balances")))
}
25 changes: 20 additions & 5 deletions R/convertIEA.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#'
convertIEA <- function(x, subtype) {
if (grepl("EnergyBalances", subtype)) {

# aggregate Kosovo to Serbia
x1 <- x["KOS", , ]
getItems(x1, dim = 1) <- c("SRB")
Expand All @@ -37,8 +36,10 @@ convertIEA <- function(x, subtype) {
# disaggregating Other Africa (IAF),
# Other non-OECD Americas (ILA) and
# Other non-OECD Asia (IAS) regions to countries
mappingfile <- toolGetMapping(type = "regional", name = "regionmappingIEA_Other2016.csv",
returnPathOnly = TRUE, where = "mappingfolder")
mappingfile <- toolGetMapping(
type = "regional", name = "regionmappingIeaOther2016.csv",
returnPathOnly = TRUE, where = "mrcommons"
)
mapping <- read.csv2(mappingfile, stringsAsFactors = TRUE) %>%
filter(!(!!sym("CountryCode") %in% getItems(x, dim = 1)))
xadd <- toolAggregate(x[levels(mapping[[2]]), , ], mapping, weight = w[as.vector(mapping[[1]]), , ])
Expand Down Expand Up @@ -71,8 +72,9 @@ convertIEA <- function(x, subtype) {
missingFlows <- setdiff(
expand.grid(
iea_product = getItems(x[, , "ELMAINE"], dim = 3.1) %>% # nolint
union(getItems(x[, , "ELMAINC"], dim = 3.1)) %>%
union(getItems(x[, , "HEMAINC"], dim = 3.1)), iea_flows = c("ELMAINE", "ELMAINC", "HEMAINC")) %>%
union(getItems(x[, , "ELMAINC"], dim = 3.1)) %>%
union(getItems(x[, , "HEMAINC"], dim = 3.1)), iea_flows = c("ELMAINE", "ELMAINC", "HEMAINC")
) %>%
unite("product.flow", c("iea_product", "iea_flows"), sep = ".") %>%
pull("product.flow"), getItems(x, dim = 3)
)
Expand Down Expand Up @@ -105,6 +107,19 @@ convertIEA <- function(x, subtype) {
x[, , "ELAUTOE"] <- tmp
tmp <- mcalc(d, ELAUTOC ~ ifelse(HEAUTOC > 0, ELAUTOC, 0), append = FALSE)
x[, , "ELAUTOC"] <- tmp


# Correct transport reporting issue in IEA data for NONBIODIES.MARBUNK in RUS
# FE is reported in 1990 and 2010 but not in the years in between.
# This cause problems in the harmonization of EDGE-Transport and the IEA data
# in 2005 as there is no MARBUNK demand at all for REF regions.

x["RUS", seq(1990, 2010, 1), "NONBIODIES.MARBUNK"] <-
x["RUS", c(1990, 2010), "NONBIODIES.MARBUNK"] |> time_interpolate(seq(1990, 2010, 1))

# Adjust totals
x["RUS", seq(1991, 2009, 1), "TOTAL.MARBUNK"] <-
x["RUS", seq(1991, 2009, 1), "TOTAL.MARBUNK"] + x["RUS", seq(1991, 2009, 1), "NONBIODIES.MARBUNK"]
}

return(x)
Expand Down
4 changes: 2 additions & 2 deletions R/readMAgPIE.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ readMAgPIE <- function(subtype) {
# Please update scenario names in mrremind::calcBiomassPrices.R if necessary

scenarioNames <- c(
"f30_bioen_price_SSP1-SSP1-NPi_replaced_flat",
"f30_bioen_price_SSP1-SSP1-PkBudg650_replaced_flat",
"f30_bioen_price_SDP-MC-SSP1-NPi_replaced_flat",
"f30_bioen_price_SDP-MC-SSP1-PkBudg650_replaced_flat",
"f30_bioen_price_SSP2-SSP2_lowEn-NPi_replaced_flat",
"f30_bioen_price_SSP2-SSP2_lowEn-PkBudg1000_replaced_flat",
"f30_bioen_price_SSP2-SSP2_lowEn-PkBudg650_replaced_flat",
Expand Down
48 changes: 48 additions & 0 deletions R/toolSplitBiomass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Split Biomass into modern and traditional
#'
#' We assume that below a given GDP/cap level, all biomass is traditional and
#' above a higher limit, all biomass is modern with a linear transition between
#' the limits.
#'
#' @param x MagPIE object including biomass data
#' @param gdppop MagPIE object with GDP/cap data
#' @param split character, name of item to split
#' @param into character vector of length two with the names of the split items
#' @param dim dimension of \code{x} with the item to split
#' @param limits numeric vector of length two with the corresponding GDP/cap
#' limits. The default values used to be 10k and 15k USD/cap converted from
#' 2005 to 2017 dollars.
#' @returns MagPIE object including split biomass data
#'
#' @author Robin Hasse
#'
#' @importFrom dplyr %>%
#' @importFrom magclass time_interpolate setNames getYears
#' @export

toolSplitBiomass <- function(x,
gdppop,
split = "biomass",
into = c("biotrad", "biomod"),
dim = 3.1,
limits = c(1.23E4, 1.85E4)) {

.rename <- function(x, to) {
getItems(x, dim)[getItems(x, dim) == split] <- to
return(x)
}

r <- getItems(x, 1)
weight <- ((gdppop[r, , ] - limits[1]) / (limits[2] - limits[1])) %>%
collapseDim() %>%
time_interpolate(getYears(x), extrapolation_type = "constant") %>%
pmax(0) %>%
pmin(1)

items <- setdiff(getItems(x, dim), split)
xSplit <- lapply(into, function(i) if (i %in% items) x[, , i] else 0)

mbind(xSplit[[2]] + .rename(x[, , split] * weight, into[2]),
xSplit[[1]] + .rename(x[, , split] * (1 - weight), into[1]),
x[, , setdiff(getItems(x, dim), c(split, into))])
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRat commons Input Data Library

R package **mrcommons**, version **1.46.6**
R package **mrcommons**, version **1.46.9**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

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

Bodirsky B, Karstens K, Baumstark L, Weindl I, Wang X, Mishra A, Wirth S, Stevanovic M, Steinmetz N, Kreidenweis U, Rodrigues R, Popov R, Humpenoeder F, Giannousakis A, Levesque A, Klein D, Araujo E, Beier F, Oeser J, Pehl M, Leip D, Crawford M, Molina Bacca E, von Jeetze P, Martinelli E, Schreyer F, Soergel B, Sauer P, Hötten D, Hasse R, Abrahão G, Weigmann P, Dietrich J (2024). _mrcommons: MadRat commons Input Data Library_. doi:10.5281/zenodo.3822009 <https://doi.org/10.5281/zenodo.3822009>, R package version 1.46.6, <https://github.com/pik-piam/mrcommons>.
Bodirsky B, Karstens K, Baumstark L, Weindl I, Wang X, Mishra A, Wirth S, Stevanovic M, Steinmetz N, Kreidenweis U, Rodrigues R, Popov R, Humpenoeder F, Giannousakis A, Levesque A, Klein D, Araujo E, Beier F, Oeser J, Pehl M, Leip D, Crawford M, Molina Bacca E, von Jeetze P, Martinelli E, Schreyer F, Soergel B, Sauer P, Hötten D, Hasse R, Abrahão G, Weigmann P, Dietrich J (2024). _mrcommons: MadRat commons Input Data Library_. doi:10.5281/zenodo.3822009 <https://doi.org/10.5281/zenodo.3822009>, R package version 1.46.9, <https://github.com/pik-piam/mrcommons>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrcommons: MadRat commons Input Data Library},
author = {Benjamin Leon Bodirsky and Kristine Karstens and Lavinia Baumstark and Isabelle Weindl and Xiaoxi Wang and Abhijeet Mishra and Stephen Wirth and Mishko Stevanovic and Nele Steinmetz and Ulrich Kreidenweis and Renato Rodrigues and Roman Popov and Florian Humpenoeder and Anastasis Giannousakis and Antoine Levesque and David Klein and Ewerton Araujo and Felicitas Beier and Julian Oeser and Michaja Pehl and Debbora Leip and Michael Crawford and Edna {Molina Bacca} and Patrick {von Jeetze} and Eleonora Martinelli and Felix Schreyer and Bjoern Soergel and Pascal Sauer and David Hötten and Robin Hasse and Gabriel Abrahão and Pascal Weigmann and Jan Philipp Dietrich},
year = {2024},
note = {R package version 1.46.6},
note = {R package version 1.46.9},
url = {https://github.com/pik-piam/mrcommons},
doi = {10.5281/zenodo.3822009},
}
Expand Down
Loading

0 comments on commit 5ee42ae

Please sign in to comment.