a
    iDfd                     @  sD  d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	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 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$G dd dej#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)dOd"d#d$d%d&Z*dPd'd'd"d#d(d)d*d+Z+d,d-d(dd,d.d/d0Z,d1d2 Z-d3d4 Z.G d5d6 d6e"Z/dd7d8d9Z0d:d'd;d<d=d>Z1G d?d@ d@e"Z2G dAdB dBe"Z3G dCdD dDe"Z4G dEdF dFe"Z5G dGdH dHe"Z6G dIdJ dJe"Z7G dKdL dLe"Z8G dMdN dNe"Z9dS )Qz'Coders for individual Variable objects.    )annotationsN)HashableMutableMapping)partial)TYPE_CHECKINGAnyCallableUnion)dtypesduck_array_opsindexing)Variable)get_chunked_array_type)is_chunked_array.c                   @  s   e Zd ZdZdS )SerializationWarningz9Warnings about encoding/decoding issues in serialization.N)__name__
__module____qualname____doc__ r   r   d/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/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   decode0   s    zVariableCoder.decode)N)Nr   r   r   r   r!   r"   r   r   r   r   r      s   r   c                   @  s`   e Zd ZdZdddddZeddd	d
Zdd Zdd Zdd Z	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J t|| _|| _|| _d S N)r   r   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(   C   s    z_ElementwiseFunctionArray.dtypec                 C  s   t | | jj| | j| jS r)   )typer+   oindexr'   r(   r    keyr   r   r   _oindex_getG   s    z%_ElementwiseFunctionArray._oindex_getc                 C  s   t | | jj| | j| jS r)   )r2   r+   vindexr'   r(   r4   r   r   r   _vindex_getJ   s    z%_ElementwiseFunctionArray._vindex_getc                 C  s   t | | j| | j| jS r)   )r2   r+   r'   r(   r4   r   r   r   __getitem__M   s    z%_ElementwiseFunctionArray.__getitem__c                 C  s   |  | j S r)   )r'   r+   get_duck_arrayr1   r   r   r   r:   P   s    z(_ElementwiseFunctionArray.get_duck_arraystrc                 C  s(   t | j d| jd| jd| jdS )N(z, func=z, dtype=))r2   r   r+   r'   r(   r1   r   r   r   __repr__S   s    z"_ElementwiseFunctionArray.__repr__N)r   r   r   r   r-   propertyr(   r6   r8   r9   r:   r>   r   r   r   r   r$   5   s   r$   c                   @  sR   e Zd ZdZdZddddZedddd	Zd
d Zdd Z	ddddZ
dS )NativeEndiannessArraya  Decode arrays on the fly from non-native to native endianness

    This is useful for decoding arrays from netCDF3 files (which are all
    big endian) into native endianness, so they can be used with Cython
    functions, such as those found in bottleneck and pandas.

    >>> x = np.arange(5, dtype=">i2")

    >>> x.dtype
    dtype('>i2')

    >>> NativeEndiannessArray(x).dtype
    dtype('int16')

    >>> indexer = indexing.BasicIndexer((slice(None),))
    >>> NativeEndiannessArray(x)[indexer].dtype
    dtype('int16')
    r+   Noner/   c                 C  s   t || _d S r)   r   r*   r+   r    r+   r   r   r   r-   m   s    zNativeEndiannessArray.__init__r.   c                 C  s   t | jjjt| jjj S r)   )r0   r(   r+   kindr;   itemsizer1   r   r   r   r(   p   s    zNativeEndiannessArray.dtypec                 C  s   t j| jj| | jdS Nr(   r0   asarrayr+   r3   r(   r4   r   r   r   r6   t   s    z!NativeEndiannessArray._oindex_getc                 C  s   t j| jj| | jdS rG   r0   rJ   r+   r7   r(   r4   r   r   r   r8   w   s    z!NativeEndiannessArray._vindex_get
np.ndarrayc                 C  s   t j| j| | jdS rG   r0   rJ   r+   r(   r4   r   r   r   r9   z   s    z!NativeEndiannessArray.__getitem__Nr   r   r   r   	__slots__r-   r?   r(   r6   r8   r9   r   r   r   r   r@   W   s   r@   c                   @  sR   e Zd ZdZdZddddZedddd	Zd
d Zdd Z	ddddZ
dS )BoolTypeArraya  Decode arrays on the fly from integer to boolean datatype

    This is useful for decoding boolean arrays from integer typed netCDF
    variables.

    >>> x = np.array([1, 0, 1, 1, 0], dtype="i1")

    >>> x.dtype
    dtype('int8')

    >>> BoolTypeArray(x).dtype
    dtype('bool')

    >>> indexer = indexing.BasicIndexer((slice(None),))
    >>> BoolTypeArray(x)[indexer].dtype
    dtype('bool')
    rA   rB   r/   c                 C  s   t || _d S r)   rC   rD   r   r   r   r-      s    zBoolTypeArray.__init__r.   c                 C  s
   t dS )Nbool)r0   r(   r1   r   r   r   r(      s    zBoolTypeArray.dtypec                 C  s   t j| jj| | jdS rG   rI   r4   r   r   r   r6      s    zBoolTypeArray._oindex_getc                 C  s   t j| jj| | jdS rG   rK   r4   r   r   r   r8      s    zBoolTypeArray._vindex_getrL   c                 C  s   t j| j| | jdS rG   rM   r4   r   r   r   r9      s    zBoolTypeArray.__getitem__NrN   r   r   r   r   rP   ~   s   rP   r   r%   r&   c                 C  s0   t | r t| }|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.
    rH   N)r   r   Z
map_blocksr$   )r+   r'   r(   Zchunkmanagerr   r   r   lazy_elemwise_func   s    rR   r   
T_VarTuple)varr   c                 C  s   | j | j| j | j fS r)   )dimsdataattrscopyencodingrT   r   r   r   unpack_for_encoding   s    r[   c                 C  s   | j | j| j | j fS r)   )rU   _datarW   rX   rY   rZ   r   r   r   unpack_for_decoding   s    r]   r   r   )r5   r   c                 C  s<   || v r0|rd|nd}t d| d| d|| |< d S )Nz on variable  z+failed to prevent overwriting existing key z	 in attrsz. 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.)
ValueError)destr5   valuer   Zvar_strr   r   r   safe_setitem   s    rb   r   r   )sourcer`   r5   r   r   c                 C  s(   |  |d}|du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   )poprb   )rc   r`   r5   r   ra   r   r   r   pop_to   s    rf   rL   list)rV   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.rH   F)r0   rJ   where)rV   rh   ri   r(   	conditionfvr   r   r   _apply_mask   s
    rm   c                   sp    d u rdS g d}t   d v rVddlm} z|  W n tyP   Y dS 0 dS t fdd|D S d S )	NF)dayshoursminutessecondsZmillisecondsmicrosecondsnanosecondsZsincer   )_unpack_netcdf_time_unitsTc                 3  s   | ]}| kV  qd S r)   r   ).0Ztstrunitsr   r   	<genexpr>      z _is_time_like.<locals>.<genexpr>)r;   Zxarray.coding.timesrt   r_   any)rw   Ztime_stringsrt   r   rv   r   _is_time_like   s    	r{   c                   s   i  fdddD  t  }tD ]}| }dd t|D }|st|tjrtjdd|d|d	|d
	tdd |= n||O }t	|dkr(tjdd| dtdd q(|fS )z "Check _FillValue and missing_value if available.

    Return dictionary with raw fill values and set with encoded fill values.

    Issue SerializationWarning if appropriate.
    c                   s   g | ]}t  |d qS rd   )rf   )ru   attrrW   r   raw_fill_dictr   r   
