Skip to content

Commit

Permalink
Merge pull request #91 from robinhasse/eeaGhgIntensityElec
Browse files Browse the repository at this point in the history
Migrate EEA_EuropeanEnvironmentAgency from mrremind with new subtype: ghg intensity of electricity
  • Loading branch information
robinhasse authored Sep 8, 2023
2 parents b789042 + 73ee783 commit c6e067a
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '26291646'
ValidationKey: '26469450'
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.34.1
date-released: '2023-09-06'
version: 1.35.0
date-released: '2023-09-07'
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
6 changes: 3 additions & 3 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.34.1
Date: 2023-09-06
Version: 1.35.0
Date: 2023-09-07
Authors@R: c(person("Benjamin Leon", "Bodirsky", email = "[email protected]", role = "aut"),
person("Kristine", "Karstens", role = "aut"),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down Expand Up @@ -38,7 +38,7 @@ Description: Provides useful functions and a common structure to all the input d
and REMIND of model input data.
Depends:
R (>= 2.10.0),
magclass (>= 3.17),
magclass (>= 6.11.0),
madrat (>= 2.20.9),
mrdrivers (>= 1.0.0),
mstools
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ importFrom(data.table,":=")
importFrom(data.table,as.data.table)
importFrom(data.table,fread)
importFrom(dplyr,"%>%")
importFrom(dplyr,.data)
importFrom(dplyr,all_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_cols)
Expand Down Expand Up @@ -168,6 +169,7 @@ importFrom(purrr,pmap)
importFrom(purrr,walk)
importFrom(quitte,add_countrycode_)
importFrom(quitte,as.quitte)
importFrom(quitte,calc_addVariable)
importFrom(quitte,character.data.frame)
importFrom(quitte,inline.data.frame)
importFrom(raster,"res<-")
Expand Down Expand Up @@ -228,6 +230,7 @@ importFrom(tidyr,expand_grid)
importFrom(tidyr,extract)
importFrom(tidyr,nesting)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,replace_na)
importFrom(tidyr,starts_with)
importFrom(tidyr,unite)
importFrom(tidyr,unnest)
Expand Down
34 changes: 34 additions & 0 deletions R/convertEEA_EuropeanEnvironmentAgency.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Convert European Environment Agency (EEA) data
#'
#' Read-in European Environment Agency (EEA) data on ETS emissions as magclass object
#'
#' @param x MAgPIE object to be converted
#' @param subtype data subtype. Either "ETS", "historical", "projections", or "projections-detailed"
#' @return magpie object of European Environment Agency (EEA) ETS emissions (GtCO2)
#' @author Renato Rodrigues, Robin Hasse
#' @seealso \code{\link{readSource}}
#' @examples
#' \dontrun{
#' a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS")
#' }
#'
#' @importFrom magclass as.magpie getItems getItems<-
#' @importFrom madrat toolCountry2isocode toolCountryFill
#'

convertEEA_EuropeanEnvironmentAgency <- function(x, subtype) { # nolint: object_name_linter.
if (subtype %in% c("ETS", "ESR")) {
# fill up zero countries
x <- toolCountryFill(x)
# remove NAs
x[is.na(x)] <- 0
} else if (subtype == "total") {
x <- toolCountryFill(x)
} else if (subtype == "sectoral") {
x <- toolCountryFill(x, no_remove_warning = "EUR")
} else if (subtype %in% c("projections", "projections-detailed", "ghgEmissionIntensityElec")) {
getItems(x, 1) <- toolCountry2isocode(getItems(x, 1), warn = FALSE)
x <- toolCountryFill(x, verbosity = 2, no_remove_warning = NA)
}
return(x)
}
170 changes: 170 additions & 0 deletions R/readEEA_EuropeanEnvironmentAgency.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#' Read European Environment Agency (EEA) data
#'
#' Read-in European Environment Agency (EEA) data on ETS emissions as magclass object
#'
#'
#' @param subtype data subtype. Either "ETS", "ESR", "total", "sectoral", "projections", or "projections-detailed"
#' @return magpie object of European Environment Agency (EEA) ETS emissions (GtCO2)
#' @author Renato Rodrigues, Falk Benke, Robin Hasse
#' @seealso \code{\link{readSource}}
#' @examples
#' \dontrun{
#' a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS")
#' }
#'
#' @importFrom dplyr left_join select filter mutate relocate %>% .data
#' @importFrom tidyr replace_na
#' @importFrom magclass as.magpie collapseDim
#' @importFrom quitte calc_addVariable
#' @importFrom readxl read_excel excel_sheets read_xlsx
#' @importFrom reshape2 melt
#' @importFrom madrat toolCountry2isocode
#' @importFrom utils read.csv

