Skip to content

Commit

Permalink
Merge pull request #135 from fbenke-pik/eurostat
Browse files Browse the repository at this point in the history
add latest Eurostat env_air_gge data
  • Loading branch information
fbenke-pik authored Dec 4, 2024
2 parents 234b8c1 + 2392215 commit 22426ec
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '29459326'
ValidationKey: '293076600'
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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exclude: '^tests/testthat/_snaps/.*$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: bae853d82da476eee0e0a57960ee6b741a3b3fb7 # frozen: v0.4.3
rev: 3b70240796cdccbe1474b0176560281aaded97e6 # frozen: v0.4.3.9003
hooks:
- id: parsable-R
- id: deps-in-desc
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.9
date-released: '2024-11-27'
version: 1.46.10
date-released: '2024-12-03'
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.9
Date: 2024-11-27
Version: 1.46.10
Date: 2024-12-03
Authors@R: c(person("Benjamin Leon", "Bodirsky", email = "[email protected]", role = "aut"),
person("Kristine", "Karstens", role = "aut"),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down
116 changes: 67 additions & 49 deletions R/convertEurostat.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Converts Eurostat historical emissions
#' Read Eurostat historical emissions (env_air_gge)
#'
#' @param x MAgPIE object to be converted
#' @param subtype emissions for original eurostat emissions split, MACCemi for MACC historical emissions, or
#' sectorEmi for sector specific emissions
#' @param subtype 'emissions' for original Eurostat emissions split,
#' 'MACCemi' for MACC historical emissions, or 'sectorEmi' for sector specific
#' emissions, or 'latest' for most up-to-date data
#' @return A MAgPIE object containing the Eurostat historical emissions (MtCO2)
#' @author Renato Rodrigues
#' @examples
Expand All @@ -11,72 +12,89 @@
#' }
#'
convertEurostat <- function(x, subtype) {

switch(subtype,
"emissions" = toolCountryFill(x, fill = NA, verbosity = 2),
"sectorEmi" = convertEurostatSectorEmi(x),
"MACCemi" = convertEurostatMACCemi(x),
stop("Bad input for convertEurostat. Invalid 'subtype' argument."))
"emissions" = toolCountryFill(x, fill = NA, verbosity = 2),
"sectorEmi" = convertEurostatSectorEmi(x),
"MACCemi" = convertEurostatMACCemi(x),
"latest" = toolCountryFill(x, fill = NA, verbosity = 2),
stop("Bad input for convertEurostat. Invalid 'subtype' argument.")
)
}

######################################################################################
# Functions
######################################################################################
convertEurostatSectorEmi <- function(x) {
x <- toolCountryFill(x, fill = NA, verbosity = 2)
x <- toolCountryFill(x, fill = NA, verbosity = 2)
# mapping eurostat to sector emissions
mapping <- list("power" = list("energy" = c("Fuel combustion in public electricity and heat production")),
"refining" = list("energy" = c("Fuel combustion in petroleum refining")),
"solids" = list("energy" = c("Fuel combustion in manufacture of solid fuels and other energy industries", #nolint
"Other fuel combustion sectors n_e_c_")),
"extraction" = list("process" = c("Solid fuels - fugitive emissions",
"Oil, natural gas and other energy production - fugitive emissions")),
"build" = list("energy" = c("Fuel combustion in commercial and institutional sector",
"Fuel combustion by households")),
"indst" = list("energy" = c("Fuel combustion in manufacturing industries and construction"),
"process" = c("Industrial processes and product use")),
"trans" = list("energy" = c("Fuel combustion in transport")),
"bunkers" = list("energy" = c("International bunkers (memo item)")),
"agriculture" = list("energy" = c("Fuel combustion in agriculture, forestry and fishing"),
"process" = c("Agriculture")),
"waste" = list("process" = c("Waste management")),
"lulucf" = list("process" = c("Land use, land use change, and forestry (LULUCF)")),
"cdr" = list("process" = c("Transport and storage of CO2 (memo item)")),
"other" = list("energy" = c("Multilateral operations (memo item)"),
"process" = c("Other sectors")),
"indirect" = list("process" = c("Indirect CO2")))
mapping <- list(
"power" = list("energy" = c("Fuel combustion in public electricity and heat production")),
"refining" = list("energy" = c("Fuel combustion in petroleum refining")),
"solids" = list("energy" = c(
"Fuel combustion in manufacture of solid fuels and other energy industries",
"Other fuel combustion sectors n_e_c_"
)),
"extraction" = list("process" = c(
"Solid fuels - fugitive emissions",
"Oil, natural gas and other energy production - fugitive emissions"
)),
"build" = list("energy" = c(
"Fuel combustion in commercial and institutional sector",
"Fuel combustion by households"
)),
"indst" = list(
"energy" = c("Fuel combustion in manufacturing industries and construction"),
"process" = c("Industrial processes and product use")
),
"trans" = list("energy" = c("Fuel combustion in transport")),
"bunkers" = list("energy" = c("International bunkers (memo item)")),
"agriculture" = list(
"energy" = c("Fuel combustion in agriculture, forestry and fishing"),
"process" = c("Agriculture")
),
"waste" = list("process" = c("Waste management")),
"lulucf" = list("process" = c("Land use, land use change, and forestry (LULUCF)")),
"cdr" = list("process" = c("Transport and storage of CO2 (memo item)")),
"other" = list(
"energy" = c("Multilateral operations (memo item)"),
"process" = c("Other sectors")
),
"indirect" = list("process" = c("Indirect CO2"))
)

x <- mbind(lapply(names(mapping), function(var) {
mbind(lapply(names(mapping[[var]]), function(t) {
add_dimension(
add_dimension(
dimSums(x[, , mapping[[var]][[t]]], na.rm = TRUE, dim = 3.2),
dim = 3.2, add = "sector", nm = var),
dim = 3.3, add = "type", nm = t)
dim = 3.2, add = "sector", nm = var
),
dim = 3.3, add = "type", nm = t
)
}))
}))
}



