Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pik-piam/remind2 into ecemf
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Sep 8, 2023
2 parents 1207039 + 4d06daf commit 685ff84
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '218545744'
ValidationKey: '219049404'
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: 'remind2: The REMIND R package (2nd generation)'
version: 1.115.2
date-released: '2023-08-28'
version: 1.117.2
date-released: '2023-09-07'
abstract: Contains the REMIND-specific routines for data and model output manipulation.
authors:
- family-names: Rodrigues
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: remind2
Title: The REMIND R package (2nd generation)
Version: 1.115.2
Date: 2023-08-28
Version: 1.117.2
Date: 2023-09-07
Authors@R: c(
person("Renato", "Rodrigues", , "[email protected]", role = c("aut", "cre")),
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 @@ -7,6 +7,7 @@ export(calcPrice)
export(calcTimeSeriesStats)
export(calc_CES_marginals)
export(calc_regionSubset_sums)
export(checkVariableNames)
export(checkVsCalibData)
export(colorScenConf)
export(compareCalibrationTargets)
Expand Down
25 changes: 25 additions & 0 deletions R/checkVariableNames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' checkVariablesNames checks remind2 reporting on inconsistency in variable names
#'
#' Pass a vector of variable names including the units. Get warnings if inconsistencies are found
#' for the reporting
#' @param vars vector with variable names and units such as "PE (EJ)"
#' @author Oliver Richters
#' @export
checkVariableNames <- function(vars) {

barspace <- grep("[\\| ]{2}|^[\\| ]|[\\| ]$", vars, value = TRUE)
if (length(barspace) > 0) {
warning("These variable names have wrong bars and spaces: ", paste(barspace, collapse = ", "))
}

NAvar <- grep("[\\|\\( ]NA[\\|\\) ]|^NA", vars, value = TRUE)
NAvar <- NAvar[! grepl("^Services and Products\\|Transport\\|non-LDV\\|S", NAvar)] # unit NA, but ok, see issue #408
if (length(NAvar) > 0) {
warning("These variables and units contain NA: ", paste(NAvar, collapse = ", "))
}

noUnit <- grep(" \\(.*\\)$", vars, value = TRUE, invert = TRUE)
if (length(noUnit) > 0) {
warning("These variables have no units: ", paste(noUnit, collapse = ", "))
}
}
4 changes: 3 additions & 1 deletion R/convGDX2MIF.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ convGDX2MIF <- function(gdx, gdx_ref = NULL, file = NULL, scenario = "default",
output <- add_dimension(output,dim=3.1,add = "model",nm = "REMIND")
output <- add_dimension(output,dim=3.1,add = "scenario",nm = scenario)

checkVariableNames(getNames(output, dim = 3))

sumChecks <- piamInterfaces::checkSummations(
mifFile = output, outputDirectory = NULL,
summationsFile = "extractVariableGroups",
absDiff = 1.5e-8, relDiff = 1e-8, roundDiff = FALSE
absDiff = 1.5e-8, relDiff = 1e-8, roundDiff = TRUE
) %>% filter(abs(!!sym("diff")) >= 1.5e-8)

# either write the *.mif or return the magpie object
Expand Down
47 changes: 45 additions & 2 deletions R/reportEmi.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200
as.magpie(spatial = 2, temporal = 1, data = ncol(.)) %>%
ifelse(is.finite(.), ., 0) # replace NaN by 0

rm(vm_emiIndCCS_tibble, subsector_emissions, subsector_total_emissions,
pm_emifac_tibble)
rm(vm_emiIndCCS_tibble, subsector_total_emissions, pm_emifac_tibble)

# utility functions ----
# Convert a mixer table into a list that can be passed to mselect() to
Expand Down Expand Up @@ -672,6 +671,50 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200
x[['variable']])
}) %>%
mbind())

