R/soil_inputs.R

Defines functions soil_inputs.grass soil_inputs.custom soil_inputs.manure soil_inputs.crop soil_inputs.default soil_inputs

Documented in soil_inputs soil_inputs.crop soil_inputs.custom soil_inputs.default soil_inputs.grass soil_inputs.manure

#' @title S3 method for calculating organic matter, carbon, nitrogen and lignin inputs
#' @description Takes a management list object output by one of \code{add_crop} or \code{add_manure}
#' and converts it into an estimate of soil organic matter, carbon, nitrogen and lignin inputs,
#' in tonnes ha^-1.
#' @param mgmt_obj A management list object output by one of \code{add_crop}, \code{add_manure}, or
#' \code{add_custom}.
#' @importFrom purrr map_dbl
#' @export
soil_inputs <- function(mgmt_obj) UseMethod("soil_inputs")

#' @title Default output for soil inputs S3
#' @description Warns of unrecognised object if no method exists for object.
#' @param mgmt_obj A management list object output by one of \code{add_crop}, \code{add_manure}, or
#' \code{add_custom}.
#' @export
soil_inputs.default <- function(mgmt_obj) stop("Unrecognised management class")

#' @title S3 method for calculating organic matter, carbon, nitrogen and lignin inputs from crop
#' cultivation
#' @description Takes a management list object output by \code{add_crop} and converts it into an
#' estimate of soil organic matter, carbon, nitrogen and lignin inputs, in tonnes ha^-1. Utilises
#' IPCC (2019) crop residue biomass estimation; run \code{?crop_agrc} or \code{crop_bgrc} for more
#' detail.
#' @param mgmt_obj A management list object output by \code{add_crop}.
#' @importFrom purrr map_dbl
#' @export
soil_inputs.crop <- function(mgmt_obj) {

  # extract coefficients
  slope <- map_dbl(mgmt_obj$crop, ~crop_agrc[[.x]]$slope)
  intercept <- map_dbl(mgmt_obj$crop, ~crop_agrc[[.x]]$intercept)
  rs <- map_dbl(mgmt_obj$crop, ~crop_bgrc[[.x]]$rs)
  dry <- map_dbl(mgmt_obj$crop, ~crop_bgrc[[.x]]$dry)
  n_frac <- map_dbl(mgmt_obj$crop, ~crop_fractions[[.x]]$n_frac)
  lignin_frac <- map_dbl(mgmt_obj$crop, ~crop_fractions[[.x]]$lignin_frac)

  # calculate residues remaining
  yield_dry <- mgmt_obj$yield * dry
  agdm <- yield_dry * slope + intercept
  # note below line is from IPCC (2019) equation 11.6 --- presumed error in equivalent equation 5.0H
  bgr <- (yield_dry + agdm) * rs * mgmt_obj$frac_renew
  agr <- agdm * mgmt_obj$frac_renew * (1 - mgmt_obj$frac_remove)
  crop_om <- agr + bgr

  # inputs from residues
  soil_inputs <- list(
    om_input = crop_om,
    c_input = crop_om * 0.42, # assumption of 42% carbon in residue organic matter
    n_input = crop_om * n_frac,
    lignin_input = crop_om * lignin_frac
  )

  return(soil_inputs)
}

#' @title S3 method for calculating organic matter, carbon, nitrogen and lignin inputs from manure
#' application
#' @description Takes a management list object output by \code{add_manure} and converts it into an
#' estimate of soil organic matter, carbon, nitrogen and lignin inputs, in tonnes ha^-1. Utilises
#' IPCC (2019) manure fraction estimates; run \code{?manure_fractions} for more detail.
#' @param mgmt_obj A management list object output by \code{add_manure}.
#' @importFrom purrr map_dbl
#' @export
soil_inputs.manure <- function(mgmt_obj) {

  # extract coefficients
  cn_ratio <- map_dbl(mgmt_obj$livestock_type, ~man_fractions[[.x]]$cn_ratio)
  n_frac <- map_dbl(mgmt_obj$livestock_type, ~man_fractions[[.x]]$n_frac)
  lignin_frac <- map_dbl(mgmt_obj$livestock_type, ~man_fractions[[.x]]$lignin_frac)
  c_frac <- n_frac * cn_ratio

  # manure C in tonnes ha-1
  c_input <- cn_ratio * mgmt_obj$n_rate * 10^-3
  man_om <- c_input / c_frac
  man_om <- ifelse(is.na(man_om), 0, man_om) # catch NaN errors resulting where c_frac == 0

  # calculate output
  soil_inputs <- list(
    om_input = man_om,
    c_input = c_input,
    n_input = man_om * n_frac,
    lignin_input = man_om * lignin_frac
    )

  return(soil_inputs)
}

#' @title S3 method for calculating organic matter, carbon, nitrogen and lignin inputs from a custom
#' management object.
#' @description Takes a management list object output by \code{add_custom} and converts it into an
#' estimate of soil organic matter, carbon, nitrogen and lignin inputs, in tonnes ha^-1. Essentially
#' a boilerplate function since the role of \code{add_custom} is to provide direct access to the
#' management input, allow non-standard functions to be incorporated into the setup stage.
#' @param mgmt_obj A management list object output by \code{add_custom}.
#' @export
soil_inputs.custom <- function(mgmt_obj) {

  # no output calculation needed
  soil_inputs <- mgmt_obj
  class(soil_inputs) <- "list"

  return(soil_inputs)
}

#' @title S3 method for calculating organic matter, carbon, nitrogen and lignin inputs from a
#' grassland management object.
#' @description Takes a management list object output by \code{add_grass} and converts it into an
#' estimate of soil organic matter, carbon, nitrogen and lignin inputs, in tonnes ha^-1. Essentially
#' a boilerplate function since the role of \code{add_custom} is to provide direct access to the
#' management input, allow non-standard functions to be incorporated into the setup stage.
#' @param mgmt_obj A management list object output by \code{add_grass}.
#' @export
soil_inputs.grass <- function(mgmt_obj) {

  # extract coefficients
  n_frac <- map_dbl("grass", ~crop_fractions[[.x]]$n_frac)
  lignin_frac <- map_dbl("grass", ~crop_fractions[[.x]]$lignin_frac)

  # calculate residues remaining
  grassland_om <- mgmt_obj$yield_t_dm_ha + mgmt_obj$res_t_dm_ha + mgmt_obj$root_t_dm_ha

  # inputs from residues
  soil_inputs <- list(
    om_input = grassland_om,
    c_input = grassland_om * 0.42, # assumption of 42% carbon in residue organic matter
    n_input = grassland_om * n_frac,
    lignin_input = grassland_om * lignin_frac
  )

  return(soil_inputs)
}
aj-sykes92/soilc.ipcc documentation built on March 19, 2021, 11:52 a.m.