R/fertilizer_inputs.R

Defines functions mineral_N_input organic_fertilizer_stable_OM organic_fertilizer_inputs

#' C and N inputs of organic fertilization operations
#'
#' Applies [CN_input_amendments()] to all rows with the operation
#'  "organic_fertilization". Warnings of [CN_input_amendments()] (e.g. unknown
#'  amendments) are given once per amendment instead of once per row.
#'
#' @param var_MGMT_data a `management_df`
#'
#' @return a data frame with the columns `C_input_org` and `N_input_org`
#'  (kg/ha, `NA` for other operations and if no input could be calculated)
#'
#' @md
#' @noRd
organic_fertilizer_inputs <- function(var_MGMT_data) {
  n <- nrow(var_MGMT_data)
  out <- data.frame(C_input_org = rep(NA_real_, n), N_input_org = rep(NA_real_, n))
  messages <- character()
  for (i in which(var_MGMT_data$operation %in% "organic_fertilization")) {
    res <- withCallingHandlers(
      CN_input_amendments(var_MGMT_data$value[i], amd_type = var_MGMT_data$product[i],
                          DMC = var_MGMT_data$DMC[i], C_content = var_MGMT_data$C_content[i],
                          N_content = var_MGMT_data$N_content[i], unit = var_MGMT_data$unit[i]),
      warning = function(w) {
        messages <<- c(messages, conditionMessage(w))
        invokeRestart("muffleWarning")
      })
    out$C_input_org[i] <- res$C_input_org
    out$N_input_org[i] <- res$N_input_org
  }
  for (m in unique(messages)) warning(m, call. = FALSE)
  out
}

#' Stable organic matter of organic fertilization operations
#'
#' Applies [stable_OM_amendments()] to all rows with the operation
#'  "organic_fertilization". Warnings (e.g. unknown amendments or ISMO) are
#'  given once per amendment instead of once per row.
#'
#' @param var_MGMT_data a `management_df`
#'
#' @return a data frame with the columns `OM_input_org` and `stable_OM_amended`
#'  (kg OM/ha, `NA` for other operations and if no value could be calculated)
#'
#' @md
#' @noRd
organic_fertilizer_stable_OM <- function(var_MGMT_data) {
  n <- nrow(var_MGMT_data)
  out <- data.frame(OM_input_org = rep(NA_real_, n), stable_OM_amended = rep(NA_real_, n))
  messages <- character()
  for (i in which(var_MGMT_data$operation %in% "organic_fertilization")) {
    res <- withCallingHandlers(
      stable_OM_amendments(var_MGMT_data$value[i], amd_type = var_MGMT_data$product[i],
                           DMC = var_MGMT_data$DMC[i], C_content = var_MGMT_data$C_content[i],
                           N_content = var_MGMT_data$N_content[i], unit = var_MGMT_data$unit[i]),
      warning = function(w) {
        messages <<- c(messages, conditionMessage(w))
        invokeRestart("muffleWarning")
      })
    out$OM_input_org[i] <- res$OM_input_org
    out$stable_OM_amended[i] <- res$stable_OM_amended
  }
  for (m in unique(messages)) warning(m, call. = FALSE)
  out
}

#' N input of mineral fertilization operations
#'
#' Mineral N is entered either directly as `kg N/ha` or as product amount
#'  (`kg/ha`, or `l/ha` for liquid fertilizers) together with a fertilizer of
#'  `mineral_fertilizers_LUT`. If the same product is entered on the same date
#'  both as product amount and as `kg N/ha`, only the product amount is used.
#'
#' @param var_MGMT_data a `management_df`
#'
#' @return numeric vector with the mineral N input (kg N/ha) per row, `NA` for
#'  rows without (known) mineral N input
#'
#' @md
#' @noRd
mineral_N_input <- function(var_MGMT_data) {
  d <- var_MGMT_data
  mineral <- d$operation %in% "mineral_fertilization"
  N <- rep(NA_real_, nrow(d))

  # N entered directly
  direct <- mineral & d$unit %in% "kg N/ha"
  N[direct] <- d$value[direct]

  # N from the product amount
  lut <- SoilManageR::mineral_fertilizers_LUT
  i <- match(d$product, lut$Fertilizer)
  by_mass <- mineral & d$unit %in% "kg/ha"
  by_volume <- mineral & d$unit %in% "l/ha"
  N_content <- lut$N_content[i]
  N[by_mass] <- d$value[by_mass] * N_content[by_mass] / 1000
  N[by_volume] <- d$value[by_volume] * lut$Density[i][by_volume] * N_content[by_volume] / 1000

  unknown <- (by_mass | by_volume) & is.na(i)
  if (any(unknown)) {
    warning(paste0("Mineral fertilizers entered as product amount (kg/ha or l/ha) are not in the mineral_fertilizers_LUT, ",
                   "no N input is calculated for: ", paste(unique(d$product[unknown]), collapse = ", ")), call. = FALSE)
  }
  no_N <- (by_mass | by_volume) & !is.na(i) & is.na(N_content)
  if (any(no_N)) {
    warning(paste0("The N content of the following mineral fertilizers is unknown, no N input is calculated: ",
                   paste(unique(d$product[no_N]), collapse = ", ")), call. = FALSE)
  }
  no_density <- by_volume & !is.na(i) & !is.na(N_content) & is.na(lut$Density[i])
  if (any(no_density)) {
    warning(paste0("The density of the following mineral fertilizers is unknown, no N input is calculated for l/ha: ",
                   paste(unique(d$product[no_density]), collapse = ", ")), call. = FALSE)
  }

  # the same product on the same date as product amount and as kg N/ha: use only the product amount
  from_product <- (by_mass | by_volume) & !is.na(N)
  double <- direct & !is.na(d$product) &
    paste(d$date, d$product) %in% paste(d$date[from_product], d$product[from_product])
  if (any(double)) {
    warning(paste0("Mineral fertilizers entered on the same date both as product amount and as kg N/ha, ",
                   "only the product amount is used: ",
                   paste(unique(paste(d$product[double], "on", d$date[double])), collapse = ", ")), call. = FALSE)
    N[double] <- NA_real_
  }
  N
}

Try the SoilManageR package in your browser

Any scripts or data that you put into this service are public.

SoilManageR documentation built on Nov. 17, 2026, 5:08 p.m.