support_nddata#
- astropy.nddata.support_nddata(_func=None, accepts=<class 'astropy.nddata.nddata.NDData'>, repack=False, returns=None, keeps=None, **attribute_argument_mapping)[source]#
Decorator to wrap functions that could accept an NDData instance with its properties passed as function arguments.
- Parameters:
- _func
python:callable(),python:None, optional The function to decorate or
Noneif used as factory. The first positional argument should bedataand take a numpy array. It is possible to overwrite the name, seeattribute_argument_mappingargument. Default isNone.- acceptsclass, optional
The class or subclass of
NDDatathat should be unpacked before calling the function. Default isNDData- repackbool, optional
Should be
Trueif the return should be converted to the input class again after the wrapped function call. Default isFalse.Note
Must be
Trueif either one ofreturnsorkeepsis specified.- returnspython:iterable,
python:None, optional An iterable containing strings which returned value should be set on the class. For example if a function returns data and mask, this should be
['data', 'mask']. IfNoneassume the function only returns one argument:'data'. Default isNone.Note
Must be
Noneifrepack=False.- keepsiterable.
python:None, optional An iterable containing strings that indicate which values should be copied from the original input to the returned class. If
Noneassume that no attributes are copied. Default isNone.Note
Must be
Noneifrepack=False.- attribute_argument_mapping
Keyword parameters that optionally indicate which function argument should be interpreted as which attribute on the input. By default it assumes the function takes a
dataargument as first argument, but if the first argument is calledinputone should passsupport_nddata(..., data='input')to the function.
- _func
- Returns:
- decorator_factory or decorated_function
python:callable() If
_func=Nonethis returns a decorator, otherwise it returns the decorated_func.
- decorator_factory or decorated_function
Notes
If properties of
NDDataare set but have no corresponding function argument a Warning is shown.If a property is set of the
NDDataare set and an explicit argument is given, the explicitly given argument is used and a Warning is shown.The supported properties are:
maskunitwcsmetauncertaintyflags
Examples
This function takes a Numpy array for the data, and some WCS information with the
wcskeyword argument:def downsample(data, wcs=None): # downsample data and optionally WCS here pass
However, you might have an NDData instance that has the
wcsproperty set and you would like to be able to call the function withdownsample(my_nddata)and have the WCS information, if present, automatically be passed to thewcskeyword argument.This decorator can be used to make this possible:
@support_nddata def downsample(data, wcs=None): # downsample data and optionally WCS here pass
This function can now either be called as before, specifying the data and WCS separately, or an NDData instance can be passed to the
dataargument.