Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pik-piam/mrmagpie into ma…
Browse files Browse the repository at this point in the history
…ster_mppalves

merge
  • Loading branch information
mppalves committed Dec 17, 2023
2 parents a59de7f + 38e0d8b commit ae5e42f
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '27381835'
ValidationKey: '27487080'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
9 changes: 5 additions & 4 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: 'mrmagpie: madrat based MAgPIE Input Data Library'
version: 1.39.1
date-released: '2023-11-24'
version: 1.39.5
date-released: '2023-12-13'
abstract: Provides functions for MAgPIE country and cellular input data generation.
authors:
- family-names: Karstens
Expand All @@ -18,11 +18,12 @@ authors:
given-names: Michael
- family-names: Alves
given-names: Marcos
- family-names: Köberle
given-names: Alexandre
- family-names: Beier
given-names: Felicitas
email: [email protected]
- family-names: Köberle
given-names: Alexandre
email: [email protected]
- family-names: v. Jeetze
given-names: Patrick
email: [email protected]
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: mrmagpie
Title: madrat based MAgPIE Input Data Library
Version: 1.39.1
Date: 2023-11-24
Version: 1.39.5
Date: 2023-12-13
Authors@R: c(
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
13 changes: 7 additions & 6 deletions R/calcFoodDemandGridded.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param attribute dm or calories ("ge") or other massbalance attribute
#' @param feed whether to include feed demand in the gridded demand
#' @param prod for memory reasons
#' @param cells magpiecell or lpjcell (default)
#' @author David M Chen
#' @importFrom withr local_options
#' @importFrom magpiesets findset
Expand All @@ -12,7 +13,7 @@
#' calcOutput("FoodDemandGridded")
#' }
#'
calcFoodDemandGridded <- function(attribute = "dm", prod = "k", feed = TRUE) {
calcFoodDemandGridded <- function(attribute = "dm", prod = "k", feed = TRUE, cells = "lpjcell") {

# Country-level food demand
foodDemand <- calcOutput("FAOmassbalance", aggregate = FALSE)
Expand All @@ -26,7 +27,7 @@ calcFoodDemandGridded <- function(attribute = "dm", prod = "k", feed = TRUE) {

# Gridded population
gridPop <- collapseNames(calcOutput("GridPop", aggregate = FALSE,
cellular = TRUE, cells = "lpjcell",
cellular = TRUE, cells = cells,
source = "Gao", urban = TRUE)[, hist, "SSP2"])
# Country-level population
popAgg <- dimSums(gridPop, dim = c("x", "y"))
Expand All @@ -49,15 +50,15 @@ calcFoodDemandGridded <- function(attribute = "dm", prod = "k", feed = TRUE) {
local_options(magclass_sizeLimit = 1e+12)
prods <- findset(prod)
foodDisaggUrb <- foodDisaggUrb[, , prods]

# Sum up demand dimension, this uses a lot of memory so split again
pr1 <- prods[c(1:(length(prods)/2))]
pr2 <- prods[c((length(prods)/2 + 1):(length(prods) + 0.5))] #0.5 to ensure if odd numbers
pr1 <- prods[c(1:(length(prods) / 2))]
pr2 <- prods[c((length(prods) / 2 + 1):(length(prods) + 0.5))] #0.5 to ensure if odd numbers

foodDisaggUrb1 <- dimSums(foodDisaggUrb[, , pr1], dim = 3.2)
foodDisaggUrb2 <- dimSums(foodDisaggUrb[, , pr2], dim = 3.2)
foodDisaggUrb <- mbind(foodDisaggUrb1, foodDisaggUrb2)

return(list(x = foodDisaggUrb,
weight = NULL,
unit = "Mt",
Expand Down
12 changes: 7 additions & 5 deletions R/calcNonLocalProduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
#' assuming that food produced in the grid cell is first consumed by local population
#' i.e. amount of food greater than local rural demand, split into that which feeds the local
#' urban population, and that which exceeds total local demand and is available to export
#' @param cells magpiecell or lpjcell (default)
#' @author David M Chen
#' @examples
#' \dontrun{
#' calcOutput("NonLocalTransport")
#' }
calcNonLocalProduction <- function() {
calcNonLocalProduction <- function(cells = "lpjcell") {

productionPri <- calcOutput("Production", products = "kcr",
cellular = TRUE, cells = "lpjcell",
cellular = TRUE, cells = cells,
aggregate = FALSE)
productionLi <- calcOutput("Production", products = "kli",
cellular = TRUE, cells = "lpjcell",
cellular = TRUE, cells = cells,
aggregate = FALSE)
productionPast <- calcOutput("Production", products = "pasture", cellular = TRUE,
cells = "lpjcell", aggregate = FALSE)
cells = cells, aggregate = FALSE)
productionPast <- add_dimension(productionPast, dim = 3.1, add = "Item", nm = "pasture")
production <- collapseNames(mbind(productionPri, productionLi)[, , "dm"])
production <- mbind(production, collapseNames(productionPast[, , "dm"], collapsedim = 2))

foodDisaggUrb <- calcOutput("FoodDemandGridded", feed = TRUE, aggregate = FALSE)
foodDisaggUrb <- calcOutput("FoodDemandGridded", feed = TRUE,
cells = cells, aggregate = FALSE)
# note this also includes feed demand!

foodDemPrim <- collapseNames(foodDisaggUrb[, , getItems(production, dim = 3)])
Expand Down
65 changes: 38 additions & 27 deletions R/calcTransportCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,55 @@
#' transport costs, cellular production, and cellular travel time
#' @param transport "all" or "nonlocal". "all" means all production incurs transport costs,
#' while "nonlocal" sees only production greater than local rural consumption with transport costs
#' @param gtapVersion "9" or "81"
#' @return List of magpie objects with results on country level, weight on country level, unit and description.
#' @author David M Chen
#' @seealso
#' [calcTransportTime()],
#' [calcGTAPTotalTransportCosts()]
#' @examples
#' \dontrun{
#' calcOutput("TransportCosts")
#' calcOutput("TransportCosts_new")
#' }
#'
calcTransportCosts <- function(transport = "all") { # nolint
calcTransportCosts <- function(transport = "all", gtapVersion = "9") { # nolint

# load distance (travel time), production, and gtap transport costs
distance <- calcOutput("TransportTime", subtype = "cities50", cells = "lpjcell", aggregate = FALSE)
distance <- calcOutput("TransportTime", subtype = "cities50", aggregate = FALSE)

if (gtapVersion == "9") {
yr <- 2011
} else {
yr <- 2004
}

if (transport == "all") {

production <- calcOutput("Production", cellular = TRUE, cells = "lpjcell",
irrigation = FALSE, aggregate = FALSE)[, 2005, "dm"] * 10^6
productionLi <- calcOutput("Production", cellular = TRUE, cells = "lpjcell",
production <- calcOutput("Production", cellular = TRUE,
irrigation = FALSE, aggregate = FALSE)[, , "dm"] * 10^6
productionLi <- calcOutput("Production", cellular = TRUE,
irrigation = FALSE, aggregate = FALSE,
products = "kli")[, 2005, "dm"] * 10^6
products = "kli")[, , "dm"] * 10^6
production <- mbind(production, productionLi)

production <- time_interpolate(production, interpolated_year = c(2004:2011),
integrate_interpolated_years = TRUE)
production[production < 0] <- 0
production <- production[, yr, ]
} else if (transport == "nonlocal") {
production <- calcOutput("NonLocalProduction", aggregate = FALSE)[, 2005, ] * 10^6
production <- calcOutput("NonLocalProduction",
aggregate = FALSE) * 10^6
production <- time_interpolate(production, interpolated_year = c(2004:2011),
integrate_interpolated_years = TRUE)
production[production < 0] <- 0
production <- production[, yr, ]

} else {
stop("only all or nonlocal production available for subtype")
}

transportGtap <- calcOutput("GTAPTotalTransportCosts", aggregate = FALSE)[, 2004, ] * 10^6
transportGtap <- calcOutput("GTAPTotalTransportCosts", version = gtapVersion,
aggregate = FALSE)[, yr, ] * 10^6
# transform 10^6 USD -> USD
# this is in 2004 and 2007 current USD, and we don't have same year for travel time, and pretend GTAP is 2005
# some processed products available in GTAP are neglected for the moment neglected here, as we don't have cellular
# production data for them. These are "pfb" (fibres) "sgr" ("sugar") "vol" ("oils") "b_t" ("alcohol") "fst" "wood"

Expand Down Expand Up @@ -65,17 +81,14 @@ calcTransportCosts <- function(transport = "all") { # nolint
# calculate transport power (amount * distance) &
# create average transport costs per ton per distance dummy
# calculate transport power (amount * distance)
tmpPower <- new.magpie(cells_and_regions = getItems(distance, dim = 1),
years = "y2005",
names = names(cftRel),
fill = 0,
sets = c("x", "y", "iso", "year", "data"))

transportPower <- new.magpie(cells_and_regions = getItems(distance, dim = "iso"),
years = "y2005",
names = names(cftRel),
fill = 0,
sets = c("iso", "year", "data"))

tmpPower <- new.magpie(0, cells_and_regions = getItems(distance, dim = 1),
years = yr,
names = names(cftRel))

transportPower <- new.magpie(0, cells_and_regions = unique(getItems(tmpPower, dim = 1.3)),
years = yr,
names = names(cftRel))

# create average transport costs per ton per distance dummy
transportPerTonPerDistance <- NULL
Expand All @@ -85,8 +98,7 @@ calcTransportCosts <- function(transport = "all") { # nolint
# should also include rural consumers, and farms
tmpPower[, , names(cftRel)[i]] <- dimSums(production[, , cftRel[[i]]], dim = 3) * distance

transportPower[, , names(cftRel)[i]] <- dimSums(x = tmpPower[, , names(cftRel)[i]],
dim = c("x", "y"))
transportPower[, , names(cftRel)[i]] <- dimSums(tmpPower[, , names(cftRel)[i]], dim = c("region", "region1"))

tpFilled <- toolCountryFill(transportPower, fill = 0) # need to fill first to divide
transportPerTonPerDistance <- mbind(transportPerTonPerDistance,
Expand All @@ -99,10 +111,10 @@ calcTransportCosts <- function(transport = "all") { # nolint
# Rename GTAP to MAgPIE commodities
magpieComms <- unlist(cftRel)

transportMagpie <- transportPowerMagpie <- new.magpie(fill = 0,
transportMagpie <- transportPowerMagpie <- new.magpie(0,
cells_and_regions = getItems(transportPerTonPerDistance,
dim = 1),
years = "y2005",
years = yr,
names = magpieComms)

for (i in getNames(transportMagpie)) {
Expand All @@ -120,7 +132,6 @@ calcTransportCosts <- function(transport = "all") { # nolint
dim = 3.1, fill = 0)
transportMagpie[, , c("wood", "woodfuel")] <- transportMagpie[, , "foddr"]

# what weight to add for these?
transportPowerMagpie <- add_columns(transportPowerMagpie, addnm = c("pasture", "wood", "woodfuel"),
dim = 3.1, fill = 0)
transportPowerMagpie[, , c("wood", "woodfuel")] <- transportPowerMagpie[, , "foddr"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library

R package **mrmagpie**, version **1.39.1**
R package **mrmagpie**, version **1.39.5**

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

Expand Down Expand Up @@ -39,16 +39,16 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

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

Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Koeberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.39.1, <URL: https://github.com/pik-piam/mrmagpie>.
Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.39.5, <URL: https://github.com/pik-piam/mrmagpie>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
title = {mrmagpie: madrat based MAgPIE Input Data Library},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Alexandre Koeberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Alexandre Köberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer},
year = {2023},
note = {R package version 1.39.1},
note = {R package version 1.39.5},
doi = {10.5281/zenodo.4319612},
url = {https://github.com/pik-piam/mrmagpie},
}
Expand Down
9 changes: 8 additions & 1 deletion man/calcFoodDemandGridded.Rd

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

5 changes: 4 additions & 1 deletion man/calcNonLocalProduction.Rd

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

6 changes: 4 additions & 2 deletions man/calcTransportCosts.Rd

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

1 change: 1 addition & 0 deletions man/mrmagpie-package.Rd

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

0 comments on commit ae5e42f

Please sign in to comment.