Skip to content

Commit

Permalink
temporary correction of mismatches due to lpj-magpie crop mapping unt…
Browse files Browse the repository at this point in the history
…il more generic fix is found
  • Loading branch information
FelicitasBeier committed Nov 9, 2023
1 parent 1a7ac98 commit 2deb830
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '21853370'
ValidationKey: '21873040'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrwater: madrat based MAgPIE water Input Data Library'
version: 1.11.1
version: 1.11.2
date-released: '2023-11-09'
abstract: Provides functions for MAgPIE cellular input data generation and stand-alone
water calculations.
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrwater
Title: madrat based MAgPIE water Input Data Library
Version: 1.11.1
Version: 1.11.2
URL: https://github.com/pik-piam/mrwater,
https: //doi.org/10.5281/zenodo.5801680
License: LGPL-3 | file LICENSE
Expand Down
88 changes: 61 additions & 27 deletions R/calcPotMulticroppingShare.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' On areas where irrigation expansion takes place, full multiple cropping is
#' assumed.
#'
#' @param scenario EFP and non-agricultural water use scenario separated with a "."
#' (e.g. "on.ssp2")
#' @param lpjml LPJmL version used
#' @param climatetype Switch between different climate scenarios or
#' historical baseline "GSWP3-W5E5:historical"
Expand Down Expand Up @@ -78,7 +80,7 @@
#' @importFrom madrat calcOutput
#' @importFrom magclass collapseNames add_dimension add_columns mbind