<listcomp>  s   z&_check_fill_values.<locals>.<listcomp>)missing_value
_FillValuec                 S  s   h | ]}t |s|qS r   )pdisnull)ru   rl   r   r   r   	<setcomp>  ry   z%_check_fill_values.<locals>.<setcomp>	variable z has non-conforming  z defined, dropping z
 entirely.   
stacklevel   z has multiple fill values z% defined, decoding all values to NaN.)
setrg   r0   Zravel
issubdtypeintegerwarningswarnr   len)rW   r   r(   rh   kvZkfillr   r~   r   _check_fill_values  s:    r   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  sP  t |\}}}}t|d|j}|d}|d}	|dd u}
|d u}|	d u}|sb|sb|S |r|rt||	std|d| d|	 d|r|
s|||d< t||d|d	}|r|d|
s||	n|	|d< t||d|d	}t	
|s>t|d
r2|jjdv r2t|ttjjk||}nt||}t||||ddS )Nr(   r   r   	_Unsignedz	Variable z has conflicting _FillValue (z) and missing_value (z). Cannot encode data.rd   rw   iuTZfastpath)r[   r0   r(   getr   Zallclose_or_equivr_   r2   rf   r   r   r{   rE   rj   iinfoint64minZfillnar   )r    r   r   rU   rV   rW   rY   r(   rl   mvunsignedZ	fv_existsZ	mv_existsZ
fill_valuer   r   r   r!   0  s<    

zCFMaskCoder.encodec                   s   t |j|j\}}|rt|\}}}  fdd| D  |rt|drv|jjdv rvtj	t
tj	j }}	n6d|vrd|vrt|j\}}	nt|j|tj }}	tt||	|d}
t||
|}t||| dd	S |S d S )
Nc                   s    g | ]\}}t  ||d qS r|   )rb   )ru   r}   ra   rY   r   r   r   r   i  s   z&CFMaskCoder.decode.<locals>.<listcomp>rw   r   scale_factor
add_offset)rh   ri   r(   Tr   )r   rW   r(   r]   itemsr{   r   rE   r0   r   r   r   r
   Zmaybe_promote_choose_float_dtypenanr   rm   rR   r   )r    r   r   r   rh   rU   rV   rW   r(   ri   	transformr   r   r   r"   b  s2    

zCFMaskCoder.decode)N)Nr#   r   r   r   r   r   -  s   2r   rH   c                 C  s2   | j |dd} |d ur| |9 } |d ur.| |7 } | S )NTr(   rX   )astype)rV   r   r   r(   r   r   r   _scale_offset_decoding  s    r   r.   ztype[np.floating[Any]])r(   mappingr   c                 C  s   | d}| d}|dus$|dur|dur:tt|}|durPtt|}|dur|dur||kr|tjtjfv r| jdkrt| tjrtjS |jS |durtjS |jS | jdkrt| tj	rtjS | jdkrt| tjrtjS tjS )zBReturn a float dtype that can losslessly represent `dtype` values.r   r   N      )
r   r0   r(   r2   float32float64rF   r   r   floating)r(   r   r   r   Z
scale_typeZoffset_typer   r   r   r     s4    

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 )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|v s d|v rR|j}d|vrBd|vrBt|j|}tj||dd}d|v rn|t||d|d8 }d|v r|t||d|d }t||||ddS )	Nr   r   r   r   Tr   rd   r   )r[   r(   r   r   r   rf   r   r    r   r   rU   rV   rW   rY   r(   r   r   r   r!     s    zCFScaleOffsetCoder.encodec                 C  s   |j }d|v sd|v rt|\}}}}t||d|d}t||d|d}	t|dkrbt| }t|	dkr~t|	 }	|j}
d|vrd|vrt|
|}
t	t
||	|
d}t|||
}t||||dd	S |S d S )
Nr   r   rd   r   r   r   )r   r   r(   Tr   )rW   r]   rf   r0   ndimrJ   itemr(   r   r   r   rR   r   )r    r   r   _attrsrU   rV   rW   rY   r   r   r(   r   r   r   r   r"     s*    
zCFScaleOffsetCoder.decode)N)Nr#   r   r   r   r   r     s   r   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|v r`||d }||d< t	t
||}t||||ddS |S d S )Nr   falsetrueir   Tr   )rY   r   r[   rf   r0   r(   rF   r2   r   r   aroundr   )	r    r   r   rU   rV   rW   rY   signed_dtypenew_fillr   r   r   r!     s    zUnsignedIntegerCoder.encodec                 C  s  d|j v rt|\}}}}t||d}|jjdkr|dkrtd|jj }ttj|d}	t	||	|}d|v r|
|d }
|
|d< nz|jjdkr|dkrtd|jj }ttj|d}	t	||	|}d|v r|
|d }
|
|d< ntjd|d	td
d t||||ddS |S d S )Nr   r   r   urH   r   r   r   zH has _Unsigned attribute but is not of integer type. Ignoring attribute.r   r   Tr   )rW   r]   rf   r(   rE   r0   rF   r   rJ   rR   r2   r   r   r   r   )r    r   r   rU   rV   rW   rY   r   Zunsigned_dtyper   r   r   r   r   r   r"     s4    


