LPJmL Data

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

LPJmL Data r if(!knitr::is_latex_output()){"💾"} is an lpjmlkit module that groups around the data class LPJmLData and aims to facilitate reading and processing of LPJmL inputs and outputs by combining the raw data with available meta data (meta files, header files or manually) to avoid a large overhead. It further enables easy subsetting, transformations and basic statistics of the data and allows export to common data formats.

 

Overview

LPJmL Data first requires reading LPJmL input or output data into R by applying the read_io function (1). The returned object is of class LPJmLData (2), for which basic statistics can be calculated (3), the inner data can be modified (4), or exported (5) to common data formats.

1. r if (!knitr::is_latex_output()) {"📖"} Read function read_io\

\ read_io is a generic function to read LPJmL input and output files. It currently supports three different file formats, "meta", "clm" and "raw":

 

2. r if (!knitr::is_latex_output()) {"📁"} Data class LPJmLData\

\ read_io returns an object of an R6 class LPJmLData with two main attributes, $data and $meta:

3. r if (!knitr::is_latex_output()) {"📈"} Basic statistics of LPJmLData objects\

\ To get an overview of the data, LPJmLData supports the usage of the base functions: length(), dim(), dimension(), summary() and plot(). More methods can be added in the future.

# Self print; also via print(runoff).
runoff
# $meta |>
#   .$sim_name "lu_cf"
#   .$variable "runoff"
#   .$descr "monthly runoff"
#   .$unit "mm/month"
#   .$nbands 1
#   .$nyear 111
#   .$nstep 12
#   .$timestep 1
#   .$ncell 67420
#   .$cellsize_lon 0.5
#   .$cellsize_lat 0.5
# Note: not printing all meta data, use $meta to get all.
# $data |>
#   dimnames() |>
#     .$cell  "0" "1" "2" "3" ... "67419"
#     .$time  ""1901-01-31" "1901-02-28" "1901-03-31" "1901-04-30" ... "2011-12-31"
#     .$band  "1"
# $summary()
#        1
#  Min.   :   0.0000
#  1st Qu.:   0.0619
#  Median :   4.4320
#  Mean   :  28.7658
#  3rd Qu.:  27.5627
#  Max.   :2840.9602
# Note: summary is not weighted by grid area.

# Return the dimension length of $data array; dimnames function is also available.
dim(runoff)
#  cell  time  band
# 67420  1332     1

# Plot as maps or time series, depending on the dimensions.
plot(runoff)

 

4. r if (!knitr::is_latex_output()) {"✏"} Modify LPJmLData objects\

\ Each LPJmLData object comes with a bundle of methods to modify its state: add_grid(), transform() and subset().

# Common R notation (overwriting the original object) runoff <- add_grid(runoff)

# Use the read_io arguments if a grid file cannot be detected automatically. runoff <- add_grid(runoff, "./output/grid.clm") ```

# Transform into year and month dimensions (day not available for monthly # runoff) runoff <- transform(runoff, to = "year_month_day") runoff # [...] # $data |> # dimnames() |> # .$lat "-55.75" "-55.25" "-54.75" "-54.25" ... "83.75" # .$lon "-179.75" "-179.25" "-178.75" "-178.25" ... "179.75" # .$month "1" "2" "3" "4" ... "12" # .$year "1901" "1902" "1903" "1904" ... "2011" # .$band "1" # [...]

# Transform back to original dimensions. runoff <- transform(runoff, to = c("cell", "time")) runoff # [...] # $data |> # dimnames() |> # .$cell "0" "1" "2" "3" ... "67419" # .$time "1901-01-31" "1901-02-28" "1901-03-31" "1901-04-30" ... "2100-12-31" # .$band "1" # [...] ```

# Subset by indices runoff <- subset(runoff, cell = 28697:28700) runoff # $meta |> # .$nyear 1 # .$ncell 4 # .$subset TRUE # [...] # Note: not printing all meta data, use $meta to get all. # $data |> # dimnames() |> # .$cell "28696" "28697" "28698" "28699" # .$time "1991-05-31" # .$band "1" # [...] ```  

5. r if (!knitr::is_latex_output()) {"&#128230;"} Export LPJmLData objects\

\ Finally, LPJmLData objects can be exported into common R data formats: array, tibble, raster and terra.
More export methods can be added in the future.

# Export the first time step as a terra SpatRaster object. as_terra(runoff, subset = list(time = 1)) # class : SpatRaster # dimensions : 280, 720, 1 (nrow, ncol, nlyr) # resolution : 0.5, 0.5 (x, y) # extent : -180, 180, -56, 84 (xmin, xmax, ymin, ymax) # coord. ref. : lon/lat WGS 84 (EPSG:4326) # source : memory # name : runoff # min value : -1.682581e-13 # max value : 6.718747e+02 # unit : mm/month # nolint:commented_code_linter.

