pik-piam / piamInterfaces

0 stars 19 forks source link

Project specific interfaces to REMIND / MAgPIE

R package piamInterfaces, version 0.37.0

CRAN status R build status codecov r-universe

Purpose and Functionality

Project specific interfaces to REMIND / MAgPIE.

Tutorial

Mappings

Mappings found in the inst/mappings folder serve to map variables from the PIAM framework to variables needed for the submission to databases. The mappings are ;-separated files, using # as comment character, with the following mandatory columns:

Recommended column:

Additionally, some mappings use those columns:

To edit a mapping in R, use:

mappingdata <- getMapping("AR6")
...
write.csv2(mappingdata, "test.csv", na = "", row.names = FALSE, quote = FALSE)

Opening the csv files in Excel can be problematic, as it sometimes changes values and quotation marks. You can edit the files in LibreOffice Calc using these settings in the Text Import dialog:

The github diff on a large semicolon-separated file is often unreadable. For a human-readable output, save the old version of the mapping and run:

remind2::compareScenConf(fileList = c("oldfile.csv", "mappingfile.csv"), row.names = NULL)

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.

  1. Identify which existing mappings are most relevant for your new mapping. Criteria might include the time at which the existing mapping was created and the proximity of the templates (e.g. follow-up project). If you are unsure, ask your experienced colleagues for advice. This provides you with a list of existing mappings that is ordered by relevance, say mapping_OLD1.csv, ... , mapping_OLD9.csv.
  2. Use read.csv2 to get the template as a dataframe template.
  3. Looping over the existing mappings in descending order of relevance,
    • use getMapping to get the existing mapping mapping_OLDi as a dataframe,
    • filter mapping_OLDi for variables which are contained in template and which have not yet been added to mapping_NEW,
    • left_join (by variable) the filtered mapping_OLDi with template to add the information from the template (consider using str_to_lower for case-insensitive matching when filtering and joining),
    • select/mutate the columns of the joined dataframe to keep the desired columns for the new mapping (see above for description of mandatory and recommended columns),
    • bind_rows to mapping_NEW
  4. Use write.csv2 to export mapping_NEW as mapping_NEW.csv. It is recommended to make a few checks (e.g. by looking at all variables for which the description or the unit does not agree between the existing mapping and the template).

Model intercomparison

Installation

For installation of the most recent package version an additional repository has to be added in R:

options(repos = c(CRAN = "@CRAN@", pik = "https://rse.pik-potsdam.de/r/packages"))

The additional repository can be made available permanently by adding the line above to a file called .Rprofile stored in the home folder of your system (Sys.glob("~") in R returns the home directory).

After that the most recent version of the package can be installed using install.packages:

install.packages("piamInterfaces")

Package updates can be installed using update.packages (make sure that the additional repository has been added before running that command):

update.packages()

Questions / Problems

In case of questions / problems please contact Falk Benke benke@pik-potsdam.de.

Citation

To cite package piamInterfaces in publications use:

Benke F, Richters O (2024). piamInterfaces: Project specific interfaces to REMIND / MAgPIE. R package version 0.37.0, https://github.com/pik-piam/piamInterfaces.

A BibTeX entry for LaTeX users is

@Manual{,
  title = {piamInterfaces: Project specific interfaces to REMIND / MAgPIE},
  author = {Falk Benke and Oliver Richters},
  year = {2024},
  note = {R package version 0.37.0},
  url = {https://github.com/pik-piam/piamInterfaces},
}