Skip to content

Commit

Permalink
reworked land use reporting for SEALS
Browse files Browse the repository at this point in the history
  • Loading branch information
pvjeetze committed Jul 5, 2023
1 parent f5cf1b2 commit 2e3eba3
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '232244785'
ValidationKey: '232366270'
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: 1.188.5
date-released: '2023-07-03'
version: 1.189.0
date-released: '2023-07-05'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
authors:
Expand Down
5 changes: 3 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: 1.188.5
Date: 2023-07-03
Version: 1.189.0
Date: 2023-07-05
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -54,6 +54,7 @@ Imports:
Suggests:
covr,
luplot,
ncdf4,
rworldmap,
terra,
testthat
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export(Kcal)
export(LivestockDemStructure)
export(LivestockShare)
export(ManureExcretion)
export(MapLandUseForDownscaling)
export(NetForestChange)
export(NitrogenBudget)
export(NitrogenBudgetNonagland)
Expand Down Expand Up @@ -178,6 +177,7 @@ export(reportLaborProductivity)
export(reportLandFootprint)
export(reportLandUse)
export(reportLandUseChange)
export(reportLandUseForSEALS)
export(reportLivestockDemStructure)
export(reportLivestockShare)
export(reportLocalDemandShare)
Expand Down
54 changes: 0 additions & 54 deletions R/MapLandUseForDownscaling.R

This file was deleted.

1 change: 0 additions & 1 deletion R/PrimSecdOtherLand.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' @param unit "Mha" or "share". Defines the unit of the gridded output, see also \code{level}.
#' @return \code{x} including land area for primary and secondary non-forested vegetation in MAgPIE (other land) as MAgPIE object; either as unit of area (Mha) or as fraction of total land per grid cell (share).
#' @author Patrick v. Jeetze, Kristine Karstens
#' @seealso \code{\link{MapLandUseForDownscaling}}
#' @examples
#' \dontrun{
#' x <- "./cell.land_0.5.nc"
Expand Down
76 changes: 76 additions & 0 deletions R/reportLandUseForSEALS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#' @title reportLandUseForSEALS
#' @description Restructures NetCDF of magpie land use output and writes it to a specific NetCDF
#' that can be read by the Spatial Economic Allocation Landscape Simulator (SEALS) model
#' for generating high resolution land use maps.
#'
#' @export
#'
#' @param magCellLand Disaggregated land use (grid-cell land area share) in NetCDF format from a MAgPIE run.
#' @param outFile a file name the output should be written to using \code{ncdf4::nc_create} and \code{ncdf4::ncvar_put}
#' @param dir output directory which contains cellular magpie output
#' @param scenName Optional scenario name
#' @param selectyears Numeric vector of years to provide data for.
#'
#' @return Proportions of different land use classes per grid sell in a NetCDF format.
#'
#' @author Patrick v. Jeetze
#' @examples
#' \dontrun{
#' x <- reportLandUseForSEALS(magCellLand = "cell.land_0.5_share.nc",
#' outFile = "cell.land_0.5_SEALS.nc",
#' selectyears = c(2015, 2050))
#' }
#'
reportLandUseForSEALS <- function(magCellLand = "cell.land_0.5_share.nc", outFile = "cell.land_0.5_SEALS.nc",
scenName = NULL, dir = ".", selectyears = c(2015, 2050)) {
if (!is.null(scenName)) {
outFile <- sub(".nc", paste0("_", scenName, ".nc"), outFile)
}

### Open the MAgPIE output NetCDF
magLand <- ncdf4::nc_open(file.path(dir, magCellLand))

### Extract dimensions
lon <- ncdf4::ncdim_def("lon", "degrees_east", ncdf4::ncvar_get(magLand, "lon"))
# Make sure that order of latitudinal coordinates is in the right order
# Check whether latitudinal coordinates are ascending or descending
latAscends <- all(diff(ncdf4::ncvar_get(magLand, "lat")) > 0)
if (latAscends) {
lat <- ncdf4::ncdim_def("lat", "degrees_north", rev(ncdf4::ncvar_get(magLand, "lat")))
} else {
lat <- ncdf4::ncdim_def("lat", "degrees_north", ncdf4::ncvar_get(magLand, "lat"))
}
time <- ncdf4::ncdim_def("time", "years", selectyears, unlim = TRUE)

# Get variable names
vnames <- names(magLand$var)

# Create a new netCDF file with all variable names
sealsLand <- ncdf4::nc_create(file.path(dir, outFile),
vars = list(
ncdf4::ncvar_def("crop", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("past", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("primforest", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("secdforest", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("forestry", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("urban", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999),
ncdf4::ncvar_def("other", "grid-cell land area fraction", dim = list(lon, lat, time), missval = -9999)
)
)

# Write values to NetCDF
for (vname in vnames) {
if (latAscends) {
yrIndx <- which(ncdf4::ncvar_get(magLand, "time") %in% selectyears)
ncdf4::ncvar_put(sealsLand, vname, ncdf4::ncvar_get(magLand, vname)[, lat$len:1, yrIndx])
} else {
ncdf4::ncvar_put(sealsLand, vname, ncdf4::ncvar_get(magLand, vname)[, , yrIndx])
}
}

# Close connection
ncdf4::nc_close(sealsLand)

# Report completion
message(paste0("Finished writing '", outFile, "' into\n'", dir, "'"))
}
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 **1.188.5**
R package **magpie4**, version **1.189.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 (2023). _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 1.188.5, <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 (2023). _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 1.189.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 = {2023},
note = {R package version 1.188.5},
note = {R package version 1.189.0},
doi = {10.5281/zenodo.1158582},
url = {https://github.com/pik-piam/magpie4},
}
Expand Down
43 changes: 0 additions & 43 deletions man/MapLandUseForDownscaling.Rd

This file was deleted.

3 changes: 0 additions & 3 deletions man/PrimSecdOtherLand.Rd

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

44 changes: 44 additions & 0 deletions man/reportLandUseForSEALS.Rd

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

0 comments on commit 2e3eba3

Please sign in to comment.