convertEurostatMACCemi <- function(x) {
x <- toolCountryFill(x, fill = NA, verbosity = 2)
x <- toolCountryFill(x, fill = NA, verbosity = 2)
# mapping eurostat to MACC emissions
mapping <- list("ch4coal" = list("emi" = "CH4", "accounts" = c("Solid fuels - fugitive emissions")),
"ch4wstl" = list("emi" = "CH4", "accounts" = c("Solid waste disposal")),
"ch4wsts" = list("emi" = "CH4", "accounts" = c("Wastewater treatment and discharge")),
"ch4rice" = list("emi" = "CH4", "accounts" = c("Rice cultivation")),
"ch4animals" = list("emi" = "CH4", "accounts" = c("Enteric fermentation")),
"ch4anmlwst" = list("emi" = "CH4", "accounts" = c("Manure management")),
"ch4agwaste" = list("emi" = "CH4", "accounts" = c("Field burning of agricultural residues")),
"n2otrans" = list("emi" = "N2O", "accounts" = c("Fuel combustion in transport")),
"n2oadac" = list("emi" = "N2O", "accounts" = c("Adipic acid production")),
"n2onitac" = list("emi" = "N2O", "accounts" = c("Nitric acid production")),
"n2oagwaste" = list("emi" = "N2O", "accounts" = c("Field burning of agricultural residues")),
# or full Waste management n2O?
"n2owaste" = list("emi" = "N2O", "accounts" = c("Wastewater treatment and discharge")),
"co2cement_process" = list("emi" = "CO2", "accounts" = c("Industrial processes and product use")),
"co2luc" = list("emi" = "CO2", "accounts" = c("Land use, land use change, and forestry (LULUCF)")))
mapping <- list(
"ch4coal" = list("emi" = "CH4", "accounts" = c("Solid fuels - fugitive emissions")),
"ch4wstl" = list("emi" = "CH4", "accounts" = c("Solid waste disposal")),
"ch4wsts" = list("emi" = "CH4", "accounts" = c("Wastewater treatment and discharge")),
"ch4rice" = list("emi" = "CH4", "accounts" = c("Rice cultivation")),
"ch4animals" = list("emi" = "CH4", "accounts" = c("Enteric fermentation")),
"ch4anmlwst" = list("emi" = "CH4", "accounts" = c("Manure management")),
"ch4agwaste" = list("emi" = "CH4", "accounts" = c("Field burning of agricultural residues")),
"n2otrans" = list("emi" = "N2O", "accounts" = c("Fuel combustion in transport")),
"n2oadac" = list("emi" = "N2O", "accounts" = c("Adipic acid production")),
"n2onitac" = list("emi" = "N2O", "accounts" = c("Nitric acid production")),
"n2oagwaste" = list("emi" = "N2O", "accounts" = c("Field burning of agricultural residues")),
# or full Waste management n2O?
"n2owaste" = list("emi" = "N2O", "accounts" = c("Wastewater treatment and discharge")),
"co2cement_process" = list("emi" = "CO2", "accounts" = c("Industrial processes and product use")),
"co2luc" = list("emi" = "CO2", "accounts" = c("Land use, land use change, and forestry (LULUCF)"))
)

# other MACCs do not have a direct mapping to the eurostat data (use sector information instead)
# ch4gas + ch4oil -> extraction.process.ch4 - ch4coal #nolint
Expand Down
42 changes: 38 additions & 4 deletions R/readEurostat.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#' Read Eurostat historical emissions
#' Read Eurostat historical emissions (env_air_gge)
#'
#' Read-in Eurostat historical emissions csv files as magclass object
#'
#' @param subtype emissions for original eurostat emissions split, MACCemi for MACC historical emissions, or
#' sectorEmi for sector specific emissions
#' @param subtype 'emissions' for original Eurostat emissions split,
#' 'MACCemi' for MACC historical emissions, or 'sectorEmi' for sector specific
#' emissions, or 'latest' for most up-to-date data
#' @return magpie object of Eurostat historical emissions (MtCO2)
#' @author Renato Rodrigues
#' @seealso \code{\link{readSource}}
Expand All @@ -21,6 +22,7 @@ readEurostat <- function(subtype = "emissions") {
"emissions" = readEurostatEmissions(),
"MACCemi" = readEurostatEmissions(),
"sectorEmi" = readEurostatEmissions(),
"latest" = readEurostatEmissionsLatest(),
stop("Bad input for readEurostat. Invalid 'subtype' argument.")
)
}
Expand All @@ -29,8 +31,8 @@ readEurostat <- function(subtype = "emissions") {
######################################################################################
# Functions
######################################################################################
# Reading Eurostat historical emissions from 2019
readEurostatEmissions <- function() {
# Reading Eurostat historical emissions
type <- c("GHG", "CO2", "CH4", "CH4_native", "N2O", "N2O_native", "HFC", "PFC", "HFC_PFC_NSP", "SF6", "NF3")
data <- NULL
for (t in type) {
Expand All @@ -48,3 +50,35 @@ readEurostatEmissions <- function() {
)
return(as.magpie(data, spatial = 2, temporal = 1, datacol = 5))
}

# Reading Eurostat latest historical emissions from 2024
readEurostatEmissionsLatest <- function() {

# read in latest Eurostat data from 2024 ----
df <- read.csv(file.path("2024", "env_air_gge_linear.csv")) %>%
filter(.data$unit == "MIO_T", .data$geo != "EU27_2020") %>%
select("region" = "geo", "period" = "TIME_PERIOD", "emi" = "airpol", "sector" = "src_crf", "value" = "OBS_VALUE")

df$region <- toolCountry2isocode(df$region, mapping = c("EL" = "GRC"))

x <- as.magpie(df, spatial = 1, temporal = 2, datacol = 5)
getNames(x, dim = 1) <- c(
"CH4_native", "CH4", "CO2", "GHG", "HFC", "HFC_PFC_NSP",
"N2O_native", "N2O", "NF3", "PFC", "SF6"
)

sectorMap <- toolGetMapping("EurostatCRFLabels.csv", type = "sectoral", where = "mrcommons")
sectorMap <- sectorMap[match(getNames(x, dim = 2), sectorMap$crf), ]
getNames(x, dim = 2) <- sectorMap[, "label"]

# read in GBR values from 2019 database ----
gbr <- readEurostatEmissions()["GBR", , ]
gbr <- add_columns(gbr, addnm = c("y2020", "y2021", "y2022"), dim = "period", fill = NA)

# convert CH4 and N2O to use AR5 GWP values instead of AR4
gbr[, , "CH4"] <- gbr[, , "CH4"] * 28 / 25
gbr[, , "N2O"] <- gbr[, , "N2O"] * 265 / 298

gbr <- magclass::matchDim(gbr, x, dim = 3)
return(mbind(x, gbr))
}
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.9**
R package **mrcommons**, version **1.46.10**

[![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.9, <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.10, <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.9},
note = {R package version 1.46.10},
url = {https://github.com/pik-piam/mrcommons},
doi = {10.5281/zenodo.3822009},
}
Expand Down
Loading

0 comments on commit 22426ec

Please sign in to comment.