Skip to content

Commit

Permalink
fix cost warning
Browse files Browse the repository at this point in the history
  • Loading branch information
flohump committed Apr 27, 2024
1 parent 151d68f commit 5f63b3d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3987639'
ValidationKey: '4007680'
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: 'magpie4: MAgPIE outputs R package for MAgPIE version 4.x'
version: 2.0.1
date-released: '2024-04-26'
version: 2.0.2
date-released: '2024-04-27'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
authors:
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: magpie4
Title: MAgPIE outputs R package for MAgPIE version 4.x
Version: 2.0.1
Date: 2024-04-26
Version: 2.0.2
Date: 2024-04-27
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
Expand Down
73 changes: 37 additions & 36 deletions R/costs.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#' }
#'
costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE) {

if (!type %in% c("annuity", "investment")) stop("The type selected is not valid. Options: 'annuity' or 'investment'")

tmpCost <- function(gdx, name, label) {
cost <- readGDX(gdx, name, format = "first_found", select = list(type = "level"), react = "quiet")
if (is.null(cost)) return(NULL)
Expand All @@ -32,24 +32,24 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
dimnames(cost)[[3]] <- label
return(cost)
}

fAn <- 1

if (type == "investment") {

intRate <- intRate <- readGDX(gdx, "pm_interest")[, readGDX(gdx, "t"), ]
t <- getYears(intRate, as.integer = TRUE)
tStep <- t - c(1990, t[seq_len(length(t))[1:(length(t) - 1)]])
tSm <- intRate

for (y in seq_len(length(getYears(tSm)))) {
tSm[, y, ] <- tStep[y]
}

fAn <- (1 + intRate) / (intRate) / tSm

}

x <- list(
tmpCost(gdx, "ov_cost_landcon", "Land Conversion") * fAn,
tmpCost(gdx, "ov_cost_transp", "Transport"),
Expand All @@ -73,14 +73,15 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
tmpCost(gdx, "ov_cost_bv_loss", "Biodiversity"),
tmpCost(gdx, "ov_cost_urban", "Punishment urban deviation"),
tmpCost(gdx, "ov_water_cost", "Irrigation water"),
tmpCost(gdx, "ov_cost_packaging", "Wholesale Costs")
tmpCost(gdx, "ov_cost_packaging", "Wholesale Costs"),
tmpCost(gdx, "ov_cost_cropland", "Cropland costs")
)

# Input factors
if (suppressWarnings(!is.null(readGDX(gdx, "ov_cost_prod")))) {
if (suppressWarnings(is.null(readGDX(gdx, "p38_capital_mobile")))) {
inputCosts <- tmpCost(gdx, "ov_cost_prod", "Input Factors")

} else {
if (type == "annuity") {
inputCosts <- tmpCost(gdx, "ov_cost_prod", "Input Factors") +
Expand All @@ -90,7 +91,7 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
(tmpCost(gdx, "ov38_investment_immobile", "Input Factors") +
tmpCost(gdx, "ov38_investment_mobile", "Input Factors")) / tSm
}

}
} else {
if (suppressWarnings(is.null(readGDX(gdx, "p38_capital_mobile")))) {
Expand All @@ -99,15 +100,15 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
tmpCost(gdx, "ov_cost_prod_past", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_livst", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_fish", "Input Factors")

} else {
if (type == "annuity") {
inputCosts <- tmpCost(gdx, "ov_cost_prod_crop", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_kres", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_past", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_livst", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_fish", "Input Factors")

} else if (type == "investment") {
inputCosts <- tmpCost(gdx, "ov_cost_prod_kres", "Input Factors") +
tmpCost(gdx, "ov_cost_prod_past", "Input Factors") +
Expand All @@ -117,43 +118,43 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
select = list(type = "level"), react = "quiet")[, , "labor"], "Input Factors") +
(tmpCost(gdx, "ov38_investment_immobile", "Input Factors") +
tmpCost(gdx, "ov38_investment_mobile", "Input Factors")) / tSm

}


}
}

