Skip to content

Commit

Permalink
Merge pull request pik-piam#649 from orichters/units
Browse files Browse the repository at this point in the history
 add Price|FE variables to global aggregation, set to NA if fails
  • Loading branch information
orichters authored Aug 27, 2024
2 parents 250d278 + bee3959 commit ac3b82e
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '229791032'
ValidationKey: '229962240'
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.151.2
date-released: '2024-08-26'
version: 1.152.0
date-released: '2024-08-27'
abstract: Contains the REMIND-specific routines for data and model output manipulation.
authors:
- family-names: Rodrigues
Expand Down
10 changes: 5 additions & 5 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.151.2
Date: 2024-08-26
Version: 1.152.0
Date: 2024-08-27
Authors@R: c(
person("Renato", "Rodrigues", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down Expand Up @@ -54,7 +54,7 @@ Imports:
gms,
lucode2 (>= 0.43.0),
lusweave,
madrat (>= 3.11.3),
madrat (>= 3.13.0),
mip (>= 0.149.2),
openxlsx,
piamInterfaces (>= 0.17.11),
Expand All @@ -71,13 +71,13 @@ Imports:
tidyr,
tidyselect,
withr,
knitr,
yaml,
digest
Suggests:
covr,
gridExtra,
htmltools,
kableExtra,
knitr,
testthat,
tidyverse
VignetteBuilder:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ importFrom(magclass,read.report)
importFrom(magclass,setItems)
importFrom(magclass,setNames)
importFrom(magclass,setYears)
importFrom(magclass,unitsplit)
importFrom(magclass,write.report)
importFrom(mip,plotstyle)
importFrom(openxlsx,addStyle)
Expand Down
3 changes: 3 additions & 0 deletions R/reportCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,9 @@ reportCosts <- function(gdx,output=NULL,regionSubsetList=NULL,t=c(seq(2005,2060,
if (!is.null(regionSubsetList))
tmp <- mbind(tmp, calc_regionSubset_sums(tmp, regionSubsetList))

# cannot be summed for aggregation
tmp[c("GLO", names(regionSubsetList)),,"Costs|Biomass|Adjfactor (unitless)"] <- NA

getSets(tmp)[3] <- "variable"
return(tmp)
}
8 changes: 6 additions & 2 deletions R/reportExtraction.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,16 @@ grades[is.na(grades)] <- 0
out <- mbind(tmp1, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8)
####### add global value #############
out <- mbind(out, dimSums(out, dim = 1))

out <- mbind(out, tmp2)

# add other region aggregations
if (!is.null(regionSubsetList))
if (!is.null(regionSubsetList)) {
out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList))
}

# costs cannot be summed for aggregation
costvars <- grep("Average Supply Costs", getNames(out), value = TRUE)
out[c("GLO", names(regionSubsetList)),,costvars] <- NA

getSets(out)[3] <- "variable"
return(out)
Expand Down
48 changes: 19 additions & 29 deletions R/reportPrices.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' @importFrom dplyr %>% case_when distinct filter inner_join tibble left_join rename
#' @importFrom gdx readGDX
#' @importFrom magclass mbind getYears getRegions setNames dimExists new.magpie lowpass complete_magpie getItems<- getNames
#' @importFrom magclass mbind getYears getRegions setNames dimExists new.magpie lowpass complete_magpie getItems<- getNames unitsplit
#' @importFrom quitte df.2.named.vector getColValues
#' @importFrom readr read_csv
#' @importFrom madrat toolAggregate
Expand Down Expand Up @@ -955,34 +955,23 @@ reportPrices <- function(gdx, output=NULL, regionSubsetList=NULL,
}

## add weights definition for region aggregation for FE prices that were added automatically
if(length(pm_FEPrice_by_FE) > 0) {
if (length(pm_FEPrice_by_FE) > 0) {
margPriceVars <- grep("Price|Final Energy|", getItems(out,3), fixed = TRUE, value = TRUE)
margPriceVars <- setdiff(margPriceVars, names(int2ext))
vars <- gsub("US\\$2005/GJ", "EJ/yr", gsub("Price\\|Final Energy\\|","FE|",margPriceVars))
names(vars) <- margPriceVars
vars <- gsub("Efuel","Hydrogen",vars) ###warning FE variable should be renamed and this line should be removed in the future
# for(var in vars){ # display price variables with no matching FE weight
# if(!(var %in% getItems(output,3))){
# print(var)
# }
# }
vars <- vars[vars %in% getItems(output,3)]
int2ext <- c(int2ext, vars)
}

## moving averages and rawdata
avgs <- getNames(out.lowpass)
rawdata <- getNames(out.rawdata)
## exclude detailed FE prices from global aggregation
avgs <- setdiff(avgs, grep("Total LCOE|Transport and Distribution|Other Taxes|Fuel Cost|Carbon Price Component", avgs, value = TRUE))
rawdata <- setdiff(rawdata, grep("Total LCOE|Transport and Distribution|Other Taxes|Fuel Cost|Carbon Price Component", rawdata, value = TRUE))
int2ext <- c(int2ext,
stats::setNames(int2ext[gsub("\\|Moving Avg", "", avgs)], avgs),
stats::setNames(int2ext[gsub("\\|Rawdata", "", rawdata)], rawdata)
)
int2ext <- int2ext[! is.na(int2ext)]

## filtering out vars with missing moving average / rawdata weights
int2ext <- int2ext[!is.na(int2ext)]
## add subvariables, plus moving averages and rawdata. .addSubvariable is defined at the bottom of this file
int2ext <- .addSubvariable(int2ext, c("|Fuel Cost", "|Other Taxes", "|Total LCOE",
"|Transport and Distribution", "|Carbon Price Component"))
int2ext <- .addSubvariable(int2ext, c("|Moving Avg", "|Rawdata"))
int2ext <- int2ext[names(int2ext) %in% getNames(out)]

# # ---- internal price variables used for model diagnostics -----

Expand Down Expand Up @@ -1030,19 +1019,14 @@ reportPrices <- function(gdx, output=NULL, regionSubsetList=NULL,

# add global prices
map <- data.frame(region=getRegions(out),world="GLO",stringsAsFactors=FALSE)
tmp_GLO <- new.magpie("GLO",getYears(out),getNames(out),fill=0)
tmp_GLO <- new.magpie("GLO", getYears(out), getNames(out), fill = NA)

int2ext <- int2ext[intersect(names(int2ext), getNames(out))] # select only data that exists

for (i2e in names(int2ext)) {
tmp_GLO["GLO",,i2e] <- toolAggregate(out[,,i2e], rel = map, weight = output[map$region,,int2ext[i2e]], zeroWeight = "allow")
for(t in getYears(out)){
if(all(output[map$region,t,int2ext[i2e]]==0)){
tmp_GLO["GLO",t,i2e] <- NA
}
}
tmp_GLO["GLO",,i2e] <- toolAggregate(out[,,i2e], rel = map, weight = output[map$region,,int2ext[i2e]], zeroWeight = "setNA")
}
out <- mbind(out,tmp_GLO)
out <- mbind(out, tmp_GLO)

# add other region aggregations
if (!is.null(regionSubsetList)){
Expand All @@ -1064,8 +1048,6 @@ reportPrices <- function(gdx, output=NULL, regionSubsetList=NULL,
out <- mbind(out, tmp_RegAgg)
}

out[is.na(out)] <- 0 # out is NA if weight is zero for all regions within the GLO or the specific region aggregation. Therefore, we replace all NAs with zeros.

# prices that are same for all regions, including GLO
glob_price <- new.magpie(getRegions(out),getYears(out),fill=0)
for(i in getRegions(out)) glob_price[i,,] <- pm_pvp[,,"peur"]*1000
Expand Down Expand Up @@ -1151,3 +1133,11 @@ reportPrices <- function(gdx, output=NULL, regionSubsetList=NULL,
getSets(out)[3] <- "variable"
return(out)
}

.addSubvariable <- function(int2ext, subvar) {
split <- unitsplit(names(int2ext))
for (sv in subvar) {
int2ext <- c(int2ext, stats::setNames(int2ext, paste0(split$variable, sv, " (", split$unit, ")")))
}
return(int2ext)
}
5 changes: 2 additions & 3 deletions R/reportTax.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ reportTax <- function(gdx,output=NULL,regionSubsetList=NULL,t=c(seq(2005,2060,5)
out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList))

# select variables that cannot be aggregated by simply sums and set their values to NA
vars <- getNames(out)[grepl("rate", getNames(out))]
out["GLO",,vars] <- NA
out[names(regionSubsetList),,vars] <- NA
vars <- grep("[Rr]ate", getNames(out), value = TRUE)
out[c("GLO", names(regionSubsetList)),,vars] <- NA

getSets(out)[3] <- "variable"
return(out)
Expand Down
6 changes: 3 additions & 3 deletions R/reportTechnology.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(
int2ext <- get_global_mapping(category, unit, techmap)

if (CDR_mod != "off") {
unit <- "US$2005/t CO2 yr"
unit <- "US$2005/t CO2/yr"
factor <- 1000 / 3.6
tmp <- bind_category(tmp, v_investcost, category, unit, factor, cdrmap)
int2ext <- c(int2ext, get_global_mapping(category, unit, cdrmap))
Expand All @@ -275,7 +275,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(
}

if (CDR_mod != "off") {
unit <- "US$2005/t CO2 yr"
unit <- "US$2005/t CO2/yr"
factor <- 1000 / 3.6
tmp <- bind_category(tmp, v_investcost + v_adjustteinv_avg, category, unit, factor, cdrmap)
int2ext <- c(int2ext, get_global_mapping(category, unit, cdrmap))
Expand Down Expand Up @@ -339,7 +339,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(
if (CDR_mod != "off") {
## op costs for CDR technologies ###
category <- "OM Cost|fixed"
unit <- "US$2005/t CO2 yr"
unit <- "US$2005/t CO2/yr"
tmp <- bind_category(tmp, omf * v_investcost, category, unit, 1000 / 3.66, cdrmap)
int2ext <- c(int2ext, get_global_mapping(category, unit, cdrmap))
}
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.151.2**
R package **remind2**, version **1.152.0**

[![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, Duerrwaechter J, 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, Weigmann P, Rüter T (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.151.2, <https://github.com/pik-piam/remind2>.
Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Duerrwaechter J, 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, Weigmann P, Rüter T (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.152.0, <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 Jakob Duerrwaechter 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 and Pascal Weigmann and Tonn Rüter},
year = {2024},
note = {R package version 1.151.2},
note = {R package version 1.152.0},
url = {https://github.com/pik-piam/remind2},
}
```
7 changes: 7 additions & 0 deletions tests/testthat/test-addSubvariable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that(".addSubvariable works", {
int2ext <- c("Price|Final Energy (US$2005/GJ)" = "FE (EJ)")
int2extafter <- c("Price|Final Energy (US$2005/GJ)" = "FE (EJ)",
"Price|Final Energy|Rawdata (US$2005/GJ)" = "FE (EJ)")
expect_equal(int2extafter,
.addSubvariable(int2ext, "|Rawdata"))
})

0 comments on commit ac3b82e

Please sign in to comment.