-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added critical nitrogen boundary exceedance to getReportGridNitrogenP…
…ollution
- Loading branch information
1 parent
b3bfe56
commit 08f4cfa
Showing
7 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.