Skip to content

Commit

Permalink
replace unit conversion functions with madrat tool wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Debbora Leip committed Oct 14, 2024
1 parent eaea127 commit f00f6e4
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '12520000'
ValidationKey: '12546270'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
lucode2
covr
madrat
magclass
citation
gms
goxygen
GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest 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: 'mrland: MadRaT land data package'
version: 0.62.6
date-released: '2024-10-04'
version: 0.62.7
date-released: '2024-10-14'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
Expand Down
6 changes: 3 additions & 3 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.62.6
Date: 2024-10-04
Version: 0.62.7
Date: 2024-10-14
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down Expand Up @@ -34,12 +34,12 @@ Depends:
mrfactors (>= 0.4.0),
mrfaocore (>= 1.0.0),
mrlandcore (>= 1.0.0),
GDPuc (>= 1.3.0),
R (>= 2.10.0)
Imports:
countrycode,
data.table,
dplyr,
GDPuc,
magpiesets,
mstools (>= 0.6.0),
ncdf4,
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(readEvapotranspiration)
export(readPYieldCoeff)
export(spatial_header)
export(toolPatternScaling)
import(GDPuc)
import(countrycode)
import(madrat)
import(magclass)
Expand All @@ -30,7 +31,7 @@ import(mrfaocore)
import(mrlandcore)
import(readxl)
import(utils)
importFrom(GDPuc,convertGDP)
importFrom(GDPuc,toolConvertGDP)
importFrom(SPEI,thornthwaite)
importFrom(data.table,fread)
importFrom(dplyr,left_join)
Expand Down
10 changes: 5 additions & 5 deletions R/calcGDPdeflator.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param currency "PPP" or "MER"
#' @return List of magpie objects with results on country level, weight on country level, unit and description.
#' @author David Chen
#' @importFrom GDPuc convertGDP
#' @importFrom GDPuc toolConvertGDP

