U
    CvfC                     @  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	dddd	d
ddddddddh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 )$    )annotationsN)coding)Variablez_.@+- !"#$%&\()*,:;<=>?[]^`{|}~bytecharshortZushortintZuintint64uint64floatrealdoubleboolstringZint32Zint16Zint8)r	   r
   Zuint32Zuint16Zuint8r   utf-8c                 C  sL   t | j}|tk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   ;/tmp/pip-unpacked-wheel-h316xyqg/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_valueK   s    


r!   c                 C  s   dd |   D S )Nc                 S  s   i | ]\}}|t |qS r   )r!   ).0kvr   r   r   
<dictcomp>X   s      z$encode_nc3_attrs.<locals>.<dictcomp>)items)attrsr   r   r   encode_nc3_attrsW   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_variable[   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   _isalnumMUTF8f   s    r1   c                 C  sz   t | tsdS t| d}td| | kox| tkox|dkoxd| k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kV  qd S )N)r1   _specialchars)r"   r0   r   r   r   	<genexpr>   s     z$is_valid_nc3_name.<locals>.<genexpr>)	r   r   r/   r   unicodedata	normalize_reserved_namesr1   r   )s	num_bytesr   r   r   is_valid_nc3_nameo   s     

r>   )
__future__r   r9   Znumpyr   Zxarrayr   Zxarray.core.variabler   r7   r;   r   r   r   r!   r(   r-   r1   r>   r   r   r   r   <module>   sB   
	