From f86fe2e9d9a7097d9c0d9d7cf3ec2bccb634c502 Mon Sep 17 00:00:00 2001 From: florianh Date: Tue, 22 Oct 2024 21:32:58 +0200 Subject: [PATCH] added disaggregation of other land to reporting --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- NAMESPACE | 1 + R/OtherLand.R | 50 +++++++++++++++++++++++++++++++++++++++++++++++ R/reportLandUse.R | 8 +++++++- README.md | 6 +++--- man/OtherLand.Rd | 31 +++++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 R/OtherLand.R create mode 100644 man/OtherLand.Rd diff --git a/.buildlibrary b/.buildlibrary index 47d9dc0..c60f371 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '426562270' +ValidationKey: '426603598' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 3233411..1646d7d 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: 'magpie4: MAgPIE outputs R package for MAgPIE version 4.x' -version: 2.13.10 -date-released: '2024-10-21' +version: 2.13.11 +date-released: '2024-10-22' abstract: Common output routines for extracting results from the MAgPIE framework (versions 4.x). authors: diff --git a/DESCRIPTION b/DESCRIPTION index 0e2541a..fea3f42 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: magpie4 Title: MAgPIE outputs R package for MAgPIE version 4.x -Version: 2.13.10 -Date: 2024-10-21 +Version: 2.13.11 +Date: 2024-10-22 Authors@R: c( person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Florian", "Humpenoeder", , "humpenoeder@pik-potsdam.de", role = "aut"), diff --git a/NAMESPACE b/NAMESPACE index 96a31dd..081ac9b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,6 +31,7 @@ export(NitrogenBudget) export(NitrogenBudgetNonagland) export(NitrogenBudgetPasture) export(NitrogenBudgetWithdrawals) +export(OtherLand) export(PeatlandArea) export(PeatlandEmissions) export(PlantationEstablishment) diff --git a/R/OtherLand.R b/R/OtherLand.R new file mode 100644 index 0000000..c8b607c --- /dev/null +++ b/R/OtherLand.R @@ -0,0 +1,50 @@ +#' @title OtherLand +#' @description Disaggregation of other land into initial, restored and recovered land based on a MAgPIE gdx file +#' +#' @export +#' +#' @param gdx GDX file +#' @param level Level of regional aggregation; "cell", "reg" (regional), "glo" (global), "regglo" (regional and global) or any aggregation level defined in superAggregate. In addition "climate" for the 3 climate regions tropical, temperate and boreal is available. +#' @details initial, restored and recovered land +#' @return Other land area in Mha +#' @author Florian Humpenoeder +#' @importFrom magclass dimSums collapseNames +#' @importFrom luscale superAggregate +#' @examples +#' +#' \dontrun{ +#' x <- PeatlandArea(gdx) +#' } + +OtherLand <- function(gdx, level="reg"){ + + a <- dimSums(readGDX(gdx,"ov_land_other",select=list(type="level"),react = "silent"),dim="othertype35") + b <- readGDX(gdx,"pm_land_conservation",react = "silent") + x <- m_yeardiff(gdx) + x[,1,] <- 5 + ac <- readGDX(gdx,"ac") + ac_est <- new.magpie("GLO",getYears(x),ac,fill=0) + for(t in getYears(x)) { + ac_est[,t,getNames(ac_est[,t,])[1:(x[,t,]/5)]] <- 1 + } + ac_sub <- 1 - ac_est + ac_sub[,,"acx"] <- 0 + + restoredEst <- collapseDim(b[,,"other.restore"]) + restoredSub <- collapseDim(as.magpie(apply(b[, , "other.restore"], c(1, 3), cumsum))) + recoveredEst <- dimSums(a*ac_est,dim=3) - restoredEst + recoveredSub <- dimSums(a*ac_sub,dim=3) - restoredSub + + initial <- setNames(collapseDim(a[,,"acx"]), "other_initial") + recovered <- setNames(recoveredEst+recoveredSub, "other_recovered") + restored <- setNames(restoredEst+restoredSub, "other_restored") + z <- mbind(initial, recovered, restored) + + #check + indicator <- sum(dimSums(a,dim=3) - dimSums(z,dim=3)) + if(abs(indicator) > 1e-6) warning("Other land sub-categories don't add-up to total. Check OtherLand.R function") + + if (level != "cell") z <- superAggregate(z, aggr_type = "sum", level = level,na.rm = FALSE) + + return(z) +} diff --git a/R/reportLandUse.R b/R/reportLandUse.R index dbc56ea..0cc9abe 100644 --- a/R/reportLandUse.R +++ b/R/reportLandUse.R @@ -19,6 +19,9 @@ reportLandUse <- function(gdx) { #read in regional data a <- land(gdx,level = "reg",types = NULL,subcategories = c("crop","forestry"),sum = FALSE) a <- setNames(a,gsub("indc","ndc",getNames(a))) + a <- a[,,"other",invert=TRUE] + b <- OtherLand(gdx) + a <- mbind(b,a) #add global a <- mbind(a, setItems(dimSums(a,dim=1), dim = 1, "GLO")) @@ -32,7 +35,10 @@ reportLandUse <- function(gdx) { x <- mbind(x,setNames(a[,,"crop_treecover"],paste0("Resources|Land Cover|Cropland|+|", reportingnames("crop_treecover")," (million ha)"))) x <- mbind(x,setNames(a[,,"past"],paste0("Resources|Land Cover|+|", reportingnames("past")," (million ha)"))) x <- mbind(x,setNames(a[,,"urban"],paste0("Resources|Land Cover|+|", reportingnames("urban")," (million ha)"))) - x <- mbind(x,setNames(a[,,"other"],paste0("Resources|Land Cover|+|", reportingnames("other")," (million ha)"))) + x <- mbind(x,setNames(dimSums(a[,,c("other_initial","other_recovered","other_restored")],dim=3),paste0("Resources|Land Cover|+|", reportingnames("other")," (million ha)"))) + x <- mbind(x,setNames(a[,,"other_initial"],paste0("Resources|Land Cover|", reportingnames("other"),"|Initial (million ha)"))) + x <- mbind(x,setNames(a[,,"recovered"],paste0("Resources|Land Cover|", reportingnames("other"),"|Recovered (million ha)"))) + x <- mbind(x,setNames(a[,,"other_restored"],paste0("Resources|Land Cover|", reportingnames("other"),"|Restored (million ha)"))) x <- mbind(x,setNames(dimSums(a[,,c("primforest","secdforest","forestry_aff","forestry_ndc","forestry_plant")],dim=3),paste0("Resources|Land Cover|+|", reportingnames("forest")," (million ha)"))) x <- mbind(x,setNames(dimSums(a[,,c("primforest","secdforest")],dim=3), paste0("Resources|Land Cover|Forest|+|", reportingnames("natrforest")," (million ha)"))) x <- mbind(x,setNames(dimSums(a[,,"primforest"],dim=3), paste0("Resources|Land Cover|Forest|Natural Forest|+|", reportingnames("primforest")," (million ha)"))) diff --git a/README.md b/README.md index d517d01..665900f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MAgPIE outputs R package for MAgPIE version 4.x -R package **magpie4**, version **2.13.10** +R package **magpie4**, version **2.13.11** [![CRAN status](https://www.r-pkg.org/badges/version/magpie4)](https://cran.r-project.org/package=magpie4) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158582.svg)](https://doi.org/10.5281/zenodo.1158582) [![R build status](https://github.com/pik-piam/magpie4/workflows/check/badge.svg)](https://github.com/pik-piam/magpie4/actions) [![codecov](https://codecov.io/gh/pik-piam/magpie4/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/magpie4) [![r-universe](https://pik-piam.r-universe.dev/badges/magpie4)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky , R package version 2.13.10, . +Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 , R package version 2.13.11, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {magpie4: MAgPIE outputs R package for MAgPIE version 4.x}, author = {Benjamin Leon Bodirsky and Florian Humpenoeder and Jan Philipp Dietrich and Miodrag Stevanovic and Isabelle Weindl and Kristine Karstens and Xiaoxi Wang and Abhijeet Mishra and Felicitas Beier and Jannes Breier and Amsalu Woldie Yalew and David Chen and Anne Biewald and Stephen Wirth and Patrick {von Jeetze} and Debbora Leip and Michael Crawford and Marcos Alves}, year = {2024}, - note = {R package version 2.13.10}, + note = {R package version 2.13.11}, url = {https://github.com/pik-piam/magpie4}, doi = {10.5281/zenodo.1158582}, } diff --git a/man/OtherLand.Rd b/man/OtherLand.Rd new file mode 100644 index 0000000..bac11ec --- /dev/null +++ b/man/OtherLand.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/OtherLand.R +\name{OtherLand} +\alias{OtherLand} +\title{OtherLand} +\usage{ +OtherLand(gdx, level = "reg") +} +\arguments{ +\item{gdx}{GDX file} + +\item{level}{Level of regional aggregation; "cell", "reg" (regional), "glo" (global), "regglo" (regional and global) or any aggregation level defined in superAggregate. In addition "climate" for the 3 climate regions tropical, temperate and boreal is available.} +} +\value{ +Other land area in Mha +} +\description{ +Disaggregation of other land into initial, restored and recovered land based on a MAgPIE gdx file +} +\details{ +initial, restored and recovered land +} +\examples{ + + \dontrun{ + x <- PeatlandArea(gdx) + } +} +\author{ +Florian Humpenoeder +}