R/reportNitrogenBudgetCropland.R

Defines functions reportNitrogenBudgetCropland

Documented in reportNitrogenBudgetCropland

#' @title reportNitrogenBudgetCropland
#' @description Reports the Nitrogen Budgets of Croplands for future MAgPIE projections
#'
#' @importFrom magpiesets reportingnames
#' @export
#'
#' @param gdx GDX file
#' @param include_emissions TRUE also divides the N surplus into different emissions
#' @param grid grid provides outputs on grid level of 0.5 degree
#' @param dir for gridded outputs: magpie output directory which contains a mapping file (rds or spam) disaggregation
#' @author Benjamin Leon Bodirsky
#' @seealso
#' \code{\link{NitrogenBudget}}
#'
#' @examples
#' \dontrun{
#' x <- reportNitrogenBudgetCropland(gdx)
#' }
#'
reportNitrogenBudgetCropland <- function(gdx, include_emissions = FALSE, grid = FALSE, # nolint
                                         dir = ".") {

  if (grid == FALSE) {
    budget <- NitrogenBudget(gdx, level = "regglo", include_emissions = include_emissions)
    budget[, , "som"] <- -budget[, , "som"]


    all <- getNames(budget)
    withdrawaltypes <- c("harvest", "ag", "bg")
    balancetypes <- c("surplus", "som", "balanceflow")
    if (include_emissions) {
      emissiontypes <- c("n2o_n", "nh3_n", "no2_n", "no3_n")
    } else {
      emissiontypes <- NULL
    }
    inputtypes <- setdiff(all, c(withdrawaltypes, balancetypes, emissiontypes))

    tmp <- budget[, , inputtypes]
    getNames(tmp) <- paste0("Resources|Nitrogen|Cropland Budget|Inputs|+|", reportingnames(getNames(tmp)))
    inputs <- mbind(
      setNames(dimSums(tmp, dim = 3), "Resources|Nitrogen|Cropland Budget|Inputs"),
      tmp
    )

    tmp <- budget[, , withdrawaltypes]
    getNames(tmp) <- paste0("Resources|Nitrogen|Cropland Budget|Withdrawals|+|", reportingnames(getNames(tmp)))
    withdrawals <- mbind(
      setNames(dimSums(tmp, dim = 3), "Resources|Nitrogen|Cropland Budget|Withdrawals"),
      tmp
    )

    tmp <- budget[, , balancetypes]
    getNames(tmp) <- paste0("Resources|Nitrogen|Cropland Budget|Balance|+|", reportingnames(getNames(tmp)))
    balance <- mbind(
      setNames(dimSums(tmp, dim = 3), "Resources|Nitrogen|Cropland Budget|Balance"),
      tmp
    )

    if (include_emissions) {
      tmp <- budget[, , emissiontypes]
      getNames(tmp) <- paste0("Resources|Nitrogen|Cropland Budget|Balance|Nutrient Surplus|",
        reportingnames(getNames(tmp)))
      emissions <- tmp
    } else {
      emissions <- NULL
    }

    out <- mbind(
      inputs,
      withdrawals,
      balance,
      emissions
    )

    getNames(out) <- paste0(getNames(out), " (Mt Nr/yr)")

  } else {

    out <- NitrogenBudget(gdx, level = "grid", dir = dir, include_emissions = include_emissions)
    out[, , "som"] <- -out[, , "som"]
    getNames(out) <- reportingnames(getNames(out))
    descr <- paste0("Total land area in its primary land cover categories.",
      " Other includes non-forest natural vegetation like savannas. Soil organic matter quantifies the nitrogen flow into soil stocks; negative numbers is released Nr from soil depletion.")
    out <- metadata_comments(x = out, unit = "Mt Nr/yr", description = descr, comment = "", note = "")
  }

  return(out)
}
pik-piam/magpie4 documentation built on April 22, 2024, 3:34 p.m.