Skip to content

Commit

Permalink
readLPJmL
Browse files Browse the repository at this point in the history
  • Loading branch information
mscrawford committed Sep 30, 2024
1 parent 3cd25f5 commit b321301
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2358938'
ValidationKey: '2399520'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
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: 'mrlandcore: One-line description of this awesome package'
version: 1.1.8
date-released: '2024-09-25'
version: 1.2.0
date-released: '2024-09-30'
abstract: One-paragraph description of this awesome package.
authors:
- family-names: Beier
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: mrlandcore
Title: One-line description of this awesome package
Version: 1.1.8
Date: 2024-09-25
Version: 1.2.0
Date: 2024-09-30
Authors@R: c(
person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")),
person("Kristine", "Karstens", role = "aut"),
Expand Down
102 changes: 102 additions & 0 deletions R/readLPJmL.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' @title readLPJmL
#'
#' @description Read in LPJmL outputs
#'
#' @param subtype Switch between different inputs
#' (eg. "LPJmL5.2_Pasture:IPSL_CM6A_LR:ssp126_co2_limN_00:soilc_past_hist")
#'
#' @return List of magpie objects with results on cellular level, weight, unit and description.
#'
#' @author Felicitas Beier, Sebastian Ostberg, Michael Crawford
#'
#' @seealso
#' [readLPJ()]
#' @examples
#' \dontrun{
#' readSource("LPJmL", convert = FALSE)
#' }

# nolint start
### This function should be adjusted during the LPJmL - MAgPIE - Hackathon ###
# Testing locations:
# /p/projects/rd3mod/inputdata/sources/LPJmL
# example subtype: lpjml5.9.5.mag1.MRI.ESM2.0.ssp370.crop.sdate

# Testing data:
# subtype <- "lpjml5.9.5.mag1.MRI.ESM2.0.ssp370.crop.sdate"
# setwd("/p/projects/rd3mod/inputdata/sources/LPJmL/lpjml5.9.5.mag1.MRI.ESM2.0.ssp370.pnv.mrunoff")
# setwd("/p/projects/rd3mod/inputdata/sources/LPJmL/lpjml5.9.5.mag1.MRI.ESM2.0.ssp370.crop.sdate")
# nolint end

readLPJmL <- function(subtype = "lpjml5.9.5.mag1.MRI.ESM2.0.ssp370.crop.sdate") {

# filenames for dataset and grid
files <- list.files(path = subtype, pattern = "\\.bin\\.json$", full.names = TRUE)
gridname <- lpjmlkit::find_varfile(subtype, variable = "grid")
dataname <- grep("grid", files, invert = TRUE, value = TRUE)
if (length(dataname) != 1) {
stop("More than one data file is present in the LPJmL source directory.")
}

# read in LPJmL dataset
x <- lpjmlkit::read_io(dataname)

# generate a mapping from LPJmL lon-lat to MAgPIE coord
# extract grid information
x$add_grid(gridname, silent = TRUE)
grid <- x$grid$data

# transform to format of magpie object while maintaining cell order
lon <- gsub("\\.", "p", grid[, "lon"])
lat <- gsub("\\.", "p", grid[, "lat"])
coordsLPJmL <- paste(lon, lat, sep = ".")

# sort mapping according to provided grid to ensure consistency
mapping <- mstools::toolGetMappingCoord2Country(pretty = FALSE, extended = FALSE)
matches <- match(x = coordsLPJmL, table = mapping$coords)
if (any(is.na(matches))) {
stop("Discrepancy between spatial extent of LPJmL coords and MAgPIE coords")
}
mapping <- mapping[matches, ]

# transform time dimension
x$transform(to = "year_month_day")
if ("month" %in% names(dimnames(x))) {
x <- aperm(x$data, c("cell", "year", "month", "band"))
} else {
x <- x$data
}
x <- drop(x)

# transform x into a MAgPIE object
x <- magclass::as.magpie(x, spatial = 1)

meta <- lpjmlkit::read_meta(dataname)
bandNames <- sub("^(rainfed|irrigated)\\s+", "", meta$band_names)

# MIKE: Move mapping to mrlandcore
lpj2mag <- madrat::toolGetMapping("MAgPIE_LPJmL.csv", type = "sectoral", where = "mappingfolder")
hasCrops <- any(bandNames %in% lpj2mag$LPJmL5)
if (hasCrops) {

# differentiate between rainfed and irrigated crops in the case of crop data
irrigation <- sub(" .*", "", getNames(x)) # select first word (of, e.g. "rainfed temperature cereals")
crop <- sub("^[^ ]+\\s+", "", getNames(x)) # select everything after first word
x <- magclass::add_dimension(x, dim = 3.1, add = "irrigation", nm = "dummy")
getNames(x, dim = "irrigation") <- irrigation
getNames(x, dim = "band") <- crop

# transform LPJmL5 to LPJmL-internal names
x <- madrat::toolAggregate(x, rel = lpj2mag, from = "LPJmL5", to = "LPJmL", dim = 3.2, partrel = TRUE)
magclass::getSets(x)["d3.2"] <- "crop"

}

# use coordinate mapping to assign MAgPIE coords and iso
x <- magclass::add_dimension(x, dim = 1.1, add = "lon", nm = "dummy")
x <- magclass::add_dimension(x, dim = 1.2, add = "lat", nm = "dummy")
magclass::getItems(x, dim = 1, raw = TRUE) <- paste(mapping$coords, mapping$iso, sep = ".")
magclass::getSets(x)[c("d1.1", "d1.2", "d1.3")] <- c("x", "y", "iso")

return(x)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# One-line description of this awesome package

R package **mrlandcore**, version **1.1.8**
R package **mrlandcore**, version **1.2.0**

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

Expand Down Expand Up @@ -38,7 +38,7 @@ In case of questions / problems please contact Felicitas Beier <beier@pik-potsda

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

Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U (2024). _mrlandcore: One-line description of this awesome package_. R package version 1.1.8, <https://github.com/pik-piam/mrlandcore>.
Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U (2024). _mrlandcore: One-line description of this awesome package_. R package version 1.2.0, <https://github.com/pik-piam/mrlandcore>.

A BibTeX entry for LaTeX users is

Expand All @@ -47,7 +47,7 @@ A BibTeX entry for LaTeX users is
title = {mrlandcore: One-line description of this awesome package},
author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis},
year = {2024},
note = {R package version 1.1.8},
note = {R package version 1.2.0},
url = {https://github.com/pik-piam/mrlandcore},
}
```
29 changes: 29 additions & 0 deletions man/readLPJmL.Rd

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

0 comments on commit b321301

Please sign in to comment.