Skip to content

Commit

Permalink
add SOC valitation
Browse files Browse the repository at this point in the history
  • Loading branch information
k4rst3ns committed Jun 15, 2024
1 parent 832e748 commit 84b14ed
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '51377672'
ValidationKey: '51515100'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package .* was built under R version'
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: '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
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: 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", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down
78 changes: 78 additions & 0 deletions R/calcValidSOCShare.R
Original file line number Diff line number Diff line change
@@ -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")
)
}
51 changes: 48 additions & 3 deletions R/calcValidSOCStocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)"),
Expand Down
4 changes: 4 additions & 0 deletions R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 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)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky <bodirsky@

To cite package **mrvalidation** in publications use:

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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.58.4, <https://github.com/pik-piam/mrvalidation>.
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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.59.0, <https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
Expand Down
26 changes: 26 additions & 0 deletions man/calcValidSOCShare.Rd

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

2 changes: 1 addition & 1 deletion man/calcValidSOCStocks.Rd

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

0 comments on commit 84b14ed

Please sign in to comment.