Package gavo :: Package base :: Module unitconv
[frames] | no frames]

Module unitconv

source code

A python module to parse VOUnit strings and compute conversion factors.

We believe this implements the full VOUnit specification.

To use this, you must have the gavo utils package installed. For details, see http://soft.g-vo.org. The changes required to make this work without gavo.utils are minute, though. If you want that, talk to the authors.

Unit tests for this are at

http://svn.ari.uni-heidelberg.de/svn/gavo/python/trunk/tests/unitconvtest.py

Classes
  IncompatibleUnits
  BadUnit
  UnitNode
a terminal node containing a unit, possibly with a prefix
  FunctionApplication
A function applied to a term.
  QuotedUnitNode
a quoted ("defined unknown") unit.
  Factor
A UnitNode with a power.
  Term
A Node containing two factors and an operator.
  Expression
The root node of an expression tree.
  getUnitGrammar
the grammar to parse VOUnits.
Functions
 
formatScaleFactor(aFloat)
returns a reasonable decorative but python-readable representation of aFloat.
source code
 
evalAll(s, p, toks)
a parse action evaluating the whole match as a python expression.
source code
 
asSequence(unitDict)
returns a sorted tuple of (si, power) for a result of getSI().
source code
 
parseUnit(unitStr, unitGrammar=unit expression) source code
 
computeConversionFactor(unitStr1, unitStr2)
returns the factor needed to get from quantities given in unitStr1 to unitStr2.
source code
 
computeColumnConversions(newColumns, oldColumns)
returns a dict of conversion factors between newColumns and oldColumns.
source code
Variables
  PLANCK_H = 6.62607004e-34
  LIGHT_C = 299792458.0
  units = {'A': (1, 'A'), 'AU': (1.49598e+11, 'm'), 'Angstrom': ...
  PLAIN_UNITS = {'A': (1, 'A'), 'AU': (1.49598e+11, 'm'), 'Angst...
  EXCEPTIONAL_CONVERSIONS = {((('m', -1.0)), (('J', 1))): 1.9864...
  NON_PREFIXABLE = frozenset(['AU', 'D', 'Ry', 'arcmin', 'au', '...
  prefixes = {'': 1, 'E': 1e+18, 'G': 1000000000.0, 'M': 1000000...
  PREFIXES = {'': 1, 'E': 1e+18, 'G': 1000000000.0, 'M': 1000000...
  AMBIGUOUS_STRINGS = {'B': ('Byte', ''), 'Pa': ('Pa', ''), 'au'...
  __package__ = 'gavo.base'
Function Details

formatScaleFactor(aFloat)

source code 

returns a reasonable decorative but python-readable representation of aFloat.

Floats looking good as simple decimals (modulus between 0.01 and 1000) are returned without exponent.

evalAll(s, p, toks)

source code 

a parse action evaluating the whole match as a python expression.

Obviously, this should only be applied to carefully screened nonterminals.

asSequence(unitDict)

source code 

returns a sorted tuple of (si, power) for a result of getSI().

These should be more suitable for dimensional analysis.

computeConversionFactor(unitStr1, unitStr2)

source code 

returns the factor needed to get from quantities given in unitStr1 to unitStr2.

Both must be given in VOUnits form.

This function may raise a BadUnit if one of the strings are malformed, or an IncompatibleUnit exception if the units don't have the same SI base.

If the function is successful, unitStr1 = result*unitStr2

computeColumnConversions(newColumns, oldColumns)

source code 

returns a dict of conversion factors between newColumns and oldColumns.

Both arguments are iterables of columns.

For every column in newColumn, the function sees if the units of newColumn and oldColumn match. If they don't, compute a conversion factor to be multiplied to oldColumns values to make them newColumns values and add it to the result dict.

The function raises a DataError if a column in newColumns has no match in oldColumns.


Variables Details

units

Value:
{'A': (1, 'A'),
 'AU': (1.49598e+11, 'm'),
 'Angstrom': (1e-10, 'm'),
 'C': (1, 'C'),
 'D': (3.33333333333e-20, 'D'),
 'F': (1, 'F'),
 'G': (0.0001, 'T'),
 'H': (1, 'H'),
...

PLAIN_UNITS

Value:
{'A': (1, 'A'),
 'AU': (1.49598e+11, 'm'),
 'Angstrom': (1e-10, 'm'),
 'C': (1, 'C'),
 'D': (3.33333333333e-20, 'D'),
 'F': (1, 'F'),
 'G': (0.0001, 'T'),
 'H': (1, 'H'),
...

EXCEPTIONAL_CONVERSIONS

Value:
{((('m', -1.0)), (('J', 1))): 1.98644582417e-25}

NON_PREFIXABLE

Value:
frozenset(['AU',
           'D',
           'Ry',
           'arcmin',
           'au',
           'beam',
           'bin',
           'chan',
...

prefixes

Value:
{'': 1,
 'E': 1e+18,
 'G': 1000000000.0,
 'M': 1000000.0,
 'P': 1e+15,
 'T': 1e+12,
 'Y': 1e+24,
 'Z': 1e+21,
...

PREFIXES

Value:
{'': 1,
 'E': 1e+18,
 'G': 1000000000.0,
 'M': 1000000.0,
 'P': 1e+15,
 'T': 1e+12,
 'Y': 1e+24,
 'Z': 1e+21,
...

AMBIGUOUS_STRINGS

Value:
{'B': ('Byte', ''),
 'Pa': ('Pa', ''),
 'au': ('AU', ''),
 'cd': ('cd', ''),
 'dB': ('Bel', 'd'),
 'dadu': ('adu', 'd'),
 'ha': ('yr', 'h')}