Skip to content

Commit

Permalink
ensure that dimensions in calcFullIrrigationRequirements match
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicitasBeier committed Aug 11, 2023
1 parent 24f09e4 commit 9751fd3
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3876444'
ValidationKey: '3896420'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
92 changes: 21 additions & 71 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Run CI for R using https://eddelbuettel.github.io/r-ci/

name: check

on:
Expand All @@ -8,92 +6,44 @@ on:
pull_request:
branches: [main, master]

env:
USE_BSPM: "true"
_R_CHECK_FORCE_SUGGESTS_: "false"
NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen", "GDPuc", "roxygen2")'

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Bootstrap
run: |
sudo chown runner -R .
sudo locale-gen en_US.UTF-8
sudo add-apt-repository -y ppa:ubuntugis/ppa
curl -OLs https://eddelbuettel.github.io/r-ci/run.sh
chmod 0755 run.sh
./run.sh bootstrap
rm -f bspm_*.tar.gz
- name: Enable r-universe repo, modify bspm integration
run: |
# install packages from https://pik-piam.r-universe.dev and CRAN
echo '
options(repos = c(universe = "https://pik-piam.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
' >> .Rprofile
cat .Rprofile
# modify bspm integration to never install binary builds of PIK CRAN packages
sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site
# need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above)
printf '
local({
expr <- quote({
if (!is.null(repos)) {
noBinaryInstallRPackages <- %s
pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]),
pkgs[pkgs %%in%% noBinaryInstallRPackages])
}
type <- "source"
})
trace(utils::install.packages, expr, print = FALSE)
})
' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null
cat /etc/R/Rprofile.site
- name: Set up Pandoc
uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-pandoc@v2

- name: Set up Python 3.9
uses: actions/setup-python@v4
- uses: r-lib/actions/setup-r@v2
with:
python-version: 3.9
use-public-rspm: true
extra-repositories: "https://rse.pik-potsdam.de/r/packages"

- name: Cache R libraries
if: ${{ !env.ACT }} # skip when running locally via nektos/act
uses: pat-s/always-upload-cache@v3
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: /usr/local/lib/R/
key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }}
restore-keys: |
3-${{ runner.os }}-usr-local-lib-R-
- name: Restore R library permissions
run: |
sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library
- name: Install dependencies
run: |
./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev
./run.sh install_all
./run.sh install_r_binary covr rstudioapi
./run.sh install_r lucode2
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install python dependencies if applicable
run: |
[ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true
[ -f requirements.txt ] && pip install -r requirements.txt || true
- name: Remove bspm integration # to get rid of error when running install.packages
run: |
sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site
cat /etc/R/Rprofile.site
- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9013
rev: v0.3.2.9019
hooks:
- id: parsable-R
- id: deps-in-desc
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.9.8
date-released: '2023-08-09'
version: 1.9.9
date-released: '2023-08-11'
abstract: Provides functions for MAgPIE cellular input data generation and stand-alone
water calculations.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Type: Package
Package: mrwater
Title: madrat based MAgPIE water Input Data Library
Version: 1.9.8
Version: 1.9.9
URL: https://github.com/pik-piam/mrwater,
https: //doi.org/10.5281/zenodo.5801680
License: LGPL-3 | file LICENSE
Date: 2023-08-09
Date: 2023-08-11
Authors@R: c(person("Felicitas", "Beier", email = "beier@pik-potsdam.de", role = c("aut","cre")),
person("Jens", "Heinke", email = "heinke@pik-potsdam.de", role = "aut"),
person("Kristine", "Karstens", email = "karstens@pik-potsdam.de", role = "aut"),
Expand Down
4 changes: 2 additions & 2 deletions R/calcAreaPotIrrig.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ calcAreaPotIrrig <- function(selectyears, comagyear, iniyear, landScen,
efrMethod = efrMethod,
multicropping = multicropping, transDist = transDist,
aggregate = FALSE))
if (any(scenarios != unique(getItems(dimSums(comIrrigArea, dim = 3.1), dim = 3)))) {
if (any(scenarios != unique(getItems(dimSums(comIrrigArea, dim = "crop"), dim = 3)))) {
stop("Apparently the number of scenarios or format has changed.
Please adjust default argument of mrwater::calcAreaPotIrrig accordingly.")
}
comIrrigArea <- collapseNames(dimSums(comIrrigArea, dim = 3.1))
comIrrigArea <- collapseNames(dimSums(comIrrigArea, dim = "crop"))
out <- out - comIrrigArea
}

Expand Down
19 changes: 19 additions & 0 deletions R/calcFullIrrigationRequirement.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ calcFullIrrigationRequirement <- function(lpjml, climatetype,
lpjml = lpjml, climatetype = climatetype,
irrigationsystem = irrigationsystem, multicropping = multicropping,
aggregate = FALSE)[, , croplist]
# Transform object dimensionality
.transformObject <- function(x, gridcells, years, names) {
# empty magpie object structure
object0 <- new.magpie(
cells_and_regions = gridcells,
years = years,
names = names,
fill = 0,
sets = c("x.y.iso", "year", "EFP.scen.crop")
)
# bring object x to dimension of object0
out <- object0 + x
return(out)
}
irrigWat <- .transformObject(x = irrigWat,
gridcells = getItems(irrigWat, dim = 1),
years = getItems(irrigWat, dim = 2),
names = getItems(croparea, dim = 3))


# # correct irrigation water requirements where irrigation would lead to 0 yield gains
# tmp <- calcOutput("IrrigCropYieldGain", priceAgg = "GLO",
Expand Down
4 changes: 2 additions & 2 deletions R/calcIrrigAreaCommitted.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ calcIrrigAreaCommitted <- function(selectyears, iniyear) {
irrigArea <- new.magpie(cells_and_regions = getCells(tmp),
years = seq(iniyear, tail(selectyears, 1), by = 1),
names = getNames(tmp),
sets = c("x.y.iso", "year", "data"))
sets = c("x.y.iso", "year", "crop"))

# Each year certain share (parameter: "depreciation") of irrigated cropland is lost
# Note: Depreciation in yearly time-steps!
Expand All @@ -69,7 +69,7 @@ calcIrrigAreaCommitted <- function(selectyears, iniyear) {

return(list(x = irrigArea,
weight = NULL,
unit = "mio. ha",
unit = "Mha",
description = "Cropland area reserved for irrigation per crop",
isocountries = FALSE))
}
3 changes: 0 additions & 3 deletions R/fullMULTICROPPING.R
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,6 @@ fullMULTICROPPING <- function() {
}





}


Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE water Input Data Library

R package **mrwater**, version **1.9.8**
R package **mrwater**, version **1.9.9**

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

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

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

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

A BibTeX entry for LaTeX users is

Expand All @@ -57,7 +57,7 @@ A BibTeX entry for LaTeX users is
title = {mrwater: madrat based MAgPIE water Input Data Library},
author = {Felicitas Beier and Jens Heinke and Kristine Karstens and Benjamin Leon Bodirsky and Jan Philipp Dietrich},
year = {2023},
note = {R package version 1.9.8},
note = {R package version 1.9.9},
url = {https://github.com/pik-piam/mrwater},
}
```

0 comments on commit 9751fd3

Please sign in to comment.