Skip to content

Commit

Permalink
reworked gridded output of protectedArea.R
Browse files Browse the repository at this point in the history
  • Loading branch information
pvjeetze committed Aug 13, 2023
1 parent 4ce8f28 commit df2ba69
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '233229291'
ValidationKey: '233260784'
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.191.1
date-released: '2023-08-12'
version: 1.191.2
date-released: '2023-08-13'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
authors:
Expand Down
4 changes: 2 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.191.1
Date: 2023-08-12
Version: 1.191.2
Date: 2023-08-13
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
Expand Down
110 changes: 106 additions & 4 deletions R/protectedArea.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @export
#'
#' @param gdx GDX file
#' @param cfg config file
#' @param file a file name the output should be written to using write.magpie
#' @param level Level of regional aggregation; "cell", "grid", "iso, "reg" (regional), "glo" (global), "regglo" (regional and global) or any secdforest aggregation level defined in superAggregate
#' @param sum sum over land pools (default = FALSE)
Expand All @@ -20,14 +21,116 @@
#' x <- protectedArea(gdx)
#' }
#'
protectedArea <- function(gdx, file = NULL, level = "cell", sum = FALSE, dir = "."){
protectedArea <- function(gdx, cfg = NULL, file = NULL, level = "cell", sum = FALSE, dir = "."){

# read in protected areas
if (level %in% c("grid","iso")) {
mapfile <- system.file("extdata", "mapping_grid_iso.rds", package="magpie4")
map_grid_iso <- readRDS(mapfile)
a <- setCells(read.magpie(file.path(dir, "cell.conservation_land_0.5.mz")), map_grid_iso$grid)
if(level == "iso") a <- gdxAggregate(gdx, a , to = "iso", dir = dir)
cmap <- Sys.glob(file.path(dir, "clustermap_*.rds"))

wdpa_hr_file <- c(
file.path(dir, "wdpa_baseline_0.5.mz"),
"input/wdpa_baseline_0.5.mz",
"modules/22_land_conservation/input/wdpa_baseline_0.5.mz",
"../input/wdpa_baseline_0.5.mz",
"../modules/22_land_conservation/input/wdpa_baseline_0.5.mz",
"../../input/wdpa_baseline_0.5.mz",
"../../modules/22_land_conservation/input/wdpa_baseline_0.5.mz.mz"
)
wdpa_hr_file <- suppressWarnings(wdpa_hr_file[min(which(file.exists(wdpa_hr_file)))])

consv_prio_hr_file <- c(
file.path(dir, "consv_prio_areas_0.5.mz"),
"input/consv_prio_areas_0.5.mz",
"modules/22_land_conservation/input/consv_prio_areas_0.5.mz",
"../input/consv_prio_areas_0.5.mz",
"../modules/22_land_conservation/input/consv_prio_areas_0.5.mz",
"../../input/consv_prio_areas_0.5.mz",
"../../modules/22_land_conservation/input/consv_prio_areas_0.5.mz"
)
consv_prio_hr_file <- suppressWarnings(consv_prio_hr_file[min(which(file.exists(consv_prio_hr_file)))])

if (!is.na(wdpa_hr_file)) {

land_consv_lr <- readGDX(gdx, "pm_land_conservation", react = "silent")
wdpa_hr <- read.magpie(wdpa_hr_file)

# create full time series
land_consv_hr <- new.magpie(map_grid_iso$grid, getYears(land_consv_lr), getNames(wdpa_hr))
land_consv_hr[, getYears(land_consv_hr), ] <- wdpa_hr[, nyears(wdpa_hr), ]
land_consv_hr[, getYears(wdpa_hr), ] <- wdpa_hr

if (!is.null(cfg)) {
if (!all(c(cfg$gms$c22_protect_scenario, cfg$gms$c22_protect_scenario_noselect) %in% "none")) {
if (!is.na(consv_prio_hr_file)) {
consv_prio_all <- read.magpie(consv_prio_hr_file)
consv_prio_hr <- new.magpie(
cells_and_regions = map_grid_iso$grid,
names = getNames(consv_prio_all, dim = 2), fill = 0
)
iso <- readGDX(gdx, "iso")
consv_iso <- readGDX(gdx, "policy_countries22")
consv_iso <- consv_iso[consv_iso %in% getItems(consv_prio_all, dim = 1.1)]
consv_select <- cfg$gms$c22_protect_scenario
consv_noselect <- cfg$gms$c22_protect_scenario_noselect

if (consv_noselect != "none") {
consv_prio_hr <- collapseDim(consv_prio_all[, , consv_noselect], dim = 3.1)
}
if (consv_select != "none") {
consv_prio_hr[consv_iso, , ] <- collapseDim(consv_prio_all[consv_iso, , consv_select], dim = 3.1)
} else if (consv_select == "none") {
consv_prio_hr[consv_iso, , ] <- 0
}
# future conservation only pertains to natveg
consv_prio_hr[, , c("crop", "past", "forestry", "urban")] <- 0

consv_fader <- readGDX(gdx, "p22_conservation_fader", format = "first_found")
consv_prio_hr <- consv_prio_hr * consv_fader[, getYears(land_consv_hr), ]

# add conservation priority areas
land_consv_hr <- (land_consv_hr + consv_prio_hr)
} else {
warning(paste(
"Future land conservation used in MAgPIE run but high resolution",
"conservation priority data for disaggregation not found."
))
}
}
}

# Due to internal constraints and compensation (e.g. NDC forest conservation)
# the actual land conservation can sometimes be smaller than the land
# conservation in the input data (this can especially happen also if
# land restoration is switched off). Therefore a scaling is applied here separately
# for grassland and natural vegetation
if (!is.null(land_consv_lr)) {
natveg <- c("primforest", "secdforest", "other")
consv_sum_lr <- mbind(
dimSums(land_consv_lr[, , "past"], 3.2),
setNames(dimSums(land_consv_lr[, , natveg], dim = 3), "natveg")
)
consv_sum_hr_agg <- mbind(
toolAggregate(land_consv_hr[, , "past"], cmap, from = "cell", to = "cluster"),
toolAggregate(setNames(dimSums(land_consv_hr[, , natveg], dim = 3), "natveg"),
cmap,
from = "cell", to = "cluster"
)
)
consv_scaling <- consv_sum_lr / consv_sum_hr_agg
consv_scaling[is.na(consv_scaling) | is.infinite(consv_scaling)] <- 1
consv_scaling <- toolAggregate(consv_scaling, cmap, from = "cluster", to = "cell")
land_consv_hr[, , "past"] <- consv_scaling[, , "past"] * land_consv_hr[, , "past"]
land_consv_hr[, , natveg] <- consv_scaling[, , "natveg"] * land_consv_hr[, , natveg]
}
a <- land_consv_hr
} else {
stop("Cannot find gridded land conservation input data in output folder.")
}
if(level == "iso") {
a <- gdxAggregate(gdx, a , to = "iso", dir = dir)
}
} else {
a <- readGDX(gdx, "pm_land_conservation", react = "silent")
# sum protection and restoration area
Expand Down Expand Up @@ -61,6 +164,5 @@ protectedArea <- function(gdx, file = NULL, level = "cell", sum = FALSE, dir = "
# sum
if (sum) a <- dimSums(a, dim = 3.1)


out(a, file)
}
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.191.1**
R package **magpie4**, version **1.191.2**

[![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.191.1, <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 (URL: https://doi.org/10.5281/zenodo.1158582), R package version 1.191.2, <URL: 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.191.1},
note = {R package version 1.191.2},
doi = {10.5281/zenodo.1158582},
url = {https://github.com/pik-piam/magpie4},
}
Expand Down
13 changes: 11 additions & 2 deletions man/protectedArea.Rd

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

0 comments on commit df2ba69

Please sign in to comment.