Skip to content

Commit

Permalink
minor revision of calcSNVTargetCropland
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick von Jeetze authored and Patrick von Jeetze committed Feb 5, 2024
1 parent c357913 commit 4f2eb03
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '10509660'
ValidationKey: '10531014'
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.53.2
date-released: '2024-02-02'
version: 0.53.3
date-released: '2024-02-05'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
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: mrland
Title: MadRaT land data package
Version: 0.53.2
Date: 2024-02-02
Version: 0.53.3
Date: 2024-02-05
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down
12 changes: 8 additions & 4 deletions R/calcSNVTargetCropland.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title calcSNVTargetCropland
#'
#' @description Returns cropland area (Mha) that requires relocation in response of increasing
#' semi-natural vegetation in farmed landscapes.
#' @description Returns cropland area (Mha) that requires relocation in response of
#' maintaining 20% or 50% semi-natural vegetation in farmed landscapes.

#' @param maginput Whether data should be corrected to align with cropland
#' initialised in MAgPIE.
Expand Down Expand Up @@ -33,7 +33,10 @@ calcSNVTargetCropland <- function(maginput = TRUE, cells = "magpiecell") {

# SNV target cropland area is corrected to make sure that it is not
# larger than cropland area reported by LUH
out <- pmin(targetCropland, luh[, , "crop"])
for (i in seq(ndata(targetCropland))) {
targetCropland[, , i] <- pmin(targetCropland[, , i], luh[, , "crop"])
}
out <- targetCropland
} else {
out <- targetCropland
}
Expand All @@ -50,7 +53,8 @@ calcSNVTargetCropland <- function(maginput = TRUE, cells = "magpiecell") {
unit = "Mha",
description = paste(
"Cropland area (Mha) that requires relocation in",
"response of increasing semi-natural vegetation in farmed landscapes"
"response of a share of 20% and 50% semi-natural vegetation",
"in farmed landscapes"
),
isocountries = FALSE
))
Expand Down
37 changes: 21 additions & 16 deletions R/readCopernicus.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,38 @@ readCopernicus <- function(subtype = "CroplandTreecover") {
terraOptions(tempdir = local_tempdir(tmpdir = getConfig("tmpfolder")), todisk = TRUE, memfrac = 0.5)
defer(terraOptions(tempdir = tempdir()))

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

# read data
if (subtype == "CroplandTreecover") {
r <- rast(paste0(
"./land_cover/copernicus_lc100_2019_crop_treecover_area_0.5.tif"
))
name <- "CropTreecoverArea"
r <- rast("./land_cover/copernicus_lc100_2019_crop_treecover_area_0.5.tif")
nm <- "CropTreecoverArea"

# transform raster to magpie object
out <- as.magpie(extract(r, map[c("lon", "lat")], ID = FALSE), spatial = 1)
} else if (subtype == "SNVTargetCropland") {
r <- rast(paste0(
"./land_cover/copernicus_lc100_2019_SNV50_target_crop_area_0.5.tif"
))
name <- "SNVTargetCropland"
r <- c(
rast("./land_cover/copernicus_lc100_2019_SNV20_target_crop_area_0.5.tif"),
rast("./land_cover/copernicus_lc100_2019_SNV50_target_crop_area_0.5.tif")
)
nm <- c("SNV20TargetCropland", "SNV50TargetCropland")
names(r) <- nm

# transform raster to magpie object
out <- mbind(
as.magpie(extract(r[["SNV20TargetCropland"]], map[c("lon", "lat")], ID = FALSE), spatial = 1),
as.magpie(extract(r[["SNV50TargetCropland"]], map[c("lon", "lat")], ID = FALSE), spatial = 1)
)
} 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
"data" = nm
)

return(out)
Expand Down
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.53.2**
R package **mrland**, version **0.53.3**

[![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.53.2, <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.3, <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.53.2},
note = {R package version 0.53.3},
doi = {10.5281/zenodo.3822083},
url = {https://github.com/pik-piam/mrland},
}
Expand Down
4 changes: 2 additions & 2 deletions man/calcSNVTargetCropland.Rd

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

0 comments on commit 4f2eb03

Please sign in to comment.