Skip to content

Commit

Permalink
add new subtype specific output option for CarbonInputMultiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
k4rst3ns committed Apr 8, 2024
1 parent cb4e4be commit aacbdfb
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ValidationKey: '4207815'
ValidationKey: '4360620'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: unable to verify current time
AutocreateReadme: yes
allowLinterWarnings: yes
enforceVersionUpdate: no
10 changes: 7 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -36,7 +35,7 @@ jobs:
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9

Expand All @@ -49,6 +48,11 @@ jobs:
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)

- name: Verify that lucode2::buildLibrary was successful
if: github.event_name == 'pull_request'
shell: Rscript {0}
run: lucode2:::isVersionUpdated()

- name: Checks
shell: Rscript {0}
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9025
rev: v0.4.0
hooks:
- id: parsable-R
- id: deps-in-desc
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: 'mrsoil: MadRat Soil Organic Carbon Budget Library'
version: 2.1.3
date-released: '2024-02-02'
version: 2.2.0
date-released: '2024-04-08'
abstract: This packages provides functions for soil organic carbon budget for mineral
soils using the steady-state method (Tier 2) of the 2019 Refinement to the 2006
IPP Guidelines for National Greenhouse Gas Inventories.
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrsoil
Type: Package
Title: MadRat Soil Organic Carbon Budget Library
Version: 2.1.3
Date: 2024-02-02
Version: 2.2.0
Date: 2024-04-08
Authors@R: c(person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = c("aut","cre")),
person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = "aut"))
Description: This packages provides functions for soil organic carbon budget for mineral soils using the steady-state method (Tier 2) of the 2019 Refinement to the 2006 IPP Guidelines for National Greenhouse Gas Inventories.
Expand Down
55 changes: 35 additions & 20 deletions R/calcCarbonInputMultiplier.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' using the steady-state method (Tier 2) of the 2019 Refinement to the 2006 IPP Guidelines
#' for National Greenhouse Gas Inventories
#'
#' @param inputType switch between 'kcr' (default), or 'kli', 'generic'
#'
#' @return magpie object on choosen resolution
#' @author Kristine Karstens
#'
Expand All @@ -13,7 +15,7 @@
#'
#' @importFrom magpiesets findset