# Baseline emission before CCS, corresponds to energy carbon content
out <- mbind(
# energy emissions
subsector_emissions %>%
mutate(
secInd37 = case_when(
'cement' == .data$secInd37 ~ 'Cement',
'chemicals' == .data$secInd37 ~ 'Chemicals',
'steel' == .data$secInd37 ~ 'Steel',
'otherInd' == .data$secInd37 ~ 'Other Industry',
TRUE ~ NA_character_),
fety = case_when(
'fesos' == .data$fety ~ 'Solids',
'fehos' == .data$fety ~ 'Liquids',
'fegas' == .data$fety ~ 'Gases',
TRUE ~ NA_character_),
origin = case_when(
grepl('fos$', .data$sety) ~ 'Fossil',
grepl('bio$', .data$sety) ~ 'Biomass',
grepl('syn$', .data$sety) ~ 'Hydrogen',
TRUE ~ NA_character_)) %>%
assert(not_na, everything()) %>%
group_by(.data$t, .data$regi, .data$secInd37, .data$fety,
.data$origin) %>%
summarise(value = sum(.data$subsector_emissions)
* as.numeric(GtC_2_MtCO2),
.groups = 'drop') %>%
mutate(d3 = paste0('Emi|CO2|pre-CCS|Energy|Demand|Industry|',
.data$secInd37, '|', .data$fety, '|', .data$origin,
' (Mt CO2/yr)')) %>%
select('t', 'regi', 'd3', 'value') %>%
as.magpie(spatial = 2, temporal = 1, datacol = ncol(.)) %>%
`getSets<-`(fulldim = FALSE, value = getSets(out)),

# process emissions
readGDX(gdx, 'vm_macBaseInd', field = 'l', restore_zeros = FALSE) %>%
`[`(,,'co2cement_process.cement') %>%
`*`(as.numeric(GtC_2_MtCO2)) %>%
`getSets<-`(fulldim = FALSE, value = getSets(out)) %>%
`getNames<-`(
value = 'Emi|CO2|pre-CCS|Industrial Processes|Cement (Mt CO2/yr)'),

out)
} else {

# if o37_demFeIndSub not existing in GDX, calculate reporting parameter here, note: works for industry fixed_shares only
Expand Down
11 changes: 5 additions & 6 deletions R/reportSE.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ reportSE <- function(gdx, regionSubsetList = NULL, t = c(seq(2005, 2060, 5), seq
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = "igcc", name = "SE|Electricity|Coal|++|Gasification Combined Cycle w/o CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = "igccc", name = "SE|Electricity|Coal|++|Gasification Combined Cycle w/ CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = "pc", name = "SE|Electricity|Coal|++|Pulverised Coal w/o CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = "pcc", name = "SE|Electricity|Coal|++|Pulverised Coal w/ CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = "coalchp", name = "SE|Electricity|Coal|++|Combined Heat and Power w/o CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = setdiff(pe2se$all_te, c("igcc", "igccc", "pcc", "pc", "coalchp")),
se.prod(vm_prodSe, dataoc, oc2te, entySe, "pecoal", "seel", te = setdiff(pe2se$all_te, c("igcc", "igccc", "pc", "coalchp")),
name = "SE|Electricity|Coal|++|Other (EJ/yr)"),

se.prod(vm_prodSe, dataoc, oc2te, entySe, "pegas", "seel", name = "SE|Electricity|+|Gas (EJ/yr)"),
Expand Down Expand Up @@ -240,22 +239,22 @@ reportSE <- function(gdx, regionSubsetList = NULL, t = c(seq(2005, 2060, 5), seq

## Gases
if (!(is.null(vm_macBase) & is.null(vm_emiMacSector))) {
## correction for the reused gas from waste landfills
## exogenous variable for representing reused gas from waste landfills (accounted in the model as segabio)
MtCH4_2_TWa <- readGDX(gdx, "sm_MtCH4_2_TWa", react = "silent")
if (is.null(MtCH4_2_TWa)) {
MtCH4_2_TWa <- 0.001638
}
tmp1 <- mbind(tmp1,
setNames(MtCH4_2_TWa * (vm_macBase[, , "ch4wstl"] - vm_emiMacSector[, , "ch4wstl"]), "SE|Gases|+|Waste (EJ/yr)")
setNames(MtCH4_2_TWa * (vm_macBase[, , "ch4wstl"] - vm_emiMacSector[, , "ch4wstl"]), "SE|Gases|Biomass|Waste (EJ/yr)")
)
} else {
tmp1 <- mbind(tmp1,
setNames(new.magpie(cells_and_regions = getRegions(dataoc), years = y, fill = 0), "SE|Gases|+|Waste (EJ/yr)")
setNames(new.magpie(cells_and_regions = getRegions(dataoc), years = y, fill = 0), "SE|Gases|Biomass|Waste (EJ/yr)")
)
}

tmp1 <- mbind(tmp1,
setNames(se.prod(vm_prodSe, dataoc, oc2te, entySe, input_gas, se_Gas) + tmp1[, , "SE|Gases|+|Waste (EJ/yr)"], "SE|Gases (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, input_gas, se_Gas, name = "SE|Gases (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, pebio, se_Gas, name = "SE|Gases|+|Biomass (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, pebio, se_Gas, te = teccs, name = "SE|Gases|Biomass|+|w/ CC (EJ/yr)"),
se.prod(vm_prodSe, dataoc, oc2te, entySe, pebio, se_Gas, te = tenoccs, name = "SE|Gases|Biomass|+|w/o CC (EJ/yr)"),
Expand Down
1 change: 0 additions & 1 deletion R/reportTechnology.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(
"biochp" = "Electricity|Biomass|Combined Heat and Power w/o CC",
"igccc" = "Electricity|Coal|Gasification Combined Cycle w/ CC",
"igcc" = "Electricity|Coal|Gasification Combined Cycle w/o CC",
"pcc" = "Electricity|Coal|Pulverised Coal w/ CC",
"pc" = "Electricity|Coal|Pulverised Coal w/o CC",
"coalchp" = "Electricity|Coal|Combined Heat and Power w/o CC",
"ngccc" = "Electricity|Gas|Combined Cycle w/ CC",
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The REMIND R package (2nd generation)

R package **remind2**, version **1.115.2**
R package **remind2**, version **1.117.2**

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

Expand Down Expand Up @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues <renato.rodrigue

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

Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P (2023). _remind2: The REMIND R package (2nd generation)_. R package version 1.115.2, <https://github.com/pik-piam/remind2>.
Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P (2023). _remind2: The REMIND R package (2nd generation)_. R package version 1.117.2, <https://github.com/pik-piam/remind2>.

A BibTeX entry for LaTeX users is

Expand All @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is
title = {remind2: The REMIND R package (2nd generation)},
author = {Renato Rodrigues and Lavinia Baumstark and Falk Benke and Jan Philipp Dietrich and Alois Dirnaichner and Pascal Führlich and Anastasis Giannousakis and Robin Hasse and Jérome Hilaire and David Klein and Johannes Koch and Katarzyna Kowalczyk and Antoine Levesque and Aman Malik and Anne Merfort and Leon Merfort and Simón Morena-Leiva and Michaja Pehl and Robert Pietzcker and Sebastian Rauner and Oliver Richters and Marianna Rottoli and Christof Schötz and Felix Schreyer and Kais Siala and Björn Sörgel and Mike Spahr and Jessica Strefler and Philipp Verpoort},
year = {2023},
note = {R package version 1.115.2},
note = {R package version 1.117.2},
url = {https://github.com/pik-piam/remind2},
}
```
3 changes: 1 addition & 2 deletions inst/markdown/compareScenarios2/cs2_04_energy_supply.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ items <- c(
"SE|Gases|Biomass|w/o CC",
"SE|Gases|Fossil|Coal|w/ CC",
"SE|Gases|Fossil|Coal|w/o CC",
"SE|Gases|Hydrogen",
"SE|Gases|Waste")
"SE|Gases|Hydrogen")
showAreaAndBarPlots(data, items, tot, scales = "fixed")
```

Expand Down
18 changes: 18 additions & 0 deletions man/checkVariableNames.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-checkVariableNames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("checkVariableNames works", {
fine <- c("PE (EJ)", "PE (NA/yr)", "FE|What (yr)", "FE|Electricity (EJ/yr)")
for (v in fine) {
expect_no_warning(checkVariableNames(v))
}
wrong <- c("PE", "PE| (EJ)", "PE||Elec (EJ)", "PE||Elec", "PE (EJ)", "PE (NA)",
"NA|PE (EJ)", "PE|NA|What (EJ)", " PE (EJ)", "PE (EJ) ")
for (v in wrong) {
w <- capture_warnings(checkVariableNames(v))
expect_true(length(w) > 0)
}
})
18 changes: 1 addition & 17 deletions tests/testthat/test-convGDX2mif.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@
library(dplyr)
library(gdx)

checkVariableNames <- function(out) {

barspace <- grep("[\\| ]{2}", getNames(out, dim = 3), value = TRUE)

if (length(barspace) > 0) {
warning("These variable names have wrong bars and spaces: ", paste(barspace, collapse = ", "))
}

NAvar <- grep("[\\|\\( ]NA[\\|\\) ]|^NA", getNames(out, dim = 3), value = TRUE)
NAvar <- NAvar[! grepl("^Services and Products\\|Transport\\|non-LDV\\|S", NAvar)] # unit NA, but ok, see issue #408
if (length(NAvar) > 0) {
warning("These variables and units contain NA: ", paste(NAvar, collapse = ", "))
}
}


test_that("Test if REMIND reporting is produced as it should and check data integrity", {
skip_if_not(as.logical(gdxrrw::igdx(silent = TRUE)), "gdxrrw is not initialized properly")

Expand Down Expand Up @@ -80,7 +64,7 @@ test_that("Test if REMIND reporting is produced as it should and check data inte
message("Running convGDX2MIF(", gdxPath, ")...")
mifContent <- convGDX2MIF(gdxPath, gdx_refpolicycost = gdxPath)

checkVariableNames(mifContent)
expect_no_warning(checkVariableNames(getNames(mifContent, dim = 3)))

magclass::write.report(
x = magclass::collapseNames(mifContent),
Expand Down

0 comments on commit 685ff84

Please sign in to comment.