U
    Cvf&                  
   @  s~  d Z ddlmZ ddlZddlZddlmZmZ zddl	Z
dZW n ek
r\   eZ
dZY nX ddd	d
ddddddg
ZddgZddgZddddddddgZddgZdZd Zd!Zd"ZdId$d%Zejfd&d'Zd(d) Zd*d+ Zd,d- ZdJd.d/Zd0d1 Zd2d3 Zd4d5 Zd6d7 Z d8d9 eD Z!d:d; Z"ed<Z#ed=Z$ed>Z%eej&d?d@Z'dAdB Z(G dCdD dDZ)G dEdF dFZ*G dGdH dHZ+dS )KzDefine core operations for xarray objects.

TODO(shoyer): rewrite this module, making use of xarray.core.computation,
NumPy's __array_ufunc__ and mixin classes instead of the unintuitive "inject"
functions.
    )annotationsN)dtypesduck_array_opsTFaddsubmultruedivfloordivmodpowandxororitemZsearchsortedallanymaxminZmeanprodsumZstdvarZmedianZcumsumZcumproda  Apply `{name}` along some dimension of {cls}.

Parameters
----------
{extra_args}
skipna : bool, optional
    If True, skip missing values (as marked by NaN). By default, only
    skips missing values for float dtypes; other dtypes either do not
    have a sentinel missing value (int) or skipna=True has not been
    implemented (object, datetime64 or timedelta64).
keep_attrs : bool, optional
    If True, the attributes (`attrs`) will be copied from the original
    object to the new one.  If False (default), the new object will be
    returned without attributes.
**kwargs : dict
    Additional keyword arguments passed on to `{name}`.

Returns
-------
cumvalue : {cls}
    New {cls} object with `{name}` applied to its data along the
    indicated dimension.
ai  Reduce this {cls}'s data by applying `{name}` along some dimension(s).

Parameters
----------
{extra_args}{skip_na_docs}{min_count_docs}
keep_attrs : bool, optional
    If True, the attributes (`attrs`) will be copied from the original
    object to the new one.  If False (default), the new object will be
    returned without attributes.
**kwargs : dict
    Additional keyword arguments passed on to the appropriate array
    function for calculating `{name}` on this object's data.

Returns
-------
reduced : {cls}
    New {cls} object with `{name}` applied to its data and the
    indicated dimension(s) removed.
a  
skipna : bool, optional
    If True, skip missing values (as marked by NaN). By default, only
    skips missing values for float dtypes; other dtypes either do not
    have a sentinel missing value (int) or skipna=True has not been
    implemented (object, datetime64 or timedelta64).a  
min_count : int, default: None
    The required number of valid values to perform the operation. If
    fewer than min_count non-NA values are present the result will be
    NA. Only used if skipna is set to True or defaults to True for the
    array's dtype. New in version 0.10.8: Added with the default being
    None. Changed in version 0.17.0: if specified on an integer array
    and skipna=True, the result will be a float array.leftc              
   C  s(   ddl m} |tj| ||d|tjddS )a   Fill missing values in this object with data from the other object.
    Follows normal broadcasting and alignment rules.

    Parameters
    ----------
    join : {"outer", "inner", "left", "right"}, optional
        Method for joining the indexes of the passed objects along each
        dimension
        - "outer": use the union of object indexes
        - "inner": use the intersection of object indexes
        - "left": use indexes from the first object with each dimension
        - "right": use indexes from the last object with each dimension
        - "exact": raise `ValueError` instead of aligning when indexes to be
          aligned are not equal
    dataset_join : {"outer", "inner", "left", "right"}, optional
        Method for joining variables of Dataset objects with mismatched
        data variables.
        - "outer": take variables from both Dataset objects
        - "inner": take only overlapped variables
        - "left": take only variables from the first object
        - "right": take only variables from the last object
    r   apply_ufuncallowedT)joindaskdataset_joinZdataset_fill_value
keep_attrs)xarray.core.computationr   r   fillnanpnan)dataotherr   r   r    r%   3/tmp/pip-unpacked-wheel-h316xyqg/xarray/core/ops.pyr    y   s    r    c              
   C  s8   ddl m} |tjkrdnd}|tj| ||||dddS )a  Return elements from `self` or `other` depending on `cond`.

    Parameters
    ----------
    cond : DataArray or Dataset with boolean dtype
        Locations at which to preserve this objects values.
    other : scalar, DataArray or Dataset, optional
        Value to use for locations in this object where ``cond`` is False.
        By default, inserts missing values.

    Returns
    -------
    Same type as caller.
    r   r   innerexactr   T)r   r   r   r   )r   r   r   NAr   where_method)selfZcondr$   r   r   r%   r%   r&   r*      s    r*   c                 C  s\   zt | |}W n> tk
