-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added functions to calculate area on cropland covered by trees and sn…
…v target cropland
- Loading branch information
Patrick von Jeetze
authored and
Patrick von Jeetze
committed
Feb 2, 2024
1 parent
01dc6c9
commit d2cf510
Showing
13 changed files
with
327 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
|
@@ -58,4 +58,4 @@ VignetteBuilder: | |
knitr | ||
Encoding: UTF-8 | ||
LazyData: no | ||
RoxygenNote: 7.3.0 | ||
RoxygenNote: 7.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.