Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted package to mrcommons split #10

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '220716090'
ValidationKey: '22437280'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrwater: madrat based MAgPIE water Input Data Library'
version: 1.11.22
date-released: '2024-05-02'
version: 1.13.0
date-released: '2024-05-13'
abstract: Provides functions for MAgPIE cellular input data generation and stand-alone
water calculations.
authors:
Expand Down
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Type: Package
Package: mrwater
Title: madrat based MAgPIE water Input Data Library
Version: 1.11.22
Version: 1.13.0
URL: https://github.com/pik-piam/mrwater,
https: //doi.org/10.5281/zenodo.5801680
License: LGPL-3 | file LICENSE
Date: 2024-05-02
Date: 2024-05-13
Authors@R: c(person("Felicitas", "Beier", email = "[email protected]", role = c("aut","cre")),
person("Jens", "Heinke", email = "[email protected]", role = "aut"),
person("Kristine", "Karstens", email = "[email protected]", role = "aut"),
Expand All @@ -17,11 +17,12 @@ Depends:
R (>= 2.10.0),
madrat (>= 1.72),
magclass (>= 6.0.0),
mrcommons (>= 1.37.0),
mrland (>= 0.51.0)
mrcommons (>= 1.41.0),
mrlandcore (>= 1.0.0),
mrland (>= 0.59.0)
Imports:
magpiesets,
mstools,
mstools (>= 0.6.0),
raster,
stats,
stringr,
Expand Down
13 changes: 7 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import(madrat)
import(magclass)
import(mrcommons)
import(mrland)
import(mrlandcore)
importFrom(madrat,calcOutput)
importFrom(madrat,readSource)
importFrom(madrat,toolAggregate)
Expand Down Expand Up @@ -60,13 +61,13 @@ importFrom(magclass,time_interpolate)
importFrom(magclass,where)
importFrom(magpiesets,addLocation)
importFrom(magpiesets,findset)
importFrom(mrcommons,toolCell2isoCell)
importFrom(mrcommons,toolCoord2Isocell)
importFrom(mrcommons,toolGetMappingCoord2Country)
importFrom(mrcommons,toolHarmonize2Baseline)
importFrom(mrcommons,toolLPJmLVersion)
importFrom(mrcommons,toolSmooth)
importFrom(mrlandcore,toolLPJmLVersion)
importFrom(mstools,toolCell2isoCell)
importFrom(mstools,toolCoord2Isocell)
importFrom(mstools,toolGetMappingCoord2Country)
importFrom(mstools,toolHarmonize2Baseline)
importFrom(mstools,toolHoldConstant)
importFrom(mstools,toolSmooth)
importFrom(raster,brick)
importFrom(stats,lm)
importFrom(stats,quantile)
Expand Down
5 changes: 2 additions & 3 deletions R/calcAreaPotIrrig.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#'
#' @importFrom madrat calcOutput toolSplitSubtype
#' @importFrom magclass collapseNames getCells getYears getNames dimSums time_interpolate
#' @importFrom mrcommons toolGetMappingCoord2Country
#' @importFrom mstools toolHoldConstant
#' @importFrom mstools toolHoldConstant toolGetMappingCoord2Country

calcAreaPotIrrig <- function(selectyears, comagyear, iniyear, landScen) {

Expand Down Expand Up @@ -66,7 +65,7 @@ calcAreaPotIrrig <- function(selectyears, comagyear, iniyear, landScen) {
# urbanLand <- calcOutput("UrbanLandFuture", subtype = "LUH2v2",
# timestep = "yearly", cells = "lpjcell",
# aggregate = FALSE)[, selectyears, ]
# getItems(urbanLand, dim = 3) <- gsub("SSP", "ssp", getItems(urbanLand, dim = 3))
# getItems(urbanLand, dim = 3) <- gsub("SSP", "ssp", getItems(urbanLand, dim = 3)) # nolint: comment_code_linter

# Read in suitable land for irrigation based on Zabel [in mio. ha]
# excluding land that is marginal under irrigated conditions (< suitability index of 0.33)
Expand Down
29 changes: 15 additions & 14 deletions R/calcAvlWater.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#'
#' @import magclass
#' @import madrat
#' @importFrom mrcommons toolHarmonize2Baseline toolLPJmLVersion
#' @importFrom mstools toolHarmonize2Baseline
#' @importFrom mrlandcore toolLPJmLVersion
#'
#' @return magpie object in cellular resolution
#' @author Felicitas Beier, Kristine Karstens, Abhijeet Mishra
Expand Down Expand Up @@ -43,27 +44,27 @@ calcAvlWater <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop =
if (stage %in% c("raw", "smoothed")) {
### Monthly Discharge (unit (after calcLPJmL): mio. m^3/month)
monthDischargeMAG <- calcOutput("LPJmL_new", subtype = "mdischarge",
stage = "raw",
version = lpjmlReadin["natveg"],
climatetype = climatetype,
aggregate = FALSE)
stage = "raw",
version = lpjmlReadin["natveg"],
climatetype = climatetype,
aggregate = FALSE)

### Monthly Runoff (unit (after calcLPJmL): mio. m^3/month)
monthRunoffMAG <- calcOutput("LPJmL_new", subtype = "mrunoff",
stage = "raw",
version = lpjmlReadin["natveg"],
climatetype = climatetype,
aggregate = FALSE)
stage = "raw",
version = lpjmlReadin["natveg"],
climatetype = climatetype,
aggregate = FALSE)

## River basin water allocation algorithm:
# Read in river structure
rs <- readRDS(system.file("extdata/riverstructure_stn_coord.rds",
package = "mrwater"))
package = "mrwater"))
basinCode <- rs$endcell

