From 6474ce037ed64358bccf83295fdcc71f2ef6caae Mon Sep 17 00:00:00 2001 From: florianh Date: Wed, 3 Jul 2024 18:41:10 +0200 Subject: [PATCH] added 500m peatland map --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/readGPM2.R | 55 +++++++++++++++++++++++++++++++++++-------------- README.md | 6 +++--- man/readGPM2.Rd | 5 ++++- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 81c07453..f0610e53 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '30097872' +ValidationKey: '30119291' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package .* was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 968fec85..7489b858 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: 'mrmagpie: madrat based MAgPIE Input Data Library' -version: 1.51.2 -date-released: '2024-07-02' +version: 1.51.3 +date-released: '2024-07-03' abstract: Provides functions for MAgPIE country and cellular input data generation. authors: - family-names: Karstens diff --git a/DESCRIPTION b/DESCRIPTION index 661cf11a..229985d5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrmagpie Title: madrat based MAgPIE Input Data Library -Version: 1.51.2 -Date: 2024-07-02 +Version: 1.51.3 +Date: 2024-07-03 Authors@R: c( person("Kristine", "Karstens", , "karstens@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/R/readGPM2.R b/R/readGPM2.R index f24a4477..d7344aa7 100644 --- a/R/readGPM2.R +++ b/R/readGPM2.R @@ -1,5 +1,6 @@ #' @title readGPM2 #' @description read peatland area from GPM2 +#' @param type resolution ("1km" or "500m") #' @return List of magpie objects with results on cellular level, weight, unit and description. #' @author Florian Humpenoeder #' @examples @@ -8,30 +9,54 @@ #' } #' @importFrom magclass as.magpie #' @importFrom mstools toolGetMappingCoord2Country -readGPM2 <- function() { +readGPM2 <- function(type = "1km") { previousOptions <- terra::terraOptions(print = FALSE) terra::terraOptions(tempdir = withr::local_tempdir(tmpdir = getConfig("tmpfolder")), todisk = FALSE, memfrac = 0.5) withr::defer(do.call(terra::terraOptions, previousOptions)) - # read-in file - r <- terra::rast("peatMAY22_1x1_mw_RUS30.tif") + if (type == "1km") { + # read-in file + r <- terra::rast("peatMAY22_1x1_mw_RUS30.tif") - # choose only 1st layer - r1 <- terra::segregate(r, other = NA) - r1 <- r1[[1]] + # choose only 1st layer + r1 <- terra::segregate(r, other = NA) + r1 <- r1[[1]] - # project r to lon/lat - r2 <- terra::project(r1, "+proj=longlat +datum=WGS84", method = "near") + # project r to lon/lat + r2 <- terra::project(r1, "+proj=longlat +datum=WGS84", method = "near") - # get cell area - a <- terra::cellSize(r2, unit = "ha", mask = TRUE) - a <- a * 1e-6 + # get cell area + a <- terra::cellSize(r2, unit = "ha", mask = TRUE) + a <- a * 1e-6 - # project or aggregate to 0.5 degree - # use terra::aggregate because terra::project(a, terra::rast(res = 0.5), method = "sum") is not working - # on the cluster (method = "sum" is the problem) - r3 <- terra::aggregate(a, fact = 48, fun = "sum", na.rm = TRUE) + # project or aggregate to 0.5 degree + # use terra::aggregate because terra::project(a, terra::rast(res = 0.5), method = "sum") is not working + # on the cluster (method = "sum" is the problem) + r3 <- terra::aggregate(a, fact = 48, fun = "sum", na.rm = TRUE) + + } else if (type == "500m") { + # read-in file + r <- terra::vrt(list.files("500m", full.names = TRUE, pattern = ".tif$"), "500m/GPM2.0_500m.vrt", overwrite = TRUE) + crs(r) <- "+proj=moll" + + # choose only 1st layer + r1 <- terra::segregate(r, other = NA) + r1 <- r1[[1]] + + # project r to lon/lat + r2 <- terra::project(r1, "+proj=longlat +datum=WGS84", method = "near") + + # get cell area + a <- terra::cellSize(r2, unit = "ha", mask = TRUE) + a <- a * 1e-6 + + # project or aggregate to 0.5 degree + # use terra::aggregate because terra::project(a, terra::rast(res = 0.5), method = "sum") is not working + # on the cluster (method = "sum" is the problem) + r3 <- terra::aggregate(a, fact = 96, fun = "sum", na.rm = TRUE) + + } # get spatial mapping map <- toolGetMappingCoord2Country(pretty = TRUE) diff --git a/README.md b/README.md index c202248f..17e44894 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **1.51.2** +R package **mrmagpie**, version **1.51.3** [![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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens , R package version 1.51.2, . +Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2024). _mrmagpie: madrat based MAgPIE Input Data Library_. doi:10.5281/zenodo.4319612 , R package version 1.51.3, . A BibTeX entry for LaTeX users is @@ -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 Alexandre Köberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer}, year = {2024}, - note = {R package version 1.51.2}, + note = {R package version 1.51.3}, url = {https://github.com/pik-piam/mrmagpie}, doi = {10.5281/zenodo.4319612}, } diff --git a/man/readGPM2.Rd b/man/readGPM2.Rd index 96c8e360..ee685b64 100644 --- a/man/readGPM2.Rd +++ b/man/readGPM2.Rd @@ -4,7 +4,10 @@ \alias{readGPM2} \title{readGPM2} \usage{ -readGPM2() +readGPM2(type = "1km") +} +\arguments{ +\item{type}{resolution ("1km" or "500m")} } \value{ List of magpie objects with results on cellular level, weight, unit and description.