HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/__pycache__/conventions.cpython-36.pyc
3

���hw��@sbddlmZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
mZmZddlmZmZddlmZmZdd	lmZmZmZmZmZmZed
ddg�Zdddefd
d�Z dd�Z!dd�Z"dd�Z#dfdd�Z$dd�Z%dd�Z&e'dddddd g�Z(d!d"�Z)d#d$�Z*d%d&�Z+d'd(�Z,d)d*�Z-d+d,�Z.d-d.�Z/dgd/d0�Z0dhd1d2�Z1Gd3d4�d4ej2�Z3Gd5d6�d6ej2�Z4Gd7d8�d8ej2�Z5Gd9d:�d:ej2�Z6Gd;d<�d<ej2�Z7d=d>�Z8d?d@�Z9dAdB�Z:didCdD�Z;dEdF�Z<dGdH�Z=dIdJ�Z>djdLdM�Z?dkdNdO�Z@dldPdQ�ZAdRdS�ZBdTdU�ZCdmdVdW�ZDdndXdY�ZEdodZd[�ZFdpd\d]�ZGdqd^d_�ZHd`da�ZIdbdc�ZJddde�ZKdS)r�)�datetimeN)�defaultdict)�OutOfBoundsDatetime�)�indexing�ops�utils)�format_timestamp�
first_n_items)�as_variable�Variable)�	iteritems�
bytes_type�unicode_type�OrderedDict�PY3�
basestring�standardZ	gregorian�proleptic_gregoriancCs�tj||dd�}|dk	r�tjtj|��r�t|dd�dkr@|}n|g}x:|D]2}|jdkrjtj|||k<qL||krLtjtj�}qLW|dk	r�||9}|dk	r�||7}|S)a3Scale and mask array values according to CF conventions for packed and
    missing values

    First, values equal to the fill_value are replaced by NaN. Then, new values
    are given by the formula:

        original_values * scale_factor + add_offset

    Parameters
    ----------
    array : array-like
        Original array of values to wrap
    fill_value : number, optional
        All values equal to fill_value in the original array are replaced
        by NaN.  If an array of multiple values is provided a warning will be
        issued and all array elements matching an value in the fill_value array
        will be replaced by NaN.
    scale_factor : number, optional
        Multiply entries in the original array by this number.
    add_offset : number, optional
        After applying scale_factor, add this number to entries in the
        original array.

    Returns
    -------
    scaled : np.ndarray
        Array of masked and scaled values.

    References
    ----------
    http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html
    T)�dtype�copyN�sizerr)�np�array�all�pd�isnull�getattr�ndim�nan)r�
fill_value�scale_factor�
add_offsetr�valuesZfill_valuesZf_value�r$�2/tmp/pip-build-5_djhm0z/xray/xarray/conventions.py�mask_and_scales#

r&cCs0|j�}|jd�sd|}ddddddd�|S)	N�sz%ss�us�ms�m�h�D)�microseconds�milliseconds�seconds�minutes�hours�days)�lower�endswith)�unitsr$r$r%�_netcdf_to_numpy_timeunitKs

r6cCs:tjd|�}|std|��dd�|j�D�\}}||fS)Nz(.+) since (.+)zinvalid time units: %scSsg|]}|j��qSr$)�strip)�.0r'r$r$r%�
<listcomp>Zsz-_unpack_netcdf_time_units.<locals>.<listcomp>)�re�match�
ValueError�groups)r5�matches�delta_units�ref_dater$r$r%�_unpack_netcdf_time_unitsSs
rAcCs�ddl}tj|j|||��}|tj|�jdksD|tj|�jdkrVtjdt	dd�nFyt
|�}Wn8tk
r�}ztjdj|�t	dd�WYdd}~XnX|S)Nri�i�z�Unable to decode time axis into full numpy.datetime64 objects, continuing using dummy netCDF4.datetime objects instead, reason: dates out of range�)�
stacklevelz�Unable to decode time axis into full numpy.datetime64 objects, continuing using dummy netCDF4.datetime objects instead, reason:{0})
�netCDF4r�asarrayZnum2dateZ	nanargmin�yearZ	nanargmax�warnings�warn�RuntimeWarning�nctime_to_nptimer<�format)�	num_datesr5�calendar�nc4�dates�er$r$r%�_decode_datetime_with_netcdf4^s rQcCs�tj|td�}|j�}|dkr"d}t|�\}}yR|tkr<t�t|�}ytj	|�}Wnt
k
rjt�YnXtj||�|j}Wn$tt
fk
r�t|||�}YnX|j|j�S)amGiven an array of numeric dates in netCDF format, convert it into a
    numpy array of date time objects.

    For standard (Gregorian) calendars, this function uses vectorized
    operations, which makes it much faster than netCDF4.num2date. In such a
    case, the returned array will be of type np.datetime64.

    See also
    --------
    netCDF4.num2date
    )rNr)rrE�float�ravelrA�_STANDARD_CALENDARSrr6r�	Timestampr<�to_timedeltar#�
OverflowErrorrQ�reshape�shape)rLr5rMZflat_num_dates�deltar@rOr$r$r%�decode_cf_datetimess"
r[cCs$tj|�}|jdkr|S|j�SdS)Nr)rrEr�item)�xr$r$r%�_asarray_or_scalar�s

r^cCs8t|�}t|�}tj||dd�}|jdkr4|jd�}|S)znGiven an array of numeric timedeltas in netCDF format, convert it into a
    numpy timedelta64[ns] array.
    F)�unit�boxztimedelta64[ns])r^r6rrVr�astype)Znum_timedeltasr5�resultr$r$r%�decode_cf_timedelta�s

rcr2r1r0r/r.r-cCsFx@dD]8\}}tjd|d�}||}tj||jt�k�r|SqWdS)Nr2�Qr1�r0�<r/r�
�	�ns�r2rd�r1re�r0rf�r/r)rjrkrlrmiʚ;)r�timedelta64rra�int)�unique_timedeltasZ	time_unitrZZ
unit_deltaZdiffsr$r$r%�_infer_time_units_from_diff�srqcCshtjtj|�j�dd�}|tj|�}tjtj|��}t|�}t	|�dkrR|dnd}d|tj
|�fS)aGiven an array of datetimes, returns a CF compatible time-unit string of
    the form "{time_unit} since {date[0]}", where `time_unit` is 'days',
    'hours', 'minutes' or 'seconds' (the first one that can evenly divide all
    unique time deltas in `dates`)
    F)r`rz
1970-01-01z%s since %s)rZto_datetimerrErS�notnull�uniqueZdiffrq�lenrU)rOrpr5Zreference_dater$r$r%�infer_datetime_units�srucCs8tjtj|�j�dd�}tj|tj|��}t|�}|S)z�Given an array of timedeltas, returns a CF compatible time-unit from
    {'days', 'hours', 'minutes' 'seconds'} (the first one that can evenly
    divide all unique time deltas in `deltas`)
    F)r`)rrVrrErSrsrrrq)Zdeltasrpr5r$r$r%�infer_timedelta_units�srvcCsbtj|�}tj|jdd�}xBtj|�D]4\}}t|j|j|j|j	|j
|j�}tj|�||<q&W|S)zlGiven an array of netCDF4.datetime objects, return an array of
    numpy.datetime64 objects of the same sizezM8[ns])r)
rrE�emptyrYZndenumeraterrF�month�day�hour�minute�second�
datetime64)�times�new�i�t�dtr$r$r%rJ�s
rJcCs:t|�\}}yd|t|�f}Wntk
r4YnX|S)Nz%s since %s)rAr	r)r5rZr@r$r$r%�_cleanup_netcdf_time_units�sr�csFddl�tj|jtj�r(|jd�jt�}���fdd�}tj|�|�S)z�Fallback method for encoding dates using netCDF4-python.

    This method is more flexible than xarray's parsing using datetime64[ns]
    arrays but also slower because it loops over each element.
    rNzM8[us]cs|dkrtjS�j|���S)N)rrZdate2num)�d)rMrNr5r$r%�encode_datetime�sz6_encode_datetime_with_netcdf4.<locals>.encode_datetime)rDr�
issubdtyperr}rarZ	vectorize)rOr5rMr�r$)rMrNr5r%�_encode_datetime_with_netcdf4�s
r�cCs$tj|tjd�}||kj�r |}|S)N)r)rrZint64r)�numZint_numr$r$r%�cast_to_int_if_safe�sr�cCs�tj|�}|dkrt|�}nt|�}|dkr0d}t|�\}}y`|tksR|jjdkrVt�|jdksdt	�t
|�}tjd|�jd�}tj
tj|��}|||}Wn$ttfk
r�t|||�}YnXt|�}|||fS)z�Given an array of datetime objects, returns the tuple `(num, units,
    calendar)` suitable for a CF compliant time variable.

    Unlike `date2num`, this function can handle datetime64 arrays.

    See also
    --------
    netCDF4.date2num
    Nr�Ozdatetime64[ns]rztimedelta64[ns])rrErur�rArTr�kindr�AssertionErrorr6rnrar}rrUrWr�r�)rOr5rMrZr@r?Z
time_deltar�r$r$r%�encode_cf_datetimes&


r�cCsR|dkrt|�}t|�}d|tjd|�}tjtj|�tj|�}t|�}||fS)Ng�?r)	rvr6rrn�whererrrr�)Z
timedeltasr5Znp_unitr�r$r$r%�encode_cf_timedelta+sr�c@s>eZdZdZdddefdd�Zedd��Zdd�Zd	d
�Z	dS)�MaskedAndScaledArraya�Wrapper around array-like objects to create a new indexable object where
    values, when accessesed, are automatically scaled and masked according to
    CF conventions for packed and missing data values.

    New values are given by the formula:
        original_values * scale_factor + add_offset

    Values can only be accessed via `__getitem__`:

    >>> x = MaskedAndScaledArray(np.array([-99, -1, 0, 1, 2]), -99, 0.01, 1)
    >>> x
    MaskedAndScaledArray(array([-99, -1,  0,  1,  2]), fill_value=-99,
    scale_factor=0.01, add_offset=1)
    >>> x[:]
    array([  nan,  0.99,  1.  ,  1.01,  1.02]

    References
    ----------
    http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html
    NcCs"||_||_||_||_||_dS)a�
        Parameters
        ----------
        array : array-like
            Original array of values to wrap
        fill_value : number, optional
            All values equal to fill_value in the original array are replaced
            by NaN.
        scale_factor : number, optional
            Multiply entries in the original array by this number.
        add_offset : number, optional
            After applying scale_factor, add this number to entries in the
            original array.
        N)rr r!r"�_dtype)�selfrr r!r"rr$r$r%�__init__Ks
zMaskedAndScaledArray.__init__cCstj|j�S)N)rrr�)r�r$r$r%raszMaskedAndScaledArray.dtypecCst|j||j|j|j|j�S)N)r&rr r!r"r�)r��keyr$r$r%�__getitem__esz MaskedAndScaledArray.__getitem__cCs$dt|�j|j|j|j|j|jfS)Nz?%s(%r, fill_value=%r, scale_factor=%r, add_offset=%r, dtype=%r))�type�__name__rr r!r"r�)r�r$r$r%�__repr__iszMaskedAndScaledArray.__repr__)
r��
__module__�__qualname__�__doc__rRr��propertyrr�r�r$r$r$r%r�6sr�c@s.eZdZdZd	dd�Zedd��Zdd�ZdS)
�DecodedCFDatetimeArrayz�Wrapper around array-like objects to create a new indexable object where
    values, when accessed, are automatically converted into datetime objects
    using decode_cf_datetime.
    Nc
Cs�||_||_||_t|d�pd}yt|||�}WnPtk
r�|dkrLdnd|}d||f}tst|dtj�7}t	|��YnXt
|dtjd��|_
dS)	Nrrzthe default calendarzcalendar %rzYunable to decode time units %r with %s. Try opening your dataset with decode_times=False.z Full traceback:
r�object)rr5rMr
r[�	Exceptionr�	traceback�
format_excr<rrrr�)r�rr5rMZ
example_valuerbZcalendar_msg�msgr$r$r%r�us
zDecodedCFDatetimeArray.__init__cCs|jS)N)r�)r�r$r$r%r�szDecodedCFDatetimeArray.dtypecCst|j||j|jd�S)N)r5rM)r[rr5rM)r�r�r$r$r%r��sz"DecodedCFDatetimeArray.__getitem__)N)r�r�r�r�r�r�rr�r$r$r$r%r�ps
r�c@s,eZdZdZdd�Zedd��Zdd�ZdS)	�DecodedCFTimedeltaArrayz�Wrapper around array-like objects to create a new indexable object where
    values, when accessed, are automatically converted into timedelta objects
    using decode_cf_timedelta.
    cCs||_||_dS)N)rr5)r�rr5r$r$r%r��sz DecodedCFTimedeltaArray.__init__cCs
tjd�S)Nztimedelta64[ns])rr)r�r$r$r%r�szDecodedCFTimedeltaArray.dtypecCst|j||jd�S)N)r5)rcrr5)r�r�r$r$r%r��sz#DecodedCFTimedeltaArray.__getitem__N)r�r�r�r�r�r�rr�r$r$r$r%r��sr�c@sHeZdZdZdd�Zedd��Zedd��Zdd	�Zd
d�Z	dd
�Z
dS)�CharToStringArrayaWrapper around array-like objects to create a new indexable object where
    values, when accessed, are automatically concatenated along the last
    dimension.

    >>> CharToStringArray(np.array(['a', 'b', 'c']))[:]
    array('abc',
          dtype='|S3')
    cCs
||_dS)zx
        Parameters
        ----------
        array : array-like
            Original array of values to wrap.
        N)r)r�rr$r$r%r��szCharToStringArray.__init__cCstjdt|jjd��S)N�Sr���)rr�strrrY)r�r$r$r%r�szCharToStringArray.dtypecCs|jjdd�S)Nrr�)rrY)r�r$r$r%rY�szCharToStringArray.shapecCs(|jdkr|dj�jd�St|�SdS)Nr.zutf-8)rr\�decode�repr)r�r$r$r%�__str__�s
zCharToStringArray.__str__cCsdt|�j|jfS)Nz%s(%r))r�r�r)r�r$r$r%r��szCharToStringArray.__repr__cCsR|jjdkr|j|}n6tj||jj�}|dtd�kr@td��t|j|�}|S)Nrrztoo many indicesr�)rrrZexpanded_indexer�slice�
IndexError�char_to_string)r�r�r#r$r$r%r��szCharToStringArray.__getitem__N)r�r�r�r�r�r�rrYr�r�r�r$r$r$r%r��s	r�c@s,eZdZdZdd�Zedd��Zdd�ZdS)	�NativeEndiannessArraya�Decode arrays on the fly from non-native to native endianness

    This is useful for decoding arrays from netCDF3 files (which are all
    big endian) into native endianness, so they can be used with Cython
    functions, such as those found in bottleneck and pandas.

    >>> x = np.arange(5, dtype='>i2')

    >>> x.dtype
    dtype('>i2')

    >>> NativeEndianArray(x).dtype
    dtype('int16')

    >>> NativeEndianArray(x)[:].dtype
    dtype('int16')
    cCs
||_dS)N)r)r�rr$r$r%r��szNativeEndiannessArray.__init__cCstj|jjjt|jjj��S)N)rrrr�r��itemsize)r�r$r$r%r�szNativeEndiannessArray.dtypecCstj|j||jd�S)N)r)rrErr)r�r�r$r$r%r��sz!NativeEndiannessArray.__getitem__N)r�r�r�r�r�r�rr�r$r$r$r%r��sr�cCsBtj|ddd�}|jj}|d	kr(td��|j|jd
�j|d�S)z=Like netCDF4.stringtochar, but faster and more flexible.
    F�C)r�order�Ur�zargument must be a stringr�1)r�r�)r)rrrr�r<rXrY�view)�arrr�r$r$r%�string_to_char�s
r�cCsDtj|ddd�}|jj}|d
kr(td��|j|t|jd��dS)
z=Like netCDF4.chartostring, but faster and more flexible.
    Fr�)rr�r�r�zargument must be a stringr.r)r�r�r�).r)rrrr�r<r�r�rY)r�r�r$r$r%r��s
r�cCs ||krtd|��|||<dS)Nz)Failed hard to prevent overwriting key %r)r<)�destr��valuer$r$r%�safe_setitemsr�cCs$|j|d�}|dk	r t|||�|S)z�
    A convenience function which pops a key k from source to dest.
    None values are not passed on.  If k already exists in dest an
    error is raised.
    N)�popr�)�sourcer�r��defaultr�r$r$r%�pop_tosr�cCs|j|j|jj�|jj�fS)N)�dims�data�attrsr�encoding)�varr$r$r%�
_var_as_tuplesr�cCsltj|jtj�rht|�\}}}}t||jdd�|jdd��\}}}t|d|�t|d|�t||||�}|S)Nr5rM)	rr�rr}r�r�r�r�r)r�r�r�r�r�r5rMr$r$r%�maybe_encode_datetimes r�cCsTtj|jtj�rPt|�\}}}}t||jdd��\}}t|d|�t||||�}|S)Nr5)	rr�rrnr�r�r�r�r)r�r�r�r�r�r5r$r$r%�maybe_encode_timedelta)sr�Tcs~t�fdd�dD��rvt��\}}}}|jt|d�}d}d|krP|t||d�8}d|krh|t||d�}t||||���|fS)Nc3s|]}|�jkVqdS)N)r�)r8�k)r�r$r%�	<genexpr>4sz0maybe_encode_offset_and_scale.<locals>.<genexpr>r"r!)rrF)r"r!)�anyr�rarRr�r)r��
needs_copyr�r�r�r�r$)r�r%�maybe_encode_offset_and_scale3sr�cCsVd|jkrNt|�\}}}}t||d�}tj|�s@tj||�}d}t||||�}||fS)N�
_FillValueF)r�r�r�rrrZfillnar)r�r�r�r�r�r�r r$r$r%�maybe_encode_fill_value@s

r�cCs�d|jkr�t|�\}}}}tj|jd��}||jkr�|jdkr�tj|tj�r�tj|jtj�rzd|j	krzt
jd|tdd�t
j|�d}|dkr�|jdkr�ttj|d	��}|d
|jd
f}|j|d�}t||||�}|S)Nrr�r�zfsaving variable %s with floating point data as an integer dtype without any _FillValue to use for NaNsrB)rC.ZS1r�zstring%sr)rr�)r�r�rrr�r�r��integerZfloatingr�rGrHrIrZaroundr�rErYrar)r��namer�r�r�r�rr$r$r%�maybe_encode_dtypeLs"


r�cCsZ|jdkrtjt�}n@tj|d|j�j}|jdkrDtj|j�}n|jdkrVtd��|S)z]Given an object array with no missing values, infer its dtype from its
    first element
    rr�r�r�zGunable to infer dtype; xarray cannot serialize arbitrary Python objects)r)r�r�)rrrrRrrr�r<)rrr$r$r%�_infer_dtypeas


r�c	Cs�|jjdkr�t|�\}}}}tj|�}|j�r�||}t|�}|jdkrPd}ntj|t	�sftjt	�}tj
}tj||dd�}|||<n|jt|�d�}t
||||�}|S)	Nr�r�r��T)rr)r)r�r�)rr�r�rrr�r�rr�rRrrrar)	r�r�r�r�r��missingZnon_missing_valuesZinferred_dtyper r$r$r%�ensure_dtype_not_objectss 




r�cCsBt|�}t|�}t||�\}}t||�\}}t||�}t|�}|S)a$
    Converts an Variable into an Variable which follows some
    of the CF conventions:

        - Nans are masked using _FillValue (or the deprecated missing_value)
        - Rescaling via: scale_factor and add_offset
        - datetimes are converted to the CF 'units since time' format
        - dtype encodings are enforced.

    Parameters
    ----------
    var : xarray.Variable
        A variable holding un-encoded data.

    Returns
    -------
    out : xarray.Variable
        A variable which has been encoded as described above.
    )r�r�r�r�r�r�)r�r�r�r$r$r%�encode_cf_variable�s
r�cCst|�}|j}|j}|jj�}|jj�}|j}	|r^|jjdkr^|jjdkr^|dd�}t	|�}|�rJd|kr�d|kr�t
j|d|d�r�tdd��|j
d�|d<tjt||d��}
|
jdkr�tjdjt|
��td	d
�t||d�}t||d�}|
dk	�rtjtj|
���s"|dk	�s"|dk	�rJ|
jjdk�r6t}
nt}
t||
|||
�}|�r�d|k�r�d|dk�r�t||d�}t||d�}t|||�}n$|dtk�r�t||d�}t||�}|�r�|jj �r�t!|�}|j}	d|k�r�|	|dk�r�tjd�n|	|d<t"|t#j$|�||d�S)a�
    Decodes a variable which may hold CF encoded information.

    This includes variables that have been masked and scaled, which
    hold CF style time variables (this is almost always the case if
    the dataset has been serialized) and which have strings encoded
    as character arrays.

    Parameters
    ----------
    var : Variable
        A variable holding potentially CF encoded information.
    concat_characters : bool
        Should character arrays be concatenated to strings, for
        example: ['h', 'e', 'l', 'l', 'o'] -> 'hello'
    mask_and_scale: bool
        Lazily scale (using scale_factor and add_offset) and mask
        (using _FillValue).
    decode_times : bool
        Decode cf times ('hours since 2000-01-01') to np.datetime64.
    decode_endianness : bool
        Decode arrays from non-native to native endianness.

    Returns
    -------
    out : Variable
        A variable holding the decoded equivalent of var
    r�rNZ
missing_valuer�z�Discovered conflicting _FillValue and missing_value.  Considering opening the offending dataset using decode_cf=False, corrected the attributesz>and decoding explicitly using xarray.conventions.decode_cf(ds)zBvariable has multiple fill values {0}, decoding all values to NaN.rB)rCr!r"r�r5ZsincerMrz CF decoding is overwriting dtype)r�r�)r�r�)%r�_datar�r�rr�rr�r�r�rZ
equivalentr<r�rrr�rrGrHrKr�rIr�rrr�rRr�r��
TIME_UNITSr�Zisnativer�rrZLazilyIndexedArray)r��concat_charactersr&�decode_timesZdecode_endiannessr�Z
dimensions�
attributesr�Zoriginal_dtyper r!r"rr5rMr$r$r%�decode_cf_variable�sb






r�csdtt��x.�j�D]"}x|jD]}�|j|�qWqW��fdd�}	t�}
t|t�r^|g}n|dkrjg}t|�}t�}x�t	��D]�\}}||kr�q�|o�|j
jdko�|jdko�|	|jd�}
t
||
||d�||<|r�||j}d|kr�|d}|j�}t�fd	d
�|D��r�|||jd<|d=|
j|�q�W|�rZd|k�rZt|�}|
j|jd�j��|||
fS)zM
    Decode a several CF encoded variables.

    See: decode_cf_variable
    cs@|�krdSx.�|D]"}|jjdks4||jdkrdSqWdS)NFr�rTr�)rr�r�)Zdim�v)�dimensions_used_by�	variablesr$r%�	stackable sz&decode_cf_variables.<locals>.stackableNr�rr)r�r&r��coordinatesc3s|]}|�kVqdS)Nr$)r8r�)r�r$r%r�?sz&decode_cf_variables.<locals>.<genexpr>r�)r�listr#r��append�set�
isinstancerrr
rr�rr�r��splitrr��updater�)r�r�r�r&r��
decode_coords�drop_variablesr�r�r��coord_names�new_varsr��concatZ	var_attrsZ	coord_strZvar_coord_namesr$)r�r�r%�decode_cf_variablessB	

r�c	Cs�ddlm}ddlm}t||�r@|j}|j}	t|j�}
|j	}n*t||�rb|j
�\}}	t�}
|}ntd��t||	|||||d�\}}	}|||	d�}
|
j
|j|
��}
||
_	|
S)a�Decode the given Dataset or Datastore according to CF conventions into
    a new Dataset.

    Parameters
    ----------
    obj : Dataset or DataStore
        Object to decode.
    concat_characters : bool, optional
        Should character arrays be concatenated to strings, for
        example: ['h', 'e', 'l', 'l', 'o'] -> 'hello'
    mask_and_scale: bool, optional
        Lazily scale (using scale_factor and add_offset) and mask
        (using _FillValue).
    decode_times : bool, optional
        Decode cf times (e.g., integers since 'hours since 2000-01-01') to
        np.datetime64.
    decode_coords : bool, optional
        Use the 'coordinates' attribute on variable (or the dataset itself) to
        identify coordinates.
    drop_variables: string or iterable, optional
        A variable or list of variables to exclude from being parsed from the
        dataset.This may be useful to drop variables with problems or
        inconsistent values.

    Returns
    -------
    decoded : Dataset
    r)�Dataset)�AbstractDataStorez,can only decode Dataset or DataStore objects)r�)r�)Zcore.datasetr�Zbackends.commonr�r��
_variablesr�r��coordsZ	_file_obj�load�	TypeErrorr�Z
set_coords�union)�objr�r&r�r�r�r�r��varsr�Zextra_coordsZfile_objr�Zdsr$r$r%�	decode_cfKs&



r�cCst|||||�\}}}||fS)aj
    Decode a set of CF encoded variables and attributes.

    See Also, decode_cf_variable

    Parameters
    ----------
    variables : dict
        A dictionary mapping from variable name to xarray.Variable
    attributes : dict
        A dictionary mapping from attribute name to value
    concat_characters : bool
        Should character arrays be concatenated to strings, for
        example: ['h', 'e', 'l', 'l', 'o'] -> 'hello'
    mask_and_scale: bool
        Lazily scale (using scale_factor and add_offset) and mask
        (using _FillValue).
    decode_times : bool
        Decode cf times ('hours since 2000-01-01') to np.datetime64.

    Returns
    -------
    decoded_variables : dict
        A dictionary mapping from variable name to xarray.Variable objects.
    decoded_attributes : dict
        A dictionary mapping from attribute name to values.
    )r�)r�r�r�r&r��_r$r$r%�
cf_decoder�sr�cs"t|�}|j�}tt�}xn|D]f}||j�xV|j�D]J\}}||kr6||jkr6t�fdd�|jD��r6||j|�|j|�q6WqWtdd�|j�D��}xF|j�D]:\}}	||j	}
d|
kr�t
d|��djtt
|	��|
d<q�W|�rt|�}d|k�rt
d��djtt
|��|d<||fS)Nc3s|]}|�kVqdS)Nr$)r8r�)�target_dimsr$r%r��sz&_encode_coordinates.<locals>.<genexpr>css"|]\}}||jdd�fVqdS)F)�deepN)r)r8r�r�r$r$r%r��sr�zWcannot serialize coordinates because variable %s already has an attribute 'coordinates'� zVcannot serialize coordinates because the global attribute 'coordinates' already exists)r�rrr��itemsr��add�discardrr�r<�join�mapr�)r�r��non_dim_coord_namesZglobal_coordinatesZvariable_coordinatesZ
coord_namer�r��var_namer�r�r$)r�r%�_encode_coordinates�s0



rcCs&t|j�t|j�}t|j|j|d�S)a1Encode coordinates on the given dataset object into variable specific
    and global attributes.

    When possible, this is done according to CF conventions.

    Parameters
    ----------
    dataset : Dataset
        Object to encode.

    Returns
    -------
    variables : dict
    attrs : dict
    )r)r�r�r�rr�r�)Zdatasetrr$r$r%�encode_dataset_coordinates�s
rcCstdd�t|�D��}||fS)a�
    A function which takes a dicts of variables and attributes
    and encodes them to conform to CF conventions as much
    as possible.  This includes masking, scaling, character
    array handling, and CF-time encoding.

    Decode a set of CF encoded variables and attributes.

    See Also, decode_cf_variable

    Parameters
    ----------
    variables : dict
        A dictionary mapping from variable name to xarray.Variable
    attributes : dict
        A dictionary mapping from attribute name to value

    Returns
    -------
    encoded_variables : dict
        A dictionary mapping from variable name to xarray.Variable,
    encoded_attributes : dict
        A dictionary mapping from attribute name to value

    See also: encode_cf_variable
    css"|]\}}|t||d�fVqdS))r�N)r�)r8r�r�r$r$r%r��szcf_encoder.<locals>.<genexpr>)rr
)r�r�r�r$r$r%�
cf_encoder�sr)N)NN)N)N)T)T)N)TN)TTTT)TTTTN)TTTTN)TTT)Lrr:r�rGZnumpyrZpandasr�collectionsrZpandas.tslibr�corerrrZcore.formattingr	r
Z
core.variablerrZ
core.pycompatr
rrrrrr�rTrRr&r6rArQr[r^rc�	frozensetr�rqrurvrJr�r�r�r�r�ZNDArrayMixinr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr$r$r$r%�<module>sz 4
'




'
:$0






d
6
6
!)