wadi.unitconverter module

wadi.unitconverter.dict2str(groupdict)

This function attempts to create a string that can be parsed by Pint based on the elements of a dictionary that is returned by the regex match method in Mapper._match_regex. This function works only for the default dictionaries defined above and creates a string specifically formatted for the _str2pint method in the UnitConverter class.

class wadi.unitconverter.UnitRegexMapper(*args, func=<function dict2str>)

Bases: object

Class to make working with regular expressions to match units a little bit easier (not easy).

__init__(*args, func=<function dict2str>)

Class initialization method. Defines the dictionaries used to create the regular expressions to match the unit strings with, as well as the function to translate the groupdicts returned by the match method to a string that can be used by the _str2pint method in the UnitConverter class.

static _dict2RE(*args)

This function creates a string with a regular expression for parsing (chemical concentration) units. For more details on group names see https://docs.python.org/3/library/re.html

str(groupdict)

This function is simply a wrapper that returns the string created by the function that translates the groupdicts returned by the match method to a string that can be used by the _str2pint method in the UnitConverter class.

class wadi.unitconverter.UnitConverter

Bases: object

Class with some methods for unit parsing and conversion with Pint.

__init__()

Class initialization method.

_get_mw(s)

This method uses the molmass library to determine the molar mass of a substance.

Parameters:

s (str) – Name of the substance.

Returns:

result – The molar mass in g/mole, or None if a FormulaError was raised from within the molmass library.

Return type:

Pint Quantity object

get_uc(qs, target_units, mw_formula)

Use Pint to determine the value of the unit conversion factor.

Parameters:
  • qs (Pint Quantity object) – The source units (for example 1 mg/l).

  • target_units (str) – String that defines the target units.

  • mw_formula (str) – Chemical formula with which to convert between mass and molar concentration units.

Returns:

  • qt (Pint Quantity object) – The target units

  • uc (Pint Quantity object) – The unit conversion factor.

_str2pint(name, u_str)

This function parses the three-part string that is created by _match_regex when the units are mapped using a regular expression.

Parameters:
  • name (str) – The feature name alias. Also serves as an alternative string to determine the molar mass if Pint fails to parse u_str.

  • u_str (str) – String representation of the units to be parsed.

Returns:

  • uq (Pint Quantity object) – The units represented as Pint Quantity object.

  • mw_formula (str) – The chemical formula of the substance, to be used in get_uc to convert between mass and molar concentration units.

  • msg (str) – A message intended for the log file.

Notes

The function uses the partition function to split the string at the | symbol into a three-part tuple that contains (i) the part before the separator, (ii) the separator itself (redundant, not used), and (iii) the part after the separator. The part after the | symbol may or may not contain the formula for the molecular mass, depending on the format for the units in the input file. In that case name of the feature (passed to the function as ‘name’) is used as the formula to determine the molecular mass. Both the units and the molecular mass are converted to a Pint Quantity object (i.e., the product of a unit and a magnitude).