From 24d98a809a7f03da74bc39897ddfe925168ea5cd Mon Sep 17 00:00:00 2001 From: robinhasse Date: Thu, 7 Sep 2023 15:28:31 +0200 Subject: [PATCH] Migrate EEA_EuropeanEnvironmentAgency to mrcommons --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/convertEEA_EuropeanEnvironmentAgency.R | 35 ---- R/readEEA_EuropeanEnvironmentAgency.R | 168 -------------------- README.md | 6 +- man/convertEEA_EuropeanEnvironmentAgency.Rd | 31 ---- man/readEEA_EuropeanEnvironmentAgency.Rd | 29 ---- 8 files changed, 8 insertions(+), 271 deletions(-) delete mode 100644 R/convertEEA_EuropeanEnvironmentAgency.R delete mode 100644 R/readEEA_EuropeanEnvironmentAgency.R delete mode 100644 man/convertEEA_EuropeanEnvironmentAgency.Rd delete mode 100644 man/readEEA_EuropeanEnvironmentAgency.Rd diff --git a/.buildlibrary b/.buildlibrary index 465de40f..cd906fb6 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '32944238' +ValidationKey: '32978974' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 76c0b8d5..eac0acbb 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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.168.1 -date-released: '2023-08-29' +version: 0.168.2 +date-released: '2023-09-07' abstract: The mrremind packages contains data preprocessing for the REMIND model. authors: - family-names: Baumstark diff --git a/DESCRIPTION b/DESCRIPTION index 979f346f..c88f5385 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrremind Title: MadRat REMIND Input Data Package -Version: 0.168.1 -Date: 2023-08-29 +Version: 0.168.2 +Date: 2023-09-07 Authors@R: c( person("Lavinia", "Baumstark", , "lavinia@pik-potsdam.de", role = c("aut", "cre")), person("Renato", "Rodrigues", role = "aut"), diff --git a/R/convertEEA_EuropeanEnvironmentAgency.R b/R/convertEEA_EuropeanEnvironmentAgency.R deleted file mode 100644 index fe7c154b..00000000 --- a/R/convertEEA_EuropeanEnvironmentAgency.R +++ /dev/null @@ -1,35 +0,0 @@ -#' 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 -#' @seealso \code{\link{readSource}} -#' @examples -#' \dontrun{ -#' a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS") -#' } -#' -#' @importFrom countrycode countrycode -#' @importFrom magclass as.magpie -#' @importFrom madrat toolCountry2isocode -#' - -convertEEA_EuropeanEnvironmentAgency <- function(x, subtype) { - 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")) { - getItems(x, dim = 1) <- countrycode(getItems(x, dim = 1), "iso2c", "iso3c", warn = FALSE, ) - x <- toolCountryFill(x, verbosity = 2, no_remove_warning = NA) - } - return(x) -} diff --git a/R/readEEA_EuropeanEnvironmentAgency.R b/R/readEEA_EuropeanEnvironmentAgency.R deleted file mode 100644 index 5ae126ec..00000000 --- a/R/readEEA_EuropeanEnvironmentAgency.R +++ /dev/null @@ -1,168 +0,0 @@ -#' 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 -#' @seealso \code{\link{readSource}} -#' @examples -#' \dontrun{ -#' a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS") -#' } -#' -#' @importFrom dplyr left_join select filter mutate relocate -#' @importFrom magclass as.magpie -#' @importFrom quitte calc_addVariable -#' @importFrom readxl read_excel excel_sheets read_xlsx -#' @importFrom reshape2 melt -#' @importFrom madrat toolCountry2isocode - -readEEA_EuropeanEnvironmentAgency <- function(subtype) { - if (subtype == "ETS") { - #2020 data - data <- read.csv("ETS_Database_v44.csv", sep = "\t") - # 2019 data - #data <- read.csv("ETS_Database_v38.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 = F) - 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 = T) - data <- data[, c(1:3)] - # 2019 data - # data <- read_excel(path = "ESD-GHG_2019_revised.xlsx", trim_ws = T, skip = 11) - # data <- data[, c(1, 17:30)] - # data <- melt(data, id.vars = 1) - colnames(data) <- c("region", "period", "value") - data$region <- toolCountry2isocode(data$region, warn = F) - 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 = T) - 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 = T)) - tmp <- melt(tmp, id.vars = 1) - tmp <- mutate(tmp, !!sym("value") := ifelse(is.na(!!sym("value")), 0, !!sym("value"))) # set 0s for NAs - colnames(tmp) <- c("label", "period", "value") - tmp <- cbind(tmp[!is.na(tmp$value) & tmp$period %in% timeframe, ], region = s) - historical <- rbind(historical, tmp) - } - - mapping.variable <- 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(mapping.variable, 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( - !!sym("Year") := as.numeric(!!sym("Year")), - !!sym("Value") := as.numeric(!!sym("Value")) - ) %>% - relocate("Scenario", .after = "Year") - - x <- as.magpie(projections, spatial = 1, temporal = 2, datacol = 6) - - return(x) - } - 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(!!sym("CountryCode") := reg) - ) - } - - projections <- projections %>% - mutate( - !!sym("Year") := as.numeric(!!sym("Year")), - !!sym("Value") := as.numeric(!!sym("Value")) - ) %>% - select(6,3,2,1,4,5) %>% - distinct() - - - x <- as.magpie(projections, spatial = 1, temporal = 2, datacol = 6) - - return(x) - } - else { - stop("Not a valid subtype!") - } - - return(x) -} diff --git a/README.md b/README.md index 2c3e056e..47d6e41d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MadRat REMIND Input Data Package -R package **mrremind**, version **0.168.1** +R package **mrremind**, version **0.168.2** [![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/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Lavinia Baumstark . +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, Hasse R, Fuchs S, Mandaroux R (2023). _mrremind: MadRat REMIND Input Data Package_. R package version 0.168.2, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ 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 Hasse and Sophie Fuchs and Rahel Mandaroux}, year = {2023}, - note = {R package version 0.168.1}, + note = {R package version 0.168.2}, url = {https://github.com/pik-piam/mrremind}, } ``` diff --git a/man/convertEEA_EuropeanEnvironmentAgency.Rd b/man/convertEEA_EuropeanEnvironmentAgency.Rd deleted file mode 100644 index a56ae970..00000000 --- a/man/convertEEA_EuropeanEnvironmentAgency.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/convertEEA_EuropeanEnvironmentAgency.R -\name{convertEEA_EuropeanEnvironmentAgency} -\alias{convertEEA_EuropeanEnvironmentAgency} -\title{Convert European Environment Agency (EEA) data} -\usage{ -convertEEA_EuropeanEnvironmentAgency(x, subtype) -} -\arguments{ -\item{x}{MAgPIE object to be converted} - -\item{subtype}{data subtype. Either "ETS", "historical", "projections", or "projections-detailed"} -} -\value{ -magpie object of European Environment Agency (EEA) ETS emissions (GtCO2) -} -\description{ -Read-in European Environment Agency (EEA) data on ETS emissions as magclass object -} -\examples{ -\dontrun{ -a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS") -} - -} -\seealso{ -\code{\link{readSource}} -} -\author{ -Renato Rodrigues -} diff --git a/man/readEEA_EuropeanEnvironmentAgency.Rd b/man/readEEA_EuropeanEnvironmentAgency.Rd deleted file mode 100644 index dbbae2db..00000000 --- a/man/readEEA_EuropeanEnvironmentAgency.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readEEA_EuropeanEnvironmentAgency.R -\name{readEEA_EuropeanEnvironmentAgency} -\alias{readEEA_EuropeanEnvironmentAgency} -\title{Read European Environment Agency (EEA) data} -\usage{ -readEEA_EuropeanEnvironmentAgency(subtype) -} -\arguments{ -\item{subtype}{data subtype. Either "ETS", "ESR", "total", "sectoral", "projections", or "projections-detailed"} -} -\value{ -magpie object of European Environment Agency (EEA) ETS emissions (GtCO2) -} -\description{ -Read-in European Environment Agency (EEA) data on ETS emissions as magclass object -} -\examples{ -\dontrun{ -a <- readSource(type = "EEA_EuropeanEnvironmentAgency", subtype = "ETS") -} - -} -\seealso{ -\code{\link{readSource}} -} -\author{ -Renato Rodrigues, Falk Benke -}