Skip to content

Commit

Permalink
Merge pull request #88 from tscheypidi/master
Browse files Browse the repository at this point in the history
adapated unit calculations to new version 0.7 of the units package
  • Loading branch information
tscheypidi authored Feb 24, 2021
2 parents 65463b2 + a642ba8 commit 180ad9f
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '97942820'
ValidationKey: '98024454'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "magclass: Data Class and Tools for Handling Spatial-Temporal Data",
"version": "5.24.6",
"version": "5.24.7",
"description": "<p>Data class for increased interoperability working with spatial-\n temporal data together with corresponding functions and methods (conversions,\n basic calculations and basic data manipulation). The class distinguishes\n between spatial, temporal and other dimensions to facilitate the development\n and interoperability of tools build for it. Additional features are name-based\n addressing of data and internal consistency checks (e.g. checking for the right\n data order in calculations).<\/p>",
"creators": [
{
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: magclass
Type: Package
Title: Data Class and Tools for Handling Spatial-Temporal Data
Version: 5.24.6
Date: 2021-02-12
Version: 5.24.7
Date: 2021-02-24
Authors@R: c(person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = c("aut","cre")),
person("Benjamin Leon", "Bodirsky", email = "bodirsky@pik-potsdam.de", role = "aut"),
person("Markus", "Bonsch", role = "aut"),
Expand Down Expand Up @@ -44,7 +44,7 @@ Suggests:
reshape2,
data.tree,
raster,
units,
units(>= 0.7.0),
udunits2,
ncdf4,
covr
Expand Down
32 changes: 17 additions & 15 deletions R/getMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@
#' \code{\link{getYears}}, \code{\link{getCPR}}, \code{\link{read.magpie}},
#' \code{\link{write.magpie}}, \code{"\linkS4class{magpie}"}
#' @examples
#' withMetadata(TRUE)
#' a <- as.magpie(1)
#' #returns NULL
#' getMetadata(a)
#' #set the unit field
#' getMetadata(a, "unit") <- "GtCO2eq"
#' getMetadata(a)
#' \dontrun{
#' withMetadata(TRUE)
#' a <- as.magpie(1)
#' #returns NULL
#' getMetadata(a)
#' #set the unit field
#' getMetadata(a, "unit") <- "GtCO2eq"
#' getMetadata(a)
#'
#' #set all Metadata fields
#' M <- list(unit='kg', source=list(author='John Doe', date='January 1, 2017',
#' title='example', publication='BigJournal, Vol. 200, pp. 100-115', institution='IEA'),
#' date=as.character(Sys.time()), user='my name', calcHistory=list('downloadSource','readSource'),
#' description='nonsense data')
#' getMetadata(a) <- M
#' getMetadata(a)
#' withMetadata(FALSE)
#' #set all Metadata fields
#' M <- list(unit='kg', source=list(author='John Doe', date='January 1, 2017',
#' title='example', publication='BigJournal, Vol. 200, pp. 100-115', institution='IEA'),
#' date=as.character(Sys.time()), user='my name', calcHistory=list('downloadSource','readSource'),
#' description='nonsense data')
#' getMetadata(a) <- M
#' getMetadata(a)
#' withMetadata(FALSE)
#' }
#' @export

getMetadata <- function(x, type=NULL) {
Expand Down
72 changes: 11 additions & 61 deletions R/install_magpie_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#'
install_magpie_units <- function(x=NULL) {
if (!withMetadata()) return(x)
if (!requireNamespace("units", versionCheck = list(name = "units", op = ">=", version = "0.7.0"), quietly = TRUE)) {
stop("units package in version >= 0.7 required")
}
units::units_options(auto_convert_names_to_symbols=FALSE, allow_mixed=FALSE, negative_power=TRUE, set_units_mode="standard")

unit_syntax <- function(z) {
Expand Down Expand Up @@ -225,7 +228,7 @@ install_magpie_units <- function(x=NULL) {
}else {
base <- paste0("t_",base)
base <- gsub("of_","",base)
units::install_conversion_constant(base,"tonne",1)
units::install_unit(base,"tonne",1)
}
}else if (grepl("square",pre[jj],ignore.case=TRUE)) {
if (suff=="") {
Expand Down Expand Up @@ -255,7 +258,7 @@ install_magpie_units <- function(x=NULL) {
base <- remove_spaces(base)
}else {
warning(base," has been successfully installed but is not a recognized unit. Standardize if possible.")
units::install_symbolic_unit(base,dimensionless=FALSE)
units::install_unit(base,"")
}
}
return(units::as_units(multiplier,paste0(SI_prefix,base,suff)))
Expand All @@ -280,7 +283,7 @@ install_magpie_units <- function(x=NULL) {
if (is_unit_installed(remove_spaces(b[[i]]))) {
b[[i]] <- remove_spaces(b[[i]])
}else {
units::install_symbolic_unit(b[[i]],dimensionless=FALSE)
units::install_unit(b[[i]],"")
}
}
}
Expand Down Expand Up @@ -317,7 +320,7 @@ install_magpie_units <- function(x=NULL) {
if (is_unit_installed(remove_spaces(w[[ii]]))) {
w[[ii]] <- remove_spaces(w[[ii]])
}else {
units::install_symbolic_unit(w[[ii]],dimensionless=FALSE)
units::install_unit(w[[ii]],"")
}
}
}
Expand Down Expand Up @@ -351,32 +354,6 @@ install_magpie_units <- function(x=NULL) {
}
if (grepl(",",a,fixed=TRUE) | grepl(";",a,fixed=TRUE)) {
return(units::as_units("unknown"))
#*****Mixed units handling in development*****
# a <- unlist(strsplit(a,","))
# a <- unlist(strsplit(a,";"))
# for (i in 1:length(a)) {
# if (grepl("_for_",a[i]) | grepl("_in_",a[i])) {
# warning("Unit entry \"",a[i],"\" should only include the unit and not the description.")
# }
# a[i] <- gsub("^_*","",gsub("_*$","",a[i]))
# if (is_unit_installed(remove_spaces(a[i]))) {
# a[i] <- remove_spaces(a[i])
# }else {
# a[i] <- split_denominator(a[i])
# a[i] <- split_multiples(a[i])
# if (!is_unit_installed(a[i])) {
# a[i] <- unit_syntax(a[i])
# if (!is_unit_installed(a[i])) {
# if (!is_unit_installed(remove_spaces(a[i]))) {
# units::install_symbolic_unit(a[i],dimensionless=FALSE)
# }else {
# a[i] <- remove_spaces(a[i])
# }
# }
# }
# }
# }
# return(units::mixed_units(1,a))
}
if (grepl("/",a,fixed=TRUE) | grepl("*",a,fixed=TRUE)) {
a <- split_denominator(a)
Expand All @@ -386,7 +363,7 @@ install_magpie_units <- function(x=NULL) {
}
if (!is_unit_installed(a)) {
if (!is_unit_installed(remove_spaces(a))) {
units::install_symbolic_unit(a,dimensionless=FALSE)
units::install_unit(a,"")
}else {
a <- remove_spaces(a)
}
Expand All @@ -395,36 +372,9 @@ install_magpie_units <- function(x=NULL) {
}

#Initialize some commonly used units in MAGPIE and REMIND
if (!is_unit_installed("tDM") & !is_unit_installed("pkm") & !is_unit_installed("unknown")) {
units::install_conversion_constant("tDM","tonne",1) #tonnes of dry matter
units::install_conversion_constant("tWM","tonne",1) #tonnes of wet matter
units::install_conversion_constant("BTU","toe",2.5e-8) #tonnes of oil equivalent
units::install_symbolic_unit("people",dimensionless=FALSE) #people
units::install_conversion_constant("people","person",1) #people interchangeable with person
units::install_conversion_constant("people","capita",1) #capita interchangeable with people
units::install_symbolic_unit("animal",dimensionless=FALSE) #animals
units::install_symbolic_unit("tCO2eq",dimensionless=FALSE) #tonnes of CO2 equivalent
units::install_conversion_constant("tCO2eq","tCO2_",1) #tonnes of CO2
units::install_conversion_constant("tC","tCO2_",44/12) #tonnes of carbon
units::install_conversion_constant("tN2O","tCO2eq",298) #tonnes of N2O
units::install_conversion_constant("tN","tN2O",44/28) #tonnes of nitrogen
units::install_conversion_constant("tN","tNO2_",46/14) #tonnes of NO2
units::install_conversion_constant("tN","tNO3_",62/14) #tonnes of NO3
units::install_conversion_constant("tN","tNH3_",17/14) #tonnes of NH3
units::install_conversion_constant("tCH4_","tCO2eq",36) #tonnes of methane
units::install_symbolic_unit("USD",dimensionless=FALSE) #US Dollars in 2018 currency value (InflationTool.com)
units::install_conversion_constant("USD05_","USD",1/1.33) #US Dollars in 2005 currency value (InflationTool.com)
units::install_conversion_constant("USD95_","USD05_",1/1.27) #US Dollars in 1995 currency value (InflationTool.com)
units::install_conversion_constant("EUR","USD",1/1.13365) #Euros at exchange rate with USD on 11.12.2018 (XE.com)
units::install_conversion_constant("EUR05_","EUR",1/1.24) #Euros in 2005 currency value (InflationTool.com)
units::install_conversion_constant("EUR95_","EUR05_",1/1.22) #Euros in 1995 currency value (InflationTool.com)
units::install_symbolic_unit("share",dimensionless=TRUE) #Share (dimension)
units::install_symbolic_unit("passenger",dimensionless=FALSE) #passengers
units::install_conversion_constant("pkm","passenger*km",1) #passenger-kilometer pkm
units::install_conversion_constant("Wh","W*h",1) #Watt-hours
units::install_conversion_constant("annum","year",1) #annum
units::install_conversion_constant("Wa","watt*annum",1) #Watts-annum
units::install_symbolic_unit("unknown") #unknown units; placeholder for mixed units
if (!is_unit_installed("tDM") || !is_unit_installed("pkm") || !is_unit_installed("unknown")) {
units <- readRDS(system.file("extdata/metadata/units.rds", package = "magclass"))
for(i in 1:nrow(units)) units::install_unit(symbol = units$unit[i], def = units$def[i], name= units$name[i])
}

if (is.null(x)) {
Expand Down
12 changes: 7 additions & 5 deletions R/withMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#' @author Jan Philipp Dietrich
#' @seealso \code{\link{getMetadata}}
#' @examples
#' withMetadata()
#' withMetadata(TRUE)
#' a <- as.magpie(1)
#' getMetadata(a)
#' withMetadata(FALSE)
#' \dontrun{
#' withMetadata()
#' withMetadata(TRUE)
#' a <- as.magpie(1)
#' getMetadata(a)
#' withMetadata(FALSE)
#' }
#' @export

withMetadata <- function(set=NULL,verbosity=NULL) {
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Class and Tools for Handling Spatial-Temporal Data

R package **magclass**, version **5.24.6**
R package **magclass**, version **5.24.7**

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

Expand Down Expand Up @@ -54,11 +54,10 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

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

Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip
D (2021). _magclass: Data Class and Tools for Handling Spatial-Temporal
Data_. doi: 10.5281/zenodo.1158580 (URL:
https://doi.org/10.5281/zenodo.1158580), R package version 5.24.6,
<URL: https://github.com/pik-piam/magclass>.
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D (2021). _magclass: Data Class
and Tools for Handling Spatial-Temporal Data_. doi: 10.5281/zenodo.1158580 (URL:
https://doi.org/10.5281/zenodo.1158580), R package version 5.24.7, <URL:
https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

Expand All @@ -67,7 +66,7 @@ A BibTeX entry for LaTeX users is
title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data},
author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip},
year = {2021},
note = {R package version 5.24.6},
note = {R package version 5.24.7},
doi = {10.5281/zenodo.1158580},
url = {https://github.com/pik-piam/magclass},
}
Expand Down
10 changes: 8 additions & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
Bugfix update:
FEATURES:
- added coordinates support
- added conversion functions raster <-> magclass
- added collapseDim

- added stringsAsFactor argument explicitly to functions where it was missing to prepare for upcoming updating in default settings
NOTES:
* checking dependencies in R code ... NOTE
Missing or unexported object: 'units::install_unit'

This note might occur if an older version of the "units" package is installed on the test system. The magclass package is already prepared for the changes which come with version 0.7.0 of package "units", but it also still works with older versions of the "units" package.
Binary file added inst/extdata/metadata/units.rds
Binary file not shown.
32 changes: 17 additions & 15 deletions man/getMetadata.Rd

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

12 changes: 7 additions & 5 deletions man/withMetadata.Rd

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

7 changes: 4 additions & 3 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ test_that("Read/write metadata works", {
})

test_that("Unit calculations work", {
skip_if_not_installed("units",minimum_version = "0.7.0")
expect_identical(units(pop), install_magpie_units("1e+06 people"))
expect_identical(units(pop+pop), install_magpie_units("1e+06 people"))
expect_identical(units(pop*pop), install_magpie_units("1e+12 people^2"))
expect_identical(units(pop + pop), install_magpie_units("1e+06 people"))
expect_identical(units(pop * pop), install_magpie_units("1e+12 people^2"))
pop2 <- pop
units(pop2) <- "people"
expect_identical(units(pop2), install_magpie_units("people"))
expect_identical(as.vector(pop2[1,1,1]),as.vector(10^6*pop[1,1,1]))
expect_equivalent(pop2-pop,0*pop)
expect_equivalent(pop2 - pop, 0 * pop)
expect_identical(units(animal), install_magpie_units("km^-2"))
expect_identical(units(pop/animal), install_magpie_units("1e06 km^2*people"))
expect_identical(units(pop/animal), install_magpie_units("1e06 people*km^2"))
Expand Down

0 comments on commit 180ad9f

Please sign in to comment.