if (any(paste(getItems(monthRunoffMAG, dim = "x", full = TRUE),
getItems(monthRunoffMAG, dim = "y", full = TRUE),
sep = ".") != rs$coordinates)) {
getItems(monthRunoffMAG, dim = "y", full = TRUE),
sep = ".") != rs$coordinates)) {
stop("Wrong cell ordering of basin in calcAvlWater.R")
}

Expand Down Expand Up @@ -133,8 +134,8 @@ calcAvlWater <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop =

# Growing days per month
growDAYS <- calcOutput("GrowingPeriod", cells = "lpjcell",
lpjml = lpjmlReadin, climatetype = climatetype,
stage = stage, yield_ratio = 0.1, aggregate = FALSE)
lpjml = lpjmlReadin, climatetype = climatetype,
stage = stage, yield_ratio = 0.1, aggregate = FALSE)

# Adjust years
yearsWAT <- getYears(dailyAvlWat)
Expand Down
2 changes: 1 addition & 1 deletion R/calcCropAreaPotIrrig.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#'
#' @importFrom madrat calcOutput toolSplitSubtype
#' @importFrom magclass collapseNames getCells getYears getNames dimSums
#' @importFrom mrcommons toolGetMappingCoord2Country
#' @importFrom mstools toolGetMappingCoord2Country

