neclib.utils.quantity_utils#

Utility functions for physical quantity or unit handling.

angle_conversion_factor(original, to)[source]#

Conversion factor between angular units.

Parameters:
  • original (Literal['deg', 'rad', 'arcmin', 'arcsec']) – Original angular unit.

  • to (Literal['deg', 'rad', 'arcmin', 'arcsec']) – Unit to convert to.

Return type:

float

Notes

More general implementation may be realized using astropy.units, but it’s ~1000 times slower than this thus can be a bottleneck in time critical calculations.

Examples

>>> angle_deg = 1
>>> angle_deg * neclib.utils.angle_conversion_factor("deg", "arcsec")
3600  # arcsec
parse_quantity(quantity, *, unit=None)[source]#

Get astropy.units.Quantity object, optionally converting units.

Parameters:
  • quantity (Union[str, Quantity]) – Quantity object or its str expression.

  • unit (Optional[Union[UnitBase, str]]) – Unit(s) you want to use.

Return type:

Quantity

Examples

>>> neclib.utils.parse_quantity("3 M_sun pc^-2")
<Quantity 3. solMass / pc2>
>>> neclib.utils.parse_quantity("3 M_sun pc^-2", unit="kg")
<Quantity 5.96542625e+30 kg / pc2>

See also

partially_convert_unit

For unit conversion.

partially_convert_unit(quantity, new_unit)[source]#

Replace unit of given dimension.

Parameters:
  • quantity (Quantity) – Original Quantity object.

  • new_unit (Union[UnitBase, str]) – Unit(s) to employ.

Return type:

Quantity

Notes

new_unit should be (product of) units which construct quantity’s unit. If unit of quantity is J / s, this function can employ erg, but cannot convert to W.

Examples

>>> quantity = u.Quantity("3 L_sun s")
>>> neclib.utils.partially_convert_unit(quantity, "W")
<Quantity 1.1484e+27 s W>
>>> neclib.utils.partially_convert_unit(quantity, "W hour")
<Quantity 3.19e+23 h W>
>>> neclib.utils.partially_convert_unit(quantity, "J")
ValueError: Couldn't find equivalent units; give equivalent(s) of ["s", "solLum"].
force_data_type(az, el, unit='deg')[source]#
quantity2builtin(quantity, unit={})[source]#

Convert quantity to Python’s built-in types.

Parameters:
  • quantity (Dict[Hashable, Quantity]) – Dictionary of quantity objects to convert.

  • unit (Union[Dict[Hashable, Union[Unit, str]], Dict[Union[UnitBase, str], List[str]]]) – Dictionary of units to employ, in either formats: {parameter_name: unit}, {unit: [parameter_name]}.

Return type:

Dict[Hashable, Union[int, float, Any]]

Examples

>>> neclib.utils.quantity2builtin(
...     {"c": u.Quantity("299792458m/s")}, unit={"c": "km/s"}
... )
{'c': 299792.458}
>>> neclib.utils.quantity2builtin(
...     {"c": u.Quantity("299792458m/s")}, unit={"km/s": ["c"]}
... )
{'c': 299792.458}

Notes

If there are parameters not in unit, the values won’t be converted.

dAz2dx(az, el, unit)[source]#
Parameters:
  • az (Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]], Quantity]) –

  • el (Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]], Quantity]) –

  • unit (Union[UnitBase, str]) –

Return type:

Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]]]

dx2dAz(x, el, unit)[source]#
Parameters:
  • x (Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]], Quantity]) –

  • el (Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]], Quantity]) –

  • unit (Union[UnitBase, str]) –

Return type:

Union[int, float, ndarray[Any, dtype[number]], Array[Union[int, float]]]

get_quantity(*, default_unit: Optional[Union[UnitBase, str]]) _GetQuantity[source]#
get_quantity(*value: Union[str, int, float, ndarray[Any, dtype[number]], Array[Union[int, float]], Quantity], unit: Optional[Union[UnitBase, str]]) Quantity

Convert values to quantity.

Attention

This function has multiple signatures, one is simple converter from float values to Quantity, the other is attribute type validator. See examples.

Parameters:
  • value – Values to convert.

  • unit – Unit to employ.

  • default_unit – Default unit to use.

Examples

To use as a type converter: >>> neclib.utils.get_quantity(1, 2, 3, unit=”m”) <Quantity [1., 2., 3.] m> >>> neclib.utils.get_quantity(“1m”, “2m”, “3m”) <Quantity [1., 2., 3.] m>

To use as a type validator with default conversion: >>> class Test: … a = neclib.utils.get_quantity(default_unit=”m”) … b = neclib.utils.get_quantity(default_unit=”m”) >>> test = Test() >>> test.a = 1 >>> test.b = “2m” >>> test.a <Quantity 1. m> >>> test.b <Quantity 2. m>