Skip to content

Commit

Permalink
update transport costs validation
Browse files Browse the repository at this point in the history
  • Loading branch information
caviddhen committed Aug 2, 2023
1 parent 3ba9fb6 commit 9273549
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '49110660'
ValidationKey: '49140270'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
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.51.0
date-released: '2023-07-28'
version: 2.51.1
date-released: '2023-08-01'
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.51.0
Date: 2023-07-28
Version: 2.51.1
Date: 2023-08-01
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down
97 changes: 78 additions & 19 deletions R/calcValidCostsTransport.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title calcValidCostsTransport
#' @description calculates the validation data for transport costs
#'
#' @param datasource Datasource of validation data.
#' @param datasource Datasource of validation data
#' @return List of magpie objects with results on country level, weight on country level, unit and description.
#' @author David Chen
#' @examples
Expand All @@ -11,31 +11,89 @@
#'
#' @import mrmagpie

#' @importFrom magpiesets reporthelper summationhelper
calcValidCostsTransport <- function(datasource = "GTAP") {
#' @importFrom magpiesets reporthelper summationhelper reportingnames
calcValidCostsTransport <- function(datasource = "GTAPtransport") {

if (datasource == "GTAP") {

costs <- readSource("TransportCostsGTAP", convert = FALSE)
getYears(costs) <- 2005
missingProducts <- setdiff(findset("kall"), getNames(costs))
if (datasource %in% c("GTAPtransport", "GTAPwholesale")) {
if (datasource == "GTAPtransport") {
costType <- "transport"
} else {
costType <- "wholesale"
}

out <- add_columns(costs, addnm = missingProducts, dim = 3.1)
out[, , missingProducts] <- 0
transportGtap <- calcOutput("GTAPTotalTransportCosts", costType = costType,
aggregate = FALSE)

# re-do some of calcTransportCosts, but add processed products, and only take totals

# map gtap cfts to MAgPIE cfts
cftRel <- list()
cftRel[["pdr"]] <- c("rice_pro")
cftRel[["wht"]] <- c("tece")
cftRel[["gro"]] <- c("maiz", "trce", "begr", "betr")
cftRel[["v_f"]] <- c("others", "potato", "cassav_sp", "puls_pro")
cftRel[["osd"]] <- c("soybean", "oilpalm", "rapeseed", "sunflower", "groundnut")
cftRel[["c_b"]] <- c("sugr_beet", "sugr_cane")
cftRel[["ocr"]] <- c("foddr")
cftRel[["rmk"]] <- "livst_milk"
cftRel[["ctl"]] <- c("livst_rum")
cftRel[["oap"]] <- c("livst_chick", "livst_egg", "livst_pig")
cftRel[["pfb"]] <- c("fibres")
cftRel[["sgr"]] <- c("sugar")
cftRel[["vol"]] <- c("oils")
cftRel[["b_t"]] <- c("alcohol")

# add processed rice, cattle meat, dairy products, and other meats to their raw eq's, and remove
transportGtap[, , "pdr"] <- transportGtap[, , "pdr"] + setNames(transportGtap[, , "pcr"], NULL)
transportGtap[, , "ctl"] <- transportGtap[, , "ctl"] + setNames(transportGtap[, , "cmt"], NULL)
transportGtap[, , "rmk"] <- transportGtap[, , "rmk"] + setNames(transportGtap[, , "mil"], NULL)
transportGtap[, , "oap"] <- transportGtap[, , "oap"] + setNames(transportGtap[, , "omt"], NULL)

rm <- c("pcr", "mil", "cmt", "omt")

transportGtap <- transportGtap[, , rm, invert = TRUE]

# use production ratios to split along product categories

prod <- collapseNames(
calcOutput("FAOmassbalance", aggregate = FALSE)[, , "production"][, ,
"dm"])
prod <- time_interpolate(prod, interpolated_year = c(2000:2010),
integrate_interpolated_years = TRUE)

out <- new.magpie(cells_and_regions = getItems(transportGtap, dim = 1),
years = getYears(transportGtap),
unlist(cftRel))


## split the combined products by production ratio
for (i in seq_along(cftRel)) {
out[, , cftRel[[i]]] <- collapseNames(transportGtap[, , names(cftRel)[[i]]]) *
(prod[, getYears(out), cftRel[[i]]] /
ifelse(dimSums(prod[, getYears(out), cftRel[[i]]], dim = 3) > 0,
dimSums(prod[, getYears(out), cftRel[[i]]], dim = 3),
length(cftRel[[i]]) / 1))

}

if (costType == "transport") {
lzname <- "Costs|Transport"
description <- "Tranposrt Costs"
} else {

lzname <- "Costs|Wholesale"
description <- "Wholesale Costs"
}

out <- reporthelper(out, dim = 3.1, level_zero_name = "Costs|Transport", detail = TRUE)
out <- summationhelper(out)
out <- reporthelper(out, level_zero_name = lzname, partly = TRUE)
out <- summationhelper(out, excludeLevels = 1)

# take away + from total
getNames(out[, , "Costs|+|Transport"]) <- "Costs|Transport"
out <- add_dimension(out, dim = 3.1, add = "scenario", nm = "historical")
out <- add_dimension(out, dim = 3.2, add = "model", nm = datasource)

getNames(out) <- paste(getNames(out), "(million US$05/yr)", sep = " ")
unit <- "million US$05/yr"
unit <- "million US$05/yr"

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

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

distance <- calcOutput("TransportTime", aggregate = FALSE)
Expand Down Expand Up @@ -77,6 +135,7 @@ calcValidCostsTransport <- function(datasource = "GTAP") {

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

} else {
stop("Only own calculation and GTAP transport costs avilable currently!")
Expand All @@ -85,6 +144,6 @@ calcValidCostsTransport <- function(datasource = "GTAP") {
return(list(x = out,
weight = NULL,
unit = unit,
description = "Transport Costs")
description = description)
)
}
4 changes: 3 additions & 1 deletion R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ if (rev < 4.66) stop("mrvalidation(>= 2.34.0) does not support revision below 4.
# Costs validation
calcOutput(type = "ValidCostsOverall", datasource = "FAO", aggregate = "REG+GLO",
file = valfile, append = TRUE, try = TRUE) # Overall costs.
calcOutput(type = "ValidCostsTransport", datasource = "GTAP", aggregate = FALSE,
calcOutput(type = "ValidCostsTransport", datasource = "GTAPtransport", aggregate = FALSE,
file = valfile, append = TRUE, try = TRUE)
calcOutput(type = "ValidCostsTransport", datasource = "MAgPIEcalc", aggregate = "REG+GLO",
file = valfile, append = TRUE, try = TRUE)
Expand All @@ -318,6 +318,8 @@ if (rev < 4.66) stop("mrvalidation(>= 2.34.0) does not support revision below 4.
file = valfile, append = TRUE, try = TRUE)
calcOutput(type = "ValidTotalLaborCosts", datasource = "GTAP", aggregate = "REG+GLO",
file = valfile, append = TRUE, try = TRUE)
calcOutput(type = "ValidCostsTransport", datasource = "GTAPwholesale", aggregate = FALSE,
file = valfile, append = TRUE, try = TRUE)

# Diversity indices
calcOutput(type = "ValidBII", aggregate = "REG+GLO",
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.51.0**
R package **mrvalidation**, version **2.51.1**

[![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, Humpenoeder F, von Jeetze P, Wang X, Dietrich J, Alves M (2023). _mrvalidation: madrat data preparation for validation purposes_. doi: 10.5281/zenodo.4317826 (URL: https://doi.org/10.5281/zenodo.4317826), R package version 2.51.0, <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, Humpenoeder F, von Jeetze P, Wang X, Dietrich J, Alves M (2023). _mrvalidation: madrat data preparation for validation purposes_. doi: 10.5281/zenodo.4317826 (URL: https://doi.org/10.5281/zenodo.4317826), R package version 2.51.1, <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 Florian {Humpenoeder } and Patrick {von Jeetze } and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves},
year = {2023},
note = {R package version 2.51.0},
note = {R package version 2.51.1},
doi = {10.5281/zenodo.4317826},
url = {https://github.com/pik-piam/mrvalidation},
}
Expand Down
4 changes: 2 additions & 2 deletions man/calcValidCostsTransport.Rd

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

0 comments on commit 9273549

Please sign in to comment.