# Peatland
if (suppressWarnings(is.null(readGDX(gdx, "ov58_peatland_cost_annuity")))) {
peatland <- tmpCost(gdx, "ov_peatland_cost", "Peatland")

} else {
peatland <- tmpCost(gdx, "ov_peatland_cost", "Peatland") -
tmpCost(gdx, "ov58_peatland_cost_annuity", "Peatland") +
tmpCost(gdx, "ov58_peatland_cost_annuity", "Peatland") * fAn
}

# Forestry
if (suppressWarnings(is.null(readGDX(gdx, "ov32_cost_establishment")))) {
forestry <- tmpCost(gdx, "ov_cost_fore", "Forestry")

} else {
forestry <- tmpCost(gdx, "ov_cost_fore", "Forestry") - tmpCost(gdx, "ov32_cost_establishment", "Forestry") +
tmpCost(gdx, "ov32_cost_establishment", "Forestry") * fAn
}

# TC
if (suppressWarnings(is.null(readGDX(gdx, "ov13_cost_tc")))) {
technology <- tmpCost(gdx, "ov_tech_cost", "TC")

} else {
technology <- tmpCost(gdx, "ov_tech_cost", "TC") * fAn
}

# GHG emissions


emisCostOneoff <- readGDX(gdx, "ov56_emission_cost", select = list(type = "level"), react = "silent")
if (!is.null(emisCostOneoff)) {
emisOneoff <- readGDX(gdx, "emis_oneoff")
Expand All @@ -167,22 +168,22 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
dimSums(readGDX(gdx, "ov56_emission_costs_reg_oneoff")[, , "level"], dim = 3)
emisCostOneoff <- costsCellOneoff + costsRegOneoff
}


emissions <- tmpCost(gdx, "ov_emission_costs", "GHG Emissions") - emisCostOneoff + emisCostOneoff * fAn

x[[length(x) + 1]] <- inputCosts
x[[length(x) + 1]] <- peatland
x[[length(x) + 1]] <- forestry
x[[length(x) + 1]] <- technology
x[[length(x) + 1]] <- emissions

x <- mbind(x)

if (sum) {
x <- dimSums(x, dim = 3)
}

if (type == "annuity") {
# check
if (any(abs(readGDX(gdx, "ov11_cost_reg", select = list(type = "level")) - dimSums(x, dim = 3)) > 1e-6)) {
Expand All @@ -192,15 +193,15 @@ costs <- function(gdx, file = NULL, level = "reg", type = "annuity", sum = TRUE)
}
} else {
try(costs(gdx, file = file, level = level, type = "annuity", sum = TRUE))

}

# aggregate
if (level == "regglo" || level == "glo"|| level == "reg") {
x <- gdxAggregate(gdx, x, to = level, absolute = TRUE)
} else {
stop("Level not supported")
}

out(x, file)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MAgPIE outputs R package for MAgPIE version 4.x

R package **magpie4**, version **2.0.1**
R package **magpie4**, version **2.0.2**

[![CRAN status](https://www.r-pkg.org/badges/version/magpie4)](https://cran.r-project.org/package=magpie4) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158582.svg)](https://doi.org/10.5281/zenodo.1158582) [![R build status](https://github.com/pik-piam/magpie4/workflows/check/badge.svg)](https://github.com/pik-piam/magpie4/actions) [![codecov](https://codecov.io/gh/pik-piam/magpie4/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/magpie4) [![r-universe](https://pik-piam.r-universe.dev/badges/magpie4)](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 **magpie4** in publications use:

Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 2.0.1, <https://github.com/pik-piam/magpie4>.
Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 2.0.2, <https://github.com/pik-piam/magpie4>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {magpie4: MAgPIE outputs R package for MAgPIE version 4.x},
author = {Benjamin Leon Bodirsky and Florian Humpenoeder and Jan Philipp Dietrich and Miodrag Stevanovic and Isabelle Weindl and Kristine Karstens and Xiaoxi Wang and Abhijeet Mishra and Felicitas Beier and Jannes Breier and Amsalu Woldie Yalew and David Chen and Anne Biewald and Stephen Wirth and Patrick {von Jeetze} and Debbora Leip and Michael Crawford and Marcos Alves},
year = {2024},
note = {R package version 2.0.1},
note = {R package version 2.0.2},
doi = {10.5281/zenodo.1158582},
url = {https://github.com/pik-piam/magpie4},
}
Expand Down

0 comments on commit 5f63b3d

Please sign in to comment.