
    d&                       d Z ddlmZ ddlZddlZddlmZmZ 	 ddl	Z
dZn# e$ r eZ
dZY nw xY wg dZdd	gZd
dgZg dZddgZdZdZdZdZd,dZej        fdZd Zd Zd Zd-dZd Zd Zd Zd Z d eD             Z!d Z" ed           Z# ed!          Z$ ed"          Z% eej&        d#$          Z'd% Z( G d& d'          Z) G d( d)          Z* G d* d+          Z+dS ).zDefine 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lshiftrshiftitemsearchsortedallany)maxminmeanprodsumstdvarmediancumsum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           
     \    ddl m}  |t          j        | ||d|t          j        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_joindataset_fill_value
keep_attrs)xarray.core.computationr#   r   fillnanpnan)dataotherr%   r'   r#   s        /lib/python3.11/site-packages/xarray/core/ops.pyr+   r+   {   sL    . 433333;!6	 	 	 	    c           
     l    ddl m} |t          j        u rdnd} |t          j        | ||||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)selfcondr/   r#   r%   s        r0   r6   r6      sa     433333 vy((77gD;#	 	 	 	r1   c                    	 t          | |          } ||i |S # t          $ rC t          j        | |           t	          | d          rt          j        | j        |            w xY w)N)	func_namer.   )getattrAttributeErrorr   fail_on_dask_array_inputhasattrr.   )argnameargskwargsmethods        r0   _call_possibly_missing_methodrD      s    'd## vt&v&&&    /tDDDD3 	N3CHMMMM	s
    AA'c                h      fd} |_         t          t          j                   j        |_        |S )Nc                2    t          | j        ||          S N)rD   r.   r7   rA   rB   r@   s      r0   funcz$_values_method_wrapper.<locals>.func   s    ,TYdFKKKr1   __name__r;   r,   ndarray__doc__r@   rI   s   ` r0   _values_method_wrapperrO      sE    L L L L L DM2:t,,4DLKr1   c                h      fd} |_         t          t          j                   j        |_        |S )Nc                (    t          | ||          S rG   )rD   rH   s      r0   rI   z_method_wrapper.<locals>.func   s    ,T4vFFFr1   rJ   rN   s   ` r0   _method_wrapperrR      sE    G G G G G DM2:t,,4DLKr1   c                N      j          fd}|_          j        |_        |S )Nc                j    	  t          |           |i |S # t          $ r  | g|R i |cY S w xY wrG   )r;   r<   )r7   rA   rB   fr@   s      r0   rI   z(_func_slash_method_wrapper.<locals>.func   sh    	,&74&&7777 	, 	, 	,1T+D+++F+++++	,s    22)rK   rM   )rU   r@   rI   s   `` r0   _func_slash_method_wrapperrV      sG     |z, , , , , , DM9DLKr1   c                   d t           D             d t          D             z   dt          j        dfgz   }|D ]\  }}}t	          |dd          }t	          |dd          }|rt
          nd}|rt          nd}|                     |||          }	||	_        t          
                    || j        | j        
                    |          ||	          |	_        t          | ||	           d S )
Nc                D    g | ]}|t          t          d |           dfS )array_Fr;   r   .0r@   s     r0   
<listcomp>z)inject_reduce_methods.<locals>.<listcomp>   s?     	
 	
 	
 7>?D??;;UC	
 	
 	
