a
    iDfO#                     @  s&  d Z ddlmZ ddlmZ ddlZddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ edddZdd Zdd Zdd Zdd ZG dd deZd-ddZd.ddZddddd Z G d!d" d"eZ!d#d$ Z"d%d& Z#d'd( Z$d)d* Z%G d+d, d,ej&Z'dS )/zCoders for strings.    )annotations)partialN)VariableCoderlazy_elemwise_funcpop_tosafe_setitemunpack_for_decodingunpack_for_encoding)indexing)module_available)Variable)get_chunked_array_type)is_chunked_arraynumpyz
2.0.0.dev0)Z
minversionc                 C  s,   | t tfvrtd| tjdd| idS )Nz!unsupported type for vlen_dtype: Oelement_type)metadata)strbytes	TypeErrornpdtype)r    r   b/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/coding/strings.pycreate_vlen_dtype   s    r   c                 C  s2   | j dks| jd u rd S | jd| jdS d S )Nr   r   Zvlen)kindr   getr   r   r   r   check_vlen_dtype!   s    r   c                 C  s   | j dkpt| tkS )NU)r   r   r   r   r   r   r   is_unicode_dtype)   s    r    c                 C  s   | j dkpt| tkS )NS)r   r   r   r   r   r   r   is_bytes_dtype-   s    r"   c                   @  s>   e Zd ZdZdddZdddddd	Zddddd
dZdS )EncodedStringCoderz?Transforms between unicode strings and fixed-width UTF-8 bytes.Tc                 C  s
   || _ d S N)allows_unicode)selfr%   r   r   r   __init__4   s    zEncodedStringCoder.__init__Nr   )variablereturnc           
      C  s   t |\}}}}t|j}|ddk}|r2|d= |r|s@| jsd|v rXtd|d|dd}	t|d|	|d t||	}t	||||S ||_
|S d S )	Nr   S1Z
_FillValuez	variable z has a _FillValue specified, but _FillValue is not yet supported on unicode strings: https://github.com/pydata/xarray/issues/1647	_Encodingutf-8)name)r	   r    r   r   r%   NotImplementedErrorpopr   encode_string_arrayr   encoding)
r&   r(   r-   dimsdataattrsr1   Zcontains_unicodeZencode_as_charstring_encodingr   r   r   encode7   s     


zEncodedStringCoder.encodec           	      C  sP   t |\}}}}d|v rBt||d}tt|d}t||tt}t||||S )Nr+   r1   )	r   r   r   decode_bytes_arrayr   r   r   objectr   )	r&   r(   r-   r2   r3   r4   r1   r5   funcr   r   r   decodeQ   s    zEncodedStringCoder.decode)T)N)N)__name__
__module____qualname____doc__r'   r6   r;   r   r   r   r   r#   1   s   
r#   r,   c                   s6   t | }  fdd|  D }t j|td| jS )Nc                   s   g | ]}|  qS r   )r;   .0xr7   r   r   
<listcomp>_       z&decode_bytes_array.<locals>.<listcomp>r   )r   asarrayravelarrayr9   reshapeshape)Zbytes_arrayr1   decodedr   r7   r   r8   \   s    
r8   c                   s6   t | }  fdd|  D }t j|td| jS )Nc                   s   g | ]}|  qS r   )r6   r@   r7   r   r   rC   e   rD   z'encode_string_array.<locals>.<listcomp>r   )r   rE   rF   rG   r   rH   rI   )Zstring_arrayr1   encodedr   r7   r   r0   c   s    
r0   r   )varr)   c                 C  sD   t | jtkr<t| \}}}}tj|tjd}t||||S | S dS )zCEnsure that a variable with vlen bytes is converted to fixed width.r   N)r   r   r   r	   r   rE   bytes_r   )rL   r2   r3   r4   r1   r   r   r   ensure_fixed_length_bytesi   s
    rN   c                   @  s$   e Zd ZdZdddZdddZdS )	CharacterArrayCoderz@Transforms between arrays containing bytes and character arrays.Nc                 C  sz   t |}t|\}}}}|jjdkrl|dturlt|}d| v rR|d}nd|j	d  }||f }t