calcGDPdeflator <- function(yearFrom = 2017, yearTo = 2005, currency = "PPP") {

Expand All @@ -25,10 +25,10 @@ calcGDPdeflator <- function(yearFrom = 2017, yearTo = 2005, currency = "PPP") {
unit <- "US$MER"
}

defl2017 <- convertGDP(defl2017,
unit_in = paste("constant", yearFrom, unit, sep = " "),
unit_out = paste("constant", yearTo, unit, sep = " "),
replace_NAs = c("linear", "no_conversion"))
defl2017 <- toolConvertGDP(defl2017,
unit_in = paste("constant", yearFrom, unit, sep = " "),
unit_out = paste("constant", yearTo, unit, sep = " "),
replace_NAs = c("linear", "no_conversion"))
getNames(defl2017) <- NULL

return(list(x = defl2017,
Expand Down
16 changes: 8 additions & 8 deletions R/calcGHGPrices.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ calcGHGPrices <- function(emissions = "pollutants", datasource = "REMMAG", rev =
description <- "GHG certificate prices for different scenarios based on data from REMIND-MAgPIE coupling"

#convert from USD05MER to USD17MER based on USA values for all countries as the CO2 price is global.
x <- x * round(GDPuc::convertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)
x <- x * round(GDPuc::toolConvertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)

} else if (datasource == "Strefler2021") {
x <- readSource("Strefler2021", subtype = paste0("intensive_", rev))
Expand Down Expand Up @@ -103,8 +103,8 @@ calcGHGPrices <- function(emissions = "pollutants", datasource = "REMMAG", rev =
getNames(x) <- gsub("^([^\\.]*)\\.(.*$)", "\\2.\\1", getNames(x))

#convert from USD05MER to USD17MER based on USA values for all countries as the CO2 price is global.
x <- x * round(GDPuc::convertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)
x <- x * round(GDPuc::toolConvertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)

description <- paste("GHG certificate prices for different scenarios taken",
"from Strefler et al 2021 (DOI 10.1038/s41467-021-22211-2)")
Expand All @@ -124,8 +124,8 @@ calcGHGPrices <- function(emissions = "pollutants", datasource = "REMMAG", rev =
}

#convert from USD05MER to USD17MER based on USA values for all countries as the CO2 price is global.
x <- x * round(GDPuc::convertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)
x <- x * round(GDPuc::toolConvertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)

description <- "ghg certificate prices for different scenarios based on data from REMIND-MAgPIE-coupling"

Expand Down Expand Up @@ -202,8 +202,8 @@ calcGHGPrices <- function(emissions = "pollutants", datasource = "REMMAG", rev =
x <- time_interpolate(x, seq(1995, 2150, 5), extrapolation_type = "constant")

#convert from USD05MER to USD17MER based on USA values for all countries as the CO2 price is global.
x <- x * round(GDPuc::convertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)
x <- x * round(GDPuc::toolConvertSingle(1, "USA", unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER"), 2)

description <- "ghg certificate prices for different scenarios based on CO2 prices provided by IMAGE"

Expand Down
24 changes: 12 additions & 12 deletions R/calcIrrigationInvCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ calcIrrigationInvCosts <- function() {
# Investment costs for expanding irrigation infrastructure in 1000 US$ per hectare
# Note: in World Bank report (William I. Jones, 1991), p. 98, given in US$ per hectare
# But: csv table has transformed values (in 1000 US$ per hectare)
WBirrigation <- readSource("WBirrigation")
getYears(WBirrigation) <- NULL
wBirrigation <- readSource("WBirrigation")
getYears(wBirrigation) <- NULL

# irrigation cost constant until 2015
data <- new.magpie(cells_and_regions = getRegions(WBirrigation),
data <- new.magpie(cells_and_regions = getItems(wBirrigation, dim = 1),
years = 1995:2050,
names = "ad_unit_cost",
fill = NA)
data[, 1995:2050, ] <- WBirrigation
data[, 1995:2050, ] <- wBirrigation

# Transform fromm 1000 USD$ per hectare to US$ per hectare
data <- data * 1000

# conversion: $1995 to $2004
data <- convertGDP(data,
unit_in = "constant 1995 US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")
data <- toolConvertGDP(data,
unit_in = "constant 1995 US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")

# from 2015 onwards, data converges to value of Germany until 2050
dataDEU <- new.magpie(cells_and_regions = getRegions(WBirrigation),
years = 1995:2050,
names = "ad_unit_cost",
fill = as.numeric(data["DEU", "y1995", ]))
dataDEU <- new.magpie(cells_and_regions = getItems(wBirrigation, dim = 1),
years = 1995:2050,
names = "ad_unit_cost",
fill = as.numeric(data["DEU", "y1995", ]))

data <- convergence(origin = data, aim = dataDEU,
start_year = "y2015", end_year = "y2050",
Expand Down
10 changes: 5 additions & 5 deletions R/calcTradeMargin.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' }
#' @importFrom magpiesets findset
#' @importFrom magclass is.magpie
#' @importFrom GDPuc convertGDP
#' @importFrom GDPuc toolConvertGDP
#' @importFrom mstools toolCountryFillBilateral

calcTradeMargin <- function(gtap_version = "GTAP9", bilateral = FALSE, producer_price = "FAOini") { # nolint
Expand Down Expand Up @@ -143,10 +143,10 @@ calcTradeMargin <- function(gtap_version = "GTAP9", bilateral = FALSE, producer_
weight[, , "distillers_grain"] <- weight[, , "tece"]

if (gtap_version == "GTAP9") {
out <- GDPuc::convertGDP(out, unit_in = "current US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")
out <- GDPuc::toolConvertGDP(out, unit_in = "current US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")

out <- setYears(out[, 2011, ], NULL)
weight <- setYears(weight[, 2011, ], NULL)
} else {
Expand Down
10 changes: 5 additions & 5 deletions R/calcTradeTariff.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @importFrom reshape2 acast
#' @importFrom magclass as.data.frame add_columns
#' @importFrom magpiesets findset
#' @importFrom GDPuc convertGDP
#' @importFrom GDPuc toolConvertGDP
#' @importFrom mstools toolCountryFillBilateral
#'

Expand Down Expand Up @@ -112,10 +112,10 @@ calcTradeTariff<- function(gtap_version = "GTAP9", type_tariff = "total", bilate
p <- add_dimension(p, dim = 1.2, nm = getItems(p, dim = 1))
}
if (gtap_version == "GTAP9") {
y <- GDPuc::convertGDP(y, unit_in = "current US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")
y <- GDPuc::toolConvertGDP(y, unit_in = "current US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")

y <- setYears(y[, 2011, ], NULL)
}

Expand Down
14 changes: 7 additions & 7 deletions R/convertSathayeForest.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#' Convert Sathaye Forest data
#'
#'
#' Convert Sathaye Forest data on ISO country level.
#'
#'
#'
#'
#' @param x MAgPIE object containing Sathaye Forest data region resolution
#' @return Sathaye Forest data as MAgPIE object aggregated/disaggregated to
#' country level
#' @author Lavinia Baumstark
#' @examples
#'
#'
#' \dontrun{ a <- convertSathayeForest(x)
#' }
#'
#'
convertSathayeForest <- function(x) {

# convert data from $US-2000 into $US-2017
y <- toolAggregate(x, "regionmappingSathayeForest.csv", weight=NULL )
y <- convertGDP(y, unit_in = "constant 2000 US$MER",
y <- toolAggregate(x, "regionmappingSathayeForest.csv", weight = NULL)
y <- toolConvertGDP(y, unit_in = "constant 2000 US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = "no_conversion")

Expand Down
2 changes: 1 addition & 1 deletion R/imports.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by lucode2: do not edit by hand

#' @import madrat magclass mrcommons mrfactors mrfaocore mrlandcore
#' @import madrat magclass mrcommons mrfactors mrfaocore mrlandcore GDPuc
NULL
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.62.6**
R package **mrland**, version **0.62.7**

[![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.62.6, <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 (URL: https://doi.org/10.5281/zenodo.3822083), R package version 0.62.7, <URL: 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.62.6},
url = {https://github.com/pik-piam/mrland},
note = {R package version 0.62.7},
doi = {10.5281/zenodo.3822083},
url = {https://github.com/pik-piam/mrland},
}
```

0 comments on commit f00f6e4

Please sign in to comment.