UDUnits.jl

A Julia interface for UDUnits2 for units of physical quantities
Author Alexander-Barth
Popularity
3 Stars
Updated Last
2 Years Ago
Started In
November 2017

UDUnits

Build Status codecov.io

documentation stable documentation latest

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:

Pkg.build("UDUnits")
  • before any call to using UDUnits or import UDUnits, set the following variable:
ENV["UDUNITS2_XML_PATH"] = joinpath(Pkg.dir("UDUnits"),"deps","udunits2.xml")

Alternatives

Resources

Used By Packages

No packages found.