||||S )Nr!   r   char_dim_namestring)rN   r	   r   r   r   r   bytes_to_charkeysr/   rI   r   )r&   r(   r-   r2   r3   r4   r1   rP   r   r   r   r6   w   s    
zCharacterArrayCoder.encodec                 C  sL   t |\}}}}|jdkr>|r>|d |d< |d d }t|}t||||S )Nr*   rR   rP   )r   r   char_to_bytesr   )r&   r(   r-   r2   r3   r4   r1   r   r   r   r;      s    zCharacterArrayCoder.decode)N)N)r<   r=   r>   r?   r6   r;   r   r   r   r   rO   t   s   
rO   c                 C  sP   | j jdkrtdt| rHt| }|jt| d| j| j jf | j	gdS t| S )z?Convert numpy/dask arrays from fixed width bytes to characters.r!   z,argument must have a fixed-width bytes dtyper*   )r   chunksZnew_axis)
r   r   
ValueErrorr   r   
map_blocks_numpy_bytes_to_charrV   itemsizendim)arrchunkmanagerr   r   r   rS      s    rS   c                 C  s6   t rdnd}tj| |dtjd} | | jd dS )z8Like netCDF4.stringtochar, but faster and more flexible.NFC)copyorderr   )   r*   )HAS_NUMPY_2_0r   rG   rM   rH   rI   view)r\   r_   r   r   r   rY      s    rY   c                 C  s   | j dkrtd| js| S | jd }|sDtj| jdd tjdS t| rt| }t	| j
d dkrttd|  t dt| jd  }|jt| || j
dd | jd gd	S t| S dS )
z?Convert numpy/dask arrays from characters to fixed width bytes.r*   zargument must have dtype='S1'rR   Nr   ra   zPcannot stacked dask character array with multiple chunks in the last dimension: r!   )r   rV   Z	drop_axis)r   rW   r[   rI   r   zerosrM   r   r   lenrV   r   rX   _numpy_char_to_bytesStackedBytesArray)r\   sizer]   r   r   r   r   rU      s0    


rU   c                 C  sH   t rdnd}tj| |dd} dt| jd  }| || jdd S )z8Like netCDF4.chartostring, but faster and more flexible.NFr^   )r_   r`   r!   rR   )rb   r   rG   r   rI   rc   rH   )r\   r_   r   r   r   r   rf      s    rf   c                   @  sV   e Zd ZdZdd Zedd Zedddd	Zd
d Zdd Z	dd Z
dd ZdS )rg   a>  Wrapper around array-like objects to create a new indexable object where
    values, when accessed, are automatically stacked along the last dimension.

    >>> indexer = indexing.BasicIndexer((slice(None),))
    >>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]
    array(b'abc', dtype='|S3')
    c                 C  s"   |j dkrtdt|| _dS )zx
        Parameters
        ----------
        array : array-like
            Original array of values to wrap.
        r*   z9can only use StackedBytesArray if argument has dtype='S1'N)r   rW   r
   Zas_indexablerG   )r&   rG   r   r   r   r'      s
    
zStackedBytesArray.__init__c                 C  s   t dt| jjd  S )Nr!   rR   )r   r   r   rG   rI   r&   r   r   r   r      s    zStackedBytesArray.dtypeztuple[int, ...])r)   c                 C  s   | j jd d S )NrR   )rG   rI   ri   r   r   r   rI      s    zStackedBytesArray.shapec                 C  s   t | j d| jdS )N())typer<   rG   ri   r   r   r   __repr__   s    zStackedBytesArray.__repr__c                 C  s   t | jj| S r$   )rf   rG   Zvindexr&   keyr   r   r   _vindex_get   s    zStackedBytesArray._vindex_getc                 C  s   t | jj| S r$   )rf   rG   Zoindexrn   r   r   r   _oindex_get   s    zStackedBytesArray._oindex_getc                 C  sB   t |t|j| jj}|jd td kr4tdt| j| S )NrR   ztoo many indices)	rl   r
   Zexpanded_indexertuplerG   r[   slice
IndexErrorrf   rn   r   r   r   __getitem__  s    zStackedBytesArray.__getitem__N)r<   r=   r>   r?   r'   propertyr   rI   rm   rp   rq   ru   r   r   r   r   rg      s   
rg   )r,   )r,   )(r?   
__future__r   	functoolsr   r   r   Zxarray.coding.variablesr   r   r   r   r   r	   Zxarray.corer
   Zxarray.core.utilsr   Zxarray.core.variabler   Z xarray.namedarray.parallelcompatr   Zxarray.namedarray.pycompatr   rb   r   r   r    r"   r#   r8   r0   rN   rO   rS   rY   rU   rf   ZExplicitlyIndexedNDArrayMixinrg   r   r   r   r   <module>   s0    +

$