From d2cf510da3c880dd3e02970d333cc73a7851eab5 Mon Sep 17 00:00:00 2001 From: Patrick von Jeetze Date: Fri, 2 Feb 2024 14:26:48 +0100 Subject: [PATCH] added functions to calculate area on cropland covered by trees and snv target cropland --- .buildlibrary | 2 +- CITATION.cff | 4 +-- DESCRIPTION | 6 ++-- R/calcCroplandTreecover.R | 55 ++++++++++++++++++++++++++++++++ R/calcSNVTargetCropland.R | 57 +++++++++++++++++++++++++++++++++ R/correctCopernicus.R | 19 +++++++++++ R/readCopernicus.R | 61 ++++++++++++++++++++++++++++++++++++ README.md | 6 ++-- man/calcCroplandTreecover.Rd | 32 +++++++++++++++++++ man/calcSNVTargetCropland.Rd | 33 +++++++++++++++++++ man/correctCopernicus.Rd | 29 +++++++++++++++++ man/mrland-package.Rd | 1 - man/readCopernicus.Rd | 32 +++++++++++++++++++ 13 files changed, 327 insertions(+), 10 deletions(-) create mode 100644 R/calcCroplandTreecover.R create mode 100644 R/calcSNVTargetCropland.R create mode 100644 R/correctCopernicus.R create mode 100644 R/readCopernicus.R create mode 100644 man/calcCroplandTreecover.Rd create mode 100644 man/calcSNVTargetCropland.Rd create mode 100644 man/correctCopernicus.Rd create mode 100644 man/readCopernicus.Rd diff --git a/.buildlibrary b/.buildlibrary index 69dcdf63..b0da3bac 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -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' diff --git a/CITATION.cff b/CITATION.cff index 9230cb68..1f237a66 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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 diff --git a/DESCRIPTION b/DESCRIPTION index 956f0402..839dbe5e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "dietrich@pik-potsdam.de", role = c("aut", "cre")), person("Abhijeet", "Mishra", role = "aut"), @@ -58,4 +58,4 @@ VignetteBuilder: knitr Encoding: UTF-8 LazyData: no -RoxygenNote: 7.3.0 +RoxygenNote: 7.3.1 diff --git a/R/calcCroplandTreecover.R b/R/calcCroplandTreecover.R new file mode 100644 index 00000000..56b539eb --- /dev/null +++ b/R/calcCroplandTreecover.R @@ -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 + )) +} diff --git a/R/calcSNVTargetCropland.R b/R/calcSNVTargetCropland.R new file mode 100644 index 00000000..9a9a2c46 --- /dev/null +++ b/R/calcSNVTargetCropland.R @@ -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 + )) +} diff --git a/R/correctCopernicus.R b/R/correctCopernicus.R new file mode 100644 index 00000000..fd337b3d --- /dev/null +++ b/R/correctCopernicus.R @@ -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) +} diff --git a/R/readCopernicus.R b/R/readCopernicus.R new file mode 100644 index 00000000..bd46c625 --- /dev/null +++ b/R/readCopernicus.R @@ -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) +} diff --git a/README.md b/README.md index b30286c0..4e50d3ee 100644 --- a/README.md +++ b/README.md @@ -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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich , R package version 0.52.7, . +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 , R package version 0.53.0, . A BibTeX entry for LaTeX users is @@ -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}, } diff --git a/man/calcCroplandTreecover.Rd b/man/calcCroplandTreecover.Rd new file mode 100644 index 00000000..f557f203 --- /dev/null +++ b/man/calcCroplandTreecover.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcCroplandTreecover.R +\name{calcCroplandTreecover} +\alias{calcCroplandTreecover} +\title{calcCroplandTreecover} +\usage{ +calcCroplandTreecover(maginput = TRUE, cells = "magpiecell") +} +\arguments{ +\item{maginput}{Whether data should be corrected to align with cropland +initialised in MAgPIE.} + +\item{cells}{magpiecell (59199 cells) or lpjcell (67420 cells)} +} +\value{ +List with a magpie object +} +\description{ +Returns area on cropland covered by trees (Mha). +} +\examples{ +\dontrun{ +calcOutput("calcCroplandTreecover", aggregate = FALSE) +} + +} +\seealso{ +\code{\link{readCopernicus}} +} +\author{ +Patrick v. Jeetze +} diff --git a/man/calcSNVTargetCropland.Rd b/man/calcSNVTargetCropland.Rd new file mode 100644 index 00000000..3ae38dca --- /dev/null +++ b/man/calcSNVTargetCropland.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcSNVTargetCropland.R +\name{calcSNVTargetCropland} +\alias{calcSNVTargetCropland} +\title{calcSNVTargetCropland} +\usage{ +calcSNVTargetCropland(maginput = TRUE, cells = "magpiecell") +} +\arguments{ +\item{maginput}{Whether data should be corrected to align with cropland +initialised in MAgPIE.} + +\item{cells}{magpiecell (59199 cells) or lpjcell (67420 cells)} +} +\value{ +List with a magpie object +} +\description{ +Returns cropland area (Mha) that requires relocation in response of increasing +semi-natural vegetation in farmed landscapes. +} +\examples{ +\dontrun{ +calcOutput("calcSNVTargetCropland", aggregate = FALSE) +} + +} +\seealso{ +\code{\link{readCopernicus}} +} +\author{ +Patrick v. Jeetze +} diff --git a/man/correctCopernicus.Rd b/man/correctCopernicus.Rd new file mode 100644 index 00000000..9ca6eeec --- /dev/null +++ b/man/correctCopernicus.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/correctCopernicus.R +\name{correctCopernicus} +\alias{correctCopernicus} +\title{correctCopernicus} +\usage{ +correctCopernicus(x) +} +\arguments{ +\item{x}{magpie object provided by the read function} +} +\value{ +magpie object on cellular level +} +\description{ +correct Copernicus data. +} +\examples{ +\dontrun{ +readSource("Copernicus", convert = "onlycorrect") +} + +} +\seealso{ +\code{\link{readCopernicus}} +} +\author{ +Patrick v. Jeetze +} diff --git a/man/mrland-package.Rd b/man/mrland-package.Rd index eeb097ba..954ce122 100644 --- a/man/mrland-package.Rd +++ b/man/mrland-package.Rd @@ -3,7 +3,6 @@ \docType{package} \name{mrland-package} \alias{mrland-package} -\alias{_PACKAGE} \alias{mrland} \title{MadRaT land data package} \description{ diff --git a/man/readCopernicus.Rd b/man/readCopernicus.Rd new file mode 100644 index 00000000..cc4766b4 --- /dev/null +++ b/man/readCopernicus.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readCopernicus.R +\name{readCopernicus} +\alias{readCopernicus} +\title{readCopernicus} +\usage{ +readCopernicus(subtype = "CroplandTreecover") +} +\arguments{ +\item{subtype}{For cropland area covered by trees choose \code{"CroplandTreecover"}. +For cropland area requiring relocation in response to increasing SNV choose \code{"SNVTargetCropland"}.} +} +\value{ +Returns magpie objects with cropland area covered by trees or cropland area +requiring relocation in order to increase SNV in farmed landscapes. +} +\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) +} +\examples{ +\dontrun{ +readSource("Copernicus", subtype = "CroplandTreecover", convert = "onlycorrect") +} + +} +\author{ +Patrick v. Jeetze +}