From f00f6e4d99d03a8f356e8e7e11d7405598013b05 Mon Sep 17 00:00:00 2001 From: Debbora Leip Date: Mon, 14 Oct 2024 11:29:56 +0200 Subject: [PATCH] replace unit conversion functions with madrat tool wrappers --- .buildlibrary | 2 +- .github/workflows/check.yaml | 16 ++++++++-------- CITATION.cff | 4 ++-- DESCRIPTION | 6 +++--- NAMESPACE | 3 ++- R/calcGDPdeflator.R | 10 +++++----- R/calcGHGPrices.R | 16 ++++++++-------- R/calcIrrigationInvCosts.R | 24 ++++++++++++------------ R/calcTradeMargin.R | 10 +++++----- R/calcTradeTariff.R | 10 +++++----- R/convertSathayeForest.R | 14 +++++++------- R/imports.R | 2 +- README.md | 8 ++++---- 13 files changed, 63 insertions(+), 62 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 0857500..df27188 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -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' diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index f6ea5d4..23545e6 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -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 diff --git a/CITATION.cff b/CITATION.cff index cc04c2d..0768127 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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 diff --git a/DESCRIPTION b/DESCRIPTION index 67cbcfe..e1f6d5c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), @@ -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, diff --git a/NAMESPACE b/NAMESPACE index e3e8481..2aad0e3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(readEvapotranspiration) export(readPYieldCoeff) export(spatial_header) export(toolPatternScaling) +import(GDPuc) import(countrycode) import(madrat) import(magclass) @@ -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) diff --git a/R/calcGDPdeflator.R b/R/calcGDPdeflator.R index 283d61f..701b04d 100644 --- a/R/calcGDPdeflator.R +++ b/R/calcGDPdeflator.R @@ -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") { @@ -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, diff --git a/R/calcGHGPrices.R b/R/calcGHGPrices.R index 285578a..613736b 100644 --- a/R/calcGHGPrices.R +++ b/R/calcGHGPrices.R @@ -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)) @@ -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)") @@ -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" @@ -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" diff --git a/R/calcIrrigationInvCosts.R b/R/calcIrrigationInvCosts.R index 6185ee5..375a2f4 100644 --- a/R/calcIrrigationInvCosts.R +++ b/R/calcIrrigationInvCosts.R @@ -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", diff --git a/R/calcTradeMargin.R b/R/calcTradeMargin.R index b67a87e..7c73939 100644 --- a/R/calcTradeMargin.R +++ b/R/calcTradeMargin.R @@ -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 @@ -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 { diff --git a/R/calcTradeTariff.R b/R/calcTradeTariff.R index 8d60414..14de73e 100644 --- a/R/calcTradeTariff.R +++ b/R/calcTradeTariff.R @@ -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 #' @@ -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) } diff --git a/R/convertSathayeForest.R b/R/convertSathayeForest.R index 9ec2940..38eaecd 100644 --- a/R/convertSathayeForest.R +++ b/R/convertSathayeForest.R @@ -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") diff --git a/R/imports.R b/R/imports.R index 29b9102..1858a3e 100644 --- a/R/imports.R +++ b/R/imports.R @@ -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 diff --git a/README.md b/README.md index cb9a765..4695ee0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich , R package version 0.62.6, . +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, . A BibTeX entry for LaTeX users is @@ -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}, } ```