calcCropAreaPotIrrig <- function(selectyears, comagyear, iniyear,
cropmix, landScen) {
Expand Down
4 changes: 2 additions & 2 deletions R/calcCropAreaShare.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#'
#' @importFrom madrat calcOutput toolGetMapping
#' @importFrom magclass collapseNames getCells getSets getYears getNames new.magpie dimSums
#' @importFrom mrcommons toolCell2isoCell toolGetMappingCoord2Country
#' @importFrom mstools toolCell2isoCell toolGetMappingCoord2Country

calcCropAreaShare <- function(iniyear, cropmix) {

# read physical croparea
croparea <- calcOutput("CropareaAdjusted", iniyear = iniyear,
aggregate = FALSE)
aggregate = FALSE)

# total croparea (irrigated + rainfed)
totCroparea <- dimSums(croparea, dim = "irrigation")
Expand Down
2 changes: 1 addition & 1 deletion R/calcCropareaAdjusted.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @importFrom madrat calcOutput
#' @importFrom magclass getCells getNames add_dimension new.magpie
#' @importFrom mrcommons toolGetMappingCoord2Country
#' @importFrom mstools toolGetMappingCoord2Country

calcCropareaAdjusted <- function(iniyear, dataset = "LandInG", sectoral = "kcr") {

Expand Down
2 changes: 1 addition & 1 deletion R/calcEnvmtlFlowRequirementsShare.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @importFrom magclass collapseNames new.magpie getYears setYears as.array as.magpie add_dimension mbind
#' @importFrom madrat calcOutput
#' @importFrom mrcommons toolLPJmLVersion
#' @importFrom mrlandcore toolLPJmLVersion
#' @importFrom stats quantile
#'
#' @return magpie object in cellular resolution representing share of discharge
Expand Down
6 changes: 3 additions & 3 deletions R/calcFullIrrigationRequirement.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#' or drip, surface, sprinkler for full irrigation by selected system
#' @param landScen Land availability scenario consisting of two parts separated by ":":
#' 1. available land scenario (currCropland, currIrrig, potCropland)
#' 2. protection scenario (WDPA, or one of the scenarios available in calcConservationPriorities,
#' e.g., 30by20, BH, BH_IFL, PBL_HalfEarth,
#' 2. protection scenario (WDPA, or one of the scenarios available in
#' calcConservationPriorities, e.g., 30by20, BH, BH_IFL, PBL_HalfEarth,
#' or NA for no protection).
#' For case of no land protection select "NA" in second part of argument
#' or do not specify second part of the argument
Expand Down Expand Up @@ -47,7 +47,7 @@
#'
#' @importFrom madrat calcOutput toolGetMapping
#' @importFrom magclass collapseNames getItems new.magpie dimSums
#' @importFrom mrcommons toolCell2isoCell toolGetMappingCoord2Country
#' @importFrom mstools toolCell2isoCell toolGetMappingCoord2Country

calcFullIrrigationRequirement <- function(lpjml, climatetype,
selectyears, iniyear, comagyear,
Expand Down
42 changes: 21 additions & 21 deletions R/calcGrowingPeriod.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#'
#' @importFrom madrat toolGetMapping toolAggregate
#' @importFrom magclass collapseNames getItems new.magpie getYears dimSums magpie_expand
#' @importFrom mrcommons toolHarmonize2Baseline toolSmooth toolLPJmLVersion toolGetMappingCoord2Country
#' @importFrom mstools toolHarmonize2Baseline toolSmooth toolGetMappingCoord2Country
#' @importFrom mrlandcore toolLPJmLVersion
#'
#' @export

Expand All @@ -37,7 +38,7 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",
crop = unname(cfgCrop$readin_version))

lpjmlBaseline <- c(natveg = unname(cfgNatveg$baseline_version),
crop = unname(cfgCrop$baseline_version))
crop = unname(cfgCrop$baseline_version))

if (stage %in% c("raw", "smoothed")) {

Expand All @@ -62,8 +63,8 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",
####################################################################################

lpj2mag <- toolGetMapping("MAgPIE_LPJmL.csv",
type = "sectoral",
where = "mappingfolder")
type = "sectoral",
where = "mappingfolder")

# Read yields first
yields <- collapseNames(calcOutput("LPJmL_new", version = lpjmlReadin["crop"],
Expand All @@ -72,11 +73,11 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",

# Load Sowing dates from LPJmL (use just rainfed dates since they do not differ for irrigated and rainfed)
sowd <- collapseNames(calcOutput("LPJmL_new", version = lpjmlReadin["crop"],
climatetype = climatetype, subtype = "sdate",
stage = "raw", aggregate = FALSE)[, , "rainfed"])
climatetype = climatetype, subtype = "sdate",
stage = "raw", aggregate = FALSE)[, , "rainfed"])
hard <- collapseNames(calcOutput("LPJmL_new", version = lpjmlReadin["crop"],
climatetype = climatetype, subtype = "hdate",
stage = "raw", aggregate = FALSE)[, , "rainfed"])
climatetype = climatetype, subtype = "hdate",
stage = "raw", aggregate = FALSE)[, , "rainfed"])

