Skip to content

hertzforge.units

units

Unit handling for hertzforge.

Values whose units are known are stored in SI (m, kg, s, N, Pa, K, ...). Values read from a source that does not declare units are stored exactly as they appear in the file and tagged unknown until the user defines units. Defining units converts them to SI once and records the unit chosen, so a wrong choice can be corrected without losing anything.

Conversions are affine, si = raw * scale + offset, so offset scales such as degC and degF are handled correctly along with purely multiplicative units.

pint provides the unit registry and is used only inside this module. The core objects carry plain numpy arrays and a dimension tag: a string such as 'length' or 'acceleration', or a compound expression such as 'acceleration/force'.

Classes:

Name Description
UnitError

A unit could not be read, or is not of the dimension required.

UnitsRequired

Raised when an operation needs units that have not been defined.

UnitSystem

A named mapping of dimension tag to display unit.

Functions:

Name Description
unit_registry

The pint unit registry, built on first use.

fold_unit_case

unit with its letter case corrected, when only the corrected

normalize_unit

A unit string as hertzforge reads it, before it is given to pint.

pretty_unit

A unit string with superscript exponents, for display:

plain_unit

The inverse of pretty_unit, so a displayed unit can be read back.

is_compound_unit

Whether a unit needs brackets before it is raised to a power or

parse_dimension

Parse a dimension expression into (base_dimension, power) parts.

si_transform

(scale, offset) such that si_value = value * scale + offset.

si_factor

Multiplier converting values in unit to SI.

to_si

values, read as unit, converted to SI.

from_si

SI values converted to unit; the inverse of to_si.

convert

values converted from one unit to another, through SI.

dimension_of

The hertzforge dimension tag of a unit string, or None.

signed

A reading with its sign and unit, the minus as the minus sign. What

Classes

UnitError

Bases: ValueError

A unit could not be read, or is not of the dimension required.

It is a ValueError, since a bad unit is a bad value, and it has its own name so the app can tell a unit problem, which it explains to the user, from a programming error, which it does not catch.

UnitsRequired

UnitsRequired(message: str, required: Sequence[str] = ('length_unit',))

Bases: UnitError

Raised when an operation needs units that have not been defined.

required names the unit settings that are missing (by default ('length_unit',)), so the caller can ask for exactly those.

UnitSystem dataclass

UnitSystem(name: str, units: dict = dict(), base: UnitSystem | None = None)

A named mapping of dimension tag to display unit.

base is the coherent system a derived system was made from, such as in-slinch-lbf-s for in-slinch-lbf-s (g), which shows accelerations in g. It is None for a coherent system. UNV export writes the coherent system (see coherent), because its units block cannot declare g.

The angular, moment, density and modal units are filled in from the base units when the system does not name them.

Methods:

Name Description
unit

Display unit for a dimension tag or dimension expression.

transform

(scale, offset) converting a value in this system's unit to SI,

factor

SI value per display unit, for dimensions with no offset.

from_si

Convert SI values to this system's display unit for dimension.

to_si

Convert values from this system's units into SI.

label

Display unit text, the same as unit; empty for 'unknown'.

label_text

Display unit for plain-text output, with superscript

label_ascii

Display unit in plain ASCII, with exponents as carets: in/s^2.

label_html

Display unit as HTML, drawn as a stacked fraction when it has a

with_units

A copy of this system with some dimensions' units replaced.

Attributes:

Name Type Description
coherent UnitSystem

The coherent system this one is based on, or this system if it

Attributes
coherent property
coherent: UnitSystem

The coherent system this one is based on, or this system if it is coherent.

Methods:
unit
unit(dimension: str) -> str

Display unit for a dimension tag or dimension expression.

Expressions are built from the base units: in in-slinch-lbf-s, 'acceleration/force' gives '(in/s**2)/lbf'.

Parameters:

Name Type Description Default
dimension str

A dimension tag, such as 'acceleration', or an expression, such as 'acceleration/force'.

required

Returns:

Type Description
str

The unit string in pint's spelling; empty for 'unknown'.

Raises:

Type Description
UnitError

If the system has no unit for a dimension in the expression.

transform
transform(dimension: str) -> tuple[float, float]

(scale, offset) converting a value in this system's unit to SI, si = value * scale + offset.

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'acceleration/force'.

required

Returns:

Type Description
tuple of (float, float)

The scale and offset. The offset is non-zero only for temperature; (1.0, 0.0) for 'unknown'.

