
    d                       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mZ ddlmZ d	 Zd
 Zd Zd Z G d de          ZddZddZd Z G d de          Zd Zd Zd Z d Z! G 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)get_chunked_array_typeis_chunked_array)Variablec                v    | t           t          fvrt          d|           t          j        dd| i          S )Nz!unsupported type for vlen_dtype: Oelement_type)metadata)strbytes	TypeErrornpdtype)r   s    5lib/python3.11/site-packages/xarray/coding/strings.pycreate_vlen_dtyper      sB    C<''LLLLMMM8C><"@AAAA    c                    | j         dk    s| j        d S | j                            d| j                            d                    S )Nr   r   vlen)kindr   getr   s    r   check_vlen_dtyper       sE    zSEN2t ~!!.%.2D2DV2L2LMMMr   c                H    | j         dk    pt          |           t          k    S )NU)r   r    r   r   s    r   is_unicode_dtyper#   $   s"    :> 0 7 73 >>r   c                H    | j         dk    pt          |           t          k    S )NS)r   r    r   r   s    r   is_bytes_dtyper&   (   s"    :@ 0 7 75 @@r   c                  *    e Zd ZdZddZddZddZdS )	EncodedStringCoderz?Transforms between unicode strings and fixed-width UTF-8 bytes.Tc                    || _         d S N)allows_unicode)selfr+   s     r   __init__zEncodedStringCoder.__init__/   s    ,r   Nc                   t          |          \  }}}}t          |j                  }|                    d          dk    }|r|d= |rh|s| j        s_d|v r"t          d                    |                    |                    dd          }	t          |d|	|           t          ||	          }t          ||||          S )Nr   S1
_FillValuezvariable {!r} 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formatpopr   encode_string_arrayr   )
r,   variabler3   dimsdataattrsencodingcontains_unicodeencode_as_charstring_encodings
             r   encodezEncodedStringCoder.encode2   s    &9(&C&C#dE8+DJ77!g..$6 	"! 	> 	>t7J 	>u$$)CCI6$<<   'll;@@O_4HHHH&t_==DdE8444r   c                    t          |          \  }}}}d|v rOt          ||d          }t          t          |          }t	          ||t          j        t                              }t          ||||          S )Nr1   )r<   )	r	   r   r   decode_bytes_arrayr   r   r   objectr   )	r,   r8   r3   r9   r:   r;   r<   r?   funcs	            r   decodezEncodedStringCoder.decodeJ   sv    &9(&C&C#dE8%$UHkBBO-HHHD%dD"(62B2BCCDdE8444r   )Tr*   )__name__
__module____qualname____doc__r-   r@   rE    r   r   r(   r(   ,   sV        II- - - -5 5 5 505 5 5 5 5 5r   r(   r2   c                    t          j        |           } fd|                                 D             }t          j        |t                                        | j                  S )Nc                :    g | ]}|                               S rJ   )rE   .0xr<   s     r   
<listcomp>z&decode_bytes_array.<locals>.<listcomp>X   s%    ???aqxx!!???r   r   )r   asarrayravelarrayrC   reshapeshape)bytes_arrayr<   decodeds    ` r   rB   rB   U   s]    *[))K????;+<+<+>+>???G8G6***22;3DEEEr   c                    t          j        |           } fd|                                 D             }t          j        |t                                        | j                  S )Nc                :    g | ]}|                               S rJ   )r@   rM   s     r   rP   z'encode_string_array.<locals>.<listcomp>^   s%    @@@aqxx!!@@@r   r   )r   rQ   rR   rS   r   rT   rU   )string_arrayr<   encodeds    ` r   r7   r7   \   s]    :l++L@@@@<+=+=+?+?@@@G8G5)))11,2DEEEr   c                    t          |           \  }}}}t          |j                  t          k    r t	          j        |t          j                  }t          ||||          S )zCEnsure that a variable with vlen bytes is converted to fixed width.r   )r
   r    r   r   r   rQ   string_r   )varr9   r:   r;   r<   s        r   ensure_fixed_length_bytesr_   b   sW    "5c":":D$x
##u,,z$bj111D$x000r   c                  "    e Zd ZdZddZddZdS )CharacterArrayCoderz@Transforms between arrays containing bytes and character arrays.Nc                f   t          |          }t          |          \  }}}}|j        j        dk    rm|                    d          t
          urQt          |          }d|                                v r|                    d          }nd|j	        d          }||fz   }t          ||||          S )Nr%   r   char_dim_namestring)r_   r
   r   r   r   r   bytes_to_charkeysr6   rU   r   )r,   r8   r3   r9   r:   r;   r<   rc   s           r   r@   zCharacterArrayCoder.encoden   s    ,X66&9(&C&C#dE8:?c!!hll7&;&;3&F&F &&D(--//11 (_ = = 9B 9 9=**DdE8444r   c                    t          |          \  }}}}|j        dk    r&|r$|d         |d<   |d d         }t          |          }t          ||||          S )Nr/   re   rc   )r	   r   char_to_bytesr   )r,   r8   r3   r9   r:   r;   r<   s          r   rE   zCharacterArrayCoder.decode{   sf    &9(&C&C#dE8:$(,RH_%9D &&DdE8444r   r*   )rF   rG   rH   rI   r@   rE   rJ   r   r   ra   ra   k   sB        JJ5 5 5 55 5 5 5 5 5r   ra   c                
   | j         j        dk    rt          d          t          |           rGt	          |           }|                    t          | d| j        | j         j        fz   | j	        g          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   chunksnew_axis)