readEEA_EuropeanEnvironmentAgency <- function(subtype) { # nolint: object_name_linter.
if (subtype == "ETS") {
# v44: 2020 data, v38: 2019 data
data <- read.csv("ETS_Database_v44.csv", sep = "\t")
data <- data[, -c(2, 5)]
data$year <- as.numeric(as.character(data$year))
data <- data[(!(is.na(data$year))), ]
colnames(data) <- c("region", "ETS_info", "sector", "value", "period")
data$region <- toolCountry2isocode(data$region, warn = FALSE)
data <- data[(!(is.na(data$region))), ]
data$ETS_info <- gsub(pattern = "\\.", replacement = "_", data$ETS_info)
data$sector <- gsub(pattern = "\\.", replacement = "", data$sector)
data$value <- as.numeric(gsub(" ", "", data$value)) / 1000000
data <- data[, c(1, 5, 2, 3, 4)]
x <- as.magpie(data, spatial = 1, temporal = 2, datacol = 5)

} else if (subtype == "ESR") {
# 2020 data
data <- read_excel(path = "EEA_GHG_ESD_Dec 2020.xlsx", trim_ws = TRUE)
data <- data[, c(1:3)]
colnames(data) <- c("region", "period", "value")
data$region <- toolCountry2isocode(data$region, warn = FALSE)
data <- data[(!(is.na(data$region))), ]
data$variable <- "Emi|GHG|ESR (Mt CO2-equiv/yr)"
data <- data[, c(1, 2, 4, 3)]
x <- as.magpie(data, spatial = 1, temporal = 2, datacol = 4)

} else if (subtype == "total") {
data <- read_excel(path = "GHG_Total_historical.xlsx", trim_ws = TRUE)
data$...1 <- NULL
eur <- c(
"AUT", "BEL", "BGR", "HRV", "CYP", "CZE", "DNK", "EST", "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL",
"ITA", "LVA", "LIE", "LTU", "LUX", "MLT", "NLD", "NOR", "POL", "PRT", "ROU", "SVK", "SVN", "ESP", "SWE",
"CHE", "TUR", "GBR"
)
colnames(data) <- c("year", eur)
data <- melt(data, id.vars = 1)
colnames(data) <- c("year", "region", "value")
data <- cbind(data, variable = c("Emi|GHGtot"))
data$variable <- paste0(data$variable, sep = " ", "(Mt CO2-equiv/yr)")
data <- data[, c(1, 2, 4, 3)]
x <- as.magpie(data, spatial = 2, temporal = 1, datacol = 4)

} else if (subtype == "sectoral") {
sheets <- excel_sheets("GHG_ETS_ES_Projections_by_sector.xlsx")
historical <- NULL
timeframe <- seq(2005, 2017) # excluding WEM projections

for (s in sheets) {
tmp <- suppressMessages(read_excel(
path = "GHG_ETS_ES_Projections_by_sector.xlsx", sheet = s, skip = 1,
trim_ws = TRUE)) %>%
melt(id.vars = 1) %>%
mutate(value = replace_na(.data[["value"]], 0))
colnames(tmp) <- c("label", "period", "value")
tmp <- cbind(tmp[!is.na(tmp$value) & tmp$period %in% timeframe, ], region = s)
historical <- rbind(historical, tmp)
}

mappingVariable <- as.data.frame(
cbind(
variable = c(
"Emi|GHG|ETS",
"Emi|GHG|Energy|ETS",
"Emi|GHG|Industry|ETS",
"Emi|GHG|ESR",
"Emi|GHG|Transport|ESR",
"Emi|GHG|Buildings|ESR",
"Emi|GHG|Industry|ESR",
"Emi|GHG|Agriculture|ESR",
"Emi|GHG|Waste|ESR"
),
label = c(
"Emissions Trading System (stationary installations)",
"Energy Industries",
"Other stationary installations",
"Effort Sharing Decision and Regulation",
"Transport",
"Buildings",
"Industry and other",
"Agriculture",
"Waste"
)
)
)

historical <- left_join(mappingVariable, historical, by = c("label"))
historical$variable <- paste0(historical$variable, sep = " ", "(Mt CO2-equiv/yr)")
historical$value <- as.double(historical$value)
historical$label <- NULL
historical <- historical[, c(1, 4, 2, 3)]
x <- as.magpie(historical, spatial = 2, datacol = 4, temporal = 3)

} else if (subtype == "projections") {

projections <- read.csv(file = "GHG_projections/GHG_projections_2021_EEA.csv",
stringsAsFactors = FALSE, strip.white = TRUE) %>%
filter(!!sym("CountryCode") != "",
!!sym("CountryCode") != "EU",
!!sym("Final.Gap.filled") != as.double(0),
!is.na(!!sym("Final.Gap.filled"))) %>%
select("CountryCode", "Year", "Category", "Scenario", "Gas", "Value" = "Final.Gap.filled") %>%
mutate(Year = as.numeric(!!sym("Year")),
Value = as.numeric(!!sym("Value"))) %>%
relocate("Scenario", .after = "Year")

x <- as.magpie(projections, spatial = 1, temporal = 2, datacol = 6)

} else if (subtype == "projections-detailed") {

files <- list.files(path = "GHG_projections_detailed/2022/")

projections <- NULL
for (file in files) {
reg <- gsub("-.*", "", file)
projections <- rbind(
projections,
suppressWarnings(read_xlsx(paste0("GHG_projections_detailed/2022/", file))) %>%
select(-"RY", -"InventorySubmissionYear", -"Notation") %>%
filter(!is.na(!!sym("Value")), !!sym("Value") != 0) %>%
mutate(CountryCode = reg)
)
}

projections <- projections %>%
mutate(Year = as.numeric(!!sym("Year")),
Value = as.numeric(!!sym("Value"))) %>%
select(6, 3, 2, 1, 4, 5) %>%
distinct()

x <- as.magpie(projections, spatial = 1, temporal = 2, datacol = 6)

} else if (subtype == "ghgEmissionIntensityElec") {
# Greenhouse gas emission intensity of electricity generation in Europe
# https://www.eea.europa.eu/ims/greenhouse-gas-emission-intensity-of-1
x <- read.csv("co2-emission-intensity-13.csv") %>%
select(period = 1, region = 2, value = 3) %>%
filter(.data[["region"]] != "EU-27") %>%
as.magpie(spatial = "region", temporal = "period", datacol = "value") %>%
collapseDim()

} else {
stop("Not a valid subtype!")
}

return(x)
}
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.34.1**
R package **mrcommons**, version **1.35.0**

[![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, Führlich P, Hötten D, Hasse R, Dietrich J (2023). _mrcommons: MadRat commons Input Data Library_. doi: 10.5281/zenodo.3822009 (URL: https://doi.org/10.5281/zenodo.3822009), R package version 1.34.1, <URL: 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, Führlich P, Hötten D, Hasse R, Dietrich J (2023). _mrcommons: MadRat commons Input Data Library_. doi:10.5281/zenodo.3822009 <https://doi.org/10.5281/zenodo.3822009>, R package version 1.35.0, <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 Führlich and David Hötten and Robin Hasse and Jan Philipp Dietrich},
year = {2023},
note = {R package version 1.34.1},
note = {R package version 1.35.0},
doi = {10.5281/zenodo.3822009},
url = {https://github.com/pik-piam/mrcommons},
}
Expand Down
31 changes: 31 additions & 0 deletions man/convertEEA_EuropeanEnvironmentAgency.Rd

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

29 changes: 29 additions & 0 deletions man/readEEA_EuropeanEnvironmentAgency.Rd

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

0 comments on commit c6e067a

Please sign in to comment.