quantity_to_dataclass

sbpy.data.quantity_to_dataclass(**kwargs)[source]

Decorator that converts astropy quantities to sbpy data classes.

Use this decorator when your function is based on a single field in an sbpy DataClass.

Examples

This function accepts Ephem objects, but only uses heliocentric distance:

>>> import astropy.units as u
>>> import sbpy.data as sbd
>>>
>>> @sbd.quantity_to_dataclass(eph=(sbd.Ephem, 'rh'))
... def temperature(eph):
...     return 278 * u.K / (eph['rh'] / u.au)**0.5
>>>
>>> print(temperature(1 * u.au))    # doctest: +FLOAT_CMP
[278.] K
>>> eph = sbd.Ephem.from_dict({'rh': 1 * u.au})
>>> print(temperature(eph))         # doctest: +FLOAT_CMP
[278.] K

This decorator also validates the dimensions of function parameters against the default dimensions as listed in the Field Name List (https://sbpy.readthedocs.io/en/latest/sbpy/data/fieldnames.html#id1). Users can provide equivalencies through an optional parameter equivalencies= to be used in unit checking. Equivalencies for dimensionless angle and temperature are automatically enabled.

A UnitsError will be raised if the unit attribute of the argument is not equivalent to default unit. If the default dimension is not None, and the argument has no unit attribute, and i.e. it is not a Quantity object, a ValueError will be raised.