r   r   
ValueErrorr   r   
map_blocks_numpy_bytes_to_charrk   itemsizendim)arrchunkmanagers     r   rf   rf      s    
y~GHHH 	
-c22&& :#)"4!67hZ ' 
 
 	
  $$$r   c                    t          j        | ddt           j                  } |                     | j        dz                                 d          S )z8Like netCDF4.stringtochar, but faster and more flexible.FC)copyorderr   )   r/   )r   rS   r]   rT   rU   view)rr   s    r   ro   ro      sE     (3U#RZ
@
@
@C;;sy4'((--d333r   c                b   | j         dk    rt          d          | j        s| S | j        d         }|s-t	          j        | j        dd         t          j                  S t          |           rt          |           }t          | j
        d                   dk    r"t          d                    |                     t	          j         dt          | j        d                   z             }|                    t          | || j
        dd         | j        dz
  g	          S t          |           S )
z?Convert numpy/dask arrays from characters to fixed width bytes.r/   zargument must have dtype='S1're   Nr   rx   zRcannot stacked dask character array with multiple chunks in the last dimension: {}r%   )r   rk   	drop_axis)r   rm   rq   rU   r   zerosr]   r   r   lenrk   r5   r   rn   _numpy_char_to_bytesStackedBytesArray)rr   sizers   r   s       r   ri   ri      s+   
yD89998 
9R=D :x	#2#bj9999 &-c22sz"~""<<BF3KK  
 s39R=11122&& :crc?x!|n ' 
 
 	
 !%%%r   c                    t          j        | dd          } dt          | j        d                   z   }|                     |                              | j        dd                   S )z8Like netCDF4.chartostring, but faster and more flexible.Fru   )rv   rw   r%   re   N)r   rS   r   rU   ry   rT   )rr   r   s     r   r~   r~      sZ     (3U#
.
.
.C#cim$$$E88E??""39SbS>222r   c                  R    e Zd ZdZd Zed             Zed
d            Zd Zd Z	d	S )r   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                l    |j         dk    rt          d          t          j        |          | _        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   rm   r   as_indexablerS   )r,   rS   s     r   r-   zStackedBytesArray.__init__   s>     ;$K   *511


r   c                j    t          j        dt          | j        j        d                   z             S )Nr%   re   )r   r   r   rS   rU   r,   s    r   r   zStackedBytesArray.dtype   s)    xc$*"22"6777888r   returntuple[int, ...]c                *    | j         j        d d         S )Nre   )rS   rU   r   s    r   rU   zStackedBytesArray.shape   s    z$$r   c                @    t          |           j         d| j        dS )N())typerF   rS   r   s    r   __repr__zStackedBytesArray.__repr__   s$    t**%77
7777r   c                    t          |          t          j        |j        | j        j                            }|j        d         t          d           k    rt          d          t          | j        |                   S )Nre   ztoo many indices)	r   r   expanded_indexertuplerS   rq   slice
IndexErrorr~   )r,   keys     r   __getitem__zStackedBytesArray.__getitem__   sd    d3ii1#)TZ_MMNN9R=E$KK''/000#DJsO444r   N)r   r   )
rF   rG   rH   rI   r-   propertyr   rU   r   r   rJ   r   r   r   r      s         2 2 2 9 9 X9 % % % X%8 8 85 5 5 5 5r   r   )r2   )$rI   
__future__r   	functoolsr   numpyr   xarray.coding.variablesr   r   r   r   r	   r
   xarray.corer   xarray.core.parallelcompatr   r   xarray.core.variabler   r   r    r#   r&   r(   rB   r7   r_   ra   rf   ro   ri   r~   ExplicitlyIndexedNDArrayMixinr   rJ   r   r   <module>r      s     " " " " " "                          !           O O O O O O O O ) ) ) ) ) )B B BN N N? ? ?A A A&5 &5 &5 &5 &5 &5 &5 &5RF F F FF F F F1 1 15 5 5 5 5- 5 5 54% % %$4 4 4!& !& !&H3 3 3&5 &5 &5 &5 &5> &5 &5 &5 &5 &5r   