Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new data source: Ember #327

Merged
merged 9 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '28151720'
ValidationKey: '28350420'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/lucode2-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ jobs:
' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null
cat /etc/R/Rprofile.site

- uses: r-lib/actions/setup-pandoc@v1
- name: Set up Pandoc
uses: r-lib/actions/setup-pandoc@v1

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Cache R libraries
if: ${{ !env.ACT }} # skip when running locally via nektos/act
uses: pat-s/always-upload-cache@v3
with:
path: /usr/local/lib/R/
Expand All @@ -77,7 +79,8 @@ jobs:
run: |
./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev
./run.sh install_all
./run.sh install_r lucode2 covr rstudioapi
./run.sh install_r_binary covr rstudioapi
./run.sh install_r lucode2

- name: Install python dependencies if applicable
run: |
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.146.0",
"version": "0.147.0",
"description": "<p>The mrremind packages contains data preprocessing for the\n 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 @@
Type: Package
Package: mrremind
Title: MadRat REMIND Input Data Package
Version: 0.146.0
Date: 2022-10-17
Version: 0.147.0
Date: 2022-10-21
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(calcCement)
export(calcCementShare)
export(calcChemicalFeedstocksShare)
export(calcClinker_to_cement_ratio)
export(calcEmber)
export(calcEmissionFactorsFeedstocks)
export(calcHRE)
export(calcIEA_ETP)
Expand All @@ -24,13 +25,15 @@ export(calcindustry_subsectors_specific)
export(calcpm_delta_kap_industry)
export(convertADVANCE_WP2)
export(convertAGEB)
export(convertEmber)
export(convertHRE)
export(convertMueller)
export(convertUBA)
export(convertUNFCCC)
export(convertUSGS)
export(readADVANCE_WP2)
export(readAGEB)
export(readEmber)
export(readHRE)
export(readIEA_WEIO_2014)
export(readMueller)
Expand Down
243 changes: 154 additions & 89 deletions R/calcCapacity.R

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions R/calcEmber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' @title calc Ember
#' @description prepare the yearly Ember electricity data set
#' To use only a part of the Ember data, call calcOutput("Ember", subtype = "...")
#' and convert to TW if you want to use capacities as input data to REMIND.
#'
#' @param subtype data subtype. Either "capacity", "generation" or "all"
#'
#' @return A [`magpie`][magclass::magclass] object.
#'
#' @seealso [`calcOutput()`]
#'
#' @author Pascal Weigmann
#'
#' @importFrom madrat toolAggregate toolGetMapping
#' @importFrom magclass collapseDim
#'
#' @export

calcEmber <- function(subtype = "all") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe another name would help understanding, what is happening, e.g. calcEmberCleaned ?

Copy link
Contributor Author

@pweigmann pweigmann Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I didn't plan to use another calc function for "Ember" but would just call calcOutput("Ember", subtype = "capacity") in calcCapacity() for example. The only thing left to do there would be to convert to TW.


# get Ember data
x <- readSource("Ember")

