From f719c47380d3e9a877850a49e468ddbec6331d27 Mon Sep 17 00:00:00 2001 From: mikewin-climsci Date: Tue, 1 Dec 2020 14:16:22 +0100 Subject: [PATCH 1/6] modified calcBphEffect to fill missing entries in the dataset --- R/calcBphEffect.R | 49 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/R/calcBphEffect.R b/R/calcBphEffect.R index 244bcbb66..04d74bfe3 100644 --- a/R/calcBphEffect.R +++ b/R/calcBphEffect.R @@ -13,13 +13,56 @@ calcBphEffect <-function(){ x <- readSource("BphEffect", convert="onlycorrect") + k <- calcOutput("ClimateClass", aggregate = FALSE) + weight <- dimSums(calcOutput("LanduseInitialisation", aggregate=FALSE, cellular=TRUE, land="fao", input_magpie=TRUE, years="y1995", round=6), dim=3) - bph_mask_weight <- calcOutput("BphMask", aggregate="cluster", round=6) + + #mapping to connect cell names with latitudes + map <- toolGetMapping(type="cell", name="CountryToCellMapping.csv") + map <- data.table(map) + + #assuming 0 was NA before. + x[x==0] <- NA + + #check: How many NA cells + length(x[is.na(x)]) + + #extract climate class names + cclass <- getNames(k) + + #select dominating climate class + ccl <- apply(k,1,function(k) { + cclass[which(k==max(k))] + }) + #add to mapping + map <- cbind(map,ccl) + + #global mean, used in case of NA for all cells for a give lat + m_glo <- mean(x,na.rm=TRUE) + #m_glo <- mean(b,na.rm=FALSE) + + #Loop over climate classes + for (sel in cclass) { + print(sel) + #get the magpie cells corresponding to cl + cells <- map[ccl %in% sel,cell] + #find cells with NA + cells_NA <- cells[is.na(x[cells,,])] + #If all cells are NA, use m_glo, otherwise calc mean based on the non NA cells. + if(identical(cells,cells_NA)) { + x[cells_NA,,] <- m_glo + print("Attention: Global mean used!") + } else { + m <- mean(x[cells,,],na.rm=TRUE) + x[cells_NA,,] <- m + } + } + #check: no more NA cells + length(x[is.na(x)]) return(list( x=x, - weight=bph_mask_weight, - unit="degC", + weight=weight, unit="degC", description="Biogeophysical temp change of afforestation in degC", isocountries=FALSE)) } From c83753885dd54feba696cddcfc37522a6191a922 Mon Sep 17 00:00:00 2001 From: FeBeier Date: Thu, 3 Dec 2020 16:31:02 +0100 Subject: [PATCH 2/6] generalization of Environmental flow requirements: instead of volume (mio. m^3) they are denoted as fraction of discharge so that they can be applied to the actually used discharge in the calcWaterAllocation --- R/calcEnvmtlFlowRequirements.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/calcEnvmtlFlowRequirements.R b/R/calcEnvmtlFlowRequirements.R index d4d8fbedc..0054304aa 100644 --- a/R/calcEnvmtlFlowRequirements.R +++ b/R/calcEnvmtlFlowRequirements.R @@ -70,6 +70,8 @@ calcEnvmtlFlowRequirements <- function(version="LPJmL4", climatetype="HadGEM2_ES EFR <- LFR+HFR # Reduce EFR to 50% of available water where it exceeds this threshold (according to Smakhtin 2004) EFR <- pmin(EFR, 0.5*mean_annual_discharge) + # EFR as fraction of mean annual discharge + EFR <- ifelse(mean_annual_discharge>0, EFR/mean_annual_discharge, 0) ### Correct number of cells and transform to magpie object if (cells=="lpjcell"){ @@ -90,7 +92,7 @@ calcEnvmtlFlowRequirements <- function(version="LPJmL4", climatetype="HadGEM2_ES return(list( x=EFR, weight=NULL, - unit="mio. m^3", - description="Environmental flow requirements per cell per year", + unit="fraction of discharge", + description="Environmental flow requirements per cell per year as fraction of discharge", isocountries=FALSE)) } From 6ea467629bf9d181d2e6027dc476e26c1761c799 Mon Sep 17 00:00:00 2001 From: FeBeier Date: Thu, 3 Dec 2020 16:31:36 +0100 Subject: [PATCH 3/6] included harmonization --- R/calcWaterUseCommittedAg.R | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/R/calcWaterUseCommittedAg.R b/R/calcWaterUseCommittedAg.R index 3881ff1b3..cb4058778 100644 --- a/R/calcWaterUseCommittedAg.R +++ b/R/calcWaterUseCommittedAg.R @@ -6,6 +6,8 @@ #' @param time Time smoothing: average or spline (default) #' @param averaging_range Only specify if time=="average": number of time steps to average #' @param dof Only specify if time=="spline": degrees of freedom needed for spline +#' @param harmonize_baseline FALSE (default): no harmonization of input data to this function, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) +#' @param ref_year Reference year for harmonization baseline of input data to this function (just specify when harmonize_baseline=TRUE) #' @param iniyear Year of initialization for cropland area #' @param irrigini Initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell") #' @@ -19,7 +21,7 @@ #' calcWaterUseCommittedAg <- function(version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", selectyears=seq(1995,2095,by=5), - time="spline", dof=4, averaging_range=NULL, iniyear=1995, irrigini="Jaegermeyr_lpjcell"){ + time="spline", dof=4, averaging_range=NULL, harmonize_baseline="CRU_4", ref_year="y2015", iniyear=1995, irrigini="Jaegermeyr_lpjcell"){ ############################## ######## Read in data ######## @@ -27,13 +29,13 @@ calcWaterUseCommittedAg <- function(version="LPJmL5", climatetype="HadGEM2_ES:rc ## Irrigation system area initialization irrigation_system <- calcOutput("IrrigationSystem", source=irrigini, aggregate=FALSE) - ## Read in Irrigation Water Withdrawals (in m^3 per hectar per year) [smoothed] - irrig_withdrawal <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=FALSE, time=time, dof=dof, irrig_requirement="withdrawal", aggregate=FALSE) + ## Read in Irrigation Water Withdrawals (in m^3 per hectar per year) [smoothed and harmonized] + irrig_withdrawal <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, irrig_requirement="withdrawal", aggregate=FALSE) # Pasture is not irrigated in MAgPIE irrig_withdrawal <- irrig_withdrawal[,,"pasture",invert=T] - ## Read in Irrigation Water Consumption (in m^3 per hectar per year) [smoothed] - irrig_consumption <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=FALSE, time=time, dof=dof, irrig_requirement="consumption", aggregate=FALSE) + ## Read in Irrigation Water Consumption (in m^3 per hectar per year) [smoothed and harmonized] + irrig_consumption <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, irrig_requirement="consumption", aggregate=FALSE) # Pasture is not irrigated in MAgPIE irrig_consumption <- irrig_consumption[,,"pasture",invert=T] From 6dd8b861f44900c8abe761174eaadcf39b6e0122 Mon Sep 17 00:00:00 2001 From: FeBeier Date: Thu, 3 Dec 2020 16:32:04 +0100 Subject: [PATCH 4/6] bug fixes and updates in calcWaterAllocation --- R/calcWaterAllocation.R | 49 ++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/R/calcWaterAllocation.R b/R/calcWaterAllocation.R index 32845e719..980237862 100644 --- a/R/calcWaterAllocation.R +++ b/R/calcWaterAllocation.R @@ -15,6 +15,7 @@ #' @param gainthreshold Threshold of yield improvement potential required for water allocation in upstreamfirst algorithm (in tons per ha) #' @param irrigationsystem Irrigation system to be used for river basin discharge allocation algorithm ("surface", "sprinkler", "drip", "initialization") #' @param irrigini When "initialization" selected for irrigation system: choose initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell") +#' @param iniyear Initialization year of irrigation system #' #' @import magclass #' @import madrat @@ -28,10 +29,10 @@ #' calcWaterAllocation <- function(selectyears="all", output="consumption", - version="LPJmL4", climatetype="HadGEM2_ES:rcp2p6:co2", time="raw", averaging_range=NULL, dof=NULL, - harmonize_baseline=FALSE, ref_year="y2015", + version="LPJmL4", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, + harmonize_baseline="CRU_4", ref_year="y2015", allocationrule="optimization", allocationshare=NULL, gainthreshold=1, - irrigationsystem="initialization", irrigini="Jaegermeyr_lpjcell"){ + irrigationsystem="initialization", irrigini="Jaegermeyr_lpjcell", iniyear=1995){ ############################# ####### Read in Data ######## @@ -65,12 +66,18 @@ calcWaterAllocation <- function(selectyears="all", output="consumption", harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, averaging_range=averaging_range) lake_evap <- as.array(collapseNames(lake_evap)) lake_evap <- lake_evap[,,1] + #### LAKE EVAP PLACEHOLDER + # lake_evap <- yearly_runoff + # lake_evap[] <- 0 # Precipitation/Runoff on lakes and rivers from LPJmL (in mio. m^3 per year) [smoothed & harmonized] input_lake <- calcOutput("LPJmL", version="LPJmL4", selectyears=selectyears, climatetype=climatetype, subtype="input_lake_lpjcell", aggregate=FALSE, harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, averaging_range=averaging_range) input_lake <- as.array(collapseNames(input_lake)) input_lake <- input_lake[,,1] + #### LAKE EVAP PLACEHOLDER + # input_lake <- yearly_runoff + # input_lake[] <- 0 # runoff (on land and water) yearly_runoff <- yearly_runoff + input_lake @@ -100,7 +107,7 @@ calcWaterAllocation <- function(selectyears="all", output="consumption", NAg_wc <- pmax(NAg_wc, 0.01*NAg_ww) # Committed agricultural uses (in mio. m^3 / yr) [for initialization year] - CAU_magpie <- calcOutput("WaterUseCommittedAg",selectyears=selectyears,iniyear=1995,irrigini="Jaegermeyr_lpjcell",time="raw",dof=NULL,aggregate=FALSE) + CAU_magpie <- calcOutput("WaterUseCommittedAg",selectyears=selectyears,iniyear=iniyear,irrigini=irrigini,time=time,dof=dof,averaging_range=averaging_range,harmonize_baseline=harmonize_baseline,ref_year=ref_year,aggregate=FALSE) CAW_magpie <- as.array(collapseNames(dimSums(CAU_magpie[,,"withdrawal"],dim=3))) CAC_magpie <- as.array(collapseNames(dimSums(CAU_magpie[,,"consumption"],dim=3))) rm(CAU_magpie) @@ -156,17 +163,17 @@ calcWaterAllocation <- function(selectyears="all", output="consumption", for (EFP in c("on", "off")) { - # Environmental Flow Requirements (in mio. m^3 / yr) [long-term average] + # Environmental Flow Requirements (share of mean annual discharge) [long-term average] if (EFP=="on"){ - EFR_magpie <- calcOutput("EnvmtlFlowRequirements", version="LPJmL4", climatetype=climatetype, aggregate=FALSE, cells="lpjcell", + EFR_magpie_frac <- calcOutput("EnvmtlFlowRequirements", version="LPJmL4", climatetype="CRU_4", aggregate=FALSE, cells="lpjcell", LFR_val=0.1, HFR_LFR_less10=0.2, HFR_LFR_10_20=0.15, HFR_LFR_20_30=0.07, HFR_LFR_more30=0.00, EFRyears=c(1980:2010)) } else if (EFP=="off"){ - EFR_magpie <- new.magpie(1:NCELLS,fill=0) - getCells(EFR_magpie) <- paste(lpj_cells_map$ISO,1:67420,sep=".") + EFR_magpie_frac <- new.magpie(1:NCELLS,fill=0) + getCells(EFR_magpie_frac) <- paste(lpj_cells_map$ISO,1:67420,sep=".") } - EFR_magpie <- as.array(collapseNames(EFR_magpie)) - EFR_magpie <- EFR_magpie[,1,1] + EFR_magpie_frac <- as.array(collapseNames(EFR_magpie_frac)) + EFR_magpie_frac <- EFR_magpie_frac[,1,1] for (scen in c("ssp1","ssp2","ssp3")){ for (y in selectyears){ @@ -177,18 +184,18 @@ calcWaterAllocation <- function(selectyears="all", output="consumption", ## Global river routing variables # Naturalized discharge - discharge_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - inflow_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - lake_evap_new <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) + discharge_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + inflow_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + lake_evap_new <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) # Discharge considering human uses - discharge <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - inflow <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - avl_wat_act <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) + discharge <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + inflow <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + avl_wat_act <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) # Water fractions reserved for certain uses - frac_NAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - frac_CAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - frac_fullirrig <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) - required_wat_min <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie))) + frac_NAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + frac_CAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + frac_fullirrig <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) + required_wat_min <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) ### River Routing 1.1: Natural flows ### # Determine natural discharge @@ -209,6 +216,8 @@ calcWaterAllocation <- function(selectyears="all", output="consumption", } } + # EFRs (in mio. m^3 / yr) + EFR_magpie <- EFR_magpie_frac*discharge_nat # Minimum availability of water in river to fulfill local EFRs required_wat_min <- EFR_magpie From c8f4830015f5b403a45a244f279c8d0e9a1208e5 Mon Sep 17 00:00:00 2001 From: FeBeier Date: Thu, 3 Dec 2020 16:55:50 +0100 Subject: [PATCH 5/6] updated buildLibrary --- .buildlibrary | 2 +- .zenodo.json | 2 +- DESCRIPTION | 4 ++-- README.md | 8 ++++---- man/calcWaterAllocation.Rd | 11 +++++++---- man/calcWaterUseCommittedAg.Rd | 6 ++++++ 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index f9c8633aa..53d429421 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '5075889' +ValidationKey: '5096126' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.zenodo.json b/.zenodo.json index 562af62c1..2c95cfcef 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "mrmagpie: madrat based MAgPIE Input Data Library", - "version": "0.27.3", + "version": "0.27.4", "description": "

Provides functions for MAgPIE country and cellular input data generation.<\/p>", "creators": [ { diff --git a/DESCRIPTION b/DESCRIPTION index 579312ebd..280747a5d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: mrmagpie Type: Package Title: madrat based MAgPIE Input Data Library -Version: 0.27.3 -Date: 2020-11-27 +Version: 0.27.4 +Date: 2020-12-03 Authors@R: c(person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = c("aut","cre")), person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = "aut"), person("David", "Chen", role = "aut"), diff --git a/README.md b/README.md index 349997b41..95c1111d4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **0.27.3** +R package **mrmagpie**, version **0.27.4** @@ -38,8 +38,8 @@ In case of questions / problems please contact Kristine Karstens Date: Mon, 7 Dec 2020 12:28:55 +0100 Subject: [PATCH 6/6] delted all new water functions that will be part of mrwater --- .buildlibrary | 2 +- .zenodo.json | 2 +- DESCRIPTION | 4 +- R/calcActualIrrigWatRequirements.R | 76 ---- R/calcEnvmtlFlowRequirements.R | 98 ---- R/calcFullIrrigationRequirement.R | 83 ---- R/calcIrrigCellranking.R | 76 ---- R/calcIrrigWatRequirements.R | 128 ------ R/calcIrrigYieldImprovementPotential.R | 60 --- R/calcIrrigatedArea.R | 74 --- R/calcIrrigationSystem.R | 72 --- R/calcWaterAllocation.R | 525 ---------------------- R/calcWaterUseCommittedAg.R | 68 --- R/correctIrrigationSystem.R | 16 - R/fullCELLULARMAGPIE.R | 8 - R/readIrrigationSystem.R | 75 ---- README.md | 9 +- man/calcActualIrrigWatRequirements.Rd | 65 --- man/calcEnvmtlFlowRequirements.Rd | 50 --- man/calcFullIrrigationRequirement.Rd | 57 --- man/calcIrrigCellranking.Rd | 58 --- man/calcIrrigWatRequirements.Rd | 56 --- man/calcIrrigYieldImprovementPotential.Rd | 53 --- man/calcIrrigatedArea.Rd | 35 -- man/calcIrrigationSystem.Rd | 24 - man/calcWaterAllocation.Rd | 68 --- man/calcWaterUseCommittedAg.Rd | 51 --- man/correctIrrigationSystem.Rd | 29 -- man/readIrrigationSystem.Rd | 24 - 29 files changed, 9 insertions(+), 1937 deletions(-) delete mode 100644 R/calcActualIrrigWatRequirements.R delete mode 100644 R/calcEnvmtlFlowRequirements.R delete mode 100644 R/calcFullIrrigationRequirement.R delete mode 100644 R/calcIrrigCellranking.R delete mode 100644 R/calcIrrigWatRequirements.R delete mode 100644 R/calcIrrigYieldImprovementPotential.R delete mode 100644 R/calcIrrigatedArea.R delete mode 100644 R/calcIrrigationSystem.R delete mode 100644 R/calcWaterAllocation.R delete mode 100644 R/calcWaterUseCommittedAg.R delete mode 100644 R/correctIrrigationSystem.R delete mode 100644 R/readIrrigationSystem.R delete mode 100644 man/calcActualIrrigWatRequirements.Rd delete mode 100644 man/calcEnvmtlFlowRequirements.Rd delete mode 100644 man/calcFullIrrigationRequirement.Rd delete mode 100644 man/calcIrrigCellranking.Rd delete mode 100644 man/calcIrrigWatRequirements.Rd delete mode 100644 man/calcIrrigYieldImprovementPotential.Rd delete mode 100644 man/calcIrrigatedArea.Rd delete mode 100644 man/calcIrrigationSystem.Rd delete mode 100644 man/calcWaterAllocation.Rd delete mode 100644 man/calcWaterUseCommittedAg.Rd delete mode 100644 man/correctIrrigationSystem.Rd delete mode 100644 man/readIrrigationSystem.Rd diff --git a/.buildlibrary b/.buildlibrary index 53d429421..a661a6569 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '5096126' +ValidationKey: '5115825' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.zenodo.json b/.zenodo.json index 2c95cfcef..aff090510 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "mrmagpie: madrat based MAgPIE Input Data Library", - "version": "0.27.4", + "version": "0.27.5", "description": "

Provides functions for MAgPIE country and cellular input data generation.<\/p>", "creators": [ { diff --git a/DESCRIPTION b/DESCRIPTION index 280747a5d..ce4cd64d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: mrmagpie Type: Package Title: madrat based MAgPIE Input Data Library -Version: 0.27.4 -Date: 2020-12-03 +Version: 0.27.5 +Date: 2020-12-07 Authors@R: c(person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = c("aut","cre")), person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = "aut"), person("David", "Chen", role = "aut"), diff --git a/R/calcActualIrrigWatRequirements.R b/R/calcActualIrrigWatRequirements.R deleted file mode 100644 index e6f44ef35..000000000 --- a/R/calcActualIrrigWatRequirements.R +++ /dev/null @@ -1,76 +0,0 @@ -#' @title calcActualIrrigWatRequirements -#' @description This function calculates actual irrigation water requirements per cell given a certain irrigation system -#' -#' @param selectyears Years to be returned -#' @param iniyear Initialization year (for weight by cropland) -#' @param version Switch between LPJmL4 and LPJmL5 -#' @param climatetype Switch between different climate scenarios (default: "CRU_4") -#' @param cells Switch between "lpjcell" (67420) and "magpiecell" (59199) -#' @param crops Selects "magpie" (default) or "lpjml" crops -#' @param time Time smoothing: average, spline or raw (default) -#' @param averaging_range only specify if time=="average": number of time steps to average -#' @param dof only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) -#' @param ref_year Reference year for harmonization baseline (just specify when harmonize_baseline=TRUE) -#' @param irrig_requirement Consumptive (consumption) or non-consumptive (withdrawals) irrigation water requirements -#' @param irrig_system_source Source of irrigation system initialization data including information about cellular detail (e.g. Jaegermeyr_magpiecell) -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @seealso -#' \code{\link{calcIrrigationSystem}}, \code{\link{calcIrrigWatRequirements}} -#' -#' @examples -#' \dontrun{ calcOutput("ActualIrrigWatRequirements", aggregate=FALSE) } -#' -#' @import magpiesets -#' @import magclass -#' @import madrat - -calcActualIrrigWatRequirements <- function(selectyears="all", iniyear=1995, cells="magpiecell", crops="magpie", - version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="raw", averaging_range=NULL, dof=NULL, - harmonize_baseline=FALSE, ref_year=NULL, irrig_requirement="withdrawal", irrig_system_source="Jaegermeyr_magpiecell"){ - - if (cells!=gsub(".*_","",irrig_system_source)){ - stop("Cells mismatch. Please select the same cell type in the function arguments cells and irrig_system_source!") - } - - # irrigation water requirement per crop per system (in m^3 per ha per yr) - irrig_wat_requirement <- calcOutput("IrrigWatRequirements", selectyears=selectyears, cells=cells, crops=crops, irrig_requirement=irrig_requirement, - version=version, climatetype=climatetype, time=time, averaging_range=averaging_range, dof=dof, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, aggregate=FALSE) - names(dimnames(irrig_wat_requirement))[1] <- "iso.cell" - names(dimnames(irrig_wat_requirement))[3] <- "crop.system" - - # irrigation system share (share of irrigated area) - irrig_system_share <- calcOutput("IrrigationSystem", source=irrig_system_source, aggregate=FALSE) - - # composite mean - mean_irrig_wat_requirement <- dimSums(irrig_system_share*irrig_wat_requirement,dim=3.1)/dimSums(irrig_system_share, dim=3) - - # Correction of years - if(selectyears!="all"){ - years <- sort(findset(selectyears,noset="original")) - mean_irrig_wat_requirement <- mean_irrig_wat_requirement[,years,] - } - - # Check for NAs and negative values - if(any(is.na(mean_irrig_wat_requirement))){ - stop("produced NA irrigation water requirements") - } - if(any(mean_irrig_wat_requirement<0)){ - stop("produced negative irrigation water requirements") - } - - # irrigated cropland area as weight - irrig_area <- calcOutput("Croparea", years=iniyear, sectoral="kcr", cells=cells, physical=TRUE, cellular=TRUE, irrigation=TRUE, aggregate=FALSE) - irrig_area <- collapseNames(irrig_area[,,"irrigated"][,,"pasture",invert=T])+1e-9 - - return(list( - x=mean_irrig_wat_requirement, - weight=irrig_area, - unit="m^3 per ha per yr", - description="Irrigation water requirements for irrigation for different crop types under selected irrigation system share per cell", - isocountries=FALSE)) -} diff --git a/R/calcEnvmtlFlowRequirements.R b/R/calcEnvmtlFlowRequirements.R deleted file mode 100644 index 0054304aa..000000000 --- a/R/calcEnvmtlFlowRequirements.R +++ /dev/null @@ -1,98 +0,0 @@ -#' @title calcEnvmtlFlowRequirements -#' @description This function calculates environmental flow requirements (EFR) for MAgPIE based on LPJmL monthly discharge following Smakthin et al. (2004) -#' -#' @param version Switch between LPJmL4 and LPJmL5 -#' @param climatetype Switch between different climate scenarios (default: "CRU_4") -#' @param cells Number of cells to be reported: lpjcell (67420, default) or magpiecell (59199) -#' @param LFR_val Strictness of environmental flow requirements -#' @param HFR_LFR_less10 High flow requirements (share of total water for cells) with LFR<10percent of total water -#' @param HFR_LFR_10_20 High flow requirements (share of total water for cells) with 10percent < LFR < 20percent of total water -#' @param HFR_LFR_20_30 High flow requirements (share of total water for cells) with 20percent < LFR < 30percent of total water -#' @param HFR_LFR_more30 High flow requirements (share of total water for cells) with LFR>30percent of total water -#' @param EFRyears Long-term reference time frame for EFR calculation -#' -#' @import magclass -#' @import madrat -#' @importFrom stats quantile -#' @importFrom mrcommons toolHarmonize2Baseline -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier, Jens Heinke -#' -#' @examples -#' \dontrun{ calcOutput("EnvmtlFlowRequirements", aggregate=FALSE) } -#' - -calcEnvmtlFlowRequirements <- function(version="LPJmL4", climatetype="HadGEM2_ES:rcp2p6:co2", cells="lpjcell", - LFR_val=0.1, HFR_LFR_less10=0.2, HFR_LFR_10_20=0.15, HFR_LFR_20_30=0.07, HFR_LFR_more30=0.00, - EFRyears=c(1985:2015)){ - - # Long-term reference period for EFR calculation - EFRyears <- paste0("y",EFRyears) - - ### Monthly Discharge from LPJmL - monthly_discharge_magpie <- calcOutput("LPJmL", selectyears=EFRyears, version=version, climatetype=climatetype, subtype="mdischarge_lpjcell", aggregate=FALSE, - harmonize_baseline=FALSE, ref_year=NULL, time="raw", averaging_range=NULL, dof=NULL) - - # Extract years - years <- getYears(monthly_discharge_magpie, as.integer=TRUE) - # Transform to array (faster calculation) - monthly_discharge_magpie <- as.array(collapseNames(monthly_discharge_magpie)) - - ### Calculate LFRs - ## Note: LFRs correspond to the Q90-value (i.e. to the discharge that is exceeded in nine out of ten months) - ## (Bonsch et al. 2015). This is calculated via the 10%-quantile of monthly discharge. - - # Get the monthly LFR_val quantile for all cells (across selected long-term reference time period) - LFR_quant <- apply(monthly_discharge_magpie, MARGIN=c(1), quantile, probs=LFR_val) - - # Yearly LFRs - LFR <- LFR_quant*12 - - ### Mean annual discharge - mean_annual_discharge <- apply(monthly_discharge_magpie, MARGIN=c(1), sum)/length(years) - - ### Calculate HFR - ## Note: "For rivers with low Q90 values, high-flow events are important - ## for river channel maintenance, wetland flooding, and riparian vegetation. - ## HFRs of 20% of available water are therefore assigned to rivers with a - ## low fraction of Q90 in total discharge. Rivers with a more stable flow - ## regime receive a lower HFR." (Bonsch et al. 2015) - HFR <- LFR - HFR <- NA - HFR[LFR<0.1*mean_annual_discharge] <- HFR_LFR_less10 * mean_annual_discharge[LFR<0.1*mean_annual_discharge] - HFR[LFR>=0.1*mean_annual_discharge] <- HFR_LFR_10_20 * mean_annual_discharge[LFR>=0.1*mean_annual_discharge] - HFR[LFR>=0.2*mean_annual_discharge] <- HFR_LFR_20_30 * mean_annual_discharge[LFR>=0.2*mean_annual_discharge] - HFR[LFR>=0.3*mean_annual_discharge] <- HFR_LFR_more30 * mean_annual_discharge[LFR>=0.3*mean_annual_discharge] - HFR[mean_annual_discharge<=0] <- 0 - - ### EFR - EFR <- LFR+HFR - # Reduce EFR to 50% of available water where it exceeds this threshold (according to Smakhtin 2004) - EFR <- pmin(EFR, 0.5*mean_annual_discharge) - # EFR as fraction of mean annual discharge - EFR <- ifelse(mean_annual_discharge>0, EFR/mean_annual_discharge, 0) - - ### Correct number of cells and transform to magpie object - if (cells=="lpjcell"){ - EFR <- as.magpie(EFR, spatial=1) - } else if (cells=="magpiecell"){ - EFR <- EFR[magclassdata$cellbelongings$LPJ_input.Index] - EFR <- as.magpie(EFR, spatial=1) - dimnames(EFR)[[1]] <- paste(magclassdata$half_deg$region,1:59199,sep='.') - } else { - stop("Cell argument not supported. Select lpjcell for 67420 cells or magpiecell for 59199 cells") - } - - # Check for NAs - if(any(is.na(EFR))){ - stop("produced NA EFR") - } - - return(list( - x=EFR, - weight=NULL, - unit="fraction of discharge", - description="Environmental flow requirements per cell per year as fraction of discharge", - isocountries=FALSE)) -} diff --git a/R/calcFullIrrigationRequirement.R b/R/calcFullIrrigationRequirement.R deleted file mode 100644 index 98232ea61..000000000 --- a/R/calcFullIrrigationRequirement.R +++ /dev/null @@ -1,83 +0,0 @@ -#' @title calcFullIrrigationRequirement -#' @description This function calculates the water requirements for full irrigation per cell per crop given potentially available land -#' -#' @param selectyears years to be returned -#' @param version switch between LPJmL4 and LPJmL5 -#' @param climatetype switch between different climate scenarios (default: "CRU_4") -#' @param cells switch between "lpjcell" (67420) and "magpiecell" (59199) -#' @param time time smoothing: average, spline or raw (default) -#' @param averaging_range only specify if time=="average": number of time steps to average -#' @param dof only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) -#' @param ref_year reference year for harmonization baseline (just specify when harmonize_baseline=TRUE) -#' @param irrig_requirement consumptive (consumption) or non-consumptive (withdrawals) irrigation water requirements -#' @param iniarea if TRUE (default): already irrigated area is subracted, if FALSE: total potential land area is used -#' @param iniyear Year of initialization for cropland area -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ calcOutput("FullIrrigationRequirement", aggregate=FALSE) } -#' -#' @import madrat -#' @import magclass - -calcFullIrrigationRequirement <- function(version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", harmonize_baseline=FALSE, time="spline", dof=4, cells="lpjcell", selectyears=seq(1995,2095,by=5), iniyear=1995, iniarea=TRUE, irrig_requirement="withdrawal"){ - - # read in irrigation water requirements [in m^3 per hectar per year] (smoothed & harmonized) - irrig_wat <- calcOutput("IrrigWatRequirements", selectyears=selectyears, version=version, climatetype=climatetype, - harmonize_baseline=harmonize_baseline, time=time, dof=dof, - irrig_requirement=irrig_requirement, cells="magpiecell", aggregate=FALSE) - # pasture is not irrigated in MAgPIE - irrig_wat <- irrig_wat[,,"pasture",invert=T] - irrig_wat <- toolCell2isoCell(irrig_wat) - - # read in land available for agricultural use (in mio. ha) - land <- collapseNames(calcOutput("AvlLandSi", aggregate=FALSE)[,,"si0"]) - if (iniarea) { - # subtract area already reserved for irrigation by committed agricultural uses (in mio. ha) - crops_grown <- calcOutput("IrrigatedArea", selectyears=selectyears, iniyear=iniyear, cells="magpiecell", aggregate=FALSE) - crops_grown <- collapseNames(dimSums(crops_grown,dim=3)) - land <- land - crops_grown - } - # negative values may occur because AvlLandSi is based on Ramankutty data and Cropara based on LUH -> set to 0 - land[land<0] <- 0 - - # water requirements for full irrigation in cell per crop (in mio. m^3) - # Note on unit transformation: - # land (mio ha -> ha): multiply with 1e6, - # irrigation water requirements (m^3 per ha -> mio. m^3 per ha): devide by 1e6 - # --> cancels out -> water requirements for full irrigation (mio. m^3) - tmp <- irrig_wat*land - - # cellular dimension - if (cells=="magpiecell") { - out <- tmp - } else if (cells=="lpjcell") { - lpj_cells_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", type="cell") - getCells(tmp) <- paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep=".") - out <- new.magpie(1:67420,getYears(tmp),getNames(tmp)) - out[,,] <- 0 - out[paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep="."),,] <- tmp[,,] - getCells(out) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - } else { - stop("Cells argument not supported. Please select lpjcell for 67420 cells or magpiecell for 59199 cells") - } - - # dimension names - getSets(out)[c(1,2)] <- c("iso","cell") - getSets(out)[c(4,5)] <- c("crop","system") - - # Checks - if(any(is.na(out))){ - stop("produced NA full irrigation requirements") - } - - return(list( - x=out, - weight=NULL, - unit="mio. m^3", - description="full irrigation requirements per cell per crop per irrigation system", - isocountries=FALSE)) -} diff --git a/R/calcIrrigCellranking.R b/R/calcIrrigCellranking.R deleted file mode 100644 index 4e55b9fd1..000000000 --- a/R/calcIrrigCellranking.R +++ /dev/null @@ -1,76 +0,0 @@ -#' @title calcIrrigCellranking -#' @description This function calculates a cellranking for the river basin discharge allocation based on yield improvement potential through irrigation -#' -#' @param version switch between LPJmL version for yields -#' @param climatetype switch between different climate scenarios for yields -#' @param time time smoothing: average, spline (default) or raw -#' @param averaging_range just specify for time=="average": number of time steps to average -#' @param dof just specify for time=="spline": degrees of freedom -#' @param harmonize_baseline FALSE (default) no harmonization, harmonization: if a baseline is specified here data is harmonized to that baseline (from ref_year onwards) -#' @param ref_year just specify for harmonize_baseline != FALSE : Reference year -#' @param cellrankyear year(s) for which cell rank is calculated -#' @param cells switch between "lpjcell" (67420) and "magpiecell" (59199) -#' @param crops switch between "magpie" and "lpjml" (default) crops -#' @param method method of calculating the rank: "meancellrank" (default): mean over cellrank of proxy crops, "meancroprank": rank over mean of proxy crops -#' @param proxycrop proxycrop(s) selected for rank calculation -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ calcOutput("calcIrrigCellranking", aggregate=FALSE) } - -calcIrrigCellranking <- function(version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, harmonize_baseline=FALSE, ref_year="y2015", - cellrankyear="y1995", cells="lpjcell", crops="magpie", method="meancellrank", proxycrop=c("maiz", "rapeseed", "puls_pro")){ - - ### Read in potential yield gain per cell - yield_gain <- calcOutput("IrrigYieldImprovementPotential", version=version, climatetype=climatetype, selectyears=cellrankyear, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, averaging_range=averaging_range, dof=dof, - cells=cells, crops=crops, aggregate=FALSE) - # select proxy crops - yield_gain <- yield_gain[,,proxycrop] - - ### Calculate global cell rank - # Def. "meancellrank": ranking of cells or proxy crops, then: average over ranks - if (method=="meancellrank"){ - - # cell ranking for crop (from highest yield gain (rank=1) to lowest yield gain (rank=1+x)) - cropcellrank <- apply(-yield_gain,c(2,3),rank) - - # calculate mean over cropcellranks - glocellrank <- dimSums(cropcellrank,dim=3)/length(getNames(cropcellrank)) - # ties are solved by first occurrence - glocellrank <- apply(glocellrank,2,rank,ties.method="first") - - # Def. "meancroprank": average over yield gain of proxycrops, then: ranking of resulting average yield gain - } else if (method=="meancroprank"){ - - # normalize yield gains of proxy crops (unity-based normalization) - min_yield <- as.magpie(apply(yield_gain,c(2,3),min)) - max_yield <- as.magpie(apply(yield_gain,c(2,3),max)) - yield_gain <- (yield_gain-min_yield)/(max_yield-min_yield) - - # calculate average yield gain over normalized proxy crops - yield_gain <- dimSums(yield_gain,dim=3)/length(getNames(yield_gain)) - - # calculate rank (ties are solved by first occurence) - glocellrank <- apply(-yield_gain,c(2,3),rank,ties.method="first") - - } else { - stop("Please select a method for rank calculation") - } - - glocellrank <- as.magpie(glocellrank,spatial=1) - - # Check for NAs - if(any(is.na(glocellrank))){ - stop("Function YieldImprovementPotential produced NAs") - } - - return(list( - x=glocellrank, - weight=NULL, - unit="1", - description="Rank of cell according to yield gain potential by irrigation", - isocountries=FALSE)) -} diff --git a/R/calcIrrigWatRequirements.R b/R/calcIrrigWatRequirements.R deleted file mode 100644 index 87db31ada..000000000 --- a/R/calcIrrigWatRequirements.R +++ /dev/null @@ -1,128 +0,0 @@ -#' @title calcIrrigWatRequirements -#' @description This function calculates irrigation water requirements based on LPJmL blue water consumption of plants and considering irrigation efficiencies -#' -#' @param selectyears Years to be returned -#' @param version Switch between LPJmL4 and LPJmL5 -#' @param climatetype Switch between different climate scenarios (default: "CRU_4") -#' @param cells Switch between "lpjcell" (67420) and "magpiecell" (59199) -#' @param crops Selects "magpie" (default) or "lpjml" crops -#' @param time Time smoothing: average, spline or raw (default) of input data to this function -#' @param averaging_range only specify if time=="average": number of time steps to average -#' @param dof only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline FALSE (default): no harmonization of input data to this function, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) -#' @param ref_year Reference year for harmonization baseline of input data to this function (just specify when harmonize_baseline=TRUE) -#' @param irrig_requirement Consumptive (consumption) or non-consumptive (withdrawals) irrigation water requirements -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier, Jens Heinke -#' -#' @examples -#' \dontrun{ calcOutput("IrrigWatRequirements", aggregate=FALSE) } -#' -#' @import magpiesets -#' @import magclass -#' @import madrat - -calcIrrigWatRequirements <- function(selectyears="all", cells="lpjcell", crops="magpie", - version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, - harmonize_baseline=FALSE, ref_year=NULL, irrig_requirement="withdrawal"){ - - sizelimit <- getOption("magclass_sizeLimit") - options(magclass_sizeLimit=1e+12) - on.exit(options(magclass_sizeLimit=sizelimit)) - - ############################## - ######## Read in data ######## - ############################## - ### Mappings - lpj_cells_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", type="cell") - LPJ2MAG <- toolGetMapping( "MAgPIE_LPJmL.csv", type = "sectoral", where = "mappingfolder") - - ### Read in blue water consumption for irrigated crops (in m^3 per ha per yr): - blue_water_consumption <- collapseNames(calcOutput("LPJmL", version=version, climatetype=climatetype, harmonize_baseline=harmonize_baseline, ref_year=ref_year, - time=time, averaging_range=averaging_range, dof=dof, - selectyears=selectyears, subtype="cwater_b_lpjcell", aggregate=FALSE)[,,"irrigated"]) - names(dimnames(blue_water_consumption))[1] <- "iso.cell" - names(dimnames(blue_water_consumption))[3] <- "crop" - years <- getYears(blue_water_consumption) - cropnames <- getNames(blue_water_consumption) - systemnames <- c("drip","sprinkler","surface") - - ### Field efficiencies from Jägermeyr et al. (global values) [placeholder!] - field_efficiency <- new.magpie(1:67420,years,sort(paste(systemnames, rep(cropnames,3), sep=".")),sets=c("iso.cell","year","system.crop")) - getCells(field_efficiency) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - field_efficiency[,,"drip"] <- 0.88 - field_efficiency[,,"sprinkler"] <- 0.78 - field_efficiency[,,"surface"] <- 0.52 - ### Use field efficiency from LPJmL here (by system, by crop, on 0.5 degree) [Does it vary by year?] - - ### Conveyance efficiency proxy [placeholder] - conveyance_efficiency <- new.magpie(1:67420,years,sort(paste(systemnames, rep(cropnames,3), sep=".")),sets=c("iso.cell","year","system.crop")) - getCells(conveyance_efficiency) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - conveyance_efficiency[,,"drip"] <- 0.95 - conveyance_efficiency[,,"sprinkler"] <- 0.95 - conveyance_efficiency[,,"surface"] <- 0.7 - ### Use field efficiency from LPJmL here (by system, on 0.5 degree) [Does it vary by year?] - - ############################## - ######## Calculations ######## - ############################## - - # Calculate project efficiency from given field and conveyance efficiencies - project_efficiency <- field_efficiency * conveyance_efficiency - - # Water withdrawal = crop water consumption + field losses + conveyance losses - water_withdrawal <- blue_water_consumption/project_efficiency - - # Conveyance loss (from river to field) - conveyance_loss <- water_withdrawal*(1-conveyance_efficiency) - - # consumptive irrigation water = consumptive plant transpiration + evaporative conveyance loss - # (Note: According to Rost et al. (2007) 50% of conveyance loss are evaporative) - water_consumption <- blue_water_consumption + 0.5*conveyance_loss - - # Output: irrigation water requirements (consumption or withdrawals) - if (irrig_requirement=="consumption"){ - irrig_requirements <- water_consumption - } else if (irrig_requirement=="withdrawal"){ - irrig_requirements <- water_withdrawal - } else { - stop("Specify consumption or withdrawal in irrig_requirement") - } - - # Aggregate to MAgPIE crops - if (crops=="magpie") { - irrig_requirements <- toolAggregate(irrig_requirements, LPJ2MAG, from="LPJmL", to="MAgPIE", dim=3.1, partrel=TRUE) - } - - if(selectyears!="all"){ - years <- sort(findset(selectyears,noset="original")) - irrig_requirements <- irrig_requirements[,years,] - } - - ### Correct number of cells - if (cells=="lpjcell"){ - out <- irrig_requirements - } else if (cells=="magpiecell"){ - irrig_requirements <- irrig_requirements[magclassdata$cellbelongings$LPJ_input.Index,,] - irrig_requirements <- toolCell2isoCell(irrig_requirements) - out <- irrig_requirements - } else { - stop("Cell argument not supported. Select lpjcell for 67420 cells or magpiecell for 59199 cells") - } - - # Check for NAs and negative values - if(any(is.na(out))){ - stop("produced NA irrigation water requirements") - } - if(any(out<0)){ - stop("produced negative irrigation water requirements") - } - - return(list( - x=out, - weight=NULL, - unit="m^3 per ha per yr", - description="Irrigation water requirements for irrigation for different crop types under different irrigation systems", - isocountries=FALSE)) -} diff --git a/R/calcIrrigYieldImprovementPotential.R b/R/calcIrrigYieldImprovementPotential.R deleted file mode 100644 index 5f880d281..000000000 --- a/R/calcIrrigYieldImprovementPotential.R +++ /dev/null @@ -1,60 +0,0 @@ -#' @title calcIrrigYieldImprovementPotential -#' @description This function calculates the yield improvement potential of irrigation for different crops -#' -#' @param version switch between LPJmL4 and LPJmL5 of calcYields function -#' @param climatetype switch between different climate scenarios (default: "CRU_4") of calcYields function -#' @param selectyears years to be returned by the function -#' @param time time smoothing of calcYields function: average, spline (default) or raw -#' @param averaging_range only specify if time=="average": number of time steps to average -#' @param dof only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline harmonization in calcYields function: FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year onwards) -#' @param ref_year reference year for harmonization baseline (just specify when harmonize_baseline=TRUE) -#' @param cells switch between "lpjcell" (67420) and "magpiecell" (59199) -#' @param crops switch between "magpie" and "lpjml" (default) crops -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ calcOutput("IrrigYieldImprovementPotential", aggregate=FALSE) } -#' -#' @import madrat -#' @import magclass - -calcIrrigYieldImprovementPotential <- function(version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, - harmonize_baseline=FALSE, ref_year=NULL, selectyears=seq(1995, 2095,by=5), cells="magpiecell", crops="lpjml"){ - - # read in yields [in tons/ha] - yields <- calcOutput("Yields", version=version, climatetype=climatetype, selectyears=selectyears, crops=crops, - time=time, dof=dof, averaging_range=averaging_range, harmonize_baseline=harmonize_baseline, ref_year=ref_year, aggregate=FALSE) - - # yield gap (irrigated vs. rainfed) [in tons/ha] - tmp <- collapseNames(yields[,,"irrigated"])-collapseNames(yields[,,"rainfed"]) - # (Note: under N-stress, irrigation may lead to lower yields; also: irrigation may lead to shift in growing period -> tmp can have negative values) - - # cellular dimension - if (cells=="magpiecell") { - yield_gain <- tmp - } else if (cells=="lpjcell") { - lpj_cells_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", type="cell") - getCells(tmp) <- paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep=".") - yield_gain <- new.magpie(1:67420,getYears(tmp),getNames(tmp)) - yield_gain[,,] <- 0 - yield_gain[paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep="."),,] <- tmp[,,] - getCells(yield_gain) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - } else { - stop("Cells argument not supported. Please select lpjcell for 67420 cells or magpiecell for 59199 cells") - } - - # Check for NAs - if(any(is.na(yield_gain))){ - stop("Function YieldImprovementPotential produced NAs") - } - - return(list( - x=yield_gain, - weight=NULL, - unit="tons per ha", - description="Yield improvement potential by irrigation for different crop types.", - isocountries=FALSE)) -} diff --git a/R/calcIrrigatedArea.R b/R/calcIrrigatedArea.R deleted file mode 100644 index 3982265c6..000000000 --- a/R/calcIrrigatedArea.R +++ /dev/null @@ -1,74 +0,0 @@ -#' @title calcIrrigatedArea -#' @description calculates area reserved for irrigation based on area irrigated in initialization year and depreciation parameter -#' -#' @param iniyear initialization year -#' @param selectyears select years -#' @param depreciation parameter defining yearly depreciation rate at which previously irrigated cropland becomes "unreserved" for irrigation -#' @param cells cells to be returned by the function (lpjcell or magpiecell) -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ calcOutput("IrrigatedArea", aggregate=FALSE) } -#' -#' @import magclass -#' @import magpiesets - -calcIrrigatedArea <- function(selectyears=seq(1995,2100,by=5), iniyear=1995, depreciation=0.1, cells="lpjcell"){ - - # Read in data: crop- and water supply type specific crop area (in Mha) in initialization year: - tmp <- calcOutput("Croparea", years=iniyear, sectoral="kcr", cells="magpiecell", physical=TRUE, cellular=TRUE, irrigation=TRUE, aggregate=FALSE) - # Retrieve irrigated area (per crop) - tmp <- collapseNames(tmp[,,"irrigated"]) - - # Empty object to be filled with area reserved for irrigation in current and future time steps - irrig_area <- new.magpie(getCells(tmp),selectyears,getNames(tmp)) - - # Each year certain share (parameter: "depreciation") of irrigated cropland is lost - for (y in (1:length(selectyears))){ - # irrigated area in respective year - irrig_area[,selectyears[y],] <- tmp - # adjust yearly depreciation rate to time steps - timegap <- selectyears[y+1]-selectyears[y] - dep_adj <- (1-depreciation)^timegap - # depreciation of irrigated area - tmp <- tmp*dep_adj - } - - # Corrections - # years - if(selectyears!="all"){ - years <- sort(findset(selectyears,noset="original")) - irrig_area <- irrig_area[,years,] - } - - # number of cells - if (cells=="magpiecell") { - out <- irrig_area - } else if (cells=="lpjcell") { - lpj_cells_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", type="cell") - getCells(irrig_area) <- paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep=".") - out <- new.magpie(1:67420,getYears(irrig_area),getNames(irrig_area)) - out[,,] <- 0 - out[paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep="."),,] <- irrig_area[,,] - getCells(out) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - } else { - stop("Cells argument not supported. Please select lpjcell for 67420 cells or magpiecell for 59199 cells") - } - - # check for NAs and negative values - if(any(is.na(out))){ - stop("produced NA irrigation water requirements") - } - if(any(out<0)){ - stop("produced negative irrigation water requirements") - } - - return(list( - x=out, - weight=NULL, - unit="mio. ha", - description="Cropland area reserved for irrigation per crop", - isocountries=FALSE)) -} diff --git a/R/calcIrrigationSystem.R b/R/calcIrrigationSystem.R deleted file mode 100644 index d64c6104d..000000000 --- a/R/calcIrrigationSystem.R +++ /dev/null @@ -1,72 +0,0 @@ -#' @title calcIrrigationSystem -#' @description This function returns the irrigation system share initialization -#' -#' @param source Data source to be used: Jaegermeyr (irrigation system share based on FAO 2014, ICID 2012 and Rohwer et al. 2007) or LPJmL (dominant irrigation system per country) and number of cells (lpjcell or magpiecell) separated by _ -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ calcOutput("IrrigationSystem",source="Jaegermeyr_lpjcell",aggregate = FALSE) } -#' -#' @import magclass - -calcIrrigationSystem <- function(source="Jaegermeyr_lpjcell"){ - - # Jägermeyr et al. (2015): Shares of surface, sprinkler and drip irrigated areas - # (Note: compiled from FAO (2014), ICID (2012), Rohwer et al. (2007)) - if (grepl("Jaegermeyr", source)){ - - # Read in source - x <- readSource("IrrigationSystem", convert="onlycorrect", subtype=source) - getNames(x) <- gsub("shr_AEI_","",getNames(x)) - } - - # Irrigation functional type (IFT) from LPJmL representing the dominant irrigation system per country - # (Note: share of 100% of dominant system assumed) - if (grepl("LPJmL", source)){ - - # Read in source - tmp <- readSource("IrrigationSystem", convert="onlycorrect", subtype=source) - - # Merge to obtain one magpie object containing irrigation system shares (share of irrigated area per irrigation system) - x <- new.magpie(cells_and_regions=getCells(tmp),years=NULL,names=c("surface","sprinkler","drip"),fill=0) - - # Surface is dominant system: - x[,,"surface"][tmp==1] <- 1 - x[,,"sprinkler"][tmp==1] <- 0 - x[,,"drip"][tmp==1] <- 0 - - # Sprinkler is dominant system: - x[,,"surface"][tmp==2] <- 0 - x[,,"sprinkler"][tmp==2] <- 1 - x[,,"drip"][tmp==2] <- 0 - - # Drip is dominant system - x[,,"surface"][tmp==3] <- 0 - x[,,"sprinkler"][tmp==3] <- 0 - x[,,"drip"][tmp==3] <- 1 - } - - # When all three shares are 0, it is assumed that 100% of irrigated land (if any exists) is surface irrigation - x[,,"surface"][which(x[,,"surface"]==0 & x[,,"sprinkler"]==0 & x[,,"drip"]==0)] <- 1 - - # dimension names - getSets(x)[c(1,2)] <- c("iso","cell") - getSets(x)[4] <- "system" - - # Checks - if(any(is.na(x))){ - stop("produced NA irrigation system share") - } - if (any(round(dimSums(x, dim=3))!=1)){ - stop("sum over shares not equal to 1") - } - - return(list( - x=x, - weight=NULL, - unit="1", - description="irrigation system share (share of irrigated area)", - isocountries=FALSE)) -} diff --git a/R/calcWaterAllocation.R b/R/calcWaterAllocation.R deleted file mode 100644 index 980237862..000000000 --- a/R/calcWaterAllocation.R +++ /dev/null @@ -1,525 +0,0 @@ -#' @title calcWaterAllocation -#' @description This function calculates water availability for MAgPIE retrieved from LPJmL using a river routing and allocation algorithm for distribution of discharge within the river basin -#' -#' @param version Switch between LPJmL4 and LPJmL5 -#' @param climatetype Switch between different climate scenarios (default: "CRU_4") -#' @param time Time smoothing: average, spline or raw (default) -#' @param averaging_range only specify if time=="average": number of time steps to average -#' @param dof only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) -#' @param ref_year Reference year for harmonization baseline (just specify when harmonize_baseline=TRUE) -#' @param selectyears Years to be returned -#' @param output Water availability output to be returned: withdrawal or consumption -#' @param allocationrule Rule to be applied for river basin discharge allocation across cells of river basin ("optimization" (default), "upstreamfirst", "equality") -#' @param allocationshare Share of water to be allocated to cell (only needs to be selected in case of allocationrule=="equality") -#' @param gainthreshold Threshold of yield improvement potential required for water allocation in upstreamfirst algorithm (in tons per ha) -#' @param irrigationsystem Irrigation system to be used for river basin discharge allocation algorithm ("surface", "sprinkler", "drip", "initialization") -#' @param irrigini When "initialization" selected for irrigation system: choose initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell") -#' @param iniyear Initialization year of irrigation system -#' -#' @import magclass -#' @import madrat -#' @importFrom mrcommons toolHarmonize2Baseline -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier, Jens Heinke -#' -#' @examples -#' \dontrun{ calcOutput("WaterAllocation", aggregate = FALSE) } -#' - -calcWaterAllocation <- function(selectyears="all", output="consumption", - version="LPJmL4", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, - harmonize_baseline="CRU_4", ref_year="y2015", - allocationrule="optimization", allocationshare=NULL, gainthreshold=1, - irrigationsystem="initialization", irrigini="Jaegermeyr_lpjcell", iniyear=1995){ - - ############################# - ####### Read in Data ######## - ############################# - - ### Read in river structure - # Note: river structure derived from LPJmL input (drainage) [maybe later: implement readDrainage function] - data <- toolGetMapping("River_structure_stn.rda",type="cell") - for (i in 1:length(data)){ - assign(paste(names(data[[i]])), data[[i]][[1]]) - } - rm(data,i) - - # Number of cells to be used for calculation - NCELLS <- length(calcorder) - - ### LPJ-MAgPIE cell mapping - magpie2lpj <- magclassdata$cellbelongings$LPJ_input.Index - lpj_cells_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", type="cell") - - ### Required inputs for River Routing: - # Yearly runoff (mio. m^3 per yr) [smoothed & harmonized] - yearly_runoff <- calcOutput("LPJmL", version="LPJmL4", selectyears=selectyears, climatetype=climatetype, subtype="runoff_lpjcell", aggregate=FALSE, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, averaging_range=averaging_range) - yearly_runoff <- as.array(collapseNames(yearly_runoff)) - yearly_runoff <- yearly_runoff[,,1] - years <- getYears(yearly_runoff) - - # Yearly lake evapotranspiration (in mio. m^3 per year) [smoothed & harmonized] - lake_evap <- calcOutput("LPJmL", version="LPJmL4", selectyears=selectyears, climatetype=climatetype, subtype="evap_lake_lpjcell", aggregate=FALSE, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, averaging_range=averaging_range) - lake_evap <- as.array(collapseNames(lake_evap)) - lake_evap <- lake_evap[,,1] - #### LAKE EVAP PLACEHOLDER - # lake_evap <- yearly_runoff - # lake_evap[] <- 0 - - # Precipitation/Runoff on lakes and rivers from LPJmL (in mio. m^3 per year) [smoothed & harmonized] - input_lake <- calcOutput("LPJmL", version="LPJmL4", selectyears=selectyears, climatetype=climatetype, subtype="input_lake_lpjcell", aggregate=FALSE, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, averaging_range=averaging_range) - input_lake <- as.array(collapseNames(input_lake)) - input_lake <- input_lake[,,1] - #### LAKE EVAP PLACEHOLDER - # input_lake <- yearly_runoff - # input_lake[] <- 0 - - # runoff (on land and water) - yearly_runoff <- yearly_runoff + input_lake - - # Non-Agricultural Water Withdrawals (in mio. m^3 / yr) [smoothed] - NAg_ww_magpie <- calcOutput("WaterUseNonAg", source="WATERGAP2020", selectyears=selectyears, time=time, dof=dof, averaging_range=averaging_range, waterusetype="withdrawal", seasonality="total", aggregate=FALSE) - getCells(NAg_ww_magpie) <- paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep=".") - NAg_ww <- new.magpie(1:NCELLS,getYears(NAg_ww_magpie),getNames(NAg_ww_magpie)) - NAg_ww[,,] <- 0 - NAg_ww[paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep="."),,] <- NAg_ww_magpie[,,] - getCells(NAg_ww) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - NAg_ww <- as.array(collapseNames(NAg_ww)) - rm(NAg_ww_magpie) - - # Non-Agricultural Water Consumption (in mio. m^3 / yr) [smoothed] - NAg_wc_magpie <- calcOutput("WaterUseNonAg", source="WATERGAP2020", selectyears=selectyears, time=time, dof=dof, averaging_range=averaging_range, waterusetype="consumption", seasonality="total", aggregate=FALSE) - getCells(NAg_wc_magpie) <- paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep=".") - NAg_wc <- new.magpie(1:NCELLS,getYears(NAg_wc_magpie),getNames(NAg_wc_magpie)) - NAg_wc[,,] <- 0 - NAg_wc[paste("GLO",magclassdata$cellbelongings$LPJ_input.Index,sep="."),,] <- NAg_wc_magpie[,,] - getCells(NAg_wc) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - NAg_wc <- as.array(collapseNames(NAg_wc)) - rm(NAg_wc_magpie) - - # Harmonize non-agricultural consumption and withdrawals (withdrawals > consumption) - NAg_ww <- pmax(NAg_ww, NAg_wc) - NAg_wc <- pmax(NAg_wc, 0.01*NAg_ww) - - # Committed agricultural uses (in mio. m^3 / yr) [for initialization year] - CAU_magpie <- calcOutput("WaterUseCommittedAg",selectyears=selectyears,iniyear=iniyear,irrigini=irrigini,time=time,dof=dof,averaging_range=averaging_range,harmonize_baseline=harmonize_baseline,ref_year=ref_year,aggregate=FALSE) - CAW_magpie <- as.array(collapseNames(dimSums(CAU_magpie[,,"withdrawal"],dim=3))) - CAC_magpie <- as.array(collapseNames(dimSums(CAU_magpie[,,"consumption"],dim=3))) - rm(CAU_magpie) - CAW_magpie <- as.array(collapseNames(CAW_magpie)) - CAW_magpie <- CAW_magpie[,,1] - CAC_magpie <- as.array(collapseNames(CAC_magpie)) - CAC_magpie <- CAC_magpie[,,1] - - ### Required inputs for Allocation Algorithm: - # Required water for full irrigation per cell (in mio. m^3) - required_wat_fullirrig_ww <- calcOutput("FullIrrigationRequirement", version="LPJmL5", selectyears=seq(1995, 2095,by=5), climatetype="HadGEM2_ES:rcp2p6:co2", harmonize_baseline=FALSE, time="spline", dof=4, iniyear=1995, iniarea=TRUE, irrig_requirement="withdrawal", cells="lpjcell", aggregate=FALSE)[,,c("maiz","rapeseed","puls_pro")] - required_wat_fullirrig_ww <- pmax(required_wat_fullirrig_ww,0) - required_wat_fullirrig_wc <- calcOutput("FullIrrigationRequirement", version="LPJmL5", selectyears=seq(1995, 2095,by=5), climatetype="HadGEM2_ES:rcp2p6:co2", harmonize_baseline=FALSE, time="spline", dof=4, iniyear=1995, iniarea=TRUE, irrig_requirement="consumption", cells="lpjcell", aggregate=FALSE)[,,c("maiz","rapeseed","puls_pro")] - required_wat_fullirrig_wc <- pmax(required_wat_fullirrig_wc,0) - - # Full irrigation water requirement depending on irrigation system in use - if (irrigationsystem=="initialization") { - # read in irrigation system area initialization [share of AEI by system] - tmp <- calcOutput("IrrigationSystem", source=irrigini, aggregate=FALSE) - irrigation_system <- new.magpie(getCells(tmp), getYears(required_wat_fullirrig_ww), getNames(tmp)) - getYears(irrigation_system) <- getYears(required_wat_fullirrig_ww) - for (y in getYears(required_wat_fullirrig_ww)) { - irrigation_system[,y,] <- tmp - } - # full irrigation water requirements (in mio. m^3) - required_wat_fullirrig_ww <- dimSums(irrigation_system*required_wat_fullirrig_ww,dim=3.1) - required_wat_fullirrig_wc <- dimSums(irrigation_system*required_wat_fullirrig_wc,dim=3.1) - } else { - # whole area irrigated by one system as selected in argument "irrigationsystem" - required_wat_fullirrig_ww <- collapseNames(required_wat_fullirrig_ww[,,irrigationsystem]) - required_wat_fullirrig_wc <- collapseNames(required_wat_fullirrig_wc[,,irrigationsystem]) - } - # average required water for full irrigation across selected proxy crops - required_wat_fullirrig_ww <- dimSums(required_wat_fullirrig_ww,dim=3)/length(getNames(required_wat_fullirrig_ww)) - required_wat_fullirrig_wc <- dimSums(required_wat_fullirrig_wc,dim=3)/length(getNames(required_wat_fullirrig_wc)) - - # transform to array for further calculations - required_wat_fullirrig_ww <- as.array(collapseNames(required_wat_fullirrig_ww))[,,1] - required_wat_fullirrig_wc <- as.array(collapseNames(required_wat_fullirrig_wc))[,,1] - - # Global cell rank based on yield gain potential by irrigation of proxy crops: maize, rapeseed, pulses - meancellrank <- calcOutput("IrrigCellranking", version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, harmonize_baseline=FALSE, ref_year="y2015", - cellrankyear=seq(1995, 2095,by=5), cells="lpjcell", crops="magpie", method="meancroprank", proxycrop=c("maiz", "rapeseed", "puls_pro"), aggregate=FALSE) - meancellrank <- as.array(meancellrank)[,,1] - - - ############################################ - ####### Routing and Allocation Loop ######## - ############################################ - out_tmp1 <- NULL - out_tmp2 <- NULL - out <- NULL - - for (EFP in c("on", "off")) { - - # Environmental Flow Requirements (share of mean annual discharge) [long-term average] - if (EFP=="on"){ - EFR_magpie_frac <- calcOutput("EnvmtlFlowRequirements", version="LPJmL4", climatetype="CRU_4", aggregate=FALSE, cells="lpjcell", - LFR_val=0.1, HFR_LFR_less10=0.2, HFR_LFR_10_20=0.15, HFR_LFR_20_30=0.07, HFR_LFR_more30=0.00, - EFRyears=c(1980:2010)) - } else if (EFP=="off"){ - EFR_magpie_frac <- new.magpie(1:NCELLS,fill=0) - getCells(EFR_magpie_frac) <- paste(lpj_cells_map$ISO,1:67420,sep=".") - } - EFR_magpie_frac <- as.array(collapseNames(EFR_magpie_frac)) - EFR_magpie_frac <- EFR_magpie_frac[,1,1] - - for (scen in c("ssp1","ssp2","ssp3")){ - for (y in selectyears){ - - ############################# - ####### River routing ####### - ############################# - - ## Global river routing variables - # Naturalized discharge - discharge_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - inflow_nat <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - lake_evap_new <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - # Discharge considering human uses - discharge <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - inflow <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - avl_wat_act <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - # Water fractions reserved for certain uses - frac_NAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - frac_CAg_fulfilled <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - frac_fullirrig <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - required_wat_min <- array(data=0,dim=NCELLS,dimnames=list(names(EFR_magpie_frac))) - - ### River Routing 1.1: Natural flows ### - # Determine natural discharge - for (o in 1:max(calcorder)){ - # Note: the calcorder ensures that upstreamcells are calculated first - cells <- which(calcorder==o) - - for (c in cells){ - ### Natural water balance - # lake evap that can be fulfilled (if water available: lake evaporation considered; if not: lake evap is reduced respectively): - lake_evap_new[c] <- min(lake_evap[c,y], inflow_nat[c]+yearly_runoff[c,y]) - # natural discharge - discharge_nat[c] <- inflow_nat[c] + yearly_runoff[c,y] - lake_evap_new[c] - # inflow into nextcell - if (nextcell[c]>0){ - inflow_nat[nextcell[c]] <- inflow_nat[nextcell[c]] + discharge_nat[c] - } - } - } - - # EFRs (in mio. m^3 / yr) - EFR_magpie <- EFR_magpie_frac*discharge_nat - # Minimum availability of water in river to fulfill local EFRs - required_wat_min <- EFR_magpie - - ### River Routing 2: Non-agricultural uses considering local EFRs ### - for (o in 1:max(calcorder)) { - # Note: the calcorder ensures that the upstreamcells are calculated first - cells <- which(calcorder==o) - - for (c in cells){ - # available water in cell - avl_wat_act[c] <- inflow[c]+yearly_runoff[c,y]-lake_evap_new[c] - - # available water in cell not sufficient to fulfill requirements - # -> no more water can be withdrawn - if (avl_wat_act[c] reduce non-agricultural water consumption in upstream cells - # -> locally: cannot withdraw - if (length(upstreamcells[c])>0){ - # upstream non-agricultural water consumption - upstream_cons <- sum(NAg_wc[upstreamcells[[c]],y,scen]*frac_NAg_fulfilled[upstreamcells[[c]]]) - if (upstream_cons>required_wat_min[c]-avl_wat_act[c]){ - # if missing water (difference) can be fulfilled by upstream consumption: reduce upstream consumption - frac_NAg_fulfilled[upstreamcells[[c]]] <- (1-(required_wat_min[c]-avl_wat_act[c])/upstream_cons)*frac_NAg_fulfilled[upstreamcells[[c]]] - discharge[c] <- required_wat_min[c] - } else { - # if missing water (difference) cannot be fulfilled by upstream consumption: no upstream consumption - frac_NAg_fulfilled[upstreamcells[[c]]] <- 0 - discharge[c] <- avl_wat_act[c]+upstream_cons - } - } - - # available water in cell is sufficient to fulfill requirements - # -> further withdrawals are possible - } else { - # Non-agricultural withdrawals - if (NAg_ww[c,y,scen]>0){ - ## Water withdrawal constraint: - frac_NAg_fulfilled[c] <- min((avl_wat_act[c]-required_wat_min[c])/NAg_ww[c,y,scen], 1) - } - - ## Outflow from one cell to the next - # (Subtract local water consumption in current cell (non-ag. consumption)) - discharge[c] <- avl_wat_act[c] - NAg_wc[c,y,scen]*frac_NAg_fulfilled[c] - } - - if (nextcell[c]>0){ - inflow[nextcell[c]] <- inflow[nextcell[c]] + discharge[c] - } - } - } - - # Update minimum water required in cell: - required_wat_min <- required_wat_min + NAg_ww[,y,scen]*frac_NAg_fulfilled - - # inflow needs to be set to 0 prior to every river routing (is recalculated by the routing) - inflow[] <- 0 - - ### River Routing 3: Committed agricultural uses considering local EFRs and non-agricultural uses ### - for (o in 1:max(calcorder)) { - # Note: the calcorder ensures that the upstreamcells are calculated first - cells <- which(calcorder==o) - - for (c in cells){ - # available water in cell - avl_wat_act[c] <- inflow[c]+yearly_runoff[c,y]-lake_evap_new[c] - - # available water in cell not sufficient to fulfill requirements - # -> no more water can be withdrawn - if (avl_wat_act[c] reduce committed agricultural water consumption in upstream cells - # -> locally: cannot withdraw - if (length(upstreamcells[c])>0){ - # upstream committed agricultural water consumption: - upstream_cons <- sum(CAC_magpie[upstreamcells[[c]],y]*frac_CAg_fulfilled[upstreamcells[[c]]]) - if (upstream_cons>required_wat_min[c]-avl_wat_act[c]){ - # if upstream_cons high enough to account for difference: reduce upstream consumption respectively - frac_CAg_fulfilled[upstreamcells[[c]]] <- (1-(required_wat_min[c]-avl_wat_act[c])/upstream_cons)*frac_CAg_fulfilled[upstreamcells[[c]]] - discharge[c] <- required_wat_min[c] - NAg_wc[c,y,scen]*frac_NAg_fulfilled[c] - } else { - # if upstream_cons not sufficient to account for difference: no water can be used upstream - frac_CAg_fulfilled[upstreamcells[[c]]] <- 0 - discharge[c] <- avl_wat_act[c]+upstream_cons - NAg_wc[c,y,scen]*frac_NAg_fulfilled[c] - } - } - - # available water in cell is sufficient to fulfill requirements - # -> further withdrawal possible - } else { - # Committed agricultural withdrawals - if (CAW_magpie[c,y]>0){ - ## Water withdrawal constraint: - frac_CAg_fulfilled[c] <- min((avl_wat_act[c]-required_wat_min[c])/CAW_magpie[c,y], 1) - } - - ## Outflow from one cell to the next - # (Subtract local water consumption in current cell (committed ag. & non-agricultural consumption)) - discharge[c] <- avl_wat_act[c] - CAC_magpie[c,y]*frac_CAg_fulfilled[c] - NAg_wc[c,y,scen]*frac_NAg_fulfilled[c] - } - - if (nextcell[c]>0){ - inflow[nextcell[c]] <- inflow[nextcell[c]] + discharge[c] - } - } - } - - # Update minimum water required in cell: - required_wat_min <- required_wat_min + CAW_magpie[,y]*frac_CAg_fulfilled - - ### Interim routing: Update discharge and inflow considering known non-agricultural and committed agricultural uses of river routing 3 ### - inflow[] <- 0 - - for (o in 1:max(calcorder)){ - # Note: the calcorder ensures that the upstreamcells are calculated first - cells <- which(calcorder==o) - - for (c in cells){ - # available water - avl_wat_act[c] <- inflow[c] + yearly_runoff[c,y] - lake_evap_new[c] - # discharge - discharge[c] <- avl_wat_act[c] - NAg_wc[c,y,scen]*frac_NAg_fulfilled[c] - CAC_magpie[c,y]*frac_CAg_fulfilled[c] - # inflow into nextcell - if (nextcell[c]>0){ - inflow[nextcell[c]] <- inflow[nextcell[c]] + discharge[c] - } - } - } - - ################################################ - ####### River basin discharge allocation ####### - ################################################ - # Minimum water required - required_wat_min_allocation <- required_wat_min - - # Allocate water for full irrigation to cell with highest yield improvement through irrigation - if (allocationrule=="optimization") { - - for (c in (1:max(meancellrank[,y],na.rm=T))){ - # available water for additional irrigation withdrawals - avl_wat_ww <- max(discharge[c]-required_wat_min_allocation[c],0) - - # withdrawal constraint - if (required_wat_fullirrig_ww[c,y]>0) { - # how much withdrawals can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_ww/required_wat_fullirrig_ww[c,y],1) - - # consumption constraint - if (required_wat_fullirrig_wc[c,y]>0 & length(downstreamcells[[c]])>0) { - # available water for additional irrigation consumption (considering downstream availability) - avl_wat_wc <- max(min(discharge[downstreamcells[[c]]] - required_wat_min_allocation[downstreamcells[[c]]]),0) - # how much consumption can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_wc/required_wat_fullirrig_wc[c,y],frac_fullirrig[c]) - } - - # adjust discharge in current cell and downstream cells (subtract irrigation water consumption) - discharge[c(downstreamcells[[c]],c)] <- discharge[c(downstreamcells[[c]],c)] - required_wat_fullirrig_wc[c,y]*frac_fullirrig[c] - # update minimum water required in cell: - required_wat_min_allocation[c] <- required_wat_min_allocation[c] + frac_fullirrig[c]*required_wat_fullirrig_ww[c,y] - - } - } - } else if (allocationrule=="upstreamfirst") { - # Allocate full irrigation requirements to most upstream cell first (calcorder) - - # Only consider cells where irrigation potential > 0 - # (or even: above certain threshold (e.g. 1 t/ha), maybe flexible (set in argument)) - # STILL MISSING - - # loop over basin - for (b in unique(endcell) ) { - - # allocation to upstream first (calcorder=1) - for (o in (1:max(calcorder[which(endcell==b)],na.rm=T))){ - c <- which(endcell==b & calcorder==o) - - # several cells with same calcorder in one basin - for (k in (1:length(which(endcell==b & calcorder==o)))){ - # available water for additional irrigation withdrawals - avl_wat_ww <- max(discharge[c[k]]-required_wat_min_allocation[c[k]],0) - - # withdrawal constraint - if (required_wat_fullirrig_ww[c[k],y]>0) { - # how much withdrawals can be fulfilled by available water - frac_fullirrig[c[k]] <- min(avl_wat_ww/required_wat_fullirrig_ww[c[k],y],1) - - # consumption constraint - if (required_wat_fullirrig_wc[c[k],y]>0 & length(downstreamcells[[c[k]]])>0) { - # available water for additional irrigation consumption (considering downstream availability) - avl_wat_wc <- max(min(discharge[downstreamcells[[c[k]]]] - required_wat_min_allocation[downstreamcells[[c[k]]]]),0) - # how much consumption can be fulfilled by available water - frac_fullirrig[c[k]] <- min(avl_wat_wc/required_wat_fullirrig_wc[c[k],y],frac_fullirrig[c[k]]) - } - - # adjust discharge in current cell and downstream cells (subtract irrigation water consumption) - discharge[c(downstreamcells[[c[k]]],c[k])] <- discharge[c(downstreamcells[[c[k]]],c[k])] - required_wat_fullirrig_wc[c[k],y]*frac_fullirrig[c[k]] - # update minimum water required in cell: - required_wat_min_allocation[c[k]] <- required_wat_min_allocation[c[k]] + frac_fullirrig[c[k]]*required_wat_fullirrig_ww[c[k],y] - } - } - } - } - } else if (allocationrule=="equality") { - - ## while loop? - ## 1 run with meancellrank; identify list of cells that still can get water (avl wat ww > 0, downstreamcell consumption); run through these again by order of meancellrank - - for (c in (1:max(meancellrank[,y],na.rm=T))){ - # available water for additional irrigation withdrawals - avl_wat_ww <- max(discharge[c]-required_wat_min_allocation[c],0) - - # withdrawal constraint - if (required_wat_fullirrig_ww[c,y]>0) { - # how much withdrawals can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_ww/required_wat_fullirrig_ww[c,y],1) - - # consumption constraint - if (required_wat_fullirrig_wc[c,y]>0 & length(downstreamcells[[c]])>0) { - # available water for additional irrigation consumption (considering downstream availability) - avl_wat_wc <- max(min(discharge[downstreamcells[[c]]] - required_wat_min_allocation[downstreamcells[[c]]]),0) - # how much consumption can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_wc/required_wat_fullirrig_wc[c,y],frac_fullirrig[c]) - } - - # adjust discharge in current cell and downstream cells (subtract irrigation water consumption) - discharge[c(downstreamcells[[c]],c)] <- discharge[c(downstreamcells[[c]],c)] - required_wat_fullirrig_wc[c,y]*frac_fullirrig[c] - # update minimum water required in cell: - required_wat_min_allocation[c] <- required_wat_min_allocation[c] + frac_fullirrig[c]*required_wat_fullirrig_ww[c,y] - - } - } - - # Repeat optimization algorithm several times - # Instead of full irrigation, only up to x% (e.g.20%) are allocated to most efficient cell - # Repeat until all river basin discharge is allocated ---> STILL MISSING - - for (c in (1:max(meancellrank[,y],na.rm=T))){ - # available water for additional irrigation withdrawals - avl_wat_ww <- max(discharge[c]-required_wat_min_allocation[c],0) - - # withdrawal constraint - if (required_wat_fullirrig_ww[c,y]>0) { - # how much withdrawals can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_ww/required_wat_fullirrig_ww[c,y],1) - - # consumption constraint - if (required_wat_fullirrig_wc[c,y]>0 & length(downstreamcells[[c]])>0) { - # available water for additional irrigation consumption (considering downstream availability) - avl_wat_wc <- max(min(discharge[downstreamcells[[c]]] - required_wat_min_allocation[downstreamcells[[c]]]),0) - # how much consumption can be fulfilled by available water - frac_fullirrig[c] <- min(avl_wat_wc/required_wat_fullirrig_wc[c,y],frac_fullirrig[c]) - } - - frac_fullirrig[c] <- frac_fullirrig[c]*allocationshare - - # adjust discharge in current cell and downstream cells (subtract irrigation water consumption) - discharge[c(downstreamcells[[c]],c)] <- discharge[c(downstreamcells[[c]],c)] - required_wat_fullirrig_wc[c,y]*frac_fullirrig[c] - # update minimum water required in cell: - required_wat_min_allocation[c] <- required_wat_min_allocation[c] + frac_fullirrig[c]*required_wat_fullirrig_ww[c,y] - } - } - } else { - stop("Please choose allocation rule for river basin discharge allocation algorithm") - } - - ################# - #### OUTPUTS #### - ################# - ### MAIN OUTPUT VARIABLE: water available for irrigation (consumptive agricultural use) - wat_avl_irrig_c <- CAC_magpie[,y]*frac_CAg_fulfilled + frac_fullirrig*required_wat_fullirrig_wc - wat_avl_irrig_w <- CAW_magpie[,y]*frac_CAg_fulfilled + frac_fullirrig*required_wat_fullirrig_ww - - if (output=="consumption") { - wat_avl_irrig <- wat_avl_irrig_c - dataname <- "wat_avl_irrig_c" - } else if (output=="withdrawal") { - wat_avl_irrig <- wat_avl_irrig_w - dataname <- "wat_avl_irrig_w" - } else { - stop("specify type of water availability output: withdrawal or consumption") - } - - wat_avl_irrig <- setNames(setYears(as.magpie(wat_avl_irrig,spatial=1),y), dataname) - wat_avl_irrig <- add_dimension(wat_avl_irrig, dim=3.1, add="nonag_scen", nm=scen) - wat_avl_irrig <- add_dimension(wat_avl_irrig, dim=3.1, add="EFP", nm=EFP) - out_tmp1 <- mbind(out_tmp1, wat_avl_irrig) - } - out_tmp2 <- mbind(out_tmp2, out_tmp1) - out_tmp1 <- NULL - } - out <- mbind(out, out_tmp2) - out_tmp2 <- NULL - } - - out <- out[magclassdata$cellbelongings$LPJ_input.Index,,] - out <- toolCell2isoCell(out) - description="Available water per year" - - return(list( - x=out, - weight=NULL, - unit="mio. m^3", - description=description, - isocountries=FALSE)) -} diff --git a/R/calcWaterUseCommittedAg.R b/R/calcWaterUseCommittedAg.R deleted file mode 100644 index cb4058778..000000000 --- a/R/calcWaterUseCommittedAg.R +++ /dev/null @@ -1,68 +0,0 @@ -#' @title calcWaterUseCommittedAg -#' @description This function calculates committed agricultural water uses that are used in the river routing algorithm for distributing available water across the basin -#' -#' @param version Switch between LPJmL4 and LPJmL5 -#' @param climatetype Switch between different climate scenarios (default: "CRU_4") -#' @param time Time smoothing: average or spline (default) -#' @param averaging_range Only specify if time=="average": number of time steps to average -#' @param dof Only specify if time=="spline": degrees of freedom needed for spline -#' @param harmonize_baseline FALSE (default): no harmonization of input data to this function, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on) -#' @param ref_year Reference year for harmonization baseline of input data to this function (just specify when harmonize_baseline=TRUE) -#' @param iniyear Year of initialization for cropland area -#' @param irrigini Initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell") -#' -#' @import magclass -#' -#' @return magpie object in cellular resolution -#' @author Felicitas Beier, Jens Heinke -#' -#' @examples -#' \dontrun{ calcOutput("WaterUseCommittedAg", aggregate = FALSE) } -#' - -calcWaterUseCommittedAg <- function(version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", selectyears=seq(1995,2095,by=5), - time="spline", dof=4, averaging_range=NULL, harmonize_baseline="CRU_4", ref_year="y2015", iniyear=1995, irrigini="Jaegermeyr_lpjcell"){ - - ############################## - ######## Read in data ######## - ############################## - ## Irrigation system area initialization - irrigation_system <- calcOutput("IrrigationSystem", source=irrigini, aggregate=FALSE) - - ## Read in Irrigation Water Withdrawals (in m^3 per hectar per year) [smoothed and harmonized] - irrig_withdrawal <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, irrig_requirement="withdrawal", aggregate=FALSE) - # Pasture is not irrigated in MAgPIE - irrig_withdrawal <- irrig_withdrawal[,,"pasture",invert=T] - - ## Read in Irrigation Water Consumption (in m^3 per hectar per year) [smoothed and harmonized] - irrig_consumption <- calcOutput("IrrigWatRequirements", version="LPJmL5", cells="lpjcell", selectyears=selectyears, climatetype=climatetype, harmonize_baseline=harmonize_baseline, ref_year=ref_year, time=time, dof=dof, irrig_requirement="consumption", aggregate=FALSE) - # Pasture is not irrigated in MAgPIE - irrig_consumption <- irrig_consumption[,,"pasture",invert=T] - - ## Read in cropland area (by crop) from crop area initialization (in mio. ha) - crops_grown <- calcOutput("IrrigatedArea", selectyears=selectyears, iniyear=iniyear, cells="lpjcell", aggregate=FALSE) - - ############################## - ######## Calculations ######## - ############################## - ## Committed agricultural uses (in mio. m^3 per year) [in initialization year] - # withdrawal - CAW <- (irrigation_system[,,]*irrig_withdrawal[,,]) * crops_grown - CAW <- dimSums(CAW,dim=3.1) - getNames(CAW) <- paste(rep("withdrawal",3),getNames(CAW),sep=".") - # consumption - CAU <- (irrigation_system[,,]*irrig_consumption[,,]) * crops_grown - CAU <- dimSums(CAU,dim=3.1) - getNames(CAU) <- paste(rep("consumption",3),getNames(CAU),sep=".") - # combine - CAD <- mbind(CAW, CAU) - names(dimnames(CAD))[1] <- "iso.cell" - names(dimnames(CAD))[3] <- "wateruse.crop" - - return(list( - x=CAD, - weight=NULL, - unit="mio. m^3 per year", - description="committed agricultural water demands", - isocountries=FALSE)) -} diff --git a/R/correctIrrigationSystem.R b/R/correctIrrigationSystem.R deleted file mode 100644 index 343f7057d..000000000 --- a/R/correctIrrigationSystem.R +++ /dev/null @@ -1,16 +0,0 @@ -#' @title correctIrrigationSystem -#' @description Correct Irrigation System data -#' @return List of magpie objects with results on cellular level, weight, unit and description. -#' @param x MAgPIE object provided by the read function -#' @author Felicitas Beier -#' @seealso -#' \code{\link{readIrrigationSystem}} -#' @examples -#' \dontrun{ -#' readSource("IrrigationSystem", convert="onlycorrect") -#' } -#' - -correctIrrigationSystem <- function(x){ - return(x) -} diff --git a/R/fullCELLULARMAGPIE.R b/R/fullCELLULARMAGPIE.R index 0ec29623b..852ee55ec 100644 --- a/R/fullCELLULARMAGPIE.R +++ b/R/fullCELLULARMAGPIE.R @@ -150,14 +150,6 @@ fullCELLULARMAGPIE <- function(rev=0.1, dev="", ctype="c200", climatetype="HadGE calcOutput("AvlWater", version=waterversion, years=lpj_years, climatetype=climatetype, harmonize_baseline=ifelse(waterversion!="LPJmL2", harmonize_baseline, FALSE), ref_year=ref_year, time="spline", dof=4, seasonality="grper", aggregate="cluster", round=6, file=paste0("lpj_watavail_grper_", ctype, ".mz")) calcOutput("AvlWater", version=waterversion, years=lpj_years, climatetype=climatetype, harmonize_baseline=ifelse(waterversion!="LPJmL2", harmonize_baseline, FALSE), ref_year=ref_year, time="spline", dof=4, seasonality="total", aggregate="cluster", round=6, file=paste0("lpj_watavail_total_", ctype, ".mz")) - - if(grepl("riverrouting_allocation",dev)){ - calcOutput("WaterAllocation", selectyears=lpj_years, output="consumption", version="LPJmL4", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, harmonize_baseline=harmonize_baseline, ref_year=ref_year, - allocationrule="optimization", allocationshare=NULL, gainthreshold=1, irrigationsystem="initialization", irrigini="Jaegermeyr_lpjcell", aggregate="cluster", round=6, file=paste0("avl_wat_agr_c_",ctype,".mz")) - calcOutput("ActualIrrigWatRequirements", selectyears=lpj_years, cells="magpiecell", crops="magpie", version="LPJmL5", climatetype="HadGEM2_ES:rcp2p6:co2", time="spline", averaging_range=NULL, dof=4, - harmonize_baseline=harmonize_baseline, ref_year=ref_year, irrig_requirement="consumption", irrig_system_source="Jaegermeyr_magpiecell", aggregate="cluster", round=6, file=paste0("wat_req_crops_c_",ctype,".mz")) - } - #44 biodiversity calcOutput("Luh2SideLayers", aggregate="cluster", round=6, file=paste0("luh2_side_layers_", ctype, ".mz")) calcOutput("RRLayer", aggregate="cluster", round=6, file=paste0("rr_layer_", ctype, ".mz")) diff --git a/R/readIrrigationSystem.R b/R/readIrrigationSystem.R deleted file mode 100644 index b4d800e8d..000000000 --- a/R/readIrrigationSystem.R +++ /dev/null @@ -1,75 +0,0 @@ -#' @title readIrrigationSystem -#' @description Read in irrigation system type for initialization -#' @param subtype Data source to be used: Jaegermeyr (irrigation system share based on FAO 2014, ICID 2012 and Rohwer et al. 2007) or LPJmL (dominant irrigation system per country) and number of cells (lpjcell or magpiecell) separated by _ -#' @return MAgPIE object of at country-level -#' @author Felicitas Beier -#' -#' @examples -#' \dontrun{ readSource("IrrigationSystem", convert="onlycorrect") } -#' -#' @import madrat -#' @import magclass -#' @importFrom mrcommons toolCell2isoCell - -readIrrigationSystem <- function(subtype="Jaegermeyr_magpiecell"){ - - # Mapping - map <- toolGetMapping("CountryToCellMapping.csv",type="cell") - map <- data.frame(celliso=map$celliso, iso=map$iso) - - # Irrigation system share by country - if (grepl("Jaegermeyr", subtype)){ - - # Read in and transform data - x <- read.csv("Jaegermeyr-2015-supplement_shr.csv") - x$Country <- toolCountry2isocode(x$Country,mapping=c("bahamas, the"="BHS", "congo-brazzaville"="COG", "dr congo, former zaire"="COD", - "gambia, the"="GMB", "myanmar (burma)"="MMR", "west bank"="PSE")) - # Transform to MAgPIE object - x <- as.magpie(x) - x <- toolCountryFill(x) - - # Where system share is not given, it is assumed that 100% of irrigated land is surface irrigation - x[,,"shr_AEI_surface"][is.na(x[,,"shr_AEI_surface"])] <- 1 - x[,,"shr_AEI_sprinkler"][is.na(x[,,"shr_AEI_sprinkler"])] <- 0 - x[,,"shr_AEI_drip"][is.na(x[,,"shr_AEI_drip"])] <- 0 - - # Expand to cellular level - x <- x[map$iso,,] - - } else if(grepl("LPJmL", subtype)){ - - # Read in and transform data - x <- read.csv("LPJmL_manage_irrig_IFT.csv") - x <- subset(x, x$country!="No_Land") - x$country <- toolCountry2isocode(x$country,mapping=c("bahamas, the"="BHS", "congo-brazzaville"="COG", "dr congo, former zaire"="COD", "falkland islands (islas malvinas)"="FLK", "western samoa"="WSM", - "french southern and antarctica lands"="ATF", "gambia, the"="GMB", "myanmar (burma)"="MMR", "west bank"="PSE", "cocos keeling islands"="CCK", - "pitcairn islands"="PCN", "saint helena ascension and tristan da cunha"="SHN", "st. vincent and the grenadines"="VCT", "virgin islands"="VIR")) - x <- subset(x, !duplicated(x)) - - # Transform to MAgPIE object - x <- as.magpie(x) - x <- toolCountryFill(x) - - # Expand to cellular level - x <- x[map$iso,,] - - # Replace NAs with 1 (surface irrigation) - # (Note: only affects country SJM: Svalbard and Jan Mayen) - x <- toolConditionalReplace(x, conditions=c("is.na()","<0"), replaceby = 1) - - } - - # Object dimensions - if (grepl("lpjcell", subtype)){ - lpj_map <- toolGetMapping("LPJ_CellBelongingsToCountries.csv",type="cell") - tmp <- x - getCells(tmp) <- paste(getCells(tmp),magclassdata$cellbelongings$LPJ_input.Index,sep=".") - x <- new.magpie(cells_and_regions=paste(lpj_map$ISO,1:67420,sep="."),years=NULL,getNames(x),fill=0) - x[magclassdata$cellbelongings$LPJ_input.Index,,] <- tmp[,,] - x <- toolCell2isoCell(x,cells="lpjcell") - } else if (grepl("magpiecell", subtype)){ - x <- toolCell2isoCell(x,cells="magpiecell") - } - - return(x) -} diff --git a/README.md b/README.md index 95c1111d4..25fa9f8da 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **0.27.4** +R package **mrmagpie**, version **0.27.5** @@ -38,8 +38,9 @@ In case of questions / problems please contact Kristine Karstens 30percent of total water} - -\item{EFRyears}{Long-term reference time frame for EFR calculation} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates environmental flow requirements (EFR) for MAgPIE based on LPJmL monthly discharge following Smakthin et al. (2004) -} -\examples{ -\dontrun{ calcOutput("EnvmtlFlowRequirements", aggregate=FALSE) } - -} -\author{ -Felicitas Beier, Jens Heinke -} diff --git a/man/calcFullIrrigationRequirement.Rd b/man/calcFullIrrigationRequirement.Rd deleted file mode 100644 index 2183cb80f..000000000 --- a/man/calcFullIrrigationRequirement.Rd +++ /dev/null @@ -1,57 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcFullIrrigationRequirement.R -\name{calcFullIrrigationRequirement} -\alias{calcFullIrrigationRequirement} -\title{calcFullIrrigationRequirement} -\usage{ -calcFullIrrigationRequirement( - version = "LPJmL5", - climatetype = "HadGEM2_ES:rcp2p6:co2", - harmonize_baseline = FALSE, - time = "spline", - dof = 4, - cells = "lpjcell", - selectyears = seq(1995, 2095, by = 5), - iniyear = 1995, - iniarea = TRUE, - irrig_requirement = "withdrawal" -) -} -\arguments{ -\item{version}{switch between LPJmL4 and LPJmL5} - -\item{climatetype}{switch between different climate scenarios (default: "CRU_4")} - -\item{harmonize_baseline}{FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on)} - -\item{time}{time smoothing: average, spline or raw (default)} - -\item{dof}{only specify if time=="spline": degrees of freedom needed for spline} - -\item{cells}{switch between "lpjcell" (67420) and "magpiecell" (59199)} - -\item{selectyears}{years to be returned} - -\item{iniyear}{Year of initialization for cropland area} - -\item{iniarea}{if TRUE (default): already irrigated area is subracted, if FALSE: total potential land area is used} - -\item{irrig_requirement}{consumptive (consumption) or non-consumptive (withdrawals) irrigation water requirements} - -\item{averaging_range}{only specify if time=="average": number of time steps to average} - -\item{ref_year}{reference year for harmonization baseline (just specify when harmonize_baseline=TRUE)} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates the water requirements for full irrigation per cell per crop given potentially available land -} -\examples{ -\dontrun{ calcOutput("FullIrrigationRequirement", aggregate=FALSE) } - -} -\author{ -Felicitas Beier -} diff --git a/man/calcIrrigCellranking.Rd b/man/calcIrrigCellranking.Rd deleted file mode 100644 index 209fe3984..000000000 --- a/man/calcIrrigCellranking.Rd +++ /dev/null @@ -1,58 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIrrigCellranking.R -\name{calcIrrigCellranking} -\alias{calcIrrigCellranking} -\title{calcIrrigCellranking} -\usage{ -calcIrrigCellranking( - version = "LPJmL5", - climatetype = "HadGEM2_ES:rcp2p6:co2", - time = "spline", - averaging_range = NULL, - dof = 4, - harmonize_baseline = FALSE, - ref_year = "y2015", - cellrankyear = "y1995", - cells = "lpjcell", - crops = "magpie", - method = "meancellrank", - proxycrop = c("maiz", "rapeseed", "puls_pro") -) -} -\arguments{ -\item{version}{switch between LPJmL version for yields} - -\item{climatetype}{switch between different climate scenarios for yields} - -\item{time}{time smoothing: average, spline (default) or raw} - -\item{averaging_range}{just specify for time=="average": number of time steps to average} - -\item{dof}{just specify for time=="spline": degrees of freedom} - -\item{harmonize_baseline}{FALSE (default) no harmonization, harmonization: if a baseline is specified here data is harmonized to that baseline (from ref_year onwards)} - -\item{ref_year}{just specify for harmonize_baseline != FALSE : Reference year} - -\item{cellrankyear}{year(s) for which cell rank is calculated} - -\item{cells}{switch between "lpjcell" (67420) and "magpiecell" (59199)} - -\item{crops}{switch between "magpie" and "lpjml" (default) crops} - -\item{method}{method of calculating the rank: "meancellrank" (default): mean over cellrank of proxy crops, "meancroprank": rank over mean of proxy crops} - -\item{proxycrop}{proxycrop(s) selected for rank calculation} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates a cellranking for the river basin discharge allocation based on yield improvement potential through irrigation -} -\examples{ -\dontrun{ calcOutput("calcIrrigCellranking", aggregate=FALSE) } -} -\author{ -Felicitas Beier -} diff --git a/man/calcIrrigWatRequirements.Rd b/man/calcIrrigWatRequirements.Rd deleted file mode 100644 index 5edb34915..000000000 --- a/man/calcIrrigWatRequirements.Rd +++ /dev/null @@ -1,56 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIrrigWatRequirements.R -\name{calcIrrigWatRequirements} -\alias{calcIrrigWatRequirements} -\title{calcIrrigWatRequirements} -\usage{ -calcIrrigWatRequirements( - selectyears = "all", - cells = "lpjcell", - crops = "magpie", - version = "LPJmL5", - climatetype = "HadGEM2_ES:rcp2p6:co2", - time = "spline", - averaging_range = NULL, - dof = 4, - harmonize_baseline = FALSE, - ref_year = NULL, - irrig_requirement = "withdrawal" -) -} -\arguments{ -\item{selectyears}{Years to be returned} - -\item{cells}{Switch between "lpjcell" (67420) and "magpiecell" (59199)} - -\item{crops}{Selects "magpie" (default) or "lpjml" crops} - -\item{version}{Switch between LPJmL4 and LPJmL5} - -\item{climatetype}{Switch between different climate scenarios (default: "CRU_4")} - -\item{time}{Time smoothing: average, spline or raw (default) of input data to this function} - -\item{averaging_range}{only specify if time=="average": number of time steps to average} - -\item{dof}{only specify if time=="spline": degrees of freedom needed for spline} - -\item{harmonize_baseline}{FALSE (default): no harmonization of input data to this function, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on)} - -\item{ref_year}{Reference year for harmonization baseline of input data to this function (just specify when harmonize_baseline=TRUE)} - -\item{irrig_requirement}{Consumptive (consumption) or non-consumptive (withdrawals) irrigation water requirements} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates irrigation water requirements based on LPJmL blue water consumption of plants and considering irrigation efficiencies -} -\examples{ -\dontrun{ calcOutput("IrrigWatRequirements", aggregate=FALSE) } - -} -\author{ -Felicitas Beier, Jens Heinke -} diff --git a/man/calcIrrigYieldImprovementPotential.Rd b/man/calcIrrigYieldImprovementPotential.Rd deleted file mode 100644 index 61a277404..000000000 --- a/man/calcIrrigYieldImprovementPotential.Rd +++ /dev/null @@ -1,53 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIrrigYieldImprovementPotential.R -\name{calcIrrigYieldImprovementPotential} -\alias{calcIrrigYieldImprovementPotential} -\title{calcIrrigYieldImprovementPotential} -\usage{ -calcIrrigYieldImprovementPotential( - version = "LPJmL5", - climatetype = "HadGEM2_ES:rcp2p6:co2", - time = "spline", - averaging_range = NULL, - dof = 4, - harmonize_baseline = FALSE, - ref_year = NULL, - selectyears = seq(1995, 2095, by = 5), - cells = "magpiecell", - crops = "lpjml" -) -} -\arguments{ -\item{version}{switch between LPJmL4 and LPJmL5 of calcYields function} - -\item{climatetype}{switch between different climate scenarios (default: "CRU_4") of calcYields function} - -\item{time}{time smoothing of calcYields function: average, spline (default) or raw} - -\item{averaging_range}{only specify if time=="average": number of time steps to average} - -\item{dof}{only specify if time=="spline": degrees of freedom needed for spline} - -\item{harmonize_baseline}{harmonization in calcYields function: FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year onwards)} - -\item{ref_year}{reference year for harmonization baseline (just specify when harmonize_baseline=TRUE)} - -\item{selectyears}{years to be returned by the function} - -\item{cells}{switch between "lpjcell" (67420) and "magpiecell" (59199)} - -\item{crops}{switch between "magpie" and "lpjml" (default) crops} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates the yield improvement potential of irrigation for different crops -} -\examples{ -\dontrun{ calcOutput("IrrigYieldImprovementPotential", aggregate=FALSE) } - -} -\author{ -Felicitas Beier -} diff --git a/man/calcIrrigatedArea.Rd b/man/calcIrrigatedArea.Rd deleted file mode 100644 index 131cab60a..000000000 --- a/man/calcIrrigatedArea.Rd +++ /dev/null @@ -1,35 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIrrigatedArea.R -\name{calcIrrigatedArea} -\alias{calcIrrigatedArea} -\title{calcIrrigatedArea} -\usage{ -calcIrrigatedArea( - selectyears = seq(1995, 2100, by = 5), - iniyear = 1995, - depreciation = 0.1, - cells = "lpjcell" -) -} -\arguments{ -\item{selectyears}{select years} - -\item{iniyear}{initialization year} - -\item{depreciation}{parameter defining yearly depreciation rate at which previously irrigated cropland becomes "unreserved" for irrigation} - -\item{cells}{cells to be returned by the function (lpjcell or magpiecell)} -} -\value{ -magpie object in cellular resolution -} -\description{ -calculates area reserved for irrigation based on area irrigated in initialization year and depreciation parameter -} -\examples{ -\dontrun{ calcOutput("IrrigatedArea", aggregate=FALSE) } - -} -\author{ -Felicitas Beier -} diff --git a/man/calcIrrigationSystem.Rd b/man/calcIrrigationSystem.Rd deleted file mode 100644 index 54bda4177..000000000 --- a/man/calcIrrigationSystem.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIrrigationSystem.R -\name{calcIrrigationSystem} -\alias{calcIrrigationSystem} -\title{calcIrrigationSystem} -\usage{ -calcIrrigationSystem(source = "Jaegermeyr_lpjcell") -} -\arguments{ -\item{source}{Data source to be used: Jaegermeyr (irrigation system share based on FAO 2014, ICID 2012 and Rohwer et al. 2007) or LPJmL (dominant irrigation system per country) and number of cells (lpjcell or magpiecell) separated by _} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function returns the irrigation system share initialization -} -\examples{ -\dontrun{ calcOutput("IrrigationSystem",source="Jaegermeyr_lpjcell",aggregate = FALSE) } - -} -\author{ -Felicitas Beier -} diff --git a/man/calcWaterAllocation.Rd b/man/calcWaterAllocation.Rd deleted file mode 100644 index eeced9567..000000000 --- a/man/calcWaterAllocation.Rd +++ /dev/null @@ -1,68 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcWaterAllocation.R -\name{calcWaterAllocation} -\alias{calcWaterAllocation} -\title{calcWaterAllocation} -\usage{ -calcWaterAllocation( - selectyears = "all", - output = "consumption", - version = "LPJmL4", - climatetype = "HadGEM2_ES:rcp2p6:co2", - time = "spline", - averaging_range = NULL, - dof = 4, - harmonize_baseline = "CRU_4", - ref_year = "y2015", - allocationrule = "optimization", - allocationshare = NULL, - gainthreshold = 1, - irrigationsystem = "initialization", - irrigini = "Jaegermeyr_lpjcell", - iniyear = 1995 -) -} -\arguments{ -\item{selectyears}{Years to be returned} - -\item{output}{Water availability output to be returned: withdrawal or consumption} - -\item{version}{Switch between LPJmL4 and LPJmL5} - -\item{climatetype}{Switch between different climate scenarios (default: "CRU_4")} - -\item{time}{Time smoothing: average, spline or raw (default)} - -\item{averaging_range}{only specify if time=="average": number of time steps to average} - -\item{dof}{only specify if time=="spline": degrees of freedom needed for spline} - -\item{harmonize_baseline}{FALSE (default): no harmonization, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on)} - -\item{ref_year}{Reference year for harmonization baseline (just specify when harmonize_baseline=TRUE)} - -\item{allocationrule}{Rule to be applied for river basin discharge allocation across cells of river basin ("optimization" (default), "upstreamfirst", "equality")} - -\item{allocationshare}{Share of water to be allocated to cell (only needs to be selected in case of allocationrule=="equality")} - -\item{gainthreshold}{Threshold of yield improvement potential required for water allocation in upstreamfirst algorithm (in tons per ha)} - -\item{irrigationsystem}{Irrigation system to be used for river basin discharge allocation algorithm ("surface", "sprinkler", "drip", "initialization")} - -\item{irrigini}{When "initialization" selected for irrigation system: choose initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell")} - -\item{iniyear}{Initialization year of irrigation system} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates water availability for MAgPIE retrieved from LPJmL using a river routing and allocation algorithm for distribution of discharge within the river basin -} -\examples{ -\dontrun{ calcOutput("WaterAllocation", aggregate = FALSE) } - -} -\author{ -Felicitas Beier, Jens Heinke -} diff --git a/man/calcWaterUseCommittedAg.Rd b/man/calcWaterUseCommittedAg.Rd deleted file mode 100644 index 5110ffbcf..000000000 --- a/man/calcWaterUseCommittedAg.Rd +++ /dev/null @@ -1,51 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcWaterUseCommittedAg.R -\name{calcWaterUseCommittedAg} -\alias{calcWaterUseCommittedAg} -\title{calcWaterUseCommittedAg} -\usage{ -calcWaterUseCommittedAg( - version = "LPJmL5", - climatetype = "HadGEM2_ES:rcp2p6:co2", - selectyears = seq(1995, 2095, by = 5), - time = "spline", - dof = 4, - averaging_range = NULL, - harmonize_baseline = "CRU_4", - ref_year = "y2015", - iniyear = 1995, - irrigini = "Jaegermeyr_lpjcell" -) -} -\arguments{ -\item{version}{Switch between LPJmL4 and LPJmL5} - -\item{climatetype}{Switch between different climate scenarios (default: "CRU_4")} - -\item{time}{Time smoothing: average or spline (default)} - -\item{dof}{Only specify if time=="spline": degrees of freedom needed for spline} - -\item{averaging_range}{Only specify if time=="average": number of time steps to average} - -\item{harmonize_baseline}{FALSE (default): no harmonization of input data to this function, TRUE: if a baseline is specified here data is harmonized to that baseline (from ref_year on)} - -\item{ref_year}{Reference year for harmonization baseline of input data to this function (just specify when harmonize_baseline=TRUE)} - -\item{iniyear}{Year of initialization for cropland area} - -\item{irrigini}{Initialization data set for irrigation system initialization ("Jaegermeyr_lpjcell", "LPJmL_lpjcell")} -} -\value{ -magpie object in cellular resolution -} -\description{ -This function calculates committed agricultural water uses that are used in the river routing algorithm for distributing available water across the basin -} -\examples{ -\dontrun{ calcOutput("WaterUseCommittedAg", aggregate = FALSE) } - -} -\author{ -Felicitas Beier, Jens Heinke -} diff --git a/man/correctIrrigationSystem.Rd b/man/correctIrrigationSystem.Rd deleted file mode 100644 index 175104eaf..000000000 --- a/man/correctIrrigationSystem.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/correctIrrigationSystem.R -\name{correctIrrigationSystem} -\alias{correctIrrigationSystem} -\title{correctIrrigationSystem} -\usage{ -correctIrrigationSystem(x) -} -\arguments{ -\item{x}{MAgPIE object provided by the read function} -} -\value{ -List of magpie objects with results on cellular level, weight, unit and description. -} -\description{ -Correct Irrigation System data -} -\examples{ -\dontrun{ - readSource("IrrigationSystem", convert="onlycorrect") -} - -} -\seealso{ -\code{\link{readIrrigationSystem}} -} -\author{ -Felicitas Beier -} diff --git a/man/readIrrigationSystem.Rd b/man/readIrrigationSystem.Rd deleted file mode 100644 index 425a8159f..000000000 --- a/man/readIrrigationSystem.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readIrrigationSystem.R -\name{readIrrigationSystem} -\alias{readIrrigationSystem} -\title{readIrrigationSystem} -\usage{ -readIrrigationSystem(subtype = "Jaegermeyr_magpiecell") -} -\arguments{ -\item{subtype}{Data source to be used: Jaegermeyr (irrigation system share based on FAO 2014, ICID 2012 and Rohwer et al. 2007) or LPJmL (dominant irrigation system per country) and number of cells (lpjcell or magpiecell) separated by _} -} -\value{ -MAgPIE object of at country-level -} -\description{ -Read in irrigation system type for initialization -} -\examples{ -\dontrun{ readSource("IrrigationSystem", convert="onlycorrect") } - -} -\author{ -Felicitas Beier -}