U
    Cvf4                     @  s  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlZddlZddlmZmZmZ ddlmZ ddlmZ ereee
d	f eeef Zee
df ZG d
d deZG dd dZG dd dej Z!dddddZ"dddddZ#dddddZ$d:ddddd Z%d;d!d!ddd"d#d$d%Z&d&d'd"dd&d(d)d*Z'G d+d, d,eZ(dd-d.d/Z)d0d1d2d3d4d5Z*G d6d7 d7eZ+G d8d9 d9eZ,dS )<z'Coders for individual Variable objects.    )annotationsN)partial)TYPE_CHECKINGAnyCallableHashableMutableMappingTupleUnion)dtypesduck_array_opsindexing)is_duck_dask_array)Variable.c                   @  s   e Zd ZdZdS )SerializationWarningz9Warnings about encoding/decoding issues in serialization.N)__name__
__module____qualname____doc__ r   r   ;/tmp/pip-unpacked-wheel-h316xyqg/xarray/coding/variables.pyr      s   r   c                   @  s8   e Zd ZdZd
ddddddZddddddd	ZdS )VariableCodera  Base class for encoding and decoding transformations on variables.

    We use coders for transforming variables between xarray's data model and
    a format suitable for serialization. For example, coders apply CF
    conventions for how data should be represented in netCDF files.

    Subclasses should implement encode() and decode(), which should satisfy
    the identity ``coder.decode(coder.encode(variable)) == variable``. If any
    options are necessary, they should be implemented as arguments to the
    __init__ method.

    The optional name argument to encode() and decode() exists solely for the
    sake of better error messages, and should correspond to the name of
    variables in the underlying store.
    Nr   T_Namevariablenamereturnc                 C  s
   t  dS )z1Convert an encoded variable to a decoded variableNNotImplementedErrorselfr   r   r   r   r   encode)   s    zVariableCoder.encodec                 C  s
   t  dS )z1Convert an decoded variable to a encoded variableNr   r   r   r   r   decode-   s    zVariableCoder.decode)N)Nr   r   r   r   r!   r"   r   r   r   r   r      s   r   c                   @  sR   e Zd ZdZdddddZeddd	d
Zdd ZdddZddddZ	dS )_ElementwiseFunctionArrayzLazily computed array holding values of elemwise-function.

    Do not construct this object directly: call lazy_elemwise_func instead.

    Values are computed upon indexing or coercion to a NumPy array.
    r   np.typing.DTypeLikefuncdtypec                 C  s(   t |rtt|| _|| _|| _d S N)r   AssertionErrorr   Zas_indexablearrayr'   _dtype)r    r+   r'   r(   r   r   r   __init__:   s    z"_ElementwiseFunctionArray.__init__np.dtype)r   c                 C  s   t | jS r)   )npr(   r,   r    r   r   r   r(   @   s    z_ElementwiseFunctionArray.dtypec                 C  s   t | | j| | j| jS r)   )typer+   r'   r(   )r    keyr   r   r   __getitem__D   s    z%_ElementwiseFunctionArray.__getitem__Nc                 C  s   |  | jS r)   )r'   r+   )r    r(   r   r   r   	__array__G   s    z#_ElementwiseFunctionArray.__array__strc                 C  s   d t| j| j| j| jS )Nz{}({!r}, func={!r}, dtype={!r}))formatr1   r   r+   r'   r(   r0   r   r   r   __repr__J   s       z"_ElementwiseFunctionArray.__repr__)N)
r   r   r   r   r-   propertyr(   r3   r4   r7   r   r   r   r   r$   2   s   
r$   r   r%   r&   c                 C  s4   t | r$ddlm} |j|| |dS t| ||S dS )a  Lazily apply an element-wise function to an array.
    Parameters
    ----------
    array : any valid value of Variable._data
    func : callable
        Function to apply to indexed slices of an array. For use with dask,
        this should be a pickle-able object.
    dtype : coercible to np.dtype
        Dtype for the result of this function.

    Returns
    -------
    Either a dask.array.Array or _ElementwiseFunctionArray.
    r   Nr(   )r   Z
dask.arrayr+   Z
map_blocksr$   )r+   r'   r(   dar   r   r   lazy_elemwise_funcP   s    r;   r   
T_VarTuple)varr   c                 C  s   | j | j| j | j fS r)   )dimsdataattrscopyencodingr=   r   r   r   unpack_for_encodingg   s    rD   c                 C  s   | j | j| j | j fS r)   )r>   _datar@   rA   rB   rC   r   r   r   unpack_for_decodingk   s    rF   r   r   )r2   r   c                 C  s6   || kr*|rd|nd}t d|||| |< d S )Nz on variable  zfailed to prevent overwriting existing key {} in attrs{}. This is probably an encoding field used by xarray to describe how a variable is serialized. To proceed, remove this key from the variable's attributes manually.)
ValueErrorr6   )destr2   valuer   Zvar_strr   r   r   safe_setitemo   s     rK   r   r   )sourcerI   r2   r   r   c                 C  s(   |  |d}|dk	r$t||||d |S )z
    A convenience function which pops a key k from source to dest.
    None values are not passed on.  If k already exists in dest an
    error is raised.
    Nr   )poprK   )rL   rI   r2   r   rJ   r   r   r   pop_to{   s    rO   z
np.ndarraylist)r?   encoded_fill_valuesdecoded_fill_valuer(   r   c                 C  s6   t j| |d} d}|D ]}|| |kO }qt ||| S )z+Mask all matching values in a NumPy arrays.r9   F)r/   asarraywhere)r?   rQ   rR   r(   	conditionfvr   r   r   _apply_mask   s
    rW   c                   @  s4   e Zd ZdZd
dddddZdddddd	ZdS )CFMaskCoderz7Mask or unmask fill values according to CF conventions.Nr   r   )r   r   c                 C  s  t |\}}}}t|d|j}|d}|d}	|d k	rp|	d k	rpt||	sptd|d| d|	 d|d k	r|||d< t||d|d}
t	
|
st||
}|	d k	r||	|d< t||d|d}
t	
|
s|d krt||
}t||||d	d
S )Nr(   
_FillValuemissing_valuez	Variable z has conflicting _FillValue (z) and missing_value (z). Cannot encode data.rM   TZfastpath)rD   r/   r(   getr   Zallclose_or_equivrH   r1   rO   pdisnullZfillnar   )r    r   r   r>   r?   r@   rB   r(   rV   mvZ
fill_valuer   r   r   r!      s0    



zCFMaskCoder.encodec           
        s   t |\}}  fdddD }|rdd |D }t|dkr\tjd|tdd	 t|j\}}|rt	t