calcCarbonInputMultiplier <- function() {
calcCarbonInputMultiplier <- function(inputType = "kcr") {

soilParam <- readSource("IPCCSoil", convert = FALSE)

Expand All @@ -23,31 +25,44 @@ calcCarbonInputMultiplier <- function() {
f2struc2act <- setNames(soilParam[, , tillage2param], names(tillage2param))
f4act2slo <- calcOutput("TransferActive2Slow", aggregate = FALSE)

inputProp <- NULL
# get lignin to c and nitrogen to c values for all residues classes
inputProp <- mbind(inputProp,
calcOutput("ParamResidues", input = "IPCC+woody", aggregate = FALSE))
# get lignin to c and nitrogen to c values for all manure classes
inputProp <- mbind(inputProp,
calcOutput("ParamManure", input = "IPCC", aggregate = FALSE))
# get lignin to c and nitrogen to c values for generic input classes
param <- readSource("IPCC", subtype = "residues_table5p5b", convert = FALSE)
generic <- "Generic value for crops not indicated below"
c2dm <- 0.45
inputProp <- mbind(inputProp,
setNames(param[, , generic] / c2dm,
paste0("generic.", gsub("_dm", "", getNames(param, dim = 2)))))
# get lignin to c and nitrogen to c values for input classes
if (inputType == "kcr") {
# get lignin to c and nitrogen to c values for crop type residue classes
inputProp <- calcOutput("ParamResidues", input = "IPCC+woody", aggregate = FALSE)
} else if (inputType == "kli") {
# get lignin to c and nitrogen to c values for all manure classes
inputProp <- calcOutput("ParamManure", input = "IPCC", aggregate = FALSE)
} else if (inputType == "generic") {
# get lignin to c and nitrogen to c values for generic input classes
param <- readSource("IPCC", subtype = "residues_table5p5b", convert = FALSE)
generic <- "Generic value for crops not indicated below"
c2dm <- 0.45
inputProp <- setNames(param[, , generic] / c2dm,
paste0("generic.", gsub("_dm", "", getNames(param, dim = 2))))
} else {
stop("inputType setting unknown.")
}

# calculate carbon input multipliers for crop-related inputs
out <- toolCarbonInputMultiplier(inputProp = inputProp,
soilParam = soilParam,
f4act2slo = f4act2slo,
f2struc2act = f2struc2act)

weight <- collapseNames(calcOutput("ResBiomass", cellular = TRUE, aggregate = FALSE)[, "y1995", "c"])
weight <- mbind(dimSums(weight, dim = 3.1),
setNames(dimSums(weight, dim = 3), "generic"),
collapseNames(calcOutput("CarbonManure", aggregate = FALSE)[, "y1995", "c"]))
# get weights
if (inputType %in% c("kcr", "generic")) {

weight <- collapseNames(calcOutput("ResBiomass", cellular = TRUE, aggregate = FALSE)[, "y1995", "c"])
weight <- ifelse(inputType == "kcr",
dimSums(weight, dim = 3.1), # for kcr
setNames(dimSums(weight, dim = 3), "generic")) # for 'generic'
} else if (inputType == "kli") {

weight <- collapseNames(calcOutput("CarbonManure", aggregate = FALSE)[, "y1995", "c"])
} else {
stop("inputType setting unknown.")
}

weight <- weight + 10^-10 # important for empty categories such as betr, begr

getSets(out, fulldim = FALSE)[1] <- "x.y.iso"
Expand All @@ -56,7 +71,7 @@ calcCarbonInputMultiplier <- function() {
return(list(x = out,
weight = weight,
unit = "tC per ha",
description = "Carbon input multiplier in tonnes carbon per hectare/ tonnes carbon per hectare
description = "Carbon input multiplier in tonnes carbon per hectare / tonnes carbon per hectare
to all carbon subpools for different tillage and input (manure and residue) types",
isocountries = FALSE))
}
1 change: 1 addition & 0 deletions R/calcSoilCarbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ calcSoilCarbon <- function(output = "actualstate",
landuse)[, , output]

getSets(out, fulldim = FALSE)[1] <- "x.y.iso"
out <- collapseDim(out)

return(list(x = out,
weight = NULL,
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 Soil Organic Carbon Budget Library

R package **mrsoil**, version **2.1.3**
R package **mrsoil**, version **2.2.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrsoil)](https://cran.r-project.org/package=mrsoil) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4317933.svg)](https://doi.org/10.5281/zenodo.4317933) [![R build status](https://github.com/pik-piam/mrsoil/workflows/check/badge.svg)](https://github.com/pik-piam/mrsoil/actions) [![codecov](https://codecov.io/gh/pik-piam/mrsoil/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrsoil) [![r-universe](https://pik-piam.r-universe.dev/badges/mrsoil)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -38,7 +38,7 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

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

Karstens K, Dietrich J (2024). _mrsoil: MadRat Soil Organic Carbon Budget Library_. doi:10.5281/zenodo.4317933 <https://doi.org/10.5281/zenodo.4317933>, R package version 2.1.3, <https://github.com/pik-piam/mrsoil>.
Karstens K, Dietrich J (2024). _mrsoil: MadRat Soil Organic Carbon Budget Library_. doi:10.5281/zenodo.4317933 <https://doi.org/10.5281/zenodo.4317933>, R package version 2.2.0, <https://github.com/pik-piam/mrsoil>.

A BibTeX entry for LaTeX users is

Expand All @@ -47,7 +47,7 @@ A BibTeX entry for LaTeX users is
title = {mrsoil: MadRat Soil Organic Carbon Budget Library},
author = {Kristine Karstens and Jan Philipp Dietrich},
year = {2024},
note = {R package version 2.1.3},
note = {R package version 2.2.0},
doi = {10.5281/zenodo.4317933},
url = {https://github.com/pik-piam/mrsoil},
}
Expand Down
5 changes: 4 additions & 1 deletion man/calcCarbonInputMultiplier.Rd

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

0 comments on commit aacbdfb

Please sign in to comment.