Skip to content

Commit

Permalink
added functions to calculate area on cropland covered by trees and sn…
Browse files Browse the repository at this point in the history
…v target cropland
  • Loading branch information
Patrick von Jeetze authored and Patrick von Jeetze committed Feb 2, 2024
1 parent 01dc6c9 commit d2cf510
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '10409831'
ValidationKey: '10470150'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
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: 'mrland: MadRaT land data package'
version: 0.52.7
date-released: '2024-01-31'
version: 0.53.0
date-released: '2024-02-02'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrland
Title: MadRaT land data package
Version: 0.52.7
Date: 2024-01-31
Version: 0.53.0
Date: 2024-02-02
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down Expand Up @@ -58,4 +58,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: no
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
55 changes: 55 additions & 0 deletions R/calcCroplandTreecover.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' @title calcCroplandTreecover
#'
#' @description Returns area on cropland covered by trees (Mha).

#' @param maginput Whether data should be corrected to align with cropland
#' initialised in MAgPIE.
#' @param cells magpiecell (59199 cells) or lpjcell (67420 cells)
#'
#' @return List with a magpie object
#' @author Patrick v. Jeetze
#' @seealso
#' \code{\link{readCopernicus}}
#'
#' @examples
#' \dontrun{
#' calcOutput("calcCroplandTreecover", aggregate = FALSE)
#' }
#'
#' @importFrom mrcommons toolCoord2Isocell
#'
calcCroplandTreecover <- function(maginput = TRUE, cells = "magpiecell") {
treecover <- readSource("Copernicus", subtype = "CroplandTreecover", convert = "onlycorrect")

if (maginput) {
luh <- calcOutput("LUH2v2",
landuse_types = "magpie", aggregate = FALSE,
cellular = TRUE, cells = "lpjcell", irrigation = FALSE,
selectyears = "y2015"
)
getYears(luh) <- NULL
getCells(luh) <- getCells(treecover)

# cropland treecover area is corrected to make sure that it is not
# larger than cropland area reported by LUH
treecover <- pmax(treecover, luh[, , "crop"])
} else {
out <- treecover
}

if (cells == "magpiecell") {
out <- toolCoord2Isocell(out)
} else if (cells != "lpjcell") {
stop("Please specify cells argument")
}

return(list(
x = out,
weight = NULL,
unit = "Mha",
description = paste(
"Cropland area covered by trees"
),
isocountries = FALSE
))
}
57 changes: 57 additions & 0 deletions R/calcSNVTargetCropland.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' @title calcSNVTargetCropland
#'
#' @description Returns cropland area (Mha) that requires relocation in response of increasing
#' semi-natural vegetation in farmed landscapes.

#' @param maginput Whether data should be corrected to align with cropland
#' initialised in MAgPIE.
#' @param cells magpiecell (59199 cells) or lpjcell (67420 cells)
#'
#' @return List with a magpie object
#' @author Patrick v. Jeetze
#' @seealso
#' \code{\link{readCopernicus}}
#'
#' @examples
#' \dontrun{
#' calcOutput("calcSNVTargetCropland", aggregate = FALSE)
#' }
#'
#' @importFrom mrcommons toolCoord2Isocell
#'
calcSNVTargetCropland <- function(maginput = TRUE, cells = "magpiecell") {
targetCropland <- readSource("Copernicus", subtype = "SNVTargetCropland", convert = "onlycorrect")

if (maginput) {
luh <- calcOutput("LUH2v2",
landuse_types = "magpie", aggregate = FALSE,
cellular = TRUE, cells = "lpjcell", irrigation = FALSE,
selectyears = "y2015"
)
getYears(luh) <- NULL
getCells(luh) <- getCells(targetCropland)

# SNV target cropland area is corrected to make sure that it is not
# larger than cropland area reported by LUH
targetCropland <- pmax(targetCropland, luh[, , "crop"])
} else {
out <- targetCropland
}

if (cells == "magpiecell") {
out <- toolCoord2Isocell(out)
} else if (cells != "lpjcell") {
stop("Please specify cells argument")
}

return(list(
x = out,
weight = NULL,
unit = "Mha",
description = paste(
"Cropland area (Mha) that requires relocation in",
"response of increasing semi-natural vegetation in farmed landscapes"
),
isocountries = FALSE
))
}
19 changes: 19 additions & 0 deletions R/correctCopernicus.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' @title correctCopernicus
#' @description correct Copernicus data.
#' @return magpie object on cellular level
#' @param x magpie object provided by the read function
#' @author Patrick v. Jeetze
#' @seealso
#' \code{\link{readCopernicus}}
#' @examples
#' \dontrun{
#' readSource("Copernicus", convert = "onlycorrect")
#' }
#'
#' @importFrom madrat toolConditionalReplace

