Skip to content

Commit

Permalink
added critical nitrogen boundary exceedance to getReportGridNitrogenP…
Browse files Browse the repository at this point in the history
…ollution
  • Loading branch information
mscrawford committed Sep 2, 2024
1 parent b3bfe56 commit 08f4cfa
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '5787530'
ValidationKey: '41932800'
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: 'magpie4: MAgPIE outputs R package for MAgPIE version 4.x'
version: 2.9.0
date-released: '2024-08-22'
version: 2.10.0
date-released: '2024-09-02'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
authors:
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: magpie4
Title: MAgPIE outputs R package for MAgPIE version 4.x
Version: 2.9.0
Date: 2024-08-22
Version: 2.10.0
Date: 2024-09-02
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export(getReportFSECStevenLord)
export(getReportFableScenathon)
export(getReportGridEmissions)
export(getReportGridINMS)
export(getReportGridNitrogenPollution)
export(getReportINMS)
export(getReportIso)
export(getReportMAgPIE2GAINS)
Expand Down
123 changes: 123 additions & 0 deletions R/getReportGridNitrogenPollution.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#' @title getReportGridNitrogenPollution
#' @description Reports nutrient surplus indicators as well as exceedance of the critical nitrogen surplus at the
#' grid level
#' @author Michael Crawford
#'
#' @export
#'
#' @param reportOutputDir a folder name for the output to be written to. If NULL the report is not saved to
#' disk, and only returned to the calling function.
#' @param magpieOutputDir a magpie output directory which contains a mapping file (clustermap*.rds) for the
#' disaggregation of grid output
#' @param scenario the name of the scenario used. If NULL the report is not saved to disk, and only returned to the
#' calling function.
#'
#' @return A list of MAgPIE objects containing the reports
#'
#' @importFrom madrat toolConditionalReplace
#'
#' @examples
#'
#' \dontrun{
#' x <- getReportGridNitrogenPollution(gdx, magpieOutputDir)
#' }
#'