if (subtype == "capacity") {
# choose only capacity variables
x <- x[, , "GW"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to treat "capacity" and "generation" special here and cannot always read in all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this is already the calc-function - got it

description <- "Capacities from the yearly Ember electricity data set"

} else if (subtype == "generation") {
# choose only generation variables
x <- x[, , "TWh"]
description <- "Electricity generation from the yearly Ember electricity data set"

} else if (subtype == "all") {
# remove all variables with % as unit, as they are not used and make data preparation more complicated
x <- x[, , "%", invert = TRUE]
description <- "Capacities and electricity generation from the yearly Ember electricity data set"

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

# load and apply mapping to chosen variables
map <- toolGetMapping("Mapping_Ember.csv", type = "reportingVariables") %>%
filter(!is.na(!!sym("REMIND_Variable")), !!sym("REMIND_Variable") != "") # remove incomplete mapping rows

# combine variable and unit in mapping
map$REMIND_Variable_Unit <- paste0(map$REMIND_Variable, " (", map$REMIND_Unit, ")")

# drop unit dimension as unit is now part of the variable name
x <- collapseDim(x, dim = 3.2)

# apply mapping - careful: units are changed already, values not converted yet
x <- toolAggregate(x, dim = 3.1, rel = map, from = "Variable",
to = "REMIND_Variable_Unit", partrel = TRUE, verbosity = 2)

# convert values using factors from mapping
for (var in getNames(x, dim = 1)) {
x[, , var] <- x[, , var] * map[map$REMIND_Variable_Unit == var, "Factor"]
}

return(list(x = x, weight = NULL, unit = "various", description = description))
}
10 changes: 7 additions & 3 deletions R/calcHistorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ calcHistorical <- function() {

# Capacities historical data ====

#IRENA capacities - technologies: "csp", "geohdr", "hydro", "spv", "wind"
# IRENA capacities - technologies: "csp", "geohdr", "hydro", "spv", "wind"
IRENAcap <- readSource(type="IRENA",subtype="Capacity")[,,c("Concentrated solar power", "Geothermal", "Hydropower", "Solar photovoltaic", "Wind")] # Read IRENA renewables capacity data
IRENAcap <- IRENAcap * 1E-03 # converting MW to GW
mapping <- data.frame( IRENA_techs=c("Concentrated solar power", "Geothermal", "Hydropower", "Solar photovoltaic", "Wind"),
Expand All @@ -131,9 +131,13 @@ calcHistorical <- function() {
IRENAcap <- mbind(IRENAcap, setNames(IRENAcap[,,"Cap|Electricity|Solar|CSP (GW)"] + IRENAcap[,,"Cap|Electricity|Solar|PV (GW)"], "Cap|Electricity|Solar (GW)"))
IRENAcap <- add_dimension(IRENAcap, dim=3.1, add="model",nm="IRENA")

# Ember electricity data ====
Ember <- calcOutput("Ember", subtype = "all", aggregate = FALSE)
Ember <- add_dimension(Ember, dim=3.1, add="model", nm="Ember")

# Region specific historical data ====
# European Eurostat data
eurostat <- calcOutput("EuropeanEnergyDatasheets", subtype = "EU27", aggregate = F)
eurostat <- calcOutput("EuropeanEnergyDatasheets", subtype = "EU27", aggregate = FALSE)
eurostat <- add_dimension(eurostat, dim = 3.1, add = "model", nm = "Eurostat")

# Emissions market data
Expand Down Expand Up @@ -320,7 +324,7 @@ calcHistorical <- function() {
varlist <- list(
fe_iea, fe_weo, fe_proj, fe_hre, pe_iea, pe_weo, trade, pop, gdpp_James,
gdpp_WB, gdpp_IMF, ceds, edgar6, primap, cdiac, LU_EDGAR_LU, LU_CEDS,
LU_FAO_EmisLUC, LU_FAO_EmisAg, LU_PRIMAPhist, IRENAcap, eurostat,
LU_FAO_EmisLUC, LU_FAO_EmisAg, LU_PRIMAPhist, IRENAcap, Ember, eurostat,
# emiMktES, emiMktETS, emiMktESOthers,
EU_ReferenceScenario, emiEurostat, ARIADNE_ReferenceScenarioGdp,
ARIADNE_ReferenceScenarioGdpCorona, ARIADNE_ReferenceScenarioPop,
Expand Down
24 changes: 24 additions & 0 deletions R/convertEmber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Convert Ember data
#'
#' @md
#' @param x A [`magpie`][magclass::magclass] object returned from
#' [`readHRE()`].
#'
#' @return A [`magpie`][magclass::magclass] object.
#'
#' @author Pascal Weigmann
#'
#' @importFrom madrat toolCountryFill
#'
#' @export

convertEmber <- function(x) {

# add missing countries
x <- toolCountryFill(x, fill = 0, verbosity = 2)

# replace NA by 0 to enable aggregation over incomplete regions
x[is.na(x)] <- 0

return(x)
}
34 changes: 34 additions & 0 deletions R/readEmber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Read Ember Yearly Electricity Data
#'
#' @return A [`magpie`][magclass::magclass] object.
#'
#' @author Pascal Weigmann
#'
#' @seealso [`readSource()`]
#' @source https://ember-climate.org/data-catalogue/yearly-electricity-data/
#'
#' @importFrom dplyr select
#' @importFrom magclass as.magpie
#'
#' @export

readEmber <- function() {
filename <- "yearly_full_release_long_format.csv"
df <- read.csv(filename)

# filter out aggregated regions by only choosing rows that don't have a blank country.code
df <- df[df$Country.code != "", ]

# combine category columns to one variable column and rename according to madrat standard
df$variable <- paste(df$Category, df$Subcategory, df$Variable, sep = "|")
df <- select(df, c(region = "Country.code",
year = "Year",
variable = "variable",
unit = "Unit",
value = "Value"))

# convert to magpie object
x <- as.magpie(df, tidy = TRUE)

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 REMIND Input Data Package

R package **mrremind**, version **0.146.0**
R package **mrremind**, version **0.147.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 @@ -39,7 +39,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, Hasse R (2022). _mrremind: MadRat REMIND Input Data Package_. R package version 0.146.0, <https://github.com/pik-piam/mrremind>.
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 (2022). _mrremind: MadRat REMIND Input Data Package_. R package version 0.147.0, <URL: https://github.com/pik-piam/mrremind>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
year = {2022},
note = {R package version 0.146.0},
note = {R package version 0.147.0},
url = {https://github.com/pik-piam/mrremind},
}
```
4 changes: 2 additions & 2 deletions man/calcCapacity.Rd

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

25 changes: 25 additions & 0 deletions man/calcEmber.Rd

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

21 changes: 21 additions & 0 deletions man/convertEmber.Rd

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

23 changes: 23 additions & 0 deletions man/readEmber.Rd

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