rL   tj| |d t| drFtj| j|d  Y nX |||S d S )N)	func_namer#   )getattrAttributeErrorr   Zfail_on_dask_array_inputhasattrr#   )argnameargskwargsmethodr%   r%   r&   _call_possibly_missing_method   s    
r5   c                   s&    fdd} |_ ttj j|_|S )Nc                   s   t | j ||S N)r5   r#   r+   r2   r3   r1   r%   r&   func   s    z$_values_method_wrapper.<locals>.func__name__r-   r!   Zndarray__doc__r1   r9   r%   r8   r&   _values_method_wrapper   s    r>   c                   s&    fdd} |_ ttj j|_|S )Nc                   s   t |  ||S r6   )r5   r7   r8   r%   r&   r9      s    z_method_wrapper.<locals>.funcr:   r=   r%   r8   r&   _method_wrapper   s    r?   c                   s.   d kr j  fdd}|_  j|_|S )Nc                   s<   zt | ||W S  tk
r6    | f|| Y S X d S r6   )r-   r.   r7   fr1   r%   r&   r9      s    z(_func_slash_method_wrapper.<locals>.func)r;   r<   )rA   r1   r9   r%   r@   r&   _func_slash_method_wrapper   s    rB   c           
      C  s   dd t D dd tD  dtjdfg }|D ]|\}}}t|dd}t|dd}|rXtnd}|rdtnd}| |||}	||	_t	j
|| j| jj
|d	||d
|	_t| ||	 q.d S )Nc                 S  s"   g | ]}|t td | dfqS )Zarray_Fr-   r   .0r1   r%   r%   r&   
<listcomp>   s   z)inject_reduce_methods.<locals>.<listcomp>c                 S  s   g | ]}|t t|d fqS TrC   rD   r%   r%   r&   rF      s     countFnumeric_onlyavailable_min_count r8   )r1   cls
extra_argsskip_na_docsmin_count_docs)REDUCE_METHODSNAN_REDUCE_METHODSr   rH   r-   _SKIPNA_DOCSTRING_MINCOUNT_DOCSTRING_reduce_methodr;   _REDUCE_DOCSTRING_TEMPLATEformatZ_reduce_extra_args_docstringr<   setattr)
rL   methodsr1   rA   include_skipnarI   rJ   rN   rO   r9   r%   r%   r&   inject_reduce_methods   s.    rZ   c                 C  sl   dd t D }|D ]T\}}}t|dd}| |||}||_tj|| j| jj|dd|_t| || qd S )Nc                 S  s   g | ]}|t t|d fqS rG   rC   rD   r%   r%   r&   rF   	  s     z&inject_cum_methods.<locals>.<listcomp>rI   Fr8   )r1   rL   rM   )	NAN_CUM_METHODSr-   rT   r;   _CUM_DOCSTRING_TEMPLATErV   Z_cum_extra_args_docstringr<   rW   )rL   rX   r1   rA   rY   rI   r9   r%   r%   r&   inject_cum_methods  s    r]   c                 C  s   d|  dS )N__r%   r8   r%   r%   r&   op_str  s    r_   c                 C  s   t tt| S r6   )r-   operatorr_   r8   r%   r%   r&   get_op  s    ra   c                 C  s   i | ]}t d | t |qS )i)ra   rD   r%   r%   r&   
<dictcomp>  s     
 rc   c                 C  s   t |  S r6   )NON_INPLACE_OP)rA   r%   r%   r&   inplace_to_noninplace_op!  s    re   argsortconj	conjugateroundr8   c                 C  s   t D ]}t| |t| qd S r6   )NUMPY_SAME_METHODSrW   r>   )rL   r1   r%   r%   r&   inject_numpy_same,  s    rk   c                      s    e Zd ZdZ fddZ  ZS )IncludeReduceMethodsr%   c                   s&   t  jf | t| dd r"t|  d S NrT   )super__init_subclass__r-   rZ   rL   r3   	__class__r%   r&   ro   6  s    z&IncludeReduceMethods.__init_subclass__r;   
__module____qualname__	__slots__ro   __classcell__r%   r%   rq   r&   rl   3  s   rl   c                      s    e Zd ZdZ fddZ  ZS )IncludeCumMethodsr%   c                   s&   t  jf | t| dd r"t|  d S rm   )rn   ro   r-   r]   rp   rq   r%   r&   ro   @  s    z#IncludeCumMethods.__init_subclass__rs   r%   r%   rq   r&   rx   =  s   rx   c                      s    e Zd ZdZ fddZ  ZS )IncludeNumpySameMethodsr%   c                   s   t  jf | t|  d S r6   )rn   ro   rk   rp   rq   r%   r&   ro   J  s    z)IncludeNumpySameMethods.__init_subclass__rs   r%   r%   rq   r&   ry   G  s   ry   )r   r   )N),r<   
__future__r   r`   Znumpyr!   Zxarray.corer   r   Z
bottleneckZbnZhas_bottleneckImportErrorZNUM_BINARY_OPSrj   rP   rQ   r[   r\   rU   rR   rS   r    r)   r*   r5   r>   r?   rB   rZ   r]   r_   ra   rd   re   rf   rg   rh   ZaroundZround_rk   rl   rx   ry   r%   r%   r%   r&   <module>   sr   



%		