goodCrops <- lpj2mag$MAgPIE[which(lpj2mag$LPJmL %in% getItems(sowd, dim = 3))]
badCrops <- lpj2mag$MAgPIE[which(!lpj2mag$LPJmL %in% getItems(sowd, dim = 3))]
Expand All @@ -93,7 +94,7 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",

if (length(badCrops) > 0) {
vcat(2, "No information on the growing period found for those crops: ",
paste(unique(badCrops), collapse = ", "))
paste(unique(badCrops), collapse = ", "))
}

#####################################################################################
Expand Down Expand Up @@ -123,8 +124,8 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",
yields <- collapseNames(yields[, , goodCrops])

cell2GLO <- array(c(getItems(yields, dim = 1),
rep("GLO", length(getItems(yields, dim = 1)))),
dim = c(length(getItems(yields, dim = 1)), 2))
rep("GLO", length(getItems(yields, dim = 1)))),
dim = c(length(getItems(yields, dim = 1)), 2))
gloYields <- toolAggregate(yields, cell2GLO, weight = setYears(area, NULL))
yieldsRatio <- yields / gloYields

Expand Down Expand Up @@ -152,9 +153,8 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",

# define all crops sowed after 180 days and where sowing date is after harvest date as wintercrops
rmWintercrops[cellsNrthnHem, , ] <- ifelse(sowd[cellsNrthnHem, , ] > 180 &
hard[cellsNrthnHem, , ] < sowd[cellsNrthnHem, , ],
NA,
1)
hard[cellsNrthnHem, , ] < sowd[cellsNrthnHem, , ],
NA, 1)

####################################################################################

Expand Down Expand Up @@ -240,9 +240,9 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",

# mag object for the growing days per month
growdaysPERmonth <- new.magpie(cells_and_regions = getItems(meanSowd, dim = 1),
years = getItems(meanSowd, dim = 2),
names = month,
fill = 0)
years = getItems(meanSowd, dim = 2),
names = month,
fill = 0)

# determine the harvest day, take care if it is greater than 365
meanHard <- (meanSowd + meanGrper - 1) %% 365
Expand Down Expand Up @@ -319,8 +319,8 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",
} else {

x <- calcOutput("GrowingPeriod", lpjml = lpjml, climatetype = climatetype,
stage = "smoothed", yield_ratio = yield_ratio,
cells = "lpjcell", aggregate = FALSE)
stage = "smoothed", yield_ratio = yield_ratio,
cells = "lpjcell", aggregate = FALSE)
# Harmonize to baseline
out <- toolHarmonize2Baseline(x = x, base = baseline, ref_year = cfgNatveg$ref_year_hist)
}
Expand All @@ -329,8 +329,8 @@ calcGrowingPeriod <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de",

# read in historical data for subtype
baseline2020 <- calcOutput("GrowingPeriod", lpjml = lpjmlBaseline, climatetype = cfgNatveg$baseline_gcm,
stage = "harmonized", yield_ratio = yield_ratio,
cells = "lpjcell", aggregate = FALSE)
stage = "harmonized", yield_ratio = yield_ratio,
cells = "lpjcell", aggregate = FALSE)


if (climatetype == cfgNatveg$baseline_gcm) {
Expand Down
2 changes: 1 addition & 1 deletion R/calcIrrigWatRequirements.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#'
#' @importFrom magclass getItems new.magpie add_dimension
#' @importFrom madrat calcOutput toolAggregate toolGetMapping
#' @importFrom mrcommons toolCell2isoCell
#' @importFrom mstools toolCell2isoCell
#' @importFrom stringr str_split
#' @importFrom withr local_options

Expand Down
Loading