correctCopernicus <- function(x) {
x <- toolConditionalReplace(x, conditions = "is.na()", replaceby = 0)

return(x)
}
61 changes: 61 additions & 0 deletions R/readCopernicus.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' @title readCopernicus
#' @description Reads either information on the area on cropland covered by trees or
#' information the cropland area that requires relocation in response of increasing
#' semi-natural vegetation in farmed landscapes. The data was derived from high resolution
#' land cover information (LC100) from the Copernicus Global Land Service.
#' (https://zenodo.org/records/3939050)
#'
#' @param subtype For cropland area covered by trees choose \code{"CroplandTreecover"}.
#' For cropland area requiring relocation in response to increasing SNV choose \code{"SNVTargetCropland"}.
#'
#' @return Returns magpie objects with cropland area covered by trees or cropland area
#' requiring relocation in order to increase SNV in farmed landscapes.
#'
#' @author Patrick v. Jeetze
#'
#' @examples
#' \dontrun{
#' readSource("Copernicus", subtype = "CroplandTreecover", convert = "onlycorrect")
#' }
#'
#' @importFrom terra rast terraOptions
#' @importFrom withr local_tempdir defer
#' @importFrom mrcommons toolGetMappingCoord2Country
#'

readCopernicus <- function(subtype = "CroplandTreecover") {
# Set up 'terra' options
terraOptions(tempdir = local_tempdir(tmpdir = getConfig("tmpfolder")), todisk = TRUE, memfrac = 0.5)
defer(terraOptions(tempdir = tempdir()))

# read data
if (subtype == "CroplandTreecover") {
r <- rast(paste0(
"./land_cover/copernicus_lc100_2019_crop_treecover_area_0.5.tif"
))
name <- "CropTreecoverArea"
} else if (subtype == "SNVTargetCropland") {
r <- rast(paste0(
"./land_cover/copernicus_lc100_2019_SNV50_target_crop_area_0.5.tif"
))
name <- "SNVTargetCropland"
} else {
stop("Please select an existing subtype")
}

# get spatial mapping
map <- toolGetMappingCoord2Country(pretty = TRUE)
# transform raster to magpie object
out <- mbind(
as.magpie(extract(r, map[c("lon", "lat")], ID = FALSE), spatial = 1)
)

# set dimension names
dimnames(out) <- list(
"x.y.iso" = paste(map$coords, map$iso, sep = "."),
"t" = NULL,
"data" = name
)

return(out)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRaT land data package

R package **mrland**, version **0.52.7**
R package **mrland**, version **0.53.0**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

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

Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.52.7, <https://github.com/pik-piam/mrland>.
Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.53.0, <https://github.com/pik-piam/mrland>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrland: MadRaT land data package},
author = {Jan Philipp Dietrich and Abhijeet Mishra and Isabelle Weindl and Benjamin Leon Bodirsky and Xiaoxi Wang and Lavinia Baumstark and Ulrich Kreidenweis and David Klein and Nele Steinmetz and David Chen and Florian Humpenoeder and Patrick {von Jeetze} and Stephen Wirth and Felicitas Beier and David Hoetten},
year = {2024},
note = {R package version 0.52.7},
note = {R package version 0.53.0},
doi = {10.5281/zenodo.3822083},
url = {https://github.com/pik-piam/mrland},
}
Expand Down
32 changes: 32 additions & 0 deletions man/calcCroplandTreecover.Rd

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

33 changes: 33 additions & 0 deletions man/calcSNVTargetCropland.Rd

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

29 changes: 29 additions & 0 deletions man/correctCopernicus.Rd

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

1 change: 0 additions & 1 deletion man/mrland-package.Rd

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

32 changes: 32 additions & 0 deletions man/readCopernicus.Rd

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

0 comments on commit d2cf510

Please sign in to comment.