a
    iDfb                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ dZ	h dZ
ddddd	d	d
ZdZdZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    )annotationsN)coding)Variablez_.@+- !"#$%&\()*,:;<=>?[]^`{|}~>   int64byteuint64floatdoublerealZushortcharshortboolstringintZuintint32int16Zint8)r   r   Zuint32Zuint16Zuint8r   utf-8ad  could not safely cast array from {dtype} to {new_dtype}. While it is not always the case, a common reason for this is that xarray has deemed it safest to encode np.datetime64[ns] or np.timedelta64[ns] values with int64 values representing units of 'nanoseconds'. This is either due to the fact that the times are known to require nanosecond precision for an accurate round trip, or that the times are unknown prior to writing due to being contained in a chunked array. Ways to work around this are either to use a backend that supports writing int64 values, or to manually specify the encoding['units'] and encoding['dtype'] (e.g. 'seconds since 1970-01-01' and np.dtype('int32')) on the time variable(s) such that the times can be serialized in a netCDF3 file (note that depending on the situation, however, this latter option may result in an inaccurate round trip).c                 C  sJ   t | j}|tv rFt| }| |}|| k sBttj||d|} | S )a  Coerce an array to a data type that can be stored in a netCDF-3 file

    This function performs the dtype conversions as specified by the
    ``_nc3_dtype_coercions`` mapping:
        int64  -> int32
        uint64 -> int32
        uint32 -> int32
        uint16 -> int16
        uint8  -> int8
        bool   -> int8

    Data is checked for equality, or equivalence (non-NaN values) using the
    ``(cast_array == original_array).all()``.
    )dtype	new_dtype)strr   _nc3_dtype_coercionsZastypeall
ValueErrorCOERCION_VALUE_ERRORformat)Zarrr   r   Zcast_arr r   d/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/backends/netcdf3.pycoerce_nc3_dtype>   s    

r   c                 C  sF   t | trn6t | tr"| t} n tt| } | jdkrBt	d| S )N   z'netCDF attributes must be 1-dimensional)

isinstancebytesr   encodeSTRING_ENCODINGr   npZ
atleast_1dndimr   )valuer   r   r   encode_nc3_attr_valueZ   s    


r&   c                 C  s   dd |   D S )Nc                 S  s   i | ]\}}|t |qS r   )r&   ).0kvr   r   r   
<dictcomp>g       z$encode_nc3_attrs.<locals>.<dictcomp>)items)attrsr   r   r   encode_nc3_attrsf   s    r.   c                 C  sl   | j }|jjdv rh| jdd }|d urhtj|rh|t	tj
jk}| rht|| jdtj|}|S )NiuunitsZ
_FillValue)datar   kindr-   getr   	variablesZ_is_time_liker#   Ziinfor   minanywherenan)varr1   r0   maskr   r   r   _maybe_prepare_timesj   s    r;   c                 C  sT   t jjddt j fD ]}|| } qt| }t|}t| j}t	| j
||| jS )NF)Zallows_unicode)r   stringsZEncodedStringCoderZCharacterArrayCoderr!   r;   r   r.   r-   r   dimsencoding)r9   Zcoderr1   r-   r   r   r   encode_nc3_variablez   s    
r?   c                 C  s   |   pt| ddkS )zrReturn True if the given UTF-8 encoded character is alphanumeric
    or multibyte.

    Input is not checked!
    r   r   )isalnumlenr!   )cr   r   r   _isalnumMUTF8   s    rC   c                 C  sz   t | tsdS t| d}td| | kox| tvox|dkoxd| vox| d dkoxt| d sh| d dkoxtd	d
 | D S )ar  Test whether an object can be validly converted to a netCDF-3
    dimension, variable or attribute name

    Earlier versions of the netCDF C-library reference implementation
    enforced a more restricted set of characters in creating new names,
    but permitted reading names containing arbitrary bytes. This
    specification extends the permitted characters in names to include
    multi-byte UTF-8 encoded Unicode and additional printing characters
    from the US-ASCII alphabet. The first character of a name must be
    alphanumeric, a multi-byte UTF-8 character, or '_' (reserved for
    special names with meaning to implementations, such as the
    "_FillValue" attribute). Subsequent characters may also include
    printing special characters, except for '/' which is not allowed in
    names. Names that have trailing space characters are also not
    permitted.
    Fr   NFCr   / _c                 s  s   | ]}t |p|tv V  qd S )N)rC   _specialchars)r'   rB   r   r   r   	<genexpr>   r+   z$is_valid_nc3_name.<locals>.<genexpr>)	r   r   rA   r!   unicodedata	normalize_reserved_namesrC   r   )s	num_bytesr   r   r   is_valid_nc3_name   s     

rP   )
__future__r   rK   numpyr#   Zxarrayr   Zxarray.core.variabler   rI   rM   r   r"   r   r   r&   r.   r;   r?   rC   rP   r   r   r   r   <module>   s.   
	