Skip to content

Commit

Permalink
NitrogenBoundariesGridded
Browse files Browse the repository at this point in the history
  • Loading branch information
mscrawford committed Sep 6, 2023
1 parent 68a5e1c commit 400eb48
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '26118386'
ValidationKey: '26272040'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ jobs:
any::gms
any::goxygen
any::GDPuc
# explicitly installing all piam packages which are also on
# CRAN (madrat, magclass, citation, gms, goxygen, GDPuc) to
# get newest version instead of outdated binary version from CRAN
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
with:
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: 'mrcommons: MadRat commons Input Data Library'
version: 1.33.4
date-released: '2023-08-10'
version: 1.34.0
date-released: '2023-09-06'
abstract: Provides useful functions and a common structure to all the input data required
to run models like MAgPIE and REMIND of model input data.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrcommons
Type: Package
Title: MadRat commons Input Data Library
Version: 1.33.4
Date: 2023-08-10
Version: 1.34.0
Date: 2023-09-06
Authors@R: c(person("Benjamin Leon", "Bodirsky", email = "[email protected]", role = "aut"),
person("Kristine", "Karstens", role = "aut"),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down
50 changes: 50 additions & 0 deletions R/downloadNitrogenBoundariedGridded.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' @title downloadNitrogenBoundariesGridded
#' @description Download he grid-level regional nitrogen boundary datasets from Schulte-Uebbing et al. (2022)
#' @author Michael S. Crawford
#'
#' @seealso [downloadSource()]
#' @examples \dontrun{
#' a <- downloadSource("NitrogenBoundariesGridded")
#' }

downloadNitrogenBoundariesGridded <- function() {

baseURL <- "https://zenodo.org/record/6395016"

# download datasets
dataURL <- paste0(baseURL, "/files/Global_critical_N_surpluses_and_N_inputs_and_their_exceedances.7z?download=1")
archiveName <- "critical_N_surpluses_inputs_exceedances.7z"
download.file(dataURL, destfile = archiveName)
system(paste0("7z e ", archiveName))

# download readme
readmeURL <- paste0(baseURL, "/files/README.xlsx?download=1")
download.file(readmeURL, "README.xlsx")

# permissions
files <- list.files(".", all.files = TRUE, full.names = TRUE, recursive = TRUE)
Sys.chmod(paths = files, mode = "664", use_umask = FALSE)

# nolint start
return(list(
url = baseURL,
title = "Global spatially explicit critical nitrogen surpluses and critical nitrogen inputs, and their exceedances",
doi = "https://doi.org/10.1038/s41586-022-05158-2",
revision = "1.0",
description = "Input datafiles: Contains complete set of input files used in the calculations of global, spatially
explicit critical nitrogen surpluses and critical nitrogen inputs. All input files are output from the IMAGE-GNM model.
For further information on IMAGE-GNM, see: Beusen, A. H. W., Van Beek, L. P. H., Bouwman, A. F., Mogollon, J. M.,
& Middelburg, J. J. (2015). Coupling global models for hydrology and nutrient loading to simulate nitrogen and
phosphorus retention in surface water - Description of IMAGE-GNM and analysis of performance. Geoscientific Model
Development, 8(12), 4045:4067. https://doi.org/10.5194/gmd-8-4045-2015. Output datafiles: Selection of output datafiles,
supporting results presented in the paper. For more information, see file README.xlsx.",
unit = "various, see README.xlsx",
release_date = "2022-03-29",
license = "Creative Commons Attribution 4.0 International",
author = list("L.F. Schulte-Uebbing",
"A.H.W. Beusen",
"A.F. Bouwman",
"W. de Vries")
))
# nolint end
}
42 changes: 42 additions & 0 deletions R/readNitrogenBoundariesGridded.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' @title readNitrogenBoundariesGridded
#' @description Read the grid-level regional nitrogen boundary datasets from Schulte-Uebbing et al. (2022).
#' For the moment, this only uses the critical N surplus in all agricultural land (arable + intensively
#' managed grassland) in view of all thresholds simultaneously, measured in kg N per ha per yr.
#' @author Michael S. Crawford
#'
#' @return A MAgPIE object with the critical nitrogen surplus in kg N per ha per yr in 2010
#'
#' @seealso [readSource()]
#' @examples \dontrun{
#' a <- readSource("NitrogenBoundariesGridded")
#' }

readNitrogenBoundariesGridded <- function() {

lon <- seq(-179.750, 179.750, by = 0.50)
lat <- seq(89.750, -89.750, by = -0.50)

d <- read.table("nsur_crit_mi_all_ph.asc",
col.names = lon,
header = FALSE, skip = 6, check.names = FALSE)

d <- as.data.frame(d) %>%
dplyr::mutate(lat = lat) %>%
tidyr::pivot_longer(cols = -"lat", names_to = "lon", values_to = "critNitrSurplus") %>%
dplyr::mutate(lon = as.numeric(lon))

d$critNitrSurplus <- replace(d$critNitrSurplus, d$critNitrSurplus == -9999, NA)

# Filter to use MAgPIE's celliso format
mapping <- toolGetMapping(name = "CountryToCellMapping.rds", where = "mrcommons") %>% # nolint: object_usage_linter.
dplyr::select("celliso", "lon", "lat")

d <- dplyr::inner_join(d, mapping, by = c("lat", "lon")) %>%
dplyr::mutate(year = "y2010") %>%
dplyr::select("celliso", "year", "critNitrSurplus")

mag <- as.magpie(d, tidy = TRUE, filter = FALSE)

return(mag)

}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRat commons Input Data Library

R package **mrcommons**, version **1.33.4**
R package **mrcommons**, version **1.34.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrcommons)](https://cran.r-project.org/package=mrcommons) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3822009.svg)](https://doi.org/10.5281/zenodo.3822009) [![R build status](https://github.com/pik-piam/mrcommons/workflows/check/badge.svg)](https://github.com/pik-piam/mrcommons/actions) [![codecov](https://codecov.io/gh/pik-piam/mrcommons/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrcommons) [![r-universe](https://pik-piam.r-universe.dev/badges/mrcommons)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **mrcommons** in publications use:

Bodirsky B, Karstens K, Baumstark L, Weindl I, Wang X, Mishra A, Wirth S, Stevanovic M, Steinmetz N, Kreidenweis U, Rodrigues R, Popov R, Humpenoeder F, Giannousakis A, Levesque A, Klein D, Araujo E, Beier F, Oeser J, Pehl M, Leip D, Crawford M, Molina Bacca E, von Jeetze P, Martinelli E, Schreyer F, Soergel B, Führlich P, Hötten D, Hasse R, Dietrich J (2023). _mrcommons: MadRat commons Input Data Library_. doi:10.5281/zenodo.3822009 <https://doi.org/10.5281/zenodo.3822009>, R package version 1.33.4, <https://github.com/pik-piam/mrcommons>.
Bodirsky B, Karstens K, Baumstark L, Weindl I, Wang X, Mishra A, Wirth S, Stevanovic M, Steinmetz N, Kreidenweis U, Rodrigues R, Popov R, Humpenoeder F, Giannousakis A, Levesque A, Klein D, Araujo E, Beier F, Oeser J, Pehl M, Leip D, Crawford M, Molina Bacca E, von Jeetze P, Martinelli E, Schreyer F, Soergel B, Führlich P, Hötten D, Hasse R, Dietrich J (2023). _mrcommons: MadRat commons Input Data Library_. doi: 10.5281/zenodo.3822009 (URL: https://doi.org/10.5281/zenodo.3822009), R package version 1.34.0, <URL: https://github.com/pik-piam/mrcommons>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrcommons: MadRat commons Input Data Library},
author = {Benjamin Leon Bodirsky and Kristine Karstens and Lavinia Baumstark and Isabelle Weindl and Xiaoxi Wang and Abhijeet Mishra and Stephen Wirth and Mishko Stevanovic and Nele Steinmetz and Ulrich Kreidenweis and Renato Rodrigues and Roman Popov and Florian Humpenoeder and Anastasis Giannousakis and Antoine Levesque and David Klein and Ewerton Araujo and Felicitas Beier and Julian Oeser and Michaja Pehl and Debbora Leip and Michael Crawford and Edna {Molina Bacca} and Patrick {von Jeetze} and Eleonora Martinelli and Felix Schreyer and Bjoern Soergel and Pascal Führlich and David Hötten and Robin Hasse and Jan Philipp Dietrich},
year = {2023},
note = {R package version 1.33.4},
note = {R package version 1.34.0},
doi = {10.5281/zenodo.3822009},
url = {https://github.com/pik-piam/mrcommons},
}
Expand Down
22 changes: 22 additions & 0 deletions man/downloadNitrogenBoundariesGridded.Rd

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

27 changes: 27 additions & 0 deletions man/readNitrogenBoundariesGridded.Rd

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

0 comments on commit 400eb48

Please sign in to comment.