a
    ߙfb.                     @   sv   d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
 ddlmZ d	gZg d
Zdedddfdd	ZdS )    )deepcopy)	signature)isliceN)wraps)AstropyUserWarning   )NDDatasupport_nddata)dataZuncertaintymaskmetaZunitZwcsflagsFc                    s   dusdurst dn.du r4r4t dndu r@g du rLg |tfddD rnt d  fdd}| dur|| S |S dS )	a  Decorator to wrap functions that could accept an NDData instance with
    its properties passed as function arguments.

    Parameters
    ----------
    _func : callable, None, optional
        The function to decorate or ``None`` if used as factory. The first
        positional argument should be ``data`` and take a numpy array. It is
        possible to overwrite the name, see ``attribute_argument_mapping``
        argument.
        Default is ``None``.

    accepts : class, optional
        The class or subclass of ``NDData`` that should be unpacked before
        calling the function.
        Default is ``NDData``

    repack : bool, optional
        Should be ``True`` if the return should be converted to the input
        class again after the wrapped function call.
        Default is ``False``.

        .. note::
           Must be ``True`` if either one of ``returns`` or ``keeps``
           is specified.

    returns : iterable, 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']``. If ``None`` assume the function only
        returns one argument: ``'data'``.
        Default is ``None``.

        .. note::
           Must be ``None`` if ``repack=False``.

    keeps : iterable. None, optional
        An iterable containing strings that indicate which values should be
        copied from the original input to the returned class. If ``None``
        assume that no attributes are copied.
        Default is ``None``.

        .. note::
           Must be ``None`` if ``repack=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 ``data`` argument as first argument,
        but if the first argument is called ``input`` one should pass
        ``support_nddata(..., data='input')`` to the function.

    Returns
    -------
    decorator_factory or decorated_function : callable
        If ``_func=None`` this returns a decorator, otherwise it returns the
        decorated ``_func``.

    Notes
    -----
    If properties of ``NDData`` are set but have no corresponding function
    argument a Warning is shown.

    If a property is set of the ``NDData`` are set and an explicit argument is
    given, the explicitly given argument is used and a Warning is shown.

    The supported properties are:

    - ``mask``
    - ``unit``
    - ``wcs``
    - ``meta``
    - ``uncertainty``
    - ``flags``

    Examples
    --------

    This function takes a Numpy array for the data, and some WCS information
    with the ``wcs`` keyword argument::

        def downsample(data, wcs=None):
            # downsample data and optionally WCS here
            pass

    However, you might have an NDData instance that has the ``wcs`` property
    set and you would like to be able to call the function with
    ``downsample(my_nddata)`` and have the WCS information, if present,
    automatically be passed to the ``wcs`` keyword 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 ``data``
    argument.
    Nz3returns or keeps should only be set if repack=True.z%returns should be set if repack=True.c                 3   s   | ]}| v V  qd S )N ).0keep)returnsr   8lib/python3.9/site-packages/astropy/nddata/decorators.py	<genexpr>       z!support_nddata.<locals>.<genexpr>z;cannot specify the same attribute in `returns` and `keeps`.c                    s   g g  t  j D ]\}}|j|j|jfv r>tdz&|j|jkrX	| n
	| W q ty } z&dt
|v r	| n W Y d }~qd }~0 0 qrd ddkrtdddt  	f
dd}|S )Nz$func may not have *args or **kwargs.zDThe truth value of an array with more than one element is ambiguous.r   r
   z?Can only wrap functions whose first positional argument is `{}`c              	      s  t j| g|R i |}t|  }| }g }|sLt| trLtd j|rJttdd D ]}zt	| |}W n t
y   Y q^Y n0 |dkr|sq^n
|d u rq^||}	|	vr|| q^|	|jv r|	v s|	v r|j|	 	|	 jurtd|	||	krdnd| t q^|||	< q^| j} |rJtdd| t | g|R i |}
|rrtdkrt|
trtt|
krtd	ntdkr|
g}
d urD ]}|
tt	|| q|
d
 }dd t|
D }|j|fi |S |
S d S )NzIOnly NDData sub-classes that inherit from {} can be used by this functionr   r   zdProperty {} has been passed explicitly and as an NDData property{}, using explicitly specified value  z[The following attributes were set on the data object, but will be ignored by the function: z, z9Function did not return the expected number of arguments.r
   c                 S   s   i | ]\}}|d kr||qS )r
   r   )r   Zretresr   r   r   
<dictcomp>  s   zUsupport_nddata.<locals>.support_nddata_decorator.<locals>.wrapper.<locals>.<dictcomp>)r   Zbind
isinstancer   	TypeErrorformat__name__r   SUPPORTED_PROPERTIESgetattrAttributeErrorgetappendZ	argumentsdefaultwarningswarnr   r
   joinlentuple
ValueErrorr   indexzip	__class__)r
   argskwargsZ
bound_argsZunpackZ
input_dataZignoredZpropvalueZ	propmatchresultr   Z
resultdataZresultkwargs)
acceptsall_returnsattr_arg_mapfunc	func_argsfunc_kwargskeepsrepackr   sigr   r   wrapper   s    





zAsupport_nddata.<locals>.support_nddata_decorator.<locals>.wrapper)r   
parametersitemskindZVAR_POSITIONALZVAR_KEYWORDr(   r"   emptyr!   strr    r   r   )r3   Z
param_nameZparamexcr9   r0   r1   r2   r6   r7   r   )r3   r4   r5   r8   r   support_nddata_decorator   s,    


 _z0support_nddata.<locals>.support_nddata_decorator)r(   any)Z_funcr0   r7   r   r6   Zattribute_argument_mappingrA   r   r@   r   r	      s"    h

 )copyr   inspectr   	itertoolsr   r#   	functoolsr   Zastropy.utils.exceptionsr   Znddatar   __all__r   r	   r   r   r   r   <module>   s   