Skip to content

Commit

Permalink
groundwater accounting bugfix and introduction of calcWateruseActuall…
Browse files Browse the repository at this point in the history
…yCommittedAg
  • Loading branch information
FelicitasBeier committed Jul 17, 2023
1 parent 013fcc9 commit 11f94c8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3441152'
ValidationKey: '3461235'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
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: 'mrwater: madrat based MAgPIE water Input Data Library'
version: 1.7.6
date-released: '2023-07-14'
version: 1.7.7
date-released: '2023-07-17'
abstract: Provides functions for MAgPIE cellular input data generation and stand-alone
water calculations.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Type: Package
Package: mrwater
Title: madrat based MAgPIE water Input Data Library
Version: 1.7.6
Version: 1.7.7
URL: https://github.com/pik-piam/mrwater,
https: //doi.org/10.5281/zenodo.5801680
License: LGPL-3 | file LICENSE
Date: 2023-07-14
Date: 2023-07-17
Authors@R: c(person("Felicitas", "Beier", email = "beier@pik-potsdam.de", role = c("aut","cre")),
person("Jens", "Heinke", email = "heinke@pik-potsdam.de", role = "aut"),
person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = "aut"),
Expand Down
5 changes: 3 additions & 2 deletions R/calcIrrigAreaActuallyCommitted.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ calcIrrigAreaActuallyCommitted <- function(iteration = "committed_agriculture",
# Fossil groundwater use to fulfill committed agricultural water use
if (fossilGW) {
gw <- calcOutput("NonrenGroundwatUse",
output = "comAg", multicropping = m,
output = "comAg",
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
aggregate = FALSE)
Expand All @@ -148,14 +148,15 @@ calcIrrigAreaActuallyCommitted <- function(iteration = "committed_agriculture",
Please make sure that the fulfilled ratio is the same
for consumption and withdrawal")
}
if (any(round(wcShr[, iniyear, "off"], digits = 4) > 1)) {
if (m != FALSE && any(round(wcShr[, iniyear, "off"], digits = 4) > 1)) {
stop("In calcAreaActuallyCommitted: Water requirements are over-fulfilled.
This should not be the case in the initialization year. Please double-check!
A wild guess: it might be related to the fossil groundwater calculation.")
}

# In future time steps (after initialization year) [because of depreciation of irrigated areas]
# and under an environmental flow policy scenario,
# and in the single-cropping scenario [because default is current multiple cropping],
# the irrigated area may be over-fulfilled by non-renewable groundwater.
# These values are capped to 1:
wcShr[wcShr > 1] <- 1
Expand Down
44 changes: 18 additions & 26 deletions R/calcIrrigAreaPotential.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ calcIrrigAreaPotential <- function(cropAggregation,
cropmix, comAg, fossilGW,
multicropping, transDist) {

## Read in water available for irrigation (in mio. m^3)
## Read in (renewable and non-renewable) water available for irrigation (in mio. m^3)
avlWat <- calcOutput("WaterUsePotential", selectyears = selectyears,
lpjml = lpjml, climatetype = climatetype, efrMethod = efrMethod,
accessibilityrule = accessibilityrule, rankmethod = rankmethod,
Expand Down Expand Up @@ -124,30 +124,14 @@ calcIrrigAreaPotential <- function(cropAggregation,
comAgArea <- collapseNames(dimOrder(comAgArea, perm = c(3, 4, 2, 1), dim = 3))

# Water actually committed to agriculture (in mio. m^3)
comWater <- calcOutput("RiverHumanUseAccounting",
iteration = "committed_agriculture",
lpjml = lpjml, climatetype = climatetype,
transDist = transDist, comAg = NULL,
efrMethod = efrMethod, multicropping = m,
selectyears = selectyears, iniyear = iniyear,
accessibilityrule = NULL,
rankmethod = NULL, gainthreshold = NULL,
cropmix = NULL, yieldcalib = NULL,
irrigationsystem = NULL, landScen = NULL,
aggregate = FALSE)
comWatWW <- collapseNames(comWater[, , "currHumanWWtotal"])
comWatWC <- collapseNames(comWater[, , "currHumanWCtotal"])

# Fossil groundwater use
if (fossilGW) {
gw <- calcOutput("NonrenGroundwatUse", output = "comAg",
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
aggregate = FALSE)
comWatWW <- comWatWW + collapseNames(gw[, , "withdrawal"])
comWatWC <- comWatWC + collapseNames(gw[, , "consumption"])
}

comWatAct <- calcOutput("WaterUseActuallyCommittedAg",
lpjml = lpjml, climatetype = climatetype,
selectyears = selectyears, iniyear = iniyear,
multicropping = m, efrMethod = efrMethod,
fossilGW = fossilGW, transDist = transDist,
aggregate = FALSE)
comWatWW <- collapseNames(dimSums(comWatAct, dim = "crop")[, , "withdrawal"])
comWatWC <- collapseNames(dimSums(comWatAct, dim = "crop")[, , "consumption"])

} else {

Expand All @@ -158,10 +142,18 @@ calcIrrigAreaPotential <- function(cropAggregation,
comWatWC[, , ] <- 0
}

# Water available for potential additional irrigation (in mio. m^3)
# Water available for potential additional irrigation beyond committed ag. use (in mio. m^3)
avlWatWW <- avlWatWW - comWatWW
avlWatWC <- avlWatWC - comWatWC

# Check
if (avlWatWW < 0 || avlWatWC < 0) {
stop("In calcIrrigAreaPotential: available water for additional irrigation beyond
committed agricultural use became negative. This should not be the case.
Please double check. A wild guess: this may be related to the non-renewabled
groundwater implementation.")
}

# Irrigation water requirements for selected cropmix and irrigation system per cell (in mio. m^3)
watReq <- calcOutput("FullIrrigationRequirement", selectyears = selectyears,
lpjml = lpjml, climatetype = climatetype, iniyear = iniyear,
Expand Down
72 changes: 72 additions & 0 deletions R/calcWaterUseActuallyCommittedAg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' @title calcWaterUseActuallyCommittedAg
#' @description This function calculates committed agricultural water uses that
#' are used in the river routing algorithm for distributing
#' available water across the basin
#'
#' @param selectyears Years to be returned
#' @param iniyear Year of initialization for cropland area
#' @param lpjml LPJmL version required for respective inputs: natveg or crop
#' @param climatetype Switch between different climate scenarios or
#' historical baseline "GSWP3-W5E5:historical"
#' @param efrMethod EFR method used including selected strictness of EFRs (e.g. Smakhtin:good, VMF:fair)
#' @param fossilGW If TRUE: non-renewable groundwater can be used.
#' If FALSE: non-renewable groundwater cannot be used.
#' @param multicropping Multicropping activated (TRUE) or not (FALSE) and
#' Multiple Cropping Suitability mask selected
#' (mask can be:
#' "none": no mask applied (only for development purposes)
#' "actual:total": currently multicropped areas calculated from total harvested areas
#' and total physical areas per cell from readLanduseToolbox
#' "actual:crop" (crop-specific), "actual:irrigation" (irrigation-specific),
#' "actual:irrig_crop" (crop- and irrigation-specific) "total"
#' "potential:endogenous": potentially multicropped areas given
#' temperature and productivity limits
#' "potential:exogenous": potentially multicropped areas given
#' GAEZ suitability classification)
#' (e.g. TRUE:actual:total; TRUE:none; FALSE)
#' @param transDist Water transport distance allowed to fulfill locally
#' unfulfilled water demand by surrounding cell water availability
#'
#' @importFrom stringr str_split
#' @importFrom madrat calcOutput
#'
#' @return magpie object in cellular resolution
#' @author Felicitas Beier
#'
#' @examples
#' \dontrun{
#' calcOutput("WaterUseActuallyCommittedAg", aggregate = FALSE)
#' }
#'
calcWaterUseActuallyCommittedAg <- function(lpjml, climatetype,
selectyears, iniyear,
multicropping, efrMethod,
fossilGW, transDist) {

# Irrigation water requirements per cell per crop given irrigation
# system initialization (in m^3 per hectare per year)
irrigReq <- calcOutput("ActualIrrigWatRequirements",
irrigationsystem = "initialization",
selectyears = selectyears, iniyear = iniyear,
lpjml = lpjml, climatetype = climatetype,
multicropping = multicropping, aggregate = FALSE)

# Read in cropland area (by crop) from crop area initialization (in mio. ha)
grownCrops <- calcOutput("IrrigAreaActuallyCommitted", iteration = "committed_agriculture",
selectyears = selectyears, iniyear = iniyear,
lpjml = lpjml, climatetype = climatetype,
efrMethod = efrMethod, multicropping = multicropping,
transDist = transDist, fossilGW = fossilGW,
aggregate = FALSE)

# Committed agricultural uses (in mio. m^3 per year)
comAg <- irrigReq * grownCrops

return(list(x = comAg,
weight = NULL,
unit = "mio. m^3 per year",
description = paste0("water use actually committed to agriculture ",
"per crop given currently irrigated area and ",
"(renewable and non-renewable) water availability"),
isocountries = FALSE))
}
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.7.6**
R package **mrwater**, version **1.7.7**

[![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 @@ -48,7 +48,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.7.6, <URL: 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.7.7, <https://github.com/pik-piam/mrwater>.

A BibTeX entry for LaTeX users is

Expand All @@ -57,7 +57,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.7.6},
note = {R package version 1.7.7},
url = {https://github.com/pik-piam/mrwater},
}
```
66 changes: 66 additions & 0 deletions man/calcWaterUseActuallyCommittedAg.Rd

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

0 comments on commit 11f94c8

Please sign in to comment.