unit_checker#

arte.utils.unit_checker.assert_unit_is_equivalent(var, ref)#

Make sure that var has a unit compatible with ref

arte.utils.unit_checker.make_sure_its_a(unit, v, name='', copy=True)#

Make sure that v has the astropy unit unit.

If v does not have any unit, apply unit and return the combined value. If it has one, check that it can be converted to unit, and return the converted value. Otherwise, raise astropy.units.UnitsError.

Parameters:
  • unit (astropy unit) – the wanted unit

  • v – the value under test

  • name (string) – description of v. Will be used in error messages in case the conversion fails.

Returns:

the original value converted to unit.

Return type:

astropy Quantity

Raises:

astropy.units.UnitsError – if the conversion to unit fails.

arte.utils.unit_checker.separate_value_and_unit(var)#

Returns the argument value and unit, if var has one.

If not, var is returned unchanged and unit is set to 1.

Parameters:

var (any type) – the variable to be tested

Returns:

tuple with the value and the astropy unit of var or 1 if var does not have a unit.

Return type:

value, unit

Examples

> a = 42 * u.m > separate_value_and_unit(a) (42.0, Unit(“m”))

> b = 42 > separate_value_and_unit(b) (42, 1)

arte.utils.unit_checker.unit_check(f)#

Decorator to add type checking of astropy units to a function.

This decorator will ensure that, each time the decorated function is called, all the arguments have the correct astropy units, as defined in the default values of the decorated function, calling make_sure_its_a() for each of them.

If the function does not define a unit for a parameter, the corresponding argument is not modified, whether it has a unit or not.

If any of the checks fails, raises TypeError when the decorated function is called.