Skip to content

wellcast.units

units

Unit conversions for oilfield quantities.

Every unit belongs to a physical quantity (pressure, temperature, volume rate and so on) and is defined by an affine map to the base unit of that quantity::

base = value * scale + offset

Only temperature units use a non-zero offset. Conversions between units of different quantities raise wellcast.exceptions.UnitError.

Standard-condition volumes (stb, scf, sm3) are treated as plain volumes. The small difference between oilfield standard conditions (60 degF, 14.696 psia) and metric standard conditions (15 degC, 101.325 kPa) is ignored, as is common practice in production reporting.

Unit dataclass

Unit(name: str, quantity: str, scale: float, offset: float = 0.0)

A unit defined by an affine map to the base unit of its quantity.

Parameters:

Name Type Description Default
name str

Canonical symbol, for example "psi".

required
quantity str

Physical quantity, for example "pressure".

required
scale float

Multiplier to the base unit.

required
offset float

Additive term applied after scaling (temperatures only).

0.0

register_unit

register_unit(name: str, quantity: str, scale: float, offset: float = 0.0, *, overwrite: bool = False) -> Unit

Add a unit to the registry.

Parameters:

Name Type Description Default
name str

Unit symbol. Lookup is case-insensitive and ignores spaces.

required
quantity str

Physical quantity the unit measures.

required
scale float

Multiplier from this unit to the base unit of quantity.

required
offset float

Additive term applied after scaling. Default 0.

0.0
overwrite bool

Allow replacing an existing unit. Default False.

False

Returns:

Type Description
Unit

The registered unit.

Raises:

Type Description
UnitError

If the unit exists and overwrite is False, or scale is not a positive finite number.

Examples:

>>> from wellcast.units import convert, register_unit
>>> _ = register_unit("kbbl/d", "volume_rate", 158.987294928, overwrite=True)
>>> round(convert(1.0, "kbbl/d", "bbl/d"), 6)
1000.0

get_unit

get_unit(symbol: str) -> Unit

Look up a unit by symbol or alias.

Parameters:

Name Type Description Default
symbol str

Unit symbol such as "psi", "bopd" or "degC".

required

Returns:

Type Description
Unit

The registered unit.

Raises:

Type Description
UnitError

If the symbol is unknown.

Examples:

>>> from wellcast.units import get_unit
>>> get_unit("BOPD").name
'bbl/d'

list_units

list_units(quantity: str | None = None) -> list[str]

Return registered unit symbols, optionally for one quantity.

Parameters:

Name Type Description Default
quantity str

Restrict the list to this quantity, for example "pressure".

None

Returns:

Type Description
list of str

Sorted unit symbols.

Examples:

>>> from wellcast.units import list_units
>>> list_units("pressure")
['MPa', 'Pa', 'atm', 'bar', 'kPa', 'psi']

convert

convert(value: float, from_unit: str, to_unit: str) -> float
convert(value: NDArray[Any], from_unit: str, to_unit: str) -> npt.NDArray[np.float64]
convert(value: _T, from_unit: str, to_unit: str) -> _T
convert(value: Any, from_unit: str, to_unit: str) -> Any

Convert a value between two units of the same quantity.

Parameters:

Name Type Description Default
value (float, ndarray, Series or DataFrame)

Value(s) to convert. The container type is preserved.

required
from_unit str

Unit of value.

required
to_unit str

Target unit.

required

Returns:

Type Description
(float, ndarray, Series or DataFrame)

Converted value(s) in to_unit.

Raises:

Type Description
UnitError

If a unit is unknown or the units measure different quantities.

Notes

With base = value * scale + offset for each unit, the conversion is (value * s_from + o_from - o_to) / s_to.

Examples:

>>> from wellcast.units import convert
>>> round(convert(1000.0, "psi", "bar"), 4)
68.9476
>>> convert(212.0, "degF", "degC")
100.0
>>> round(convert(1.0, "m3/d", "bbl/d"), 4)
6.2898