zUnsignedIntegerCoder.decode)N)Nr   r   r   r!   r"   r   r   r   r   r     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 )DefaultFillvalueCoderz$Encode default _FillValue if needed.Nr   r   r   c                 C  s\   t |\}}}}d|vrTd|vrTt|jtjrT|jtj|d< t||||ddS |S d S )Nr   Tr   )r[   r0   r   r(   r   r2   r   r   r    r   r   rU   rV   rW   rY   r   r   r   r!   6  s    zDefaultFillvalueCoder.encodec                 C  s
   t  d S r)   r   r   r   r   r   r"   C  s    zDefaultFillvalueCoder.decode)N)Nr#   r   r   r   r   r   3  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 )BooleanCoderzCode boolean values.Nr   r   r   c                 C  s`   |j tkrXd|jvrXd|jvrXt|\}}}}d|d< tj|ddd}t||||ddS |S d S )Nr(   rQ   i1Tr   r   )r(   rQ   rY   rW   r[   r   r   r   r   r   r   r   r!   J  s    zBooleanCoder.encodec                 C  sR   |j dddkrJt|\}}}}|d|d< t|}t||||ddS |S d S )Nr(   FrQ   Tr   )rW   r   r]   re   rP   r   r   r   r   r   r"   X  s    zBooleanCoder.decode)N)Nr#   r   r   r   r   r   G  s   r   c                   @  s,   e Zd ZdZdd Zd
