-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from orichters/conv2005-17
allow renaming old piam_variable automatically, fix remaining transport+CCS issues
- Loading branch information
Showing
25 changed files
with
267 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Type: Package | ||
Package: piamInterfaces | ||
Title: Project specific interfaces to REMIND / MAgPIE | ||
Version: 0.40.10 | ||
Date: 2025-01-21 | ||
Version: 0.41.0 | ||
Date: 2025-01-23 | ||
Authors@R: c( | ||
person("Falk", "Benke", , "[email protected]", role = c("aut", "cre")), | ||
person("Oliver", "Richters", role = "aut") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' renameOldInMappings | ||
#' | ||
#' Loads "renamed_piam_variables.csv" and replaces all instances of old_name | ||
#' in the piam_variable columns of all mapping files | ||
#' Handle with care, better commit your changes before using it | ||
#' Best run this in your piamInterfaces folder: | ||
#' Rscript -e 'devtools::load_all(); renameOldInMappings()' | ||
#' | ||
#' @md | ||
#' @author Oliver Richters | ||
#' @param folder piamInterfaces folder | ||
#' @importFrom piamutils deletePlus | ||
#' @importFrom stringr str_split | ||
#' @export | ||
renameOldInMappings <- function(folder = ".") { | ||
|
||
# find out where the csvfile is exactly and use that as a starting point | ||
csvfile <- "renamed_piam_variables.csv" | ||
csvfiles <- normalizePath(file.path(folder, c(".", "..", "inst", "../inst"), csvfile), mustWork = FALSE) | ||
folder <- head(dirname(csvfiles[file.exists(csvfiles)]), 1) | ||
if (is.null(folder)) stop(csvfile, " not found, specify 'folder' with such file.") | ||
|
||
# search files locally to avoid overwriting anything in renv or so | ||
mappingfiles <- list.files(file.path(folder, "mappings"), pattern = "mapping_.*\\.csv", full.names = TRUE) | ||
|
||
# collect all variables and expand renamed_piam_variables based on it | ||
vars <- getMappingVariables(addunit = FALSE) | ||
csvdata <- getExpandRenamedVariables(deletePlus(vars)) | ||
|
||
# loop over mapping files | ||
for (m in mappingfiles) { | ||
mapping <- readLines(m) | ||
splitted <- str_split(mapping, ";") | ||
# col is number of piam_variable column | ||
col <- which(splitted[[1]] == "piam_variable") | ||
for (i in seq_along(splitted)) { | ||
if (length(splitted[[i]] >= col)) { | ||
replaceid <- which(csvdata$old_name == deletePlus(splitted[[i]][col])) | ||
if (length(replaceid) == 0) next | ||
replaceby <- unique(csvdata$piam_variable[replaceid]) | ||
if (length(replaceby) == 1) { | ||
message("In ", basename(m), ", replace: ", splitted[[i]][col], " -> ", replaceby) | ||
splitted[[i]][col] <- replaceby | ||
} else if (length(replaceby) > 1) { | ||
stop("For ", splitted[[i]][col], ", more than one replacement variable found. ", | ||
"Please correct renamed_piam_variables.csv: ", | ||
paste(replaceby, collapse = ", ")) | ||
} | ||
} | ||
} | ||
writeLines(unlist(lapply(splitted, paste0, collapse = ";")), con = m) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Project specific interfaces to REMIND / MAgPIE | ||
|
||
R package **piamInterfaces**, version **0.40.10** | ||
R package **piamInterfaces**, version **0.41.0** | ||
|
||
[![CRAN status](https://www.r-pkg.org/badges/version/piamInterfaces)](https://cran.r-project.org/package=piamInterfaces) [![R build status](https://github.com/pik-piam/piamInterfaces/workflows/check/badge.svg)](https://github.com/pik-piam/piamInterfaces/actions) [![codecov](https://codecov.io/gh/pik-piam/piamInterfaces/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/piamInterfaces) [![r-universe](https://pik-piam.r-universe.dev/badges/piamInterfaces)](https://pik-piam.r-universe.dev/builds) | ||
|
||
|
@@ -44,6 +44,8 @@ Additionally, some mappings use those columns: | |
- `interpolation`: sets the interpolation method for the `variable` (i.e. not `piam_variable`) (currently only supports `linear`). When set to `linear`, adds yearly values between 2005 and 2100 through linear interpolation for the selected output variables. | ||
- `weight`: calculates a weighted average of multiple entries of `piam_variable`. Provide a different `piam_variable` in this column, and `generateIIASASubmission()` will split the data on the rows which contain weight pointers, resolve these weights into numerical values (via a join operation between the submission and the input data) and then modify the value based on the weighting. This takes place in the private .resolveWeights method. | ||
|
||
### Editing a mapping | ||
|
||
To edit a mapping in `R`, use: | ||
``` | ||
mappingdata <- getMapping("AR6") | ||
|
@@ -68,6 +70,23 @@ remind2::compareScenConf(fileList = c("oldfile.csv", "mappingfile.csv"), row.nam | |
``` | ||
On the PIK cluster, you can run `comparescenconf mapping_AR6.csv` in the `inst/mappings` folder and it will compare to a recent `master` version. | ||
|
||
### Renaming a piam_variable | ||
|
||
If a variable used as `piam_variable` has to be renamed, please add it with its `old_name` to [`inst/renamed_piam_variables.csv`](./inst/renamed_piam_variables.csv). | ||
Like this, if someone arrives with a dataset that contains the old name but not the new, [`renameOldVariables()`](./R/renameOldVariables.R) makes sure the data is automatically adjusted. | ||
[`test-renameOldVariables.R`](./tests/testthat/test-renameOldVariables.R) enforces that the old variable name is not used anywhere in the mappings. | ||
To adjust the mappings automatically, make sure you commit the current state to be able to reset its results, and then run `Rscript -e "devtools::load_all(); renameOldInMappings()"`. | ||
Check the `diff` carefully, for example using `comparescenconf`, see above. | ||
|
||
### piam_factor and unit checks | ||
|
||
While running the tests, an extensive check of the compatibility of `piam_unit`, `unit` and `piam_factor` is performed. | ||
It helps to find mismatches, for example mapping `Mt` to `Gt` with a factor of `0.001` or mapping `US$2005` to `US$2017` without accounting for inflation. | ||
These checks are performed using [`checkUnitFactor()`](./R/checkUnitFactor.R). | ||
It first calls [`areUnitsIdentical()`](./R/areUnitsIdentical.R) where a number of identical units are specified (such as `Mt CO2` = `Mt CO2eq`, where `piam_factor` is 1). | ||
Then, it compares a list of accepted factors against the templates. | ||
In case your tests fails, carefully check whether the `piam_factor` is correct, and if so, add it to one of the functions. | ||
|
||
### Creating a new mapping | ||
|
||
Since templates contain between several hundreds and a few thousand variables, relying on existing mappings can save substantial amounts of work compared to setting up a new mapping from scratch. Since the template itself is most likely built based on earlier templates from other projects, chances are good that existing mappings already provide parts of the required new mapping. Using `R`, we describe a simple way to create a new mapping `mapping_NEW.csv` based on existing mappings. | ||
|
@@ -124,15 +143,17 @@ In case of questions / problems please contact Falk Benke <[email protected]> | |
|
||
To cite package **piamInterfaces** in publications use: | ||
|
||
Benke F, Richters O (2025). "piamInterfaces: Project specific interfaces to REMIND / MAgPIE - Version 0.40.10." | ||
Benke F, Richters O (2025). "piamInterfaces: Project specific interfaces to REMIND / MAgPIE." Version: 0.41.0, <https://github.com/pik-piam/piamInterfaces>. | ||
|
||
A BibTeX entry for LaTeX users is | ||
|
||
```latex | ||
@Misc{, | ||
title = {piamInterfaces: Project specific interfaces to REMIND / MAgPIE - Version 0.40.10}, | ||
title = {piamInterfaces: Project specific interfaces to REMIND / MAgPIE}, | ||
author = {Falk Benke and Oliver Richters}, | ||
date = {2025-01-21}, | ||
date = {2025-01-23}, | ||
year = {2025}, | ||
url = {https://github.com/pik-piam/piamInterfaces}, | ||
note = {Version: 0.41.0}, | ||
} | ||
``` |
Oops, something went wrong.