r1   c                >    g | ]}|t          t          |          d fS TrZ   r[   s     r0   r]   z)inject_reduce_methods.<locals>.<listcomp>   s)    
V
V
V4D'.$//6
V
V
Vr1   countFnumeric_onlyavailable_min_count r@   )r@   cls
extra_argsskip_na_docsmin_count_docs)REDUCE_METHODSNAN_REDUCE_METHODSr   r`   r;   _SKIPNA_DOCSTRING_MINCOUNT_DOCSTRING_reduce_methodrK   _REDUCE_DOCSTRING_TEMPLATEformat_reduce_extra_args_docstringrM   setattr)
re   methodsr@   rU   include_skipnara   rb   rg   rh   rI   s
             r0   inject_reduce_methodsrt      s1   	
 	
&	
 	
 	
 W
VCU
V
V
V		W
 ^)51
2	3  $+ ! !aq.%88%a)>FF,:B((0CK,,!!!^\BB1887>>D>II%) 9 
 
 	T4    ! !r1   c                :   d t           D             }|D ]\  }}}t          |dd          }|                     |||          }||_        t                              || j        | j                            |                    |_        t          | ||           d S )Nc                >    g | ]}|t          t          |          d fS r_   rZ   r[   s     r0   r]   z&inject_cum_methods.<locals>.<listcomp>  s)    WWWtgnd33T:WWWr1   ra   Frd   )r@   re   rf   )	NAN_CUM_METHODSr;   rm   rK   _CUM_DOCSTRING_TEMPLATEro   _cum_extra_args_docstringrM   rq   )re   rr   r@   rU   rs   ra   rI   s          r0   inject_cum_methodsrz   
  s    WWWWWG#* 	! 	!aq.%88!!!^\BB.554;;;FF 6 
 

 	T4    	! 	!r1   c                    d|  dS )N__ rd   s    r0   op_strr~     s    ===r1   c                F    t          t          t          |                     S rG   )r;   operatorr~   rd   s    r0   get_opr     s    8VD\\***r1   c                N    i | ]"}t          d |z             t          |          #S )i)r   r[   s     r0   
<dictcomp>r      s,    NNNt&t$$fTllNNNr1   c                    t           |          S rG   )NON_INPLACE_OP)rU   s    r0   inplace_to_noninplace_opr   #  s    !r1   argsortconj	conjugateroundrd   c                V    t           D ] }t          | |t          |                     !d S rG   )NUMPY_SAME_METHODSrq   rO   )re   r@   s     r0   inject_numpy_samer   .  s;     # 9 9T1$7788889 9r1   c                  "     e Zd ZdZ fdZ xZS )IncludeReduceMethodsr}   c                ~     t                      j        di | t          | dd           rt          |            d S d S Nrm   r}   )super__init_subclass__r;   rt   re   rB   	__class__s     r0   r   z&IncludeReduceMethods.__init_subclass__8  sS    !!++F+++3($// 	'!#&&&&&	' 	'r1   rK   
__module____qualname__	__slots__r   __classcell__r   s   @r0   r   r   5  s=        I' ' ' ' ' ' ' ' 'r1   r   c                  "     e Zd ZdZ fdZ xZS )IncludeCumMethodsr}   c                ~     t                      j        di | t          | dd           rt          |            d S d S r   )r   r   r;   rz   r   s     r0   r   z#IncludeCumMethods.__init_subclass__B  sS    !!++F+++3($// 	$s#####	$ 	$r1   r   r   s   @r0   r   r   ?  s=        I$ $ $ $ $ $ $ $ $r1   r   c                  "     e Zd ZdZ fdZ xZS )IncludeNumpySameMethodsr}   c                X     t                      j        di | t          |            d S )Nr}   )r   r   r   r   s     r0   r   z)IncludeNumpySameMethods.__init_subclass__L  s4    !!++F+++#r1   r   r   s   @r0   r   r   I  s=        I        r1   r   )r    r    rG   ),rM   
__future__r   r   numpyr,   xarray.corer   r   
bottleneckbnhas_bottleneckImportErrorNUM_BINARY_OPSr   ri   rj   rw   rx   rn   rk   rl   r+   r5   r6   rD   rO   rR   rV   rt   rz   r~   r   r   r   r   r   r   aroundround_r   r   r   r   r}   r1   r0   <module>r      s    # " " " " "      . . . . . . . .NN   	BNNN  " n-  	 	 	  Y' 2 *8 : " " " "J $*9    >	' 	' 	'       $! ! !6! ! !  + + + ON~NNN  
 /)
$
$vOK((		#	#N$9	H	H	H9 9 9' ' ' ' ' ' ' '$ $ $ $ $ $ $ $         s   ! 	--