diff --git a/.buildlibrary b/.buildlibrary index 207f772d..b38be322 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '232244785' +ValidationKey: '232366270' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 97adacd5..e791746c 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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: diff --git a/DESCRIPTION b/DESCRIPTION index c4d1c454..2445eb4b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Florian", "Humpenoeder", , "humpenoeder@pik-potsdam.de", role = "aut"), @@ -54,6 +54,7 @@ Imports: Suggests: covr, luplot, + ncdf4, rworldmap, terra, testthat diff --git a/NAMESPACE b/NAMESPACE index d52c0848..06a010ef 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,7 +26,6 @@ export(Kcal) export(LivestockDemStructure) export(LivestockShare) export(ManureExcretion) -export(MapLandUseForDownscaling) export(NetForestChange) export(NitrogenBudget) export(NitrogenBudgetNonagland) @@ -178,6 +177,7 @@ export(reportLaborProductivity) export(reportLandFootprint) export(reportLandUse) export(reportLandUseChange) +export(reportLandUseForSEALS) export(reportLivestockDemStructure) export(reportLivestockShare) export(reportLocalDemandShare) diff --git a/R/MapLandUseForDownscaling.R b/R/MapLandUseForDownscaling.R deleted file mode 100644 index d1c93d61..00000000 --- a/R/MapLandUseForDownscaling.R +++ /dev/null @@ -1,54 +0,0 @@ -#' @title MapLandUseForDownscaling -#' @description Modifies magpie land use output so that it can be fed into a downscaling model for generating high resolution land use maps. -#' -#' @importFrom magclass mbind read.magpie dimSums -#' @importFrom gdx out -#' @importFrom magpiesets findset -#' @export -#' -#' @param gdx GDX file -#' @param file a file name the output should be written to using write.magpie -#' @param dir output directory which contains cellular magpie output. -#' @param downscaling_model The only current option is "SEALS" (Spatial Economic Allocation Landscape Simulator). -#' @param selectyears Years to provide data for. -#' @param grid Whether gridded output should be used. Otherwise output will be returned at cluster level. -#' @return Shares of different land use classes per grid sell as magpie object. -#' @author Patrick v. Jeetze -#' @examples -#' \dontrun{ -#' x <- MapLandUseForDownscaling(dir = ".", downscaling_model = "SEALS") -#' } -#' -MapLandUseForDownscaling <- function(gdx, dir = ".", file = NULL, downscaling_model = "SEALS", selectyears = c("y2015", "y2050"), grid = TRUE) { - downscaling_model_list <- c( # List of available models for downscaling: - "SEALS" - ) - - if (!any(grepl(downscaling_model, downscaling_model_list))) { - stop(paste( - "No available model for downscaling specified. Current options for 'downscaling_model' are", - paste(gsub("(\\w+)", '"\\1"', downscaling_model_list), collapse = " ") - )) - } - - selectyears <- sort(findset(selectyears, noset = "original")) - - if (downscaling_model == "SEALS") { - if (grid == TRUE) { - landShr <- read.magpie(file.path(dir, "cell.land_0.5_share.mz"))[, selectyears, ] - } else { - landUse <- land(gdx, level = "cell")[, selectyears, ] - landShr <- landUse / dimSums(landUse, dim = 3) - } - - out_seals <- mbind( - setNames(landShr[, , "crop"], "crop"), - setNames(landShr[, , "past"], "past"), - setNames(landShr[, , "primforest"] + landShr[, , "secdforest"] + landShr[, , "forestry"], "forest"), - setNames(landShr[, , "urban"], "urban"), - setNames(landShr[, , "other"], "other") - ) - - out(out_seals, file) - } -} diff --git a/R/PrimSecdOtherLand.R b/R/PrimSecdOtherLand.R index 8c3fa281..3cce7815 100644 --- a/R/PrimSecdOtherLand.R +++ b/R/PrimSecdOtherLand.R @@ -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" diff --git a/R/reportLandUseForSEALS.R b/R/reportLandUseForSEALS.R new file mode 100644 index 00000000..c93947b1 --- /dev/null +++ b/R/reportLandUseForSEALS.R @@ -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, "'")) +} diff --git a/README.md b/README.md index 0d5a80ea..4caf4e52 100644 --- a/README.md +++ b/README.md @@ -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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky , R package version 1.188.5, . +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 , R package version 1.189.0, . A BibTeX entry for LaTeX users is @@ -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}, } diff --git a/man/MapLandUseForDownscaling.Rd b/man/MapLandUseForDownscaling.Rd deleted file mode 100644 index 02bc3eb8..00000000 --- a/man/MapLandUseForDownscaling.Rd +++ /dev/null @@ -1,43 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/MapLandUseForDownscaling.R -\name{MapLandUseForDownscaling} -\alias{MapLandUseForDownscaling} -\title{MapLandUseForDownscaling} -\usage{ -MapLandUseForDownscaling( - gdx, - dir = ".", - file = NULL, - downscaling_model = "SEALS", - selectyears = c("y2015", "y2050"), - grid = TRUE -) -} -\arguments{ -\item{gdx}{GDX file} - -\item{dir}{output directory which contains cellular magpie output.} - -\item{file}{a file name the output should be written to using write.magpie} - -\item{downscaling_model}{The only current option is "SEALS" (Spatial Economic Allocation Landscape Simulator).} - -\item{selectyears}{Years to provide data for.} - -\item{grid}{Whether gridded output should be used. Otherwise output will be returned at cluster level.} -} -\value{ -Shares of different land use classes per grid sell as magpie object. -} -\description{ -Modifies magpie land use output so that it can be fed into a downscaling model for generating high resolution land use maps. -} -\examples{ -\dontrun{ -x <- MapLandUseForDownscaling(dir = ".", downscaling_model = "SEALS") -} - -} -\author{ -Patrick v. Jeetze -} diff --git a/man/PrimSecdOtherLand.Rd b/man/PrimSecdOtherLand.Rd index 2ff5d9bb..dd9b761d 100644 --- a/man/PrimSecdOtherLand.Rd +++ b/man/PrimSecdOtherLand.Rd @@ -41,9 +41,6 @@ land <- PrimSecdOtherLand(x) land <- PrimSecdOtherLand(land_hr) } -} -\seealso{ -\code{\link{MapLandUseForDownscaling}} } \author{ Patrick v. Jeetze, Kristine Karstens diff --git a/man/reportLandUseForSEALS.Rd b/man/reportLandUseForSEALS.Rd new file mode 100644 index 00000000..b43643f5 --- /dev/null +++ b/man/reportLandUseForSEALS.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/reportLandUseForSEALS.R +\name{reportLandUseForSEALS} +\alias{reportLandUseForSEALS} +\title{reportLandUseForSEALS} +\usage{ +reportLandUseForSEALS( + magCellLand = "cell.land_0.5_share.nc", + outFile = "cell.land_0.5_SEALS.nc", + scenName = NULL, + dir = ".", + selectyears = c(2015, 2050) +) +} +\arguments{ +\item{magCellLand}{Disaggregated land use (grid-cell land area share) in NetCDF format from a MAgPIE run.} + +\item{outFile}{a file name the output should be written to using \code{ncdf4::nc_create} and \code{ncdf4::ncvar_put}} + +\item{scenName}{Optional scenario name} + +\item{dir}{output directory which contains cellular magpie output} + +\item{selectyears}{Numeric vector of years to provide data for.} +} +\value{ +Proportions of different land use classes per grid sell in a NetCDF format. +} +\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. +} +\examples{ +\dontrun{ +x <- reportLandUseForSEALS(magCellLand = "cell.land_0.5_share.nc", + outFile = "cell.land_0.5_SEALS.nc", + selectyears = c(2015, 2050)) +} + +} +\author{ +Patrick v. Jeetze +}