getReportGridNitrogenPollution <- function(magpieOutputDir, reportOutputDir = NULL, scenario = NULL) {

# -----------------------------------------------------------------------------------------------------------------
# Helper functions

.formatReport <- function(x, name) {
getSets(x)[c("d1.1", "d1.2", "d1.3")] <- c("x", "y", "iso")
getSets(x, fulldim = FALSE)[3] <- "variable"
getNames(x) <- name

return(x)
}

.saveReport <- function(x, file, comment = NULL) {
if (!is.null(reportOutputDir) && !is.null(scenario)) {
write.magpie(x,
file_name = file.path(reportOutputDir, paste0(scenario, "-", file, ".nc")),
comment = comment)
}
}

# -----------------------------------------------------------------------------------------------------------------
# Nutrient surplus from different land-use types

gdxPath <- file.path(magpieOutputDir, "fulldata.gdx")

# Cropland
croplandBudget <- reportNitrogenBudgetCropland(gdxPath, grid = TRUE, dir = magpieOutputDir, include_emissions = TRUE)
croplandSurplus <- croplandBudget[, , "Nutrient Surplus"]
croplandSurplus <- .formatReport(croplandSurplus, "Nutrient surplus from cropland")

# Pasture
pastureBudget <- reportNitrogenBudgetPasture(gdxPath, grid = TRUE, dir = magpieOutputDir, include_emissions = TRUE)
pastureSurplus <- pastureBudget[, , "Nutrient Surplus"]
pastureSurplus <- .formatReport(pastureSurplus, "Nutrient surplus from pasture")

# Manure excretion
manureBudget <- reportGridManureExcretion(gdxPath, dir = magpieOutputDir)
manureSurplus <- manureBudget[, , "Manure|Manure In Confinements|+|Losses"]
manureSurplus <- .formatReport(manureSurplus, "Nutrient surplus from manure losses in confinements")

# Non-agricultural land
nonAgLandBudget <- reportNitrogenBudgetNonagland(gdxPath, grid = TRUE, dir = magpieOutputDir)
nonAgLandSurplus <- nonAgLandBudget[, , "Nutrient Surplus"]
nonAgLandSurplus <- .formatReport(nonAgLandSurplus, "Nutrient surplus from non-agricultural land")

# Calculate total nutrient surplus
total <- mbind(croplandSurplus, pastureSurplus, manureSurplus, nonAgLandSurplus)
total <- dimSums(total, dim = 3)
total <- .formatReport(total, "Nutrient surplus from land and manure management")

# Save raw surpluses to output folder
surpluses <- mbind(croplandSurplus, pastureSurplus, manureSurplus, nonAgLandSurplus, total)
.saveReport(surpluses, file = "gridNitrogenSurplus", comment = "Mt N/yr")

# Total land
gridLand <- reportGridLand(gdxPath, dir = magpieOutputDir)
totalLand <- dimSums(gridLand, dim = 3)

# Calculate intensity of nutrient surplus
nutrientSurplus_perTotalArea <- (total / totalLand) * 1e3 # Mt X / Mha to kg X / ha

# Five cells have 0 "totalLand", which leads to INFs
nutrientSurplus_perTotalArea <- toolConditionalReplace(x = nutrientSurplus_perTotalArea,
conditions = "!is.finite()",
replaceby = 0)

# Save nutrient surplus intensity to output folder
nutrientSurplus_perTotalArea <- .formatReport(nutrientSurplus_perTotalArea, "Nutrient surplus intensity, incl natural vegetation")
.saveReport(nutrientSurplus_perTotalArea, file = "gridNutrientSurplus_intensity", comment = "kg N / ha")

# -----------------------------------
# Exceedance of critical nitrogen surplus (based on Schulte-Uebbing et al. 2022)

nutrientSurplus_exceedance <- NULL
criticalNitrogenSurplusPath <- file.path(magpieOutputDir, "criticalNitrogenSurplus.mz")
if (file.exists(criticalNitrogenSurplusPath)) {
criticalNitrogenSurplus <- read.magpie(criticalNitrogenSurplusPath)

# Calculate exceedance of the critical nitrogen surplus
nutrientSurplus_exceedance <- -1 * (criticalNitrogenSurplus - nutrientSurplus_perTotalArea)

# Save nutrient surplus exceedance to output folder
.saveReport(nutrientSurplus_exceedance, "Exceedance of Critical Nitrogen Surplus", comment = "kg N / ha")
}

# -----------------------------------------------------------------------------------------------------------------
# Return

return(list("nutrientSurplus_cropland" = croplandSurplus,
"nutrientSurplus_pasture" = pastureSurplus,
"nutrientSurplus_manure" = manureSurplus,
"nutrientSurplus_nonAgLand" = nonAgLandSurplus,
"nutrientSurplus_total" = total,
"nutrientSurplus_intensity" = nutrientSurplus_perTotalArea,
"nutrientSurplus_exceedance" = nutrientSurplus_exceedance))

}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MAgPIE outputs R package for MAgPIE version 4.x

R package **magpie4**, version **2.9.0**
R package **magpie4**, version **2.10.0**

[![CRAN status](https://www.r-pkg.org/badges/version/magpie4)](https://cran.r-project.org/package=magpie4) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158582.svg)](https://doi.org/10.5281/zenodo.1158582) [![R build status](https://github.com/pik-piam/magpie4/workflows/check/badge.svg)](https://github.com/pik-piam/magpie4/actions) [![codecov](https://codecov.io/gh/pik-piam/magpie4/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/magpie4) [![r-universe](https://pik-piam.r-universe.dev/badges/magpie4)](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 **magpie4** in publications use:

Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 2.9.0, <https://github.com/pik-piam/magpie4>.
Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 2.10.0, <https://github.com/pik-piam/magpie4>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {magpie4: MAgPIE outputs R package for MAgPIE version 4.x},
author = {Benjamin Leon Bodirsky and Florian Humpenoeder and Jan Philipp Dietrich and Miodrag Stevanovic and Isabelle Weindl and Kristine Karstens and Xiaoxi Wang and Abhijeet Mishra and Felicitas Beier and Jannes Breier and Amsalu Woldie Yalew and David Chen and Anne Biewald and Stephen Wirth and Patrick {von Jeetze} and Debbora Leip and Michael Crawford and Marcos Alves},
year = {2024},
note = {R package version 2.9.0},
note = {R package version 2.10.0},
url = {https://github.com/pik-piam/magpie4},
doi = {10.5281/zenodo.1158582},
}
Expand Down
39 changes: 39 additions & 0 deletions man/getReportGridNitrogenPollution.Rd

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

0 comments on commit 08f4cfa

Please sign in to comment.