wcosmo.utils.convert_quantity_if_necessary#

wcosmo.utils.convert_quantity_if_necessary(arg, unit=None, xp=<module 'numpy' from '/usr/share/miniconda/envs/test/lib/python3.13/site-packages/numpy/__init__.py'>)[source]#

Helper function to convert between astropy and unxt quantities and non-unitful values.

The order of precedence is as follows:

  • If using jax.numpy as the backend, the input is an astropy or unxt quantity or unit is specified, convert to a unxt quantity with the provided unit.

  • If using jax.numpy as the backend, the input is not a quantiy and no unit is provided, return the input.

  • If a unit and an astropy quantity are provided, convert the input to an astropy quantity with the provided unit

  • If a unit is provided, convert the input to an astropy quantity with the provided unit.

  • Else return the input as is.

Parameters:
arg: Union[astropy.units.Quantity, unxt.Quantity, array_like]

The array to convert

unit: Optional[astropy.units.Unit, str]

The unit to convert to

Returns:
Union[astropy.units.Quantity, unxt.Quantity, array_like]

The converted array