Personal tools
You are here: Home CDAT Tips and Tricks Math Tips Units conversion with genutil.udunits
Document Actions

Units conversion with genutil.udunits

by Renata McCoy last modified 2007-05-08 10:36

Goal: Learn about units conversion functions in genutil.udunits module.


First you need to import genutil.udunits module
import cdms,genutil,unidata
Initialization of a unidata object
 a = unidata.udunits(value,units)
Initialize an object 'a' measureing 5m, convert to in and save as obj. 'b', then add 'a' and 'b'. Notice that they have different units! The reuslt will be in 'm'
a=unidata.udunits(5,'m') 
b=unidata.udunits(6,'in')
c=a+b
udunits(5.1524,"m") 

More units conversions

a.units='feet‘ ; print a 
 16.4041994751 feet
c=a.to('km')
udunits(0.005,"km")
c=unidata.udunits(7,'K') ; factor, offset = c.how('degF')
(1.8, -459.67) 

Find out what are the units
# list of all known units 
lst = c.available_units()

# dictionary: units (keys) / type (values)
dict = c.known_units()
dict['k']
# returns: 'THERMODYNAMIC TEMPERATURE'

dict = c.known_units(bytype=1)
# returns a dictionary of units type (keys)
# associated with a list of units for each type >

dict['THERMODYNAMIC TEMPERATURE']
# returns ['degree_Kelvin', 'degree_Celsius', …]


Powered by Plone