Package reflectometry :: Package reduction :: Module nxsunit

Module nxsunit

source code

Define unit conversion support for NeXus style units.

The unit format is somewhat complicated. There are variant spellings and incorrect capitalization to worry about, as well as forms such as "mili*metre" and "1e-7 seconds".

This is a minimal implementation of units including only what I happen to need now. It does not support the complete dimensional analysis provided by the package udunits on which NeXus is based, or even the units used in the NeXus definition files.

Unlike other units packages, such as that in DANSE, this package does not carry the units along with the value, but merely provides a conversion function for transforming values.

Usage example:

u = nxsunit.Converter('mili*metre') # Units stored in mm v = u(3000,'m') # Convert the value 3000 mm into meters

NeXus example:

# Load sample orientation in radians regardless of how it is stored. # 1. Open the path file.openpath('/entry1/sample/sample_orientation') # 2. scan the attributes, retrieving 'units' units = [for attr,value in file.attrs() if attr == 'units'] # 3. set up the converter (assumes that units actually exists) u = nxsunit.Converter(units[0]) # 4. read the data and convert to the correct units v = u(file.read(),'radians')

This is a standalone module, not relying on either DANSE or NeXus, and can be used for other unit conversion tasks.

Note: minutes are used for angle and seconds are used for time. We cannot tell what the correct interpretation is without knowing something about the fields themselves. If this becomes an issue, we will need to allow the application to set the dimension for the units rather than getting the dimension from the units as we are currently doing.

Classes
  Converter
Unit converter for NeXus style units.
Functions
 
test() source code