From 84b14ed7cb93271070ae3f27ad94f28bac77103a Mon Sep 17 00:00:00 2001 From: Kristine Karstens Date: Sun, 16 Jun 2024 00:35:57 +0200 Subject: [PATCH] add SOC valitation --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcValidSOCShare.R | 78 +++++++++++++++++++++++++++++++++++++++ R/calcValidSOCStocks.R | 51 +++++++++++++++++++++++-- R/fullVALIDATION.R | 4 ++ README.md | 6 +-- man/calcValidSOCShare.Rd | 26 +++++++++++++ man/calcValidSOCStocks.Rd | 2 +- 9 files changed, 165 insertions(+), 12 deletions(-) create mode 100644 R/calcValidSOCShare.R create mode 100644 man/calcValidSOCShare.Rd diff --git a/.buildlibrary b/.buildlibrary index b49fa88..ef065b3 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '51377672' +ValidationKey: '51515100' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package .* was built under R version' diff --git a/CITATION.cff b/CITATION.cff index a718240..bd6e725 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: 'mrvalidation: madrat data preparation for validation purposes' -version: 2.58.4 -date-released: '2024-06-09' +version: 2.59.0 +date-released: '2024-06-16' abstract: Package contains routines to prepare data for validation exercises. authors: - family-names: Bodirsky diff --git a/DESCRIPTION b/DESCRIPTION index 2a06c2a..30b9f72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrvalidation Title: madrat data preparation for validation purposes -Version: 2.58.4 -Date: 2024-06-09 +Version: 2.59.0 +Date: 2024-06-16 Authors@R: c( person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Stephen", "Wirth", role = "aut"), diff --git a/R/calcValidSOCShare.R b/R/calcValidSOCShare.R new file mode 100644 index 0000000..e9c8d46 --- /dev/null +++ b/R/calcValidSOCShare.R @@ -0,0 +1,78 @@ +#' @title calcValidSOCShare +#' @description calculates the validation data for the soil carbon shares +#' +#' @param datasource Datasources only "histSOCbudget" +#' +#' @return List of magpie objects with results on country level, weight on country level, unit and description. +#' @author Kristine Karstens +#' +#' @examples +#' \dontrun{ +#' calcOutput("ValidSOCShare") +#' } +#' +#' @importFrom magclass mbind getYears setYears + +calcValidSOCShare <- function(datasource = "histSOCbudget") { + + if (datasource == "histSOCbudget") { + + steadyState <- calcOutput("SteadyState", aggregate = FALSE) + landuse <- calcOutput("Landuse", aggregate = FALSE)[, getYears(steadyState), ] + somStock <- calcOutput("SoilCarbon", aggregate = FALSE, output = "actualstate")[, getYears(steadyState), ] + natStock <- calcOutput("SoilCarbon", aggregate = FALSE, output = "naturalstate")[, getYears(steadyState), ] + + somStock <- mstools::toolConv2CountryByCelltype(dimSums(somStock, dim = 3.1), cells = "lpjcell") + natStock <- mstools::toolConv2CountryByCelltype(dimSums(natStock, dim = 3.1), cells = "lpjcell") + equStock <- mstools::toolConv2CountryByCelltype(dimSums(steadyState * landuse, dim = 3.1), cells = "lpjcell") + pEquStock <- mstools::toolConv2CountryByCelltype(dimSums(steadyState[, , "natveg"] * landuse, + dim = 3.1), cells = "lpjcell") + pEquStock <- collapseNames(pEquStock) + + somStock <- mbind(somStock, add_dimension(dimSums(somStock, dim = 3.1), add = "landuse", nm = "total")) + natStock <- mbind(natStock, add_dimension(dimSums(natStock, dim = 3.1), add = "landuse", nm = "total")) + equStock <- mbind(equStock, add_dimension(dimSums(equStock, dim = 3.1), add = "landuse", nm = "total")) + pEquStock <- mbind(pEquStock, add_dimension(dimSums(pEquStock, dim = 3.1), add = "landuse", nm = "total")) + + somShare <- somStock / natStock + unit <- "(tC/tC)" + zeroOrderName <- "Resources|Soil Carbon|Actual|Carbon Share|SOC in top 30 cm" + out <- mbind(setNames(somShare[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(somShare[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(somShare[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + weight <- mbind(setNames(natStock[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(natStock[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(natStock[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + zeroOrderName <- "Resources|Soil Carbon|Target|Carbon Share|SOC in top 30 cm" + equSomShare <- equStock / pEquStock + out <- mbind(out, + setNames(equSomShare[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(equSomShare[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(equSomShare[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + weight <- mbind(weight, + setNames(pEquStock[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(pEquStock[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(pEquStock[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical") + out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource) + + weight <- add_dimension(weight, dim = 3.1, add = "scenario", nm = "historical") + weight <- add_dimension(weight, dim = 3.2, add = "model", nm = datasource) + + } else { + stop("No data exist for the given datasource!") + } + + names(dimnames(out))[3] <- "scenario.model.variable" + names(dimnames(weight))[3] <- "scenario.model.variable" + + return(list(x = out, + weight = weight, + unit = "tC per tC", + description = "Soil Carbon Share") + ) +} diff --git a/R/calcValidSOCStocks.R b/R/calcValidSOCStocks.R index 7023eca..e9d1068 100644 --- a/R/calcValidSOCStocks.R +++ b/R/calcValidSOCStocks.R @@ -18,9 +18,55 @@ #' @importFrom magclass mbind getYears setYears nregions #' @importFrom mstools toolCoord2Isocell -calcValidSOCStocks <- function(datasource = "LPJ_IPCC2006", baseyear = 1995) { +calcValidSOCStocks <- function(datasource = "histSOCbudget", baseyear = 1995) { - if (datasource == "LPJ_IPCC2006") { + if (datasource == "histSOCbudget") { + + steadyState <- calcOutput("SteadyState", aggregate = FALSE) + landuse <- calcOutput("Landuse", aggregate = FALSE)[, getYears(steadyState), ] + somStock <- calcOutput("SoilCarbon", aggregate = FALSE, output = "actualstate")[, getYears(steadyState), ] + natStock <- calcOutput("SoilCarbon", aggregate = FALSE, output = "naturalstate")[, getYears(steadyState), ] + + somStock <- mstools::toolConv2CountryByCelltype(dimSums(somStock, dim = 3.1), cells = "lpjcell") + natStock <- mstools::toolConv2CountryByCelltype(dimSums(natStock, dim = 3.1), cells = "lpjcell") + equStock <- mstools::toolConv2CountryByCelltype(dimSums(steadyState * landuse, dim = 3.1), cells = "lpjcell") + + somStock <- mbind(somStock, add_dimension(dimSums(somStock, dim = 3.1), add = "landuse", nm = "total")) + natStock <- mbind(natStock, add_dimension(dimSums(natStock, dim = 3.1), add = "landuse", nm = "total")) + equStock <- mbind(equStock, add_dimension(dimSums(equStock, dim = 3.1), add = "landuse", nm = "total")) + + unit <- "(Mt C)" + zeroOrderName <- "Resources|Soil Carbon|Actual|Stock|SOC in top 30 cm" + out <- mbind(setNames(somStock[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(somStock[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(somStock[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + zeroOrderName <- "Resources|Soil Carbon|Target|Stock|SOC in top 30 cm" + out <- mbind(out, + setNames(equStock[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(equStock[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(equStock[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + somChang <- somStock - setYears(somStock[, baseyear, ], NULL) + unit <- paste0("Mt C wrt ", baseyear) + zeroOrderName <- "Resources|Soil Carbon|Actual|Stock Change|SOC in top 30 cm" + out <- mbind(out, + setNames(somChang[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(somChang[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(somChang[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + somDebt <- natStock - somStock + unit <- "(Mt C)" + zeroOrderName <- "Resources|Soil Carbon|Actual|Debt|SOC in top 30 cm" + out <- mbind(out, + setNames(somDebt[, , "total"], paste0(zeroOrderName, " ", unit)), + setNames(somDebt[, , "crop"], paste0(zeroOrderName, "|+|Cropland Soils ", unit)), + setNames(somDebt[, , "natveg"], paste0(zeroOrderName, "|+|Noncropland Soils ", unit))) + + out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical") + out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource) + + } else if (datasource == "LPJ_IPCC2006") { mapping <- toolGetMapping(name = "CountryToCellMapping.csv", type = "cell", where = "mappingfolder") somStock <- calcOutput("SOM", subtype = "stock", aggregate = FALSE) @@ -51,7 +97,6 @@ calcValidSOCStocks <- function(datasource = "LPJ_IPCC2006", baseyear = 1995) { paste0("Resources|Soil Carbon|Actual|Stock Change|", "SOC in top 30 cm|+|Noncropland Soils (Mt C wrt ", baseyear, ")")) ) - out <- mbind(out, setNames(somStock[, , "total"][, , "target_soilc"], "Resources|Soil Carbon|Target|Stock|SOC in top 30 cm (Mt C)"), diff --git a/R/fullVALIDATION.R b/R/fullVALIDATION.R index 8b3d397..2a97a65 100644 --- a/R/fullVALIDATION.R +++ b/R/fullVALIDATION.R @@ -205,6 +205,10 @@ fullVALIDATION <- function(rev = 0.1) { # Carbon calcOutput("ValidResidues", aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE) + calcOutput("ValidSOCStocks", datasource = "histSOCbudget", aggregate = "REG+GLO", + file = valfile, append = TRUE, try = TRUE) + calcOutput("ValidSOCShare", datasource = "histSOCbudget", aggregate = "REG+GLO", + file = valfile, append = TRUE, try = TRUE) # Carbon Stocks calcOutput("ValidCarbon", datasource = "LPJmL4_for_MAgPIE_44ac93de:GSWP3-W5E5:historical", aggregate = "REG+GLO", diff --git a/README.md b/README.md index 68a0af4..19b3b30 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat data preparation for validation purposes -R package **mrvalidation**, version **2.58.4** +R package **mrvalidation**, version **2.59.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrvalidation)](https://cran.r-project.org/package=mrvalidation) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4317826.svg)](https://doi.org/10.5281/zenodo.4317826) [![R build status](https://github.com/pik-piam/mrvalidation/workflows/check/badge.svg)](https://github.com/pik-piam/mrvalidation/actions) [![codecov](https://codecov.io/gh/pik-piam/mrvalidation/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrvalidation) [![r-universe](https://pik-piam.r-universe.dev/badges/mrvalidation)](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.58.4, . +Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 , R package version 2.59.0, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrvalidation: madrat data preparation for validation purposes}, author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves}, year = {2024}, - note = {R package version 2.58.4}, + note = {R package version 2.59.0}, doi = {10.5281/zenodo.4317826}, url = {https://github.com/pik-piam/mrvalidation}, } diff --git a/man/calcValidSOCShare.Rd b/man/calcValidSOCShare.Rd new file mode 100644 index 0000000..07f2207 --- /dev/null +++ b/man/calcValidSOCShare.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcValidSOCShare.R +\name{calcValidSOCShare} +\alias{calcValidSOCShare} +\title{calcValidSOCShare} +\usage{ +calcValidSOCShare(datasource = "histSOCbudget") +} +\arguments{ +\item{datasource}{Datasources only "histSOCbudget"} +} +\value{ +List of magpie objects with results on country level, weight on country level, unit and description. +} +\description{ +calculates the validation data for the soil carbon shares +} +\examples{ +\dontrun{ +calcOutput("ValidSOCShare") +} + +} +\author{ +Kristine Karstens +} diff --git a/man/calcValidSOCStocks.Rd b/man/calcValidSOCStocks.Rd index ca33344..20c494a 100644 --- a/man/calcValidSOCStocks.Rd +++ b/man/calcValidSOCStocks.Rd @@ -4,7 +4,7 @@ \alias{calcValidSOCStocks} \title{calcValidSOCStocks} \usage{ -calcValidSOCStocks(datasource = "LPJ_IPCC2006", baseyear = 1995) +calcValidSOCStocks(datasource = "histSOCbudget", baseyear = 1995) } \arguments{ \item{datasource}{Datasources for validation data, e.g. LPJ_IPCC2006, LPJmL_natural}