Skip to content

Commit

Permalink
add correct weights
Browse files Browse the repository at this point in the history
  • Loading branch information
k4rst3ns committed Jul 26, 2024
1 parent 7b63345 commit 69abd4a
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '11996656'
ValidationKey: '12157300'
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: 'mrland: MadRaT land data package'
version: 0.60.2
date-released: '2024-07-24'
version: 0.61.0
date-released: '2024-07-26'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
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: mrland
Title: MadRaT land data package
Version: 0.60.2
Date: 2024-07-24
Version: 0.61.0
Date: 2024-07-26
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down
38 changes: 26 additions & 12 deletions R/calcLossShare.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
# maybe rename to loss?
#' @title calcLossShare
#' @description Calculates share of domestic supply wasted
#'
#' @return List of magpie object with results and weight on country or cellular level, unit and description.
#' @author Benjamin Leon Bodirsky
#' @examples
#' \dontrun{
#' calcOutput("LossShare")
#' }
#' @importFrom magpiesets findset

calcLossShare <- function() {

massbalance <- calcOutput("FAOmassbalance", aggregate = FALSE)

# connected to domestic supply, as FAOSTATS flossary states that
# "Waste is often estimated as a fixed percentage of availability, the latter
# being defined as production plus imports plus stock withdrawals."
wasteshr <- dimSums(massbalance[, , "waste"][, , "dm"],
dim = c(3.2, 3.3)) / dimSums(massbalance[, , "domestic_supply"][, , "dm"],
dim = c(3.2, 3.3))
weight <- dimSums(massbalance[, , "domestic_supply"][, , "dm"], dim = c(3.2, 3.3))
wasteshr <-
dimSums(massbalance[, , "waste"][, , "dm"], dim = c(3.2, 3.3)) /
dimSums(massbalance[, , "domestic_supply"][, , "dm"], dim = c(3.2, 3.3))
weight <-
dimSums(massbalance[, , "domestic_supply"][, , "dm"], dim = c(3.2, 3.3))
newproducts <- c("begr", "betr", "scp")
#assume a waste share of 1 percent for begr, betr and scp

# assume a waste share of 1 percent for begr, betr and scp
wasteshr[, , newproducts] <- 0.01
weight[, , newproducts] <- 1
wasteshr[, , "pasture"] <- 0
weight[, , newproducts] <- 1
wasteshr[, , "pasture"] <- 0
wasteshr[is.na(wasteshr)] <- 0
weight[wasteshr == Inf] <- 0
weight[wasteshr == Inf] <- 0
wasteshr[wasteshr == Inf] <- 0

wasteshr <- toolHoldConstantBeyondEnd(wasteshr)
weight <- toolHoldConstantBeyondEnd(weight)

weight <- toolHoldConstantBeyondEnd(weight) + 10^-10

return(list(x = wasteshr, weight = weight, unit = "DM share, weight: domestic supply",
return(list(x = wasteshr,
weight = weight,
unit = "DM share, weight: domestic supply",
description = "Share of domestic supply wasted"))
}
37 changes: 18 additions & 19 deletions R/calcNitrogenFixationNdfa.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
#' @seealso
#' \code{\link{calcNitrogenFixationPast}}
#' @examples
#'
#' \dontrun{
#' \dontrun{
#' calcOutput("calcNitrogenFixationNdfa")
#' }
#'
calcNitrogenFixationNdfa<-function(){
past <- findset("past")

ndfa<-setYears(readSource("Herridge",subtype = "ndfa"),NULL)
harvest<-collapseNames(calcOutput("FAOmassbalance",aggregate = FALSE)[,,"nr"][,,"production"])
harvest<-add_dimension(harvest,dim = 3.1,add = "data1",nm = "organ")
res<-collapseNames(calcOutput("ResBiomass",aggregate = FALSE)[,past,"nr"])
biomass<-mbind(harvest[,,getNames(res,dim=2)],res)
weight<-dimSums(biomass,dim=3.1)
out<-weight<-toolHoldConstantBeyondEnd(weight)
out[,,]<-ndfa
weight[,,c("begr","betr")]<-10^-10
#'
calcNitrogenFixationNdfa <- function() {

past <- findset("past")
ndfa <- setYears(readSource("Herridge", subtype = "ndfa"), NULL)
harvest <- collapseNames(calcOutput("FAOmassbalance", aggregate = FALSE)[, , "nr"][, , "production"])
harvest <- add_dimension(harvest, dim = 3.1, add = "data1", nm = "organ")
res <- collapseNames(calcOutput("ResBiomass", aggregate = FALSE)[, past, "nr"])
biomass <- mbind(harvest[, , getNames(res, dim = 2)], res)
weight <- dimSums(biomass, dim = 3.1)
out <- weight <- toolHoldConstantBeyondEnd(weight)
out[, , ] <- ndfa
weight <- weight + 10^-10

return(list(x=out,
weight=weight,
unit="share of N",
description="Share of N in biomass derived from biological fixation"))
return(list(x = out,
weight = weight,
unit = "Share of N",
description = "Share of N in biomass derived from biological fixation"))
}
48 changes: 32 additions & 16 deletions R/calcSeedShare.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
#' @title calcSeedShare
#' @description Calculates Seed share (seed demand per production)
#'
#' @return List of magpie object with results and weight on country or cellular level, unit and description.
#' @author Benjamin Leon Bodirsky
#' @examples
#' \dontrun{
#' calcOutput("SeedShare")
#' }
#' @importFrom magpiesets findset

calcSeedShare<-function(){
massbalance<-calcOutput("FAOmassbalance",aggregate=F)
kcr<-findset("kcr")
seedshr<-dimSums(massbalance[,,"seed"][,,"dm"][,,kcr],dim=c(3.2,3.3))/dimSums(massbalance[,,"production"][,,"dm"][,,kcr],dim=c(3.2,3.3))
weight<-dimSums(massbalance[,,"production"][,,"dm"][,,kcr],dim=c(3.2,3.3))

#assume a seed share of 1 percent for begr and betr
seedshr[,,c("begr","betr")]<-0.01
weight[,,c("begr","betr")]<-1
seedshr[is.nan(seedshr)]<-0
weight[seedshr==Inf]<-0
seedshr[seedshr==Inf]<-0
calcSeedShare <- function() {

seedshr <- toolHoldConstantBeyondEnd(seedshr)
weight <- toolHoldConstantBeyondEnd(weight)

return(list(x=seedshr,weight=weight,unit="DM share, weight: production quantity",description="Share of production used as seed"))
massbalance <- calcOutput("FAOmassbalance", aggregate = FALSE)
kcr <- findset("kcr")
seedshr <-
dimSums(massbalance[, , "seed"][, , "dm"][, , kcr], dim = c(3.2, 3.3)) /
dimSums(massbalance[, , "production"][, , "dm"][, , kcr], dim = c(3.2, 3.3))
weight <- dimSums(massbalance[, , "production"][, , "dm"][, , kcr], dim = c(3.2, 3.3))

# assume a seed share of 1 percent for begr and betr
seedshr[, , c("begr", "betr")] <- 0.01
weight[, , c("begr", "betr")] <- 1
seedshr[is.nan(seedshr)] <- 0
weight[seedshr == Inf] <- 0
seedshr[seedshr == Inf] <- 0

seedshr <- mstools::toolHoldConstantBeyondEnd(seedshr)
weight <- mstools::toolHoldConstantBeyondEnd(weight) + 10^-10

return(list(x = seedshr,
weight = weight,
unit = "DM share, weight: production quantity",
description = "Share of production used as seed"))
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRaT land data package

R package **mrland**, version **0.60.2**
R package **mrland**, version **0.61.0**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

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

Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D, Sauer P (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.60.2, <https://github.com/pik-piam/mrland>.
Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D, Sauer P (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.61.0, <https://github.com/pik-piam/mrland>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,8 +48,8 @@ A BibTeX entry for LaTeX users is
title = {mrland: MadRaT land data package},
author = {Jan Philipp Dietrich and Abhijeet Mishra and Isabelle Weindl and Benjamin Leon Bodirsky and Xiaoxi Wang and Lavinia Baumstark and Ulrich Kreidenweis and David Klein and Nele Steinmetz and David Chen and Florian Humpenoeder and Patrick {von Jeetze} and Stephen Wirth and Felicitas Beier and David Hoetten and Pascal Sauer},
year = {2024},
note = {R package version 0.60.2},
url = {https://github.com/pik-piam/mrland},
note = {R package version 0.61.0},
doi = {10.5281/zenodo.3822083},
url = {https://github.com/pik-piam/mrland},
}
```
22 changes: 22 additions & 0 deletions man/calcLossShare.Rd

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

3 changes: 1 addition & 2 deletions man/calcNitrogenFixationNdfa.Rd

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

22 changes: 22 additions & 0 deletions man/calcSeedShare.Rd

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

0 comments on commit 69abd4a

Please sign in to comment.