|||d
}	t||	|}t|| ddS |S d S )Nc                   s   g | ]}t  |d qS )rM   )rO   ).0attrr@   rB   r   r   r   
<listcomp>   s   z&CFMaskCoder.decode.<locals>.<listcomp>)rZ   rY   c                 S  s*   h | ]"}t |D ]}t|s|qqS r   )r/   Zravelr]   r^   )r`   optionrV   r   r   r   	<setcomp>   s
    
z%CFMaskCoder.decode.<locals>.<setcomp>   zFvariable {!r} has multiple fill values {}, decoding all values to NaN.   
stacklevel)rQ   rR   r(   Tr[   )rF   lenwarningswarnr6   r   r   Zmaybe_promoter(   r   rW   r;   r   )
r    r   r   r>   r?   Zraw_fill_valuesrQ   r(   rR   	transformr   rb   r   r"      s8     zCFMaskCoder.decode)N)Nr#   r   r   r   r   rX      s    rX   r9   c                 C  s4   t j| |dd} |d k	r | |9 } |d k	r0| |7 } | S )NTr(   rA   )r/   r+   )r?   scale_factor
add_offsetr(   r   r   r   _scale_offset_decoding   s    rq   r.   boolztype[np.floating[Any]])r(   
has_offsetr   c                 C  sF   | j dkrt| tjrtjS | j dkr@t| tjr@|s@tjS tjS )zBReturn a float dtype that can losslessly represent `dtype` values.      )itemsizer/   Z
issubdtypeZfloatingZfloat32integerZfloat64)r(   rs   r   r   r   _choose_float_dtype   s    rx   c                   @  s8   e Zd ZdZd
ddddddZddddddd	ZdS )CFScaleOffsetCoderzScale and offset variables according to CF conventions.

    Follows the formula:
        decode_values = encoded_values * scale_factor + add_offset
    Nr   r   r   c                 C  s   t |\}}}}d|ks d|kr>t|jd|k}|j|dd}d|krZ|t||d|d8 }d|krv|t||d|d }t||||ddS )Nro   rp   Trn   rM   r[   )rD   rx   r(   astyperO   r   )r    r   r   r>   r?   r@   rB   r(   r   r   r   r!     s    zCFScaleOffsetCoder.encodec                 C  s   |j }d|ksd|krt|\}}}}t||d|d}t||d|d}	t|jd|k}
t|dkrrt| }t|	dkrt|	 }	t	t
||	|
d}t|||
}t||||ddS |S d S )Nro   rp   rM   r   )ro   rp   r(   Tr[   )r@   rF   rO   rx   r(   r/   ndimrS   itemr   rq   r;   r   )r    r   r   _attrsr>   r?   r@   rB   ro   rp   r(   rm   r   r   r   r"     s&    zCFScaleOffsetCoder.decode)N)Nr#   r   r   r   r   ry      s   ry   c                   @  s4   e Zd Zd	ddddddZd
ddddddZdS )UnsignedIntegerCoderNr   r   r   c           	      C  s   |j dddkrt|\}}}}t||d td|jj }d|kr`||d }||d< t	|
|}t||||ddS |S d S )N	_UnsignedfalsetrueirY   Tr[   )rB   r\   rD   rO   r/   r(   rv   r1   r   Zaroundrz   r   )	r    r   r   r>   r?   r@   rB   signed_dtypenew_fillr   r   r   r!   +  s    zUnsignedIntegerCoder.encodec                 C  s  d|j krt|\}}}}t||d}|jjdkr|dkrtd|jj }ttj|d}	t	||	|}d|kr|
|d }
|
|d< nz|jjdkr|dkrtd|jj }ttj|d}	t	||	|}d|kr|
|d }
|
|d< ntjd|d	td
d t||||ddS |S d S )Nr   r   r   ur9   rY   r   z	variable zH has _Unsigned attribute but is not of integer type. Ignoring attribute.rg   rh   Tr[   )r@   rF   rO   r(   kindr/   rv   r   rS   r;   r1   rk   rl   r   r   )r    r   r   r>   r?   r@   rB   ZunsignedZunsigned_dtyperm   r   r   r   r   r   r"   >  s4    


zUnsignedIntegerCoder.decode)N)N)r   r   r   r!   r"   r   r   r   r   r~   *  s   r~   )N)N)-r   
__future__r   rk   	functoolsr   typingr   r   r   r   r   r	   r
   Znumpyr/   Zpandasr]   Zxarray.corer   r   r   Zxarray.core.pycompatr   Zxarray.core.variabler   dictr<   r   RuntimeWarningr   r   ZExplicitlyIndexedNDArrayMixinr$   r;   rD   rF   rK   rO   rW   rX   rq   rx   ry   r~   r   r   r   r   <module>   s6   $ J	-