calcPotMulticroppingShare <- function(lpjml, climatetype,
calcPotMulticroppingShare <- function(scenario, lpjml, climatetype,
selectyears, iniyear,
efrMethod, accessibilityrule,
rankmethod, yieldcalib, allocationrule,
Expand All @@ -104,19 +106,25 @@ calcPotMulticroppingShare <- function(lpjml, climatetype,
aggregate = FALSE)
crops <- getItems(watReqFirst, dim = "crop")

# Correction of single cropping water requirements for perennials that
# are proxied with an annual crop
specialCrops <- c("oilpalm", "others", "cottn_pro")
watReqFirst[, , specialCrops] <- watReqYear[, , specialCrops]
# To Do: check with JENS and KRISTINE

# Potential irrigation water use (in mio. m^3 per year):
# This includes committed agricultural water use, multiple cropping expansion on irrigated areas,
# fossil groundwater and additional irrigation water potential
watPotAvl <- calcOutput("PotWater",
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
efrMethod = efrMethod, accessibilityrule = accessibilityrule,
rankmethod = rankmethod, yieldcalib = yieldcalib,
allocationrule = allocationrule, gainthreshold = gainthreshold,
irrigationsystem = irrigationsystem, landScen = landScen,
cropmix = cropmix, comAg = comAg, fossilGW = fossilGW,
multicropping = multicropping, transDist = transDist,
aggregate = FALSE)
watPotAvl <- collapseNames(calcOutput("PotWater",
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
efrMethod = efrMethod, accessibilityrule = accessibilityrule,
rankmethod = rankmethod, yieldcalib = yieldcalib,
allocationrule = allocationrule, gainthreshold = gainthreshold,
irrigationsystem = irrigationsystem, landScen = landScen,
cropmix = cropmix, comAg = comAg, fossilGW = fossilGW,
multicropping = multicropping, transDist = transDist,
aggregate = FALSE)[, , scenario])
watPotAvlWW <- collapseNames(watPotAvl[, , "wat_ag_ww"])
watPotAvlWC <- collapseNames(watPotAvl[, , "wat_ag_wc"])
rm(watPotAvl)
Expand Down Expand Up @@ -148,14 +156,12 @@ calcPotMulticroppingShare <- function(lpjml, climatetype,

# Actually committed irrigated area (crop-specific) (in Mha)
# including non-renewable groundwater (if activated)
comAgArea <- calcOutput("IrrigAreaActuallyCommitted",
fossilGW = fossilGW,
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
efrMethod = efrMethod, multicropping = m,
transDist = transDist, aggregate = FALSE)
getSets(comAgArea) <- c("x", "y", "iso", "year", "crop", "EFP", "scen")
comAgArea <- collapseNames(dimOrder(comAgArea, perm = c(2, 3, 1), dim = 3))
comAgArea <- collapseNames(calcOutput("IrrigAreaActuallyCommitted",
fossilGW = fossilGW,
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
efrMethod = efrMethod, multicropping = m,
transDist = transDist, aggregate = FALSE)[, , scenario])

# Water use for committed agricultural areas
# in main season
Expand All @@ -168,12 +174,6 @@ calcPotMulticroppingShare <- function(lpjml, climatetype,
comAgWatSecondWW <- comAgWatYearWW - comAgWatFirstWW
comAgWatSecondWC <- comAgWatYearWC - comAgWatFirstWC

# Check: watPotAvl > comAgWatFirst for case of comAg=TRUE
if (any(round(watPotAvlWW - comAgWatFirstWW, digits = 6) < 0)) {
stop("When comAg is activated, there should be enough water for the irrigation of the
main season of currently irrigated areas.
Please check what's wrong in calcPotMulticroppingShare!")
}
# Check: comAgWatSecond should be 0 for perennials and where not suitable for MC under irrigated conditions
perennials <- c("oilpalm", "sugr_cane") # what about "others" and "cottn_pro"?
if (any(round(comAgWatSecondWW[, , perennials], digits = 6) != 0)) {
Expand All @@ -199,19 +199,53 @@ calcPotMulticroppingShare <- function(lpjml, climatetype,
comAgWatSecondWC <- dimSums(comAgWatSecondWC, dim = "crop")
comAgWatFirstWW <- dimSums(comAgWatFirstWW, dim = "crop")
comAgWatFirstWC <- dimSums(comAgWatFirstWC, dim = "crop")
remainingWatWW <- watPotAvlWW - comAgWatFirstWW
remainingWatWC <- watPotAvlWC - comAgWatFirstWC

# Check: watPotAvl > comAgWatFirst for case of comAg=TRUE
if (any(round(remainingWatWW, digits = 6) < 0)) {
warning("When comAg is activated, there should be enough water for the irrigation of the
main season of currently irrigated areas.
Please fix the lpj-magpie-perennial mismatch!")
# Correction due to mismatch of lpj and magpie perennial crops
# (Note: remove once this inconsistency is solved)
remainingWatWW[remainingWatWW < 0] <- 0
remainingWatWC[remainingWatWC < 0] <- 0
}

# Share of water for second season that can be fulfilled
# with remaining water after first season irrigation.
outWW <- ifelse(comAgWatSecondWW > 0,
(watPotAvlWW - comAgWatFirstWW) / comAgWatSecondWW,
remainingWatWW / comAgWatSecondWW,
NA)
outWC <- ifelse(comAgWatSecondWC > 0,
(watPotAvlWC - comAgWatFirstWC) / comAgWatSecondWC,
remainingWatWC / comAgWatSecondWC,
NA)

outWW[outWW > 1] <- 1
outWC[outWC > 1] <- 1

# Object dimensions:
.transformObject <- function(x, gridcells, years, names) {
# empty magpie object structure
object0 <- new.magpie(cells_and_regions = gridcells,
years = years,
names = names,
fill = 0,
sets = c("x.y.iso", "year", "crop"))
# bring object x to dimension of object0
out <- object0 + x
return(out)
}
outWW <- .transformObject(x = outWW,
gridcells = getItems(suitMCir, dim = 1),
years = getItems(suitMCir, dim = 2),
names = crops)
outWC <- .transformObject(x = outWC,
gridcells = getItems(suitMCir, dim = 1),
years = getItems(suitMCir, dim = 2),
names = crops)

# Where no committed agriculture:
# full multiple cropping is assumed where it is suitable
outWW[is.na(outWW)] <- suitMCir[is.na(outWW)]
Expand Down
2 changes: 1 addition & 1 deletion R/fullMULTICROPPING.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fullMULTICROPPING <- function(allocationrule = "optimization",
file = "piwIRR_multPOT.mz")

# Potential muliple cropping share
calcOutput("PotMulticroppingShare",
calcOutput("PotMulticroppingShare", scenario = paste(efp, ssp, sep = "."),
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
efrMethod = efrMethod, accessibilityrule = accessibilityrule,
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 water Input Data Library

R package **mrwater**, version **1.11.1**
R package **mrwater**, version **1.11.2**

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

Expand Down Expand Up @@ -50,7 +50,7 @@ In case of questions / problems please contact Felicitas Beier <beier@pik-potsda

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

Beier F, Heinke J, Karstens K, Bodirsky B, Dietrich J (2023). _mrwater: madrat based MAgPIE water Input Data Library_. R package version 1.11.1, <https://github.com/pik-piam/mrwater>.
Beier F, Heinke J, Karstens K, Bodirsky B, Dietrich J (2023). _mrwater: madrat based MAgPIE water Input Data Library_. R package version 1.11.2, <https://github.com/pik-piam/mrwater>.

A BibTeX entry for LaTeX users is

Expand All @@ -59,7 +59,7 @@ A BibTeX entry for LaTeX users is
title = {mrwater: madrat based MAgPIE water Input Data Library},
author = {Felicitas Beier and Jens Heinke and Kristine Karstens and Benjamin Leon Bodirsky and Jan Philipp Dietrich},
year = {2023},
note = {R package version 1.11.1},
note = {R package version 1.11.2},
url = {https://github.com/pik-piam/mrwater},
}
```
4 changes: 4 additions & 0 deletions man/calcPotMulticroppingShare.Rd

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

0 comments on commit 2deb830

Please sign in to comment.