UDUnits
Installation
Inside the Julia shell, you can download and install the package by issuing:
using Pkg
Pkg.add("UDUnits")
Latest development version
If you want to try the latest development version, you can do this with the following commands:
using Pkg
Pkg.add(PackageSpec(url="https://github.com/Alexander-Barth/UDUnits.jl", rev="master"))
Pkg.build("UDUnits")
Loading the module
The first step is to load the module UDUnits
and to initialize the unit system sys
which correspond to the SI unit system.
using UDUnits
sys = System()
Units
The unit objects can be created for m
and cm
using either their symbol or their full name by indexing the sys
object as if sys
is a dictionary.
m = sys["meter"]
cm = sys["cm"]
Similarily to a dictionary, the function haskey
is defined to determine if a unit is valid:
haskey(sys,"μm") # returns true since UDUnits knows about micrometers
Derived units
Units can be derived using the usual mathemical operators. Of course, m_per_s
could have been also create simply by using sys["m/s"]
.
m = sys["m"]
km = sys["km"]
s = sys["s"]
h = sys["h"]
m_per_s = m/s
km_per_h = km/h
Converting data
The function areconvertible
returns true
if two units are convertible:
@show areconvertible(m_per_s,km_per_h)
To convert units, create a converter object and then apply the object to some data.
conv = Converter(m_per_s,km_per_h)
speed_in_m_per_s = 100.
speed_in_km_per_h = conv(speed_in_m_per_s)
The dot syntax can be used for an array as input:
speed_in_m_per_s = [100.,110.,120.]
speed_in_km_per_h = conv.(speed_in_m_per_s)
Windows
I did not succeed to install UDUnits.jl
on Windows using the package manager Conda.
One way to make it work on Windows is to bypass Conda:
-
extract
udunits2.dll
and all xml files from https://anaconda.org/conda-forge/udunits2/2.2.23/download/win-64/udunits2-2.2.23-vc9_1.tar.bz2 -
expat.dll from https://anaconda.org/conda-forge/expat/2.1.0/download/win-64/expat-2.1.0-vc9_1.tar.bz2
-
place all these files in the
deps
folder ofUDUnits
(i.e. the output ofjoinpath(Pkg.dir("UDUnits"),"deps")
) -
run
Pkg.build("UDUnits")
- before any call to
using UDUnits
orimport UDUnits
, set the following variable:
ENV["UDUNITS2_XML_PATH"] = joinpath(Pkg.dir("UDUnits"),"deps","udunits2.xml")