Skip to content

Commit

Permalink
WIP on master: af2fdf0 Merge branch 'master' of https://github.com/pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
mppalves committed Jul 18, 2023
2 parents af2fdf0 + b867d95 commit 0a68284
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '26521208'
ValidationKey: '26555690'
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: 'mrmagpie: madrat based MAgPIE Input Data Library'
version: 1.35.7
date-released: '2023-07-06'
version: 1.35.8
date-released: '2023-07-17'
abstract: Provides functions for MAgPIE country and cellular input data generation.
authors:
- family-names: Karstens
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: mrmagpie
Title: madrat based MAgPIE Input Data Library
Version: 1.35.7
Date: 2023-07-06
Version: 1.35.8
Date: 2023-07-17
Authors@R: c(
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
61 changes: 37 additions & 24 deletions R/calcPastureSuit.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' @title calcPastureSuit
#' @description Calculate glassland suitable for pasture management based on population and aridity criteria.
#' @param subtype Select version, climate model and period.
#' @description Calculate glassland suitable for pasture management based
#' on population and aridity criteria.
#' @param datasource Document
#' @param climatetype Document
#' @param lpjml Document
#' @param smoothPrecipitation Smooth precipitation climate data over time
#' @param smoothOut smooth the Pasture suitability areas variations over time
#' @return List of magpie object with results on cluster level
Expand All @@ -11,36 +14,54 @@
#' }
#' @importFrom raster area rasterFromXYZ

calcPastureSuit <- function(datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:ssp126", smoothPrecipitation = 10, smoothOut = 10) {
calcPastureSuit <- function(datasource = "ISIMIP3bv2", climatetype = "MRI-ESM2-0:ssp126",
lpjml = "LPJmL4_for_MAgPIE_44ac93de", smoothPrecipitation = 10, smoothOut = 10) {
x <- toolSplitSubtype(climatetype, list(climatemodel = NULL, scenario = NULL))
period = "1850-2100"
# Drivers of managed pastures
subtype <- paste(datasource, x$climatemodel, x$scenario, period, "pr", "annual_mean", sep = ":")
precipitation <- calcOutput("GCMClimate", subtype = subtype, smooth = smoothPrecipitation, aggregate = FALSE)
population <- calcOutput("GridPop", subtype = "all", cellular = TRUE, FiveYear = TRUE,
harmonize_until = 2015, aggregate = FALSE)[,,toupper(substring(x$scenario,first = 0,last = 4))]
evapotranspiration <- calcOutput("Evapotranspiration", subtype = "H08:mri-esm2-0", aggregate = FALSE)[,,x$scenario]
# Need new function for evapt. Should be something like this. So it gets LPJMl evap for the same climate model and spp as the prec
# evapotranspiration <- calcOutput("Evapotranspiration", datasource = "LPJmL"?, climatetype = climatetype, aggregate = FALSE)
harmonize_until = 2015,
aggregate = FALSE)[, , toupper(substring(x$scenario, first = 0, last = 4))]

cellPrep <- calcOutput("LPJmLClimateInput", climatetype = climatetype,
variable = "precipitation:monthlySum",
stage = "smoothed",
lpjmlVersion = lpjml,
aggregate = FALSE)


cellPet <- calcOutput(type = "LPJmL_new", climatetype = climatetype,
subtype = "mpet",
stage = "smoothed",
version = lpjml,
aggregate = FALSE)
cellPrep <- mrwater::toolLPJcell2MAgPIEcell(cellPrep)
cellPet <- mrwater::toolLPJcell2MAgPIEcell(cellPet)

yearsCellPet <- intersect(getYears(cellPet), findset("time"))
yearsCellPrep <- intersect(findset("time"), getYears(cellPrep))
years <- intersect(yearsCellPet, yearsCellPrep)
cellPrep <- dimSums(cellPrep[, years, ], dim = 3)
cellPet <- dimSums(cellPet[, years, ], dim = 3)

# Cell area calculation
landcoords <- as.data.frame(toolGetMapping("magpie_coord.rda", type = "cell", where = "mappingfolder"))
landcoords <- cbind(landcoords, rep(1, nrow(landcoords)))
landcoords <- raster::rasterFromXYZ(landcoords)
crs(landcoords) <- "+proj=longlat" # outputs cell are in km2
raster::crs(landcoords) <- "+proj=longlat" # outputs cell are in km2
cellSize <- raster::area(landcoords)
cellSize <- cellSize * landcoords
cellSize <- as.magpie(cellSize)
cellSize <- toolOrderCells(collapseDim(addLocation(cellSize), dim = c("x", "y")))

# population density
population <- population[getCells(cellPet), , ] # fixing the order of the population cells (should not be necessary!)
popDensity <- (population * 1e6) / cellSize # population density in number of people per km2
popDensity[is.infinite(popDensity)] <- 0
popDensity[is.nan(popDensity)] <- 0

yearsCom <- intersect(getYears(popDensity), getYears(precipitation))
years <- intersect(getYears(popDensity), getYears(cellPrep))

aridity <- precipitation[, yearsCom, ] / evapotranspiration[, yearsCom, ]
aridity <- cellPrep[, years, ] / cellPet[, years, ]
aridity[is.infinite(aridity) | is.nan(aridity)] <- 0
# 0.5 aridity threshold for managed pastures. Same from HYDE 3.2.
aridity[aridity < 0.5] <- 0
Expand Down Expand Up @@ -71,7 +92,8 @@ calcPastureSuit <- function(datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:
calibReg <- histPastrReg[, pastLy, ] / pastureSuitAreaReg[, pastLy, ]
calibReg[is.infinite(calibReg)] <- 1
calibReg[is.nan(calibReg)] <- 0
pastureSuitArea[, future, ] <- toolAggregate(calibReg, rel = map, from = "iso", to = "celliso") * pastureSuitArea[, future, ]
pastureSuitArea[, future, ] <- toolAggregate(calibReg, rel = map,
from = "iso", to = "celliso") * pastureSuitArea[, future, ]

pastureSuitArea[is.infinite(pastureSuitArea) | is.nan(pastureSuitArea) | is.na(pastureSuitArea)] <- 0
pastureSuitArea[pastureSuitArea < 0] <- 0
Expand All @@ -85,7 +107,7 @@ calcPastureSuit <- function(datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:
pastureSuitArea <- toolHoldConstant(pastureSuitArea, findset("time"))
pastureSuitArea <- collapseNames(pastureSuitArea)
pastureSuitArea[, pastAll, ] <- histPastr[, pastAll, ]

pastureSuitArea <- setNames(pastureSuitArea, "yields")
return(list(
x = pastureSuitArea,
weight = NULL,
Expand All @@ -94,12 +116,3 @@ calcPastureSuit <- function(datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:
isocountries = FALSE
))
}


pet <- calcOutput("LPJmL_new", version = "LPJmL4_for_MAgPIE_44ac93de",
climatetype = "MRI-ESM2-0:ssp370", subtype = "mpet",
stage = "smoothed", aggregate = FALSE)
prec <- calcOutput("LPJmLClimateInput", lpjmlVersion = "LPJmL4_for_MAgPIE_44ac93de",
climatetype = "GSWP3-W5E5:historical",
variable = "precipitation:monthlySum",
stage = "smoothed", aggregate = FALSE)
12 changes: 4 additions & 8 deletions R/fullCELLULARMAGPIE.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,13 @@ fullCELLULARMAGPIE <- function(rev = 0.1, dev = "",
subtype = "/co2/Nreturn0p5", # nolint
lsu_levels = c(seq(0, 2.2, 0.2), 2.5), past_mngmt = "mdef",
file = paste0("f31_grassl_yld.mz"), years = magYears, aggregate = FALSE)
# calcOutput("PastureSuit", subtype = paste("ISIMIP3bv2", "MRI-ESM2-0", "1850_2100", sep = ":"),
# file = paste0("f31_pastr_suitability_", ctype, ".mz"), years = magYears, aggregate = "cluster")
# calcOutput("PastureSuit", subtype = paste("ISIMIP3bv2", "MRI-ESM2-0", "1850_2100", sep = ":"),
# file = "f31_pastr_suitability.mz", years = magYears, aggregate = FALSE)

calcOutput("PastureSuit", datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:ssp126",
calcOutput("PastureSuit", datasource = "ISIMIP3bv2", climatetype = "MRI-ESM2-0:ssp126",
lpjml = "LPJmL4_for_MAgPIE_44ac93de",
file = paste0("f31_pastr_suitability_", ctype, ".mz"), years = magYears, aggregate = "cluster")
calcOutput("PastureSuit", datasource = "ISIMIP3bv2",climatetype = "MRI-ESM2-0:ssp126",
calcOutput("PastureSuit", datasource = "ISIMIP3bv2", climatetype = "MRI-ESM2-0:ssp126",
lpjml = "LPJmL4_for_MAgPIE_44ac93de",
file = "f31_pastr_suitability.mz", years = magYears, aggregate = FALSE)


if (grepl("+PastrMngtLevels", dev)) {
calcOutput("PastrMngtLevels", climatetype = paste0("MRI-ESM2-0", ":", climatescen),
options = c("brazil_1", "brazil_2", "brazil_4"), cost_level = c(1, 2, 3),
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library

R package **mrmagpie**, version **1.35.7**
R package **mrmagpie**, version **1.35.8**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

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

Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F, Führlich P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.35.7, <URL: https://github.com/pik-piam/mrmagpie>.
Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F, Führlich P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.35.8, <URL: https://github.com/pik-piam/mrmagpie>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrmagpie: madrat based MAgPIE Input Data Library},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Führlich},
year = {2023},
note = {R package version 1.35.7},
note = {R package version 1.35.8},
doi = {10.5281/zenodo.4319612},
url = {https://github.com/pik-piam/mrmagpie},
}
Expand Down
13 changes: 10 additions & 3 deletions man/calcPastureSuit.Rd

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

0 comments on commit 0a68284

Please sign in to comment.