sasdata.data_util.nxsunit module¶
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, this package does not carry the units along with the value but merely provides a conversion function for transforming values.
Usage example:
import nxsunit
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 unit rather than inferring the dimension from an example unit.
- class sasdata.data_util.nxsunit.Converter(units: str | None = None, dimension: List[str] | None = None)¶
Bases:
object
Unit converter for NeXus style units.
The converter is initialized with the units of the source value. Various source values can then be converted to target values based on target value name.
- dimension: List[str] = None¶
Type of the source units (distance, time, frequency, …)
- get_compatible_units() List[str] ¶
Return a list of compatible units for the current Convertor object
- scale(units: str = '', value: T = None) List[float] | T ¶
Scale the given value using the units string supplied
- scalebase: float = None¶
Scale base for the source units
- scalemap: List[Dict[str, float | Tuple[float, float]]] = None¶
Scale converter, mapping unit name to scale factor or (scale, offset) for temperature units.
- scaleoffset: float = None¶
- property units: str¶
- sasdata.data_util.nxsunit.standardize_units(unit: str | None) List[str] ¶
Convert supplied units to a standard format for maintainability :param unit: Raw unit as supplied :return: Unit with known, reduced values