Skip to content

Commit

Permalink
correction of 10^3 mistake in nitrogen validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bodirsky committed Jan 26, 2024
1 parent 658d03c commit 493272a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '50377148'
ValidationKey: '50396896'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrvalidation: madrat data preparation for validation purposes'
version: 2.55.1
version: 2.55.2
date-released: '2024-01-26'
abstract: Package contains routines to prepare data for validation exercises.
authors:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrvalidation
Title: madrat data preparation for validation purposes
Version: 2.55.1
Version: 2.55.2
Date: 2024-01-26
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
Expand Down
45 changes: 25 additions & 20 deletions R/calcValidNitrogenBudgetCropland.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#'
#' @param datasource Bodirsky for own calculations, Lassaletta2014 for a country dataset from
#' Lassaletta, L., G. Billen, B. Grizzetti, J. Angalde, and J. Garnier. 2014.
#' 50 Year Trends in Nitrogen Use Efficiency of World Cropping Systems: The Relationship between Yield and Nitrogen Input to Cropland.
#' 50 Year Trends in Nitrogen Use Efficiency of World Cropping Systems: The Relationship
#' between Yield and Nitrogen Input to Cropland.
#' Environmental Research Letters.
#' FAO for some N related parameters published in FAOSTAT.
#'
Expand All @@ -12,7 +13,6 @@
#' @seealso
#' \code{\link{calcNitrogenBudgetCropland}}
#' @examples
#'
#' \dontrun{
#' calcOutput("ValidNitrogenBudgetCropland")
#' }
Expand Down Expand Up @@ -58,24 +58,28 @@ calcValidNitrogenBudgetCropland <- function(datasource = "Bodirsky") {

out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
} else if (datasource == "ACCMIP") {
dep <- calcOutput("AtmosphericDeposition", datasource = "ACCMIP", scenario = c("rcp26", "rcp45", "rcp85"), aggregate = FALSE)
dep <- calcOutput("AtmosphericDeposition", datasource = "ACCMIP",
scenario = c("rcp26", "rcp45", "rcp85"), aggregate = FALSE)
out <- dep
out <- dimSums(out, dim = c(3.1, 3.3, 3.4))
out <- add_dimension(out, dim = 3.2, add = "indicator", nm = "Resources|Nitrogen|Cropland Budget|Inputs|+|Atmospheric Deposition")
out <- add_dimension(out, dim = 3.2, add = "indicator",
nm = "Resources|Nitrogen|Cropland Budget|Inputs|+|Atmospheric Deposition")
getSets(out)[3] <- "scenario"
} else if (datasource == "Lassaletta2014") {
budget <- readSource("Lassaletta2014", subtype = "budget")
out <- mbind(
setNames(budget[, , "harvest"], paste0("Resources|Nitrogen|Cropland Budget|Withdrawals|+|", reportingnames(getNames(budget[, , "harvest"])))),
setNames(budget[, , "harvest", invert = T], paste0("Resources|Nitrogen|Cropland Budget|Inputs|+|", reportingnames(getNames(budget[, , "harvest", invert = T]))))
setNames(budget[, , "harvest"], paste0("Resources|Nitrogen|Cropland Budget|Withdrawals|+|",
reportingnames(getNames(budget[, , "harvest"])))),
setNames(budget[, , "harvest", invert = TRUE],
paste0("Resources|Nitrogen|Cropland Budget|Inputs|+|",
reportingnames(getNames(budget[, , "harvest", invert = TRUE]))))
)
out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
} else if (datasource == "FAO") {
residues <- readSource("FAO_online", "EmisAgCropResid")
# residues<-setNames(residues[,,"Residues_(Crop_residues)_(tonnes_of_nutrients)"][,,"1712|All Crops + (Total)"]/10^9,"ag_recycling")
## Previous conversion was 1e9 which would have converted from ton to Giga ton <--- Confirm with Benni
## New units are provided in kg by FAO so kg to Mt should be 1e9
residues <- setNames(residues[, , "Residues_(Crop_residues)_(kg_of_nutrients)"][, , "1712|All Crops"] / 1e9, "ag_recycling")
residues <- setNames(residues[, , "Residues_(Crop_residues)_(kg_of_nutrients)"][, , "1712|All Crops"]
/ 1e9, "ag_recycling")

manure <- readSource("FAO_online", "EmisAgManureSoil")
selection <- c(
Expand All @@ -97,12 +101,13 @@ calcValidNitrogenBudgetCropland <- function(datasource = "Bodirsky") {
tmp2 <- collapseNames(manure[, , selection][, , "Manure_applied_to_soils_(N_content)_(kg)"])
mapping <- toolGetMapping(type = "sectoral", name = "IPCCitems_fao_online.csv", where = "mappingfolder")
tmp2 <- toolAggregate(tmp2, rel = mapping, from = "fao", to = "magpie", dim = 3.1)
tmp2 <- tmp2 / 1e12
tmp2 <- tmp2 / 1e9
manure <- setNames(dimSums(tmp2, dim = 3.1), "manure_conf")

# fertilizer
fertilizer <- readSource("FAO_online", "EmisAgSynthFerti")[, , c("3102|Nutrient nitrogen N (total).Agricultural_Use_in_nutrients_(kg_of_nutrients)")]
fertilizer <- dimSums(fertilizer, dim = c(3)) / 1e12
fertilizer <- (readSource("FAO_online", "EmisAgSynthFerti")
)[, , c("3102|Nutrient nitrogen N (total).Agricultural_Use_in_nutrients_(kg_of_nutrients)")]
fertilizer <- dimSums(fertilizer, dim = c(3)) / 1e9
fertilizer <- setNames(fertilizer, "fertilizer")
commonyears <- intersect(intersect(getYears(fertilizer), getYears(residues)), getYears(manure))

Expand All @@ -114,19 +119,19 @@ calcValidNitrogenBudgetCropland <- function(datasource = "Bodirsky") {

vcat(2, "instead of removing years with no data, each object should rather be blown up to full dimensionality")

names_x <- reportingnames(getNames(out))
names(names_x) <- NULL
getNames(out) <- paste0("Resources|Nitrogen|Cropland Budget|Inputs|+|", names_x)
namesX <- reportingnames(getNames(out))
names(namesX) <- NULL
getNames(out) <- paste0("Resources|Nitrogen|Cropland Budget|Inputs|+|", namesX)
out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
} else {
stop("No data exist for the given datasource!")
}
out <- add_dimension(out, dim = 3.1, add = "model", nm = datasource)
getNames(out) <- paste0(getNames(out), " (Mt Nr/yr)")

return(list(x=out,
weight=NULL,
unit="Mt Nr/yr",
description="Cropland Nitrogen Budget")
)
return(list(x = out,
weight = NULL,
unit = "Mt Nr/yr",
description = "Cropland Nitrogen Budget")
)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat data preparation for validation purposes

R package **mrvalidation**, version **2.55.1**
R package **mrvalidation**, version **2.55.2**

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

Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.55.1, <https://github.com/pik-piam/mrvalidation>.
Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.55.2, <https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrvalidation: madrat data preparation for validation purposes},
author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves},
year = {2024},
note = {R package version 2.55.1},
note = {R package version 2.55.2},
doi = {10.5281/zenodo.4317826},
url = {https://github.com/pik-piam/mrvalidation},
}
Expand Down
4 changes: 2 additions & 2 deletions man/calcValidNitrogenBudgetCropland.Rd

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

0 comments on commit 493272a

Please sign in to comment.