Inside the Julia shell, you can download and install the package by issuing:
using Pkg
Pkg.add("UDUnits")
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")
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()
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
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
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)