# Export the first 4 times step as a RasterBrick object. as_raster(runoff, subset = list(time = 1:4)) # class : RasterBrick # dimensions : 280, 720, 201600, 4 (nrow, ncol, ncell, nlayers) # resolution : 0.5, 0.5 (x, y) # extent : -180, 180, -56, 84 (xmin, xmax, ymin, ymax) # crs : +proj=longlat +datum=WGS84 +no_defs # nolint:commented_code_linter. # source : memory # names : X1901.01.31, X1901.02.28, X1901.03.31, X1901.04.30 # min values : -1.682581e-13, -1.750495e-13, -2.918900e-13, -1.516298e-13 # max values : 671.8747, 785.2363, 828.2853, 987.4359

# Export the first 4 time steps as a terra SpatRaster object. as_terra(runoff, subset = list(time = 1:4)) # class : SpatRaster # dimensions : 280, 720, 4 (nrow, ncol, nlyr) # resolution : 0.5, 0.5 (x, y) # extent : -180, 180, -56, 84 (xmin, xmax, ymin, ymax) # coord. ref. : lon/lat WGS 84 (EPSG:4326) # source : memory # names : 1901-01-31, 1901-02-28, 1901-03-31, 1901-04-30 # min values : -1.682581e-13, -1.750495e-13, -2.918900e-13, -1.516298e-13 # max values : 6.718747e+02, 7.852363e+02, 8.282853e+02, 9.874359e+02 # unit : mm/month, mm/month, mm/month, mm/month # time (days) : 1901-01-31 to 1901-04-30 ```  

Miscellaneous \

\ More helpful functionality included with LPJmL Data:

   

Usage

library(lpjmlkit)

1. Example Global Trend in net primary productivity (NPP) over the years

 

npp <- read_io(filename = "./output/npp.bin.json",
               subset = list(year = 1970:2011))

# Transform "time" into "year" and "month" dimensions.
npp$transform(to = "year_month_day")

# Plot timeseries with aggregated cell and month dimensions. Note that spatial
# aggregation across cells is not area-weighted.
plot(npp,
     aggregate = list(cell = mean, month = sum))

# Also available as data array.
global_npp_trend <- as_array(npp,
                             aggregate = list(cell = mean, month = sum))

2. Example Runoff in northern hemisphere during summertime

 

runoff <- read_io(filename = "./output/runoff.bin.json",
               subset = list(year = 2002:2011))

# Usage of pipe operator operator |> (%>% via package magrittr R version < 4.1)
runoff |>
  # Transform the time and space dimensions ...
  transform(to = c("year_month_day", "lon_lat")) |>
  # ... to subset summer months as well as northern hemisphere (positive)
  #   latitudes.
  subset(month = 6:9,
         lat = seq(0.25, 83.75, by = 0.5)) |>
  # for plotting sum up summer month and take the average over the years
  plot(aggregate = list(year = mean, month = sum))

3. Example Gross primary productivity (GPP) per latitude

 

gpp <- read_io(filename = "./output/gpp.bin.json",
               subset = list(year = 2002:2011))

# Transform into lon_lat format.
gpp$transform(to = "lon_lat")

# Plot GPP per latitude.
plot(gpp, aggregate = list(time = mean, lon = mean))

4. Example CFT fractions for area around Potsdam

 

# Coordinates for cells around Potsdam.
coordinates <- tibble::tibble(lat = c(52.25, 52.400922, 53.25),
                      lon = c(12.75, 13.03638, 12.75))

# Complete pipe notation, from reading to plotting data.
read_io(
  filename = glue("./cftfrac.bin.json"),
  subset = list(year = 2000:2018)
) |>
  transform(to = "lon_lat") |>
  # Special case for subsetting of lat and lon pairs
  subset(coords = coordinates) |>
  # Mean across spatial dimensions
  plot(aggregate = list(lon = mean, lat = mean))

   

Notes & tips

  1. LPJmLData and LPJmLMetaData objects are closed environments, each of an R6 class, that function as a data container.
    Do not replicate R6 objects like
    r my_copy <- lpjml_data # Instead use: my_copy <- lpjml_data$clone(deep = TRUE) Otherwise, my_copy and lpjml_data point to the same environment, and any subsetting or transformation methods applied to my_copy will also affect lpjml_data.

  2. Do not try to manually overwrite either the $data or any $meta data attributes within LPJmLData objects. It is either not possible or can mess up the integrity of the object. Methods surrounded by double underscores ($.__<method>__) or attributes surrounded by underscores ($._<attribute>_) are only for low-level package development and should not be used by users for their data handling.

  3. When performance is important, choose R6 method notation runoff$transform(to = "lon_lat") over common R notation transform(runoff, to = "lon_lat").

  4. The "meta" format is only supported by recent LPJmL versions. When comparing older (< LPJmL version 5.3) output data with LPJmL 5.3 output data it can be useful to combine meta ("output_metafile" : true) with the header file format ("fmt": "clm"), which has been supported since LPJmL version 4, for simplification of process pipelines.



Try the lpjmlkit package in your browser

Any scripts or data that you put into this service are public.

lpjmlkit documentation built on March 31, 2023, 9:35 p.m.