Skip to content

Commit

Permalink
update validProcessing to reflect summation changes in rerporting
Browse files Browse the repository at this point in the history
  • Loading branch information
caviddhen committed Apr 11, 2024
1 parent 9943e88 commit f2c93a9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '50848968'
ValidationKey: '50927856'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -36,7 +35,7 @@ jobs:
# 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
- uses: actions/setup-python@v5
with:
python-version: 3.9

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: 'mrvalidation: madrat data preparation for validation purposes'
version: 2.56.8
date-released: '2024-03-19'
version: 2.56.9
date-released: '2024-04-11'
abstract: Package contains routines to prepare data for validation exercises.
authors:
- family-names: Bodirsky
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrvalidation
Title: madrat data preparation for validation purposes
Version: 2.56.8
Date: 2024-03-19
Version: 2.56.9
Date: 2024-04-11
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down
4 changes: 2 additions & 2 deletions R/calcValidPovertyLine.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ calcValidPovertyLine <- function(datasource = "WBPoverty", subtype = "320Poverty
# Fill interpolating values when there are 2 or more points reported
for (i in getCells(rawPer)) {
if (length(where(rawPer[i, , ] != 0)$true$years) >= 2 &&
length(where(rawPer[i, , ] != 0)$true$years) < length(getYears(rawPer))) {
length(where(rawPer[i, , ] != 0)$true$years) < length(getYears(rawPer))) {
yrInter <- where(rawPer[i, , ] == 0)$true$years
rawPer[i, yrInter, ] <- time_interpolate(rawPer[i, , ][, yrInter, , invert = TRUE], yrInter)
}
Expand All @@ -53,7 +53,7 @@ calcValidPovertyLine <- function(datasource = "WBPoverty", subtype = "320Poverty
out <- magpiesort(setNames((round(rawPer * population[, years, ] * 1e6, digits = 0)) / 1e6, NULL))
nameScenario <- if (subtype == "190PovertyLine") "1p90 USDppp11/day" else if (subtype == "320PovertyLine")
"3p20 USDppp11/day" else if (subtype == "550PovertyLine")
"5p50 USDppp11/day"
"5p50 USDppp11/day"

getNames(out) <- paste0("Income|Number of People Below ", nameScenario)
out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
Expand Down
117 changes: 65 additions & 52 deletions R/calcValidProcessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,57 @@
#' @importFrom magpiesets reporthelper summationhelper reportingnames
#' @importFrom magclass add_columns mbind getNames

calcValidProcessing <- function(datasource = "FAO", detail = TRUE, nutrient = "dm", indicator = "secondary_from_primary") {
calcValidProcessing <- function(datasource = "FAO", detail = TRUE, nutrient = "dm", indicator = "primary_to_process") {

if (indicator == "primary_to_process") {
if (indicator == "primary_to_process") {

if (datasource == "FAO") {
mb <- collapseNames(calcOutput("FAOmassbalance", aggregate = FALSE)[, , nutrient])
if (datasource == "FAO") {
mb <- collapseNames(calcOutput("FAOmassbalance", aggregate = FALSE)[, , nutrient])

processing <- setdiff(c(findset("processing20")), c("milling", "ginning", "breeding"))
processing <- setdiff(c(findset("processing20")), c("milling", "ginning", "breeding"))
mb2 <- mb[, , c(processing)]
getNames(mb2, dim = 2) <- reportingnames(getNames(mb2, dim = 2))

mb2 <- mb[, , c(processing)]
getNames(mb2, dim = 2) <- reportingnames(getNames(mb2, dim = 2))
mb3 <- dimOrder(mb2, c(2, 1))

mb3 <- dimOrder(mb2, c(2, 1))
out <- reporthelper(x = mb3, dim = 3.2, level_zero_name = "_", detail = detail)
getNames(out) <- gsub("[^[:alnum:][:blank:]\\|]", "", getNames(out))
getNames(out) <- sub("\\|$", "", getNames(out))
getNames(out) <- paste0("Demand|Processing|", getNames(out))

out <- reporthelper(x = mb3, dim = 3.2, level_zero_name = "_", detail = detail)
getNames(out) <- gsub("[^[:alnum:][:blank:]\\|]", "", getNames(out))
getNames(out) <- sub("\\|$", "", getNames(out))
getNames(out) <- paste0("Demand|Processing|", getNames(out))
out <- summationhelper(out, sep = "+")

out <- summationhelper(out, sep = "+")
getNames(out) <- sub(getNames(out), pattern = "Processing|+", replacement = "Processing|++", fixed = TRUE)

getNames(out) <- sub(getNames(out), pattern = "Processing|+", replacement = "Processing|++", fixed = TRUE)
out <- out[, , which(dimSums(out, dim = c(1, 2)) > 0)]

out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource)

out <- out[, , which(dimSums(out, dim = c(1, 2)) > 0)]
} else {

out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource)
stop("No data exist for the given datasource!")

} else stop("No data exist for the given datasource!")
}

names(dimnames(out))[3] <- "scenario.model.variable"
names(dimnames(out))[3] <- "scenario.model.variable"

if (nutrient == "dm") {
unit <- "Mt DM/yr"
} else if (nutrient == "nr") {
unit <- "Mt Nr/yr"
} else if (nutrient == "p") {
unit <- "Mt P/yr"
} else if (nutrient == "k") {
unit <- "Mt K/yr"
} else if (nutrient == "ge") {
unit <- "PJ/yr"
} else if (nutrient == "wm") {
unit <- "Mt WM/yr"
}
if (nutrient == "dm") {
unit <- "Mt DM/yr"
} else if (nutrient == "nr") {
unit <- "Mt Nr/yr"
} else if (nutrient == "p") {
unit <- "Mt P/yr"
} else if (nutrient == "k") {
unit <- "Mt K/yr"
} else if (nutrient == "ge") {
unit <- "PJ/yr"
} else if (nutrient == "wm") {
unit <- "Mt WM/yr"
}

getNames(out) <- paste0(getNames(out), " (", unit, ")")
} else if (indicator == "secondary_from_primary") {
getNames(out) <- paste0(getNames(out), " (", unit, ")")
} else if (indicator == "secondary_from_primary") {

if (datasource == "FAO") {
mb <- collapseNames(calcOutput("FAOmassbalance", aggregate = FALSE)[, , nutrient])
Expand All @@ -85,38 +88,48 @@ unit <- "Mt WM/yr"
getNames(out, dim = 1) <- substr(getNames(out, dim = 1), start = 1, stop = nchar(getNames(out, dim = 1)) - 1)
getNames(out, dim = 1)[which(getNames(out, dim = 1) == "oil")] <- "oils"
getNames(out, dim = 1) <- reportingnames(getNames(out, dim = 1))
getNames(out) <- sub(getNames(out), pattern = "\\.", replacement = "|+|")
getSets(out) <- c("region", "year", "data")
getNames(out) <- paste0("Production|Secondary Products|", getNames(out))

out <- out[, , which(dimSums(out, dim = c(1, 2)) > 0)]
getNames(out, dim = 1) <- paste0("Processing|Raw material|Processed into ", getNames(out, dim = 1))
out <- add_columns(out, addnm = "Processing|Raw material|Processed into Secondary products", dim = 3.1, fill = 0)
out[, , "Processing|Raw material|Processed into Secondary products"] <- dimSums(out, dim = 3.1)
out <- add_columns(out, addnm = "dummy", dim = 3.2, fill = 0)
out[, , "dummy"] <- dimSums(out, dim = 3.2)

getNames(out) <- sub(getNames(out), pattern = "\\.", replacement = "|")
out <- summationhelper(out)
getNames(out) <- sub(getNames(out), pattern = "\\|\\+\\|dummy", replacement = "")
getNames(out) <- paste(getNames(out), "(Mt DM/yr)", sep = " ")

out <- out[, , where(out != 0)$true$data]

out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource)

} else stop("No data exist for the given datasource!")
} else {
stop("No data exist for the given datasource!")
}

names(dimnames(out))[3] <- "scenario.model.variable"

if (nutrient == "dm") {
unit <- "Mt DM/yr"
unit <- "Mt DM/yr"
} else if (nutrient == "nr") {
unit <- "Mt Nr/yr"
unit <- "Mt Nr/yr"
} else if (nutrient == "p") {
unit <- "Mt P/yr"
unit <- "Mt P/yr"
} else if (nutrient == "k") {
unit <- "Mt K/yr"
unit <- "Mt K/yr"
} else if (nutrient == "ge") {
unit <- "PJ/yr"
unit <- "PJ/yr"
} else if (nutrient == "wm") {
unit <- "Mt WM/yr"
}
unit <- "Mt WM/yr"
}

getNames(out) <- paste0(getNames(out), " (", unit, ")")
}
return(list(x = out,
weight = NULL,
unit = unit,
description = "Agricultural Demand")
)
}
return(list(x = out,
weight = NULL,
unit = unit,
description = "Agricultural Demand")
)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat data preparation for validation purposes

R package **mrvalidation**, version **2.56.8**
R package **mrvalidation**, version **2.56.9**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky <bodirsky@

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

Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.56.8, <https://github.com/pik-piam/mrvalidation>.
Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi: 10.5281/zenodo.4317826 (URL: https://doi.org/10.5281/zenodo.4317826), R package version 2.56.9, <URL: https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrvalidation: madrat data preparation for validation purposes},
author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves},
year = {2024},
note = {R package version 2.56.8},
note = {R package version 2.56.9},
doi = {10.5281/zenodo.4317826},
url = {https://github.com/pik-piam/mrvalidation},
}
Expand Down
2 changes: 1 addition & 1 deletion man/calcValidProcessing.Rd

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

0 comments on commit f2c93a9

Please sign in to comment.