Title: | Udunits-2 Bindings for R |
---|---|
Description: | Provides simple bindings to Unidata's udunits library. |
Authors: | James Hiebert [aut, cre], CRAN team [ctb, cph] (revisions/corrections in 2022) |
Maintainer: | ORPHANED |
License: | GPL-2 |
Version: | 0.13.2.1 |
Built: | 2024-11-01 11:45:46 UTC |
Source: | CRAN |
This package provides simple bindings to version 2 of Unidata's udunits library
Package: | udunits2 |
Type: | Package |
Version: | 0.6 |
Date: | 2011-02-11 |
License: | GPL-2 |
LazyLoad: | yes |
This package provides simple bindings to the version 2 API of Unidata's udunits library. While the entire API is not supported, we have chosen to boil it down to a few simple functions to be able to exploit the most useful functionality that the library provides. This package provides the following functions:
ud.is.parseable
ud.get.name
ud.get.symbol
ud.are.convertible
ud.convert
Please see the respective function help pages for further details and usage.
James Hiebert <[email protected]>
Maintainer: James Hiebert <[email protected]>
Unidata's udunits web page: https://www.unidata.ucar.edu/software/udunits/
ud.is.parseable ud.get.name ud.get.symbol
ud.are.convertible ud.convert
This function takes udunits compatible strings and determines whether or not it is possible to convert between them.
ud.are.convertible(u1, u2)
ud.are.convertible(u1, u2)
u1 |
A character string which is parseable into a udunits compatible unit. |
u2 |
Another character string which is also parseable into a udunits compatible unit. |
Even if two units are parseable and recognized by the udunits library, it may or may not be possible to convert from one to another. For example, it makes sense to convert from celsius to kelvin, however not from celsius to kilograms. This function allows the user to check if two units are of the same system and if there exists a defined conversion between the two.
Returns a logical: True
if the units can be converted between
each other, False
if either of the arguments is not parseable
by udunits, or if no conversion is possible.
James Hiebert [email protected]
See the udunits function ut_are_convertible: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#ut_005fare_005fconvertible_0028_0029 and the main uninits webpage: https://www.unidata.ucar.edu/software/udunits/
ud.are.convertible("miles", "km") # TRUE ud.are.convertible("grams", "kilograms") # TRUE ud.are.convertible("celsius", "grams") # FALSE ud.are.convertible("not", "parseable") # FALSE
ud.are.convertible("miles", "km") # TRUE ud.are.convertible("grams", "kilograms") # TRUE ud.are.convertible("celsius", "grams") # FALSE ud.are.convertible("not", "parseable") # FALSE
This function takes the numeric argument x
,
quantified in units u1
and converts it to be of units u2
.
ud.convert(x, u1, u2)
ud.convert(x, u1, u2)
x |
Some argument which is convertible to a numeric type by |
u1 |
A character string which is parseable into a udunits compatible unit. |
u2 |
Another character string which is also parseable into a udunits compatible unit and for which there exists a defined transformation from the units represented by u1. |
This function uses the udunits function cv_convert_doubles
to
convert the argument from one set of units to another.
Returns a numeric type having converted from one unit to another. The
attributes of the original argument x
(e.g. class, dimensions,
etc.) are preserved and then re-applied to the return value of the
transformation as such:
attributes(rv) <- attributes(x)
If either of unit u1
or u2
is unparseable, or there does
not exist a conversion from one to the other the function raises an error.
James Hiebert [email protected]
Unidata's udunits reference: https://www.unidata.ucar.edu/software/udunits/ API guide for cv_convert_doubles: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#index-cv_005fconvert_005fdoubles-39
x <- seq(10) ud.convert(x, "miles", "km") # c(1.609344, 3.218688, 4.828032, ...) x <- c(-40, 0, 100) ud.convert(x, "celsius", "degree_fahrenheit") # c(-40, 32, 212) err <- try(ud.convert(100,"miles", "grams")) # Error err <- try(ud.convert(NA, "not", "parseable")) # Error
x <- seq(10) ud.convert(x, "miles", "km") # c(1.609344, 3.218688, 4.828032, ...) x <- c(-40, 0, 100) ud.convert(x, "celsius", "degree_fahrenheit") # c(-40, 32, 212) err <- try(ud.convert(100,"miles", "grams")) # Error err <- try(ud.convert(NA, "not", "parseable")) # Error
Retrieve the udunits name or symbol from the database for a given units string.
ud.get.name(unit.string)
ud.get.name(unit.string)
unit.string |
A character string which is parseable into a udunits compatible unit. |
This function retrieves the udunits name or symbol from the udunits database and returns it. It uses the udunits functions ut_get_name and ut_get_symbol respectively.
Returns a character string stating the udunits's name/symbol for the given unit, or an empty character string if the unit does not map to a name/symbol for the default character set. If the unit is unparseable, the function raises an error.
More often than not units do not have names or symbols that are returned by the base functions. This depends entirely on what is defined in the units data base, which is–as of API version 2–an XML database which ships with the library. See Unidata's website for more information about the XML database: https://www.unidata.ucar.edu/software/udunits/udunits-2-units.html. All in all, don't put too much stock in them, for they are for convenience only. If your application requires certain names and symbols to be present, the XML database is local and editable.
James Hiebert [email protected]
Unidata's udunits reference: https://www.unidata.ucar.edu/software/udunits/ API guide for ut_get_name: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#index-ut_005fget_005fname-66 API guide for ut_get_symbol: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#index-ut_005fget_005fsymbol-67
units.to.display <- c("celsius", # has no name, messed up symbol (maybe a bug in R?) "kg", "hr", # has no symbol "K", "degrees", "m", "ohm") for (u in units.to.display) { print(ud.get.name(u)) print(ud.get.symbol(u)) }
units.to.display <- c("celsius", # has no name, messed up symbol (maybe a bug in R?) "kg", "hr", # has no symbol "K", "degrees", "m", "ohm") for (u in units.to.display) { print(ud.get.name(u)) print(ud.get.symbol(u)) }
This function check wether or not udunits has successfully found and loaded its run-time XML units database.
ud.have.unit.system()
ud.have.unit.system()
At package load time, Rudunits attempts to load a unit system from an XML units database from the file system. This might be installed with the system library (e.g. through apt or yum), or the user can use their own. The file-system location is configured using the UDUNITS2_XML_PATH environment variable.
This package will attempt to load the path contained in UDUNITS2_XML_PATH. If it's empty, it will attempt to load it from the system library. Failing that it will attempt to load its own XML database that ships with the package (from udunits source).
One can call ud.have.unit.system
to confirm that the units
database has been loaded successfully.
Returns a logical: True
if udunits has successfully found and
loaded the XML units database, False
otherwise.
James Hiebert [email protected]
ud.have.unit.system() # TRUE
ud.have.unit.system() # TRUE
Determine whether a unit string is parseable and recognized by the udunits library.
ud.is.parseable(unit.string)
ud.is.parseable(unit.string)
unit.string |
A character string representing a type of units which may be parseable by the udunits library |
ud.is.parseable
uses udunit's function ut_parse
to
determine whether or not the given unit string is parseable. If
ut_parse
returns NULL, then ud.is.parseable
will return FALSE
.
Returns a logical: True
if the units is parseable and
recognized by the udunits library, False
otherwise.
There is a note in the ut_parse
docs about how the
argument string must have no leading or trailing whitespace. We make
sure in this package to always call ut_trim
on any strings
before they are passed to ut_parse
. The package user need not
strip whitespace before-hand.
James Hiebert [email protected]
Unidata's udunits reference: https://www.unidata.ucar.edu/software/udunits/ API guide for ut_parse: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#index-ut_005fparse-43
ud.is.parseable("K") # TRUE ud.is.parseable(" K ") # TRUE ud.is.parseable("miles") # TRUE ud.is.parseable("Not parseable") # FALSE
ud.is.parseable("K") # TRUE ud.is.parseable(" K ") # TRUE ud.is.parseable("miles") # TRUE ud.is.parseable("Not parseable") # FALSE
This function sets the encoding type parameter which is global to the R udunits2 package.
ud.set.encoding(enc.string)
ud.set.encoding(enc.string)
enc.string |
A character string representing the encoding type.
Valid strings are |
Encoding type is a parameter to nearly all of the functions in the udunits library. By default, the R udunits2 pacakge sets the encoding type to UTF-8, however this package allows the user to set other encoding types which are supported by the udunits library. It presently suports UTF-8, ASCII, and ISO-8859-1
Returns no value. Raises an error if it is not given a valid encoding string.
James Hiebert [email protected]
Unidata's udunits reference: https://www.unidata.ucar.edu/software/udunits/ API guide chapter on data types: https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#Types
valid.enc.strings <- c('utf8', 'ascii', 'iso-8859-1', 'latin1') lapply(valid.enc.strings, ud.set.encoding) err <- try(ud.set.encoding("This will fail"))
valid.enc.strings <- c('utf8', 'ascii', 'iso-8859-1', 'latin1') lapply(valid.enc.strings, ud.set.encoding) err <- try(ud.set.encoding("This will fail"))