Skip to content

units

Centralized IAM→PyPSA unit conventions — one place for every conversion factor.

Conversion numbers live here, never as literals in the transforms or the Coupler, so that (a) a factor like 1e6 has a named, documented home, and (b) switching to another IAM means supplying a different table, not hunting through the code.

The single source of truth is UNIT_CONVERSIONS: a (from_unit, to_unit) → factor table. The symbol YAML declares each quantity's source/target unit (unit/units + to_unit, or per-row in a schema) and the loader resolves the factor through unit_factor. Identical units convert with factor 1.0 and need no table entry.

Naming note: molar masses use MOLAR_MASS_* (g/mol) — never MW, which here clashes with megawatts.

unit_factor(from_unit, to_unit)

Return the multiplicative factor converting from_unitto_unit.

Identical units return 1.0. An undeclared pair raises (fail loud, not silently wrong) — add it to UNIT_CONVERSIONS.

Source code in src/iampypsa/units.py
def unit_factor(from_unit: str, to_unit: str) -> float:
    """Return the multiplicative factor converting ``from_unit`` → ``to_unit``.

    Identical units return 1.0. An undeclared pair raises (fail loud, not silently wrong) —
    add it to ``UNIT_CONVERSIONS``.
    """
    if from_unit == to_unit:
        return 1.0
    try:
        return UNIT_CONVERSIONS[(from_unit, to_unit)]
    except KeyError:
        raise KeyError(
            f"No unit conversion defined for {from_unit!r} -> {to_unit!r}. "
            f"Add it to iampypsa.units.UNIT_CONVERSIONS."
        ) from None