a
    İa                     @   s~   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d Zdd Zdd Zdd Zdd Zdd ZdS )    N   )coding)Variablez_.@+- !"#$%&\()*,:;<=>?[]^`{|}~>   Zuintint64boolZbytestringZushortuint64floatcharrealZdoubleintZshortZint32Zint16Zint8)r   r   Zuint32Zuint16Zuint8r   utf-8c                 C   sL   t | j}|tv rHt| }| |}|| k sDtd| 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()``.
    z'could not safely cast array from dtype z to )strdtype_nc3_dtype_coercionsZastypeall
ValueError)Zarrr   Z	new_dtypeZcast_arr r   6lib/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_valueI   s    


r   c                 C   s   dd |   D S )Nc                 S   s   i | ]\}}|t |qS r   )r   ).0kvr   r   r   
<dictcomp>V       z$encode_nc3_attrs.<locals>.<dictcomp>)items)attrsr   r   r   encode_nc3_attrsU   s    r&   c                 C   sN   t jjddt j fD ]}|| } qt| j}t| j}t	| j
||| jS )NF)Zallows_unicode)r   stringsZEncodedStringCoderZCharacterArrayCoderr   r   datar&   r%   r   Zdimsencoding)varZcoderr(   r%   r   r   r   encode_nc3_variableY   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   _isalnumMUTF8d   s    r/   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)r/   _specialchars)r   r.   r   r   r   	<genexpr>   r#   z$is_valid_nc3_name.<locals>.<genexpr>)	r   r   r-   r   unicodedata	normalize_reserved_namesr/   r   )sZ	num_bytesr   r   r   is_valid_nc3_namem   s     

r;   )r7   Znumpyr    r   Zcore.variabler   r5   r9   r   r   r   r   r&   r+   r/   r;   r   r   r   r   <module>   s&   
	