dddddd	ZdS )EndianCoderzDecode Endianness to native.c                 C  s
   t  d S r)   r   r1   r   r   r   r!   g  s    zEndianCoder.encodeNr   r   r   c                 C  s:   t |\}}}}|jjs2t|}t||||ddS |S d S )NTr   )r]   r(   Zisnativer@   r   r   r   r   r   r"   j  s
    zEndianCoder.decode)Nr#   r   r   r   r   r   d  s   r   c                   @  s,   e Zd ZdZd
ddddddZdd	 ZdS )NonStringCoderz,Encode NonString variables if dtypes differ.Nr   r   r   c                 C  s   d|j v r|j d dtfvrt|\}}}}t|d}||jkrt|tjrt|jtjrd|j	vrd|j	vrt
jd| dtdd t|}|j|d	}t||||d
dS |S d S )Nr(   ZS1r   r   zsaving variable zT with floating point data as an integer dtype without any _FillValue to use for NaNs
   r   rH   Tr   )rY   r;   r[   r0   r(   re   r   r   r   rW   r   r   r   r   r   r   r   r   r   r   r!   v  s.    


zNonStringCoder.encodec                 C  s
   t  d S r)   r   r1   r   r   r   r"     s    zNonStringCoder.decode)Nr#   r   r   r   r   r   s  s   r   c                   @  s(   e Zd Zdd Zd	ddddddZdS )
ObjectVLenStringCoderc                 C  s   t d S r)   r   r1   r   r   r   r!     s    zObjectVLenStringCoder.encodeNr   r   r   c                 C  s8   |j tkr0|jddtkr0||jd }|S |S d S )Nr(   F)r(   objectrY   r   r;   r   r   r   r   r   r"     s    zObjectVLenStringCoder.decode)Nr   r   r   r   r   r     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 )NativeEnumCoderz)Encode Enum into variable dtype metadata.Nr   r   r   c                 C  sj   d|j v rbt|j d jrbd|j d jv rbt|\}}}}|j|j dd}t||||ddS |S d S )Nr(   enumrH   Tr   )rY   r0   r(   metadatar[   r   re   r   r   r   r   r   r!     s    zNativeEnumCoder.encodec                 C  s
   t  d S r)   r   r   r   r   r   r"     s    zNativeEnumCoder.decode)N)Nr#   r   r   r   r   r     s   r   )N)N):r   
__future__r   r   collections.abcr   r   	functoolsr   typingr   r   r   r	   numpyr0   Zpandasr   Zxarray.corer
   r   r   Zxarray.core.variabler   Z xarray.namedarray.parallelcompatr   Zxarray.namedarray.pycompatr   tupledictrS   r   RuntimeWarningr   r   ZExplicitlyIndexedNDArrayMixinr$   r@   rP   rR   r[   r]   rb   rf   rm   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sN   "'& &]	286"