hmf.core.units.to_canonical

hmf.core.units.to_canonical(value: Any, unit: UnitBase, *, context: UnitContext | None = None, where: str, name: str) → Any[source]

Convert a dimensional value to a plain array in a canonical unit.

This is the conversion unit_boundary() applies to each dimensional argument, for code that converts a value itself (a field converter, a method with an H0 argument, …). Its errors are the boundary’s.

Parameters:
  • value – A Quantity, or None (returned as it is, for optional values).

  • unit – The unit to convert to, normally one of CANONICAL_UNITS.

  • context – The UnitContext whose H0 converts physical units. None (the default) means no H0: only units with the canonical power of littleh are accepted.

  • where – The object or method the value belongs to (e.g. "CAMB" or "Behroozi.modify_dndm()"), for error messages.

  • name – The name of the value (the argument or field), for error messages.

Returns:

The values in unit (for a scalar, a 0-d array or a numpy scalar). If value is already in exactly unit (the same object), it is a view of value, not a copy.

Return type:

numpy.ndarray or None

Raises:
  • UnitBoundaryError – If value is a bare number or array.

  • astropy.units.UnitConversionError – If value can’t be converted to unit (with the context’s H0, if any).

Examples

>>> to_canonical([1, 2] * u.kpc / littleh, Mpc_h, where="example", name="r")
array([0.001, 0.002])