Raises:

Type Description
UnitError

If an expression includes an offset unit such as degF.

factor
factor(dimension: str) -> float

SI value per display unit, for dimensions with no offset.

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'acceleration/force'.

required

Returns:

Type Description
float

What a value in this system's unit is multiplied by to give SI; divide an SI value by it to display it.

Raises:

Type Description
UnitError

If the display unit has an offset (degC, degF).

from_si
from_si(values: ArrayLike, dimension: str) -> Any

Convert SI values to this system's display unit for dimension.

Parameters:

Name Type Description Default
values array_like

Values in SI.

required
dimension str

A dimension tag or expression, such as 'acceleration' or 'force'.

required

Returns:

Type Description
array_like

The same values in this system's units.

to_si
to_si(values: ArrayLike, dimension: str) -> Any

Convert values from this system's units into SI.

Parameters:

Name Type Description Default
values array_like

Values in this system's units.

required
dimension str

A dimension tag or expression, such as 'acceleration'.

required

Returns:

Type Description
array_like

The same values in SI.

label
label(dimension: str) -> str

Display unit text, the same as unit; empty for 'unknown'.

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'force'.

required

Returns:

Type Description
str

The unit string in this system.

label_text
label_text(dimension: str) -> str

Display unit for plain-text output, with superscript exponents: in/s².

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'force'.

required

Returns:

Type Description
str

The label as plain text; empty for 'unknown'.

label_ascii
label_ascii(dimension: str) -> str

Display unit in plain ASCII, with exponents as carets: in/s^2.

This is for renderers that drop characters they cannot draw. VTK's 3-D axis titles drop unicode superscripts in every text mode, so label_text's (in/s²)²/Hz is drawn as (in/s)/Hz there.

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'force'.

required

Returns:

Type Description
str

The label with no unicode characters; empty for 'unknown'.

label_html
label_html(dimension: str) -> str

Display unit as HTML, drawn as a stacked fraction when it has a denominator.

Parameters:

Name Type Description Default
dimension str

A dimension tag or expression, such as 'acceleration' or 'force'.

required

Returns:

Type Description
str

The label with HTML superscripts; empty for 'unknown'.

with_units
with_units(name: str | None = None, **overrides: str) -> UnitSystem

A copy of this system with some dimensions' units replaced.

IN_LBF_S.with_units(acceleration='g') shows acceleration in g and every other dimension in inch-pound-second units. The copy's base is this system's coherent system.

Parameters:

Name Type Description Default
name str

What to call the derived system. The default is this system's name followed by ' (custom)'.

None
**overrides str

Dimension/unit pairs to change.

{}

Returns:

Type Description
UnitSystem

A copy with those units replaced.

Raises:

Type Description
UnitError

If a unit cannot be read or is not of its dimension.

Functions:

unit_registry

unit_registry() -> Any

The pint unit registry, built on first use.

There is one registry because pint units from two registries cannot be compared. It is built on first use because building it takes about 0.1 s, which a script that converts no units should not have to wait for.

Returns:

Type Description
UnitRegistry

The shared registry, with gn defined as standard gravity.

fold_unit_case cached

fold_unit_case(unit: str) -> str

unit with its letter case corrected, when only the corrected spelling is a unit this toolset can read.

'G' becomes 'g', 'G/lbf' becomes 'g/lbf', 'LBF/IN' becomes 'lbf/in' and 'Volts' becomes 'volts'. A bare G is always gravity; otherwise a string that already names a tracked quantity is returned unchanged, so 'mV' stays millivolts and 'MV' stays megavolts. A string that no change of case makes readable is also returned unchanged, so a caller can still tell an unknown unit from a known one.

Parameters:

Name Type Description Default
unit str

The unit as written, for example in a channel table.

required

Returns:

Type Description
str

The respelled unit, or unit itself.

normalize_unit

normalize_unit(unit: str) -> str

A unit string as hertzforge reads it, before it is given to pint.

The case is corrected as in fold_unit_case, then g is read as standard gravity (never grams, which are written gram), mil as a thousandth of an inch and lbm as pound-mass.

Parameters:

Name Type Description Default
unit str

The unit as written.

required

Returns:

Type Description
str

The spelling pint is given.

pretty_unit

pretty_unit(unit: str) -> str

A unit string with superscript exponents, for display: 'm/s**2' -> 'm/s²'.

Parameters:

Name Type Description Default
unit str

The unit in pint's spelling.

required

Returns:

Type Description
str

The same unit with superscripts and · for products.

plain_unit

plain_unit(text: str) -> str

The inverse of pretty_unit, so a displayed unit can be read back.

Parameters:

Name Type Description Default
text str

A unit as pretty_unit writes it, such as 'm/s²'.

required

Returns:

Type Description
str

The unit in pint's spelling, such as 'm/s**2'.

is_compound_unit

is_compound_unit(unit: str) -> bool

Whether a unit needs brackets before it is raised to a power or divided by another.

Parameters:

Name Type Description Default
unit str

The unit string.

required

Returns:

Type Description
bool

True if it contains /, * or ·.

parse_dimension

parse_dimension(expression: str) -> list[tuple[str, int]]

Parse a dimension expression into (base_dimension, power) parts.

'acceleration/force' gives [('acceleration', 1), ('force', -1)]. The results are cached.

Parameters:

Name Type Description Default
expression str

A dimension tag or an expression of tags joined by *, / and integer ** powers.

required

Returns:

Type Description
list of (str, int)

Each base dimension and its power.

Raises:

Type Description
UnitError

If the expression names a dimension that is not tracked.

si_transform cached

si_transform(unit: str, dimension: str | None = None) -> tuple[float, float]

(scale, offset) such that si_value = value * scale + offset.

The offset is non-zero only for affine units such as degC and degF.

Parameters:

Name Type Description Default
unit str

The unit, for example 'mm' or 'degF'.

required
dimension str

If given, the unit is checked to be of this dimension.

None

Returns:

Type Description
tuple of (float, float)

The scale and the offset.

Raises:

Type Description
UnitError

If the unit cannot be read, or is not of dimension.

si_factor

si_factor(unit: str, dimension: str | None = None) -> float

Multiplier converting values in unit to SI.

Affine units (degC, degF) have no single factor; convert those with to_si and from_si.

Parameters:

Name Type Description Default
unit str

The unit, for example 'mm' or 'lbf'.

required
dimension str

If given, the unit is checked to be of this dimension.

None

Returns:

Type Description
float

The factor: a value in unit times this is the value in SI.

Raises:

Type Description
UnitError

If the unit has an offset, cannot be read, or is not of dimension.

to_si

to_si(values: ArrayLike, unit: str, dimension: str | None = None) -> Any

values, read as unit, converted to SI.

Offsets are applied, so degrees Celsius are converted to kelvin correctly.

Parameters:

Name Type Description Default
values array - like

The values, in unit.

required
unit str

The unit they are in.

required
dimension str

If given, the unit is checked to be of this dimension.

None

Returns:

Type Description
ndarray or float

The values in SI.

from_si

from_si(values: ArrayLike, unit: str, dimension: str | None = None) -> Any

SI values converted to unit; the inverse of to_si.

Parameters:

Name Type Description Default
values array - like

The values, in SI.

required
unit str

The unit wanted.

required
dimension str

If given, the unit is checked to be of this dimension.

None

Returns:

Type Description
ndarray or float

The values in unit.

convert

convert(values: ArrayLike, from_unit: str, to_unit: str) -> Any

values converted from one unit to another, through SI.

Parameters:

Name Type Description Default
values array - like

The values, in from_unit.

required
from_unit str

The unit they are in, for example 'in/s**2'.

required
to_unit str

The unit wanted, of the same dimension.

required

Returns:

Type Description
ndarray or float

The values in to_unit, the shape of values.

dimension_of

dimension_of(unit: str) -> str | None

The hertzforge dimension tag of a unit string, or None.

Only single tags are returned ('m/s**2' -> 'acceleration'). Compound quantities such as FRFs carry their dimension expression explicitly. A dimensionless unit is 'angle' if it is built on one (rad, deg) and 'dimensionless' otherwise, and rad/s and rpm are 'angular_velocity' where Hz is 'frequency'. A logarithmic unit (dB) has no dimension here, since it does not convert by a scale.

Parameters:

Name Type Description Default
unit str

The unit string.

required

Returns:

Type Description
str or None

The dimension tag, or None if the unit cannot be read or matches no tracked dimension.

signed

signed(value: float, unit: str = '', places: int = 1, missing: str = 'not read') -> str

A reading with its sign and unit, the minus as the minus sign. What rounds to zero reads +0.0, never -0.0; what is not finite reads missing.