Skip to content

Commit

Permalink
added new peatland input files
Browse files Browse the repository at this point in the history
  • Loading branch information
flohump committed Aug 10, 2023
1 parent 5b6edca commit 0f408d0
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '26521208'
ValidationKey: '26627440'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
92 changes: 21 additions & 71 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Run CI for R using https://eddelbuettel.github.io/r-ci/

name: check

on:
Expand All @@ -8,92 +6,44 @@ on:
pull_request:
branches: [main, master]

env:
USE_BSPM: "true"
_R_CHECK_FORCE_SUGGESTS_: "false"
NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen", "GDPuc", "roxygen2")'

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Bootstrap
run: |
sudo chown runner -R .
sudo locale-gen en_US.UTF-8
sudo add-apt-repository -y ppa:ubuntugis/ppa
curl -OLs https://eddelbuettel.github.io/r-ci/run.sh
chmod 0755 run.sh
./run.sh bootstrap
rm -f bspm_*.tar.gz
- name: Enable r-universe repo, modify bspm integration
run: |
# install packages from https://pik-piam.r-universe.dev and CRAN
echo '
options(repos = c(universe = "https://pik-piam.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
' >> .Rprofile
cat .Rprofile
# modify bspm integration to never install binary builds of PIK CRAN packages
sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site
# need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above)
printf '
local({
expr <- quote({
if (!is.null(repos)) {
noBinaryInstallRPackages <- %s
pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]),
pkgs[pkgs %%in%% noBinaryInstallRPackages])
}
type <- "source"
})
trace(utils::install.packages, expr, print = FALSE)
})
' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null
cat /etc/R/Rprofile.site
- name: Set up Pandoc
uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-pandoc@v2

- name: Set up Python 3.9
uses: actions/setup-python@v4
- uses: r-lib/actions/setup-r@v2
with:
python-version: 3.9
use-public-rspm: true
extra-repositories: "https://rse.pik-potsdam.de/r/packages"

- name: Cache R libraries
if: ${{ !env.ACT }} # skip when running locally via nektos/act
uses: pat-s/always-upload-cache@v3
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: /usr/local/lib/R/
key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }}
restore-keys: |
3-${{ runner.os }}-usr-local-lib-R-
- name: Restore R library permissions
run: |
sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library
- name: Install dependencies
run: |
./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev
./run.sh install_all
./run.sh install_r_binary covr rstudioapi
./run.sh install_r lucode2
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install python dependencies if applicable
run: |
[ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true
[ -f requirements.txt ] && pip install -r requirements.txt || true
- name: Remove bspm integration # to get rid of error when running install.packages
run: |
sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site
cat /etc/R/Rprofile.site
- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9013
rev: v0.3.2.9019
hooks:
- id: parsable-R
- id: deps-in-desc
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: 'mrmagpie: madrat based MAgPIE Input Data Library'
version: 1.35.7
date-released: '2023-07-06'
version: 1.36.0
date-released: '2023-08-10'
abstract: Provides functions for MAgPIE country and cellular input data generation.
authors:
- family-names: Karstens
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: mrmagpie
Title: madrat based MAgPIE Input Data Library
Version: 1.35.7
Date: 2023-07-06
Version: 1.36.0
Date: 2023-08-10
Authors@R: c(
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
40 changes: 40 additions & 0 deletions R/calcPeatland2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' @title calcPeatland2
#' @description This function calculates degraded and intact peatland area at cell level.
#' The function takes degraded and intact peatland area from the Global Peatland Database 2022 (GPD2022) at the
#' national level and downscales the peatland area to grid cell level using gridded peatland area
#' from the Global Peatland Map 2.0 (GPM2)
#' The data has been provided by Alexandra Barthelmes.
#'
#' @return magpie object in cellular resolution
#' @author Florian Humpenoeder
#'
#' @examples
#' \dontrun{
#' calcOutput("Peatland2", aggregate = FALSE)
#' }
#'
#' @importFrom madrat toolAggregate

calcPeatland2 <- function() {
# Country-level data on intact and degraded peatland from Global Peatland Database for 2022 (GPD2022)
gpd2022 <- readSource("GPD2022", convert = TRUE)

# Global Peatland Map 2.0; peatland location and extent; but no information on status peatlands (intact / degraded)
gpm2 <- readSource("GPM2", convert = "onlycorrect")

# Dissag. GPD2022 from country to cell with GPM2 as weight
map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE)
outCell <- toolAggregate(x = mrcommons::toolIso2CellCountries(gpd2022, cells = "lpjcell"), rel = map,
weight = gpm2, dim = 1, from = "iso", to = "coords")
names(dimnames(outCell)) <- c("coords", "t", "d3")
dimnames(outCell) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = getNames(outCell))

description <- "Intact and degraded peatland area (Mha) by land-use type, based GPD 2022 and GPM2.0"

return(list(
x = outCell,
weight = NULL,
unit = "Mha",
description = description,
isocountries = FALSE))
}
57 changes: 57 additions & 0 deletions R/convertGPD2022.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' @title convertGPD2022
#' @description convert GPD2022
#' @param x magpie object provided by the read function
#' @return List of magpie objects with results on iso level, weight, unit and description.
#' @author Florian Humpenoeder
#' @examples
#' \dontrun{
#' readSource("GPD2022", convert = TRUE)
#' }
#'
convertGPD2022 <- function(x) {
# convert to Mha
x <- x / 1000

# create output object for filling
out <- new.magpie(getCells(x), NULL, c("intact", "crop", "past", "forestry", "peatExtract"), fill = 0)

## Merge deeply drained (DD) and shallow drained (SD) peatlands for composite agriculture variables
ag <- setNames(dimSums(x[, , c("agricultureDD", "agricultureSD")], dim = 3), "agriculture")
# use croplandDD and grasslandDD as weight
w <- x[, , c("croplandDD", "grasslandDD")]
# assume 0.5 as weight in case of no information
w[dimSums(w, dim = 3) == 0] <- 0.5
# names
getNames(w) <- c("crop", "past")
# create the mapping
from <- c("agriculture",
"agriculture")
to <- c("crop",
"past")
map <- data.frame(from, to)
# do the disaggregation into cropland and grassland
ag2 <- toolAggregate(ag, map, from = "from", to = "to", dim = 3, weight = w)
# add the disaggregated categories to the existing categories
out[, , "crop"] <- setNames(x[, , "croplandDD", ], "crop") + ag2[, , "crop"]
out[, , "past"] <- setNames(x[, , "grasslandDD", ], "past") + ag2[, , "past"]

## Merge deeply drained (DD) and shallow drained (SD) peatlands used for forestry
out[, , "forestry"] <- dimSums(x[, , c("forestryDD", "forestrySD")], dim = 3)

## peatExtract
out[, , "peatExtract"] <- x[, , "peatExtract"]

## calculate intact peatland area
out[, , "intact"] <- collapseNames(x[, , c("peatlandAreaTotal")]) - dimSums(out, dim = 3)

# dimnames
names(dimnames(out)) <- c("iso", "t", "d3")

# area cannot be smaller than 0
out[out < 0] <- 0

# fill missing countries
out <- toolCountryFill(out, fill = 0)

return(out)
}
17 changes: 17 additions & 0 deletions R/correctGPM2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' @title correctGPM2
#' @description correct peatland area
#' @param x magpie object provided by the read function
#' @return List of magpie objects with results on cellular level, weight, unit and description.
#' @author Florian Humpenoeder
#' @examples
#'
#' \dontrun{
#' readSource("GPM2", convert="onlycorrect")
#' }

correctGPM2 <- function(x) {

x[is.na(x)] <- 0

return(x)
}
3 changes: 3 additions & 0 deletions R/fullCELLULARMAGPIE.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ fullCELLULARMAGPIE <- function(rev = 0.1, dev = "",
calcOutput("Peatland", subtype = "intact", aggregate = "cluster", round = 6,
file = paste0("f58_peatland_intact_", ctype, ".mz"))

calcOutput("Peatland2", aggregate = FALSE, round = 6, file = "f58_peatland_area_0.5.mz")
calcOutput("Peatland2", aggregate = "cluster", round = 6,
file = paste0("f58_peatland_area_", ctype, ".mz"))

# 59 som
calcOutput("SOMinitialsiationPools", aggregate = "cluster", round = 6,
Expand Down
30 changes: 30 additions & 0 deletions R/readGPD2022.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @title readGPD2022
#' @description read x
#' Data from the Global Peatland Database provided by Alexandra Barthelmes.
#' The original xls file has been clean-up manually (country names). Turkey had
#' two identical entries in the original xls file.
#' Sources:
#' "Inventory Reports and National Communications UNFCC 2014",
#' "soil and peatland science",
#' "European Mires Book" ,
#' "own estimates (incl. GIS data)",
#' @return List of magpie objects with results on cellular level, weight, unit and description.
#' @author Florian Humpenoeder
#' @examples
#' \dontrun{
#' readSource("x", convert = "onlycorrect")
#' }
#' @importFrom readxl read_xls

readGPD2022 <- function() {
# read-in xls file Global Peatland Database
x <- read_xls("GPD2022.xls")
x <- x[x$ISO3 != "NA", ]
x <- x[, names(x)[-2]]

# convert to magclass object
x <- as.magpie(x, spatial = 1)
x[is.na(x)] <- 0

return(x)
}
34 changes: 34 additions & 0 deletions R/readGPM2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' @title readGPM2
#' @description read peatland area from GPM2
#' @return List of magpie objects with results on cellular level, weight, unit and description.
#' @author Florian Humpenoeder
#' @examples
#' \dontrun{
#' readSource("GPM2", convert = "onlycorrect")
#' }
#' @importFrom magclass as.magpie

readGPM2 <- function() {
# read-in file
r <- terra::rast("peatMAY22_1x1_mw_RUS30.tif")

# 0.5 deg raster object
r05 <- terra::rast(res = 0.5)

# project r to 0.5 deg raster
r2 <- terra::project(r, r05, method = "sum")

# get cell area
a <- terra::cellSize(r2[[1]], unit = "ha", mask = TRUE) * 1e-6

# get spatial mapping
map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE)

# transform raster to magpie object
x <- as.magpie(terra::extract(a, map[c("lon", "lat")])[, -1], spatial = 1)

# set dimension names
dimnames(x) <- list("coords" = map$coords, "t" = NULL, "d3" = NULL)

return(x)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library

R package **mrmagpie**, version **1.35.7**
R package **mrmagpie**, version **1.36.0**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

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

Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F, Führlich P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.35.7, <URL: https://github.com/pik-piam/mrmagpie>.
Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F, Führlich P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi:10.5281/zenodo.4319612 <https://doi.org/10.5281/zenodo.4319612>, R package version 1.36.0, <https://github.com/pik-piam/mrmagpie>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrmagpie: madrat based MAgPIE Input Data Library},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Führlich},
year = {2023},
note = {R package version 1.35.7},
note = {R package version 1.36.0},
doi = {10.5281/zenodo.4319612},
url = {https://github.com/pik-piam/mrmagpie},
}
Expand Down
Loading

0 comments on commit 0f408d0

Please sign in to comment.