Skip to content

Commit

Permalink
yield validation for fallow implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bodirsky committed Jan 5, 2024
1 parent c6e2ee4 commit a9ab9ee
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '49929037'
ValidationKey: '50106580'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.53.1
version: 2.54.0
date-released: '2024-01-05'
abstract: Package contains routines to prepare data for validation exercises.
authors:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrvalidation
Title: madrat data preparation for validation purposes
Version: 2.53.1
Version: 2.54.0
Date: 2024-01-05
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
Expand Down
78 changes: 75 additions & 3 deletions R/calcValidYield.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,93 @@ calcValidYield <- function(datasource = "FAO", future = NULL) {
scenario <- "historical"
description <- "CalcValidYield calculates the historical yield from FAO database."

} else if (datasource == "Ostberg2023_FAO_LUH2v2") {

if (!is.null(future)) stop("Future options is not available for source type 'FAO'.")

past <- findset("past")

# Calculate areas of individual crops and pasture
croparea <- calcOutput("CropareaLandInG", aggregate = FALSE)
pastarea <- setNames(calcOutput("LanduseInitialisation", aggregate = FALSE)[, , "past"],
"pasture")
area <- mbind(croparea[, getYears(pastarea), ], pastarea)
area <- summationhelper(reporthelper(area,
level_zero_name = "Productivity|Yield by phyiscal area"))

# Calculate production
histproduction <- calcOutput("FAOmassbalance", aggregate = FALSE)
# extract DryMatter(dm) from production data
# Extract Production from subsetted production data containing only DryMatter(dm)
histproduction <- collapseNames(histproduction[, , "dm"][, , "production"])

# Figure out which commodities are common and make sure that production and area has same commodity names
kcr <- findset("kcr")
cropproduction <- histproduction[, , kcr]
pastproduction <- histproduction[, , "pasture"]
production <- mbind(cropproduction, pastproduction)

# Calculate Yields
production <- summationhelper(reporthelper(production,
level_zero_name = "Productivity|Yield by phyiscal area"))
yield <- production / area

# Check for NaN values
indexNaN <- which(is.nan(yield))
# Change NaN to 0
yield[is.nan(yield)] <- 0
if (length(indexNaN) > 0) {
vcat(verbosity = 2, "NaN values were found in the calculated yields ---> NaN values set to 0")
} else if (length(indexNaN) == 0) {
vcat(verbosity = 2, paste0("No NaN values detected in calculated yields. ",
"Yields for area reported as 0 are converted to 0."))
}

# Check for infinite values - Production reported but on 0 area
infindex <- which(is.infinite(yield)) # Lots of inf values. Being too diplomatic FAO aren't we!!
# Change inf to 0
yield[is.infinite(yield)] <- 0
if (length(infindex) > 0) {
vcat(verbosity = 2, "inf values were found in the calculated yields ---> inf values set to 0")
} else if (length(infindex) == 0) {
vcat(verbosity = 2, "No inf values found in the calculated yields")
}

## Additional checks required because we are reporting insanely high yields
## Check which(yield>200,arr.ind = TRUE)
## looks like Vatican are the best producers of oil

## We decided to set these outliers to 0
# look for the Region names which are outliers
region <- rownames(which(yield > 200, arr.ind = TRUE))
# look for the Years where reporting is flawed
year <- which(yield > 200, arr.ind = TRUE)[, 2]
# look for the scenario.model.Variable where reporting is flawed
variable <- which(yield > 200, arr.ind = TRUE)[, 3]

## Now do the operation of our magpie object
yield[region, year, variable] <- 0
vcat(verbosity = 2, paste0("Yields>200 ton/ha were converted to 0 ",
"when area and production values were ambiguous"))

scenario <- "historical"
description <- "FAO massbalance yields divided by physical area excluding fallow from Ostberg"

} else if (datasource == "calibratedLPJmL") {

irrigation <- FALSE # can be made function argument if needed
yieldLPJmLgrid <- calcOutput("ValidGridYields", datasource = "calibratedLPJmL",
future = future, aggregate = FALSE)
future = future, aggregate = FALSE)
areaMAGgrid <- setYears(calcOutput("ValidGridCroparea", aggregate = FALSE)[, "y2010", ], NULL)

cell2iso <- data.frame(cell = getItems(yieldLPJmLgrid, 1, full = TRUE),
iso = getItems(yieldLPJmLgrid,
if (dimExists("iso", yieldLPJmLgrid)) "iso" else 1.1, full = TRUE))

yield <- toolAggregate(yieldLPJmLgrid, weight = areaMAGgrid, rel = cell2iso,
from = "celliso", to = "iso", dim = 1)
area <- toolAggregate(areaMAGgrid, weight = NULL, rel = cell2iso,
from = "celliso", to = "iso", dim = 1)
from = "celliso", to = "iso", dim = 1)


rm(yieldLPJmLgrid, areaMAGgrid)
Expand Down
2 changes: 2 additions & 0 deletions R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ fullVALIDATION <- function(rev = 0.1) {

# Yield
calcOutput(type = "ValidYield", datasource = "FAO", aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE)
calcOutput(type = "ValidYield", datasource = "Ostberg2023_FAO_LUH2v2",
aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE)

# Productivity
calcOutput(type = "ValidTau", aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE)
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 data preparation for validation purposes

R package **mrvalidation**, version **2.53.1**
R package **mrvalidation**, version **2.54.0**

[![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 (URL: https://doi.org/10.5281/zenodo.4317826), R package version 2.53.1, <URL: 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.54.0, <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.53.1},
note = {R package version 2.54.0},
doi = {10.5281/zenodo.4317826},
url = {https://github.com/pik-piam/mrvalidation},
}
Expand Down

0 comments on commit a9ab9ee

Please sign in to comment.