-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Master mppalves #33
Master mppalves #33
Changes from all commits
22414c4
af2fdf0
b867d95
0a68284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"), | ||
|
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 | ||
|
@@ -11,58 +14,54 @@ | |
#' } | ||
#' @importFrom raster area rasterFromXYZ | ||
|
||
calcPastureSuit <- function(subtype = "ISIMIP3bv2:IPSL-CM6A-LR:1850-2100", smoothPrecipitation = 10, smoothOut = 10) { | ||
x <- toolSplitSubtype(subtype, list(version = NULL, climatemodel = NULL, period = NULL)) | ||
|
||
# pasture drivers | ||
calcPastureSuit <- function(datasource = "ISIMIP3bv2", climatetype = "MRI-ESM2-0:ssp126", | ||
lpjml = "LPJmL4_for_MAgPIE_44ac93de", smoothPrecipitation = 10, smoothOut = 10) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need 'smoothPrecipitation' and 'smoothOut' anymore. Also the datasource argument is a bit unclear to me. It isn't used anymore, right? |
||
x <- toolSplitSubtype(climatetype, list(climatemodel = NULL, scenario = NULL)) | ||
# Drivers of managed pastures | ||
population <- calcOutput("GridPop", subtype = "all", cellular = TRUE, FiveYear = TRUE, | ||
harmonize_until = 2015, aggregate = FALSE) | ||
|
||
precipitation <- list() | ||
scenarios <- c("ssp126", "ssp245", "ssp370", "ssp460", "ssp585") # Current ISIMIP3bv2 scenarios | ||
for (scenario in scenarios) { | ||
subtype <- paste(x$version, x$climatemodel, scenario, x$period, "pr", "annual_mean", sep = ":") | ||
precipitation[[scenario]] <- setNames(calcOutput("GCMClimate", subtype = subtype, | ||
smooth = smoothPrecipitation, aggregate = FALSE), scenario) | ||
} | ||
precipitation <- collapseNames(mbind(precipitation)) | ||
|
||
evapotranspiration <- calcOutput("Evapotranspiration", subtype = "H08:mri-esm2-0", aggregate = FALSE) | ||
|
||
# temporary mapping of evapotranspiration RCP scenarios unavailable in ISIMIP3bv2 | ||
evapotranspiration <- add_columns(evapotranspiration, addnm = "ssp245", dim = 3.1, fill = NA) | ||
evapotranspiration[, , "ssp245"] <- evapotranspiration[, , "ssp370"] | ||
evapotranspiration <- add_columns(evapotranspiration, addnm = "ssp460", dim = 3.1, fill = NA) | ||
evapotranspiration[, , "ssp460"] <- evapotranspiration[, , "ssp370"] | ||
|
||
evapotranspiration <- evapotranspiration[, , getItems(precipitation, dim = 3)] | ||
|
||
# matching available ssps scenarios | ||
regex <- paste0("[", paste0("+", strtrim(getItems(evapotranspiration, dim = 3), 4), collapse = "|"), "]", "{4}$") | ||
avlSSPs <- grep(regex, getNames(population), ignore.case = TRUE) | ||
population <- population[, , avlSSPs] | ||
harmonize_until = 2015, | ||
aggregate = FALSE)[, , toupper(substring(x$scenario, first = 0, last = 4))] | ||
|
||
cellPrep <- calcOutput("LPJmLClimateInput", climatetype = climatetype, | ||
variable = "precipitation:monthlySum", | ||
stage = "smoothed", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stage has to be 'harmonized2020' (as this is future data, right?) |
||
lpjmlVersion = lpjml, | ||
aggregate = FALSE) | ||
|
||
|
||
cellPet <- calcOutput(type = "LPJmL_new", climatetype = climatetype, | ||
subtype = "mpet", | ||
stage = "smoothed", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stage has to be 'harmonized2020' (as this is future data, right?) |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems you only need yearly precipitation and pet, is this correct? |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you really need cellsize or landarea size of a given cell? there is a function already calculating this: |
||
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 | ||
|
||
years <- intersect(getYears(popDensity), getYears(cellPrep)) | ||
|
||
yearsCom <- intersect(getYears(popDensity), getYears(precipitation)) | ||
|
||
# Aridity (the real aridity is measured as the ratio between evapotranspiration | ||
# and precipitarion (I have complete this calculation)) | ||
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 | ||
|
@@ -90,9 +89,11 @@ calcPastureSuit <- function(subtype = "ISIMIP3bv2:IPSL-CM6A-LR:1850-2100", smoot | |
map <- toolGetMapping("CountryToCellMapping.csv", type = "cell") | ||
pastureSuitAreaReg <- toolAggregate(pastureSuitArea, rel = map, from = "celliso", to = "iso") | ||
histPastrReg <- toolAggregate(histPastr, rel = map, from = "celliso", to = "iso") | ||
corrReg <- histPastrReg[, pastLy, ] / pastureSuitAreaReg[, pastLy, ] | ||
pastureSuitArea[, future, ] <- toolAggregate(corrReg, rel = map, from = "iso", to = "celliso") * | ||
pastureSuitArea[, future, ] | ||
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[is.infinite(pastureSuitArea) | is.nan(pastureSuitArea) | is.na(pastureSuitArea)] <- 0 | ||
pastureSuitArea[pastureSuitArea < 0] <- 0 | ||
|
@@ -106,7 +107,7 @@ calcPastureSuit <- function(subtype = "ISIMIP3bv2:IPSL-CM6A-LR:1850-2100", smoot | |
pastureSuitArea <- toolHoldConstant(pastureSuitArea, findset("time")) | ||
pastureSuitArea <- collapseNames(pastureSuitArea) | ||
pastureSuitArea[, pastAll, ] <- histPastr[, pastAll, ] | ||
|
||
pastureSuitArea <- setNames(pastureSuitArea, "yields") | ||
return(list( | ||
x = pastureSuitArea, | ||
weight = NULL, | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -206,12 +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 = ":"), | ||||
calcOutput("PastureSuit", datasource = "ISIMIP3bv2", climatetype = "MRI-ESM2-0:ssp126", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. climatetype and lpjml argument has to be set to the flexible arguments as they change with the preprocessing settings. See e.g.: mrmagpie/R/fullCELLULARMAGPIE.R Line 322 in 5b6edca
My guess how the call should look like: Check which years make most sense (magYears or lpjYears) and the lpjml version ('natveg' or 'grass'). |
||||
lpjml = "LPJmL4_for_MAgPIE_44ac93de", | ||||
file = paste0("f31_pastr_suitability_", ctype, ".mz"), years = magYears, aggregate = "cluster") | ||||
calcOutput("PastureSuit", subtype = paste("ISIMIP3bv2", "MRI-ESM2-0", "1850_2100", sep = ":"), | ||||
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), | ||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the "Document" here stands for? ^^'