
    d8                        d Z ddlZddlZddlmZ ddlmZ ddlZddl	Z
ddlmZmZmZ ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZmZ d
Zd Zd Zd%dZed             Zed             Z ed%d            Z!d Z"e	 d&d            Z#ed'd            Z$d Z%	 d(dZ&d)dedefdZ'dede(fd Z)d!ede(fd"Z*d#eeeef         de(fd$Z+dS )*z)Testing functions exposed to the user API    N)Hashable)Union)duck_array_ops
formattingutils)	DataArray)Dataset)IndexPandasIndexPandasMultiIndexdefault_indexes)IndexVariableVariable)assert_allcloseassert_chunks_equalassert_duckarray_equalassert_duckarray_allcloseassert_equalassert_identicalc                 F     t          j                    fd            }|S )Nc                      d}t          j                    5  d t           j        D             t           _         | i |cd d d            S # 1 swxY w Y   d S )NTc                 *    g | ]}|d          dk    |S )r   error ).0fs     .lib/python3.11/site-packages/xarray/testing.py
<listcomp>z4ensure_warnings.<locals>.wrapper.<locals>.<listcomp>#   s!    OOOaqtw    )warningscatch_warningsfilters)argskwargs__tracebackhide__funcs      r   wrapperz ensure_warnings.<locals>.wrapper   s     $&& 	) 	)OO8+;OOOH4(((		) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   (AAA)	functoolswraps)r&   r'   s   ` r   ensure_warningsr*      s:     _T) ) ) ) ) Nr   c                 r    | j         j        dk    r&t          j        j                            | dd          S | S )NSzutf-8replace)dtypekindnpcoredefchararraydecode)datas    r   _decode_string_datar5   *   s3    z#w#**4)DDDKr   h㈵>:0yE>Tc                    t          d | |fD                       r |rt          |           } t          |          }g dt          fd| |fD                       rt          j        | |          S t          j        | |||          S )Nc              3   6   K   | ]}|j         j        d k    V  dS )r,   Nr.   r/   )r   arrs     r   	<genexpr>z*_data_allclose_or_equiv.<locals>.<genexpr>1   s+      
9
9S39>S 
9
9
9
9
9
9r   )MmOr,   Uc              3   4   K   | ]}|j         j        v V  d S Nr:   )r   r;   exact_dtypess     r   r<   z*_data_allclose_or_equiv.<locals>.<genexpr>5   s,      
B
Bc39>\)
B
B
B
B
B
Br   rtolatol)anyr5   r   array_equivallclose_or_equiv)arr1arr2rE   rF   decode_bytesrC   s        @r   _data_allclose_or_equivrM   0   s    

9
9T4L
9
9
999 )l )"4(("4((,,,L

B
B
B
BdD\
B
B
BBB R)$555/dDQQQQr   c                    d}t          |           t          |          k    sJ t          | t          t          f          r5|                     |          sJ t          j        | |d                      dS t          | t                    r5|                     |          sJ t          j        | |d                      dS t          t          |            d          )a  Like :py:func:`numpy.testing.assert_array_equal`, but for xarray
    objects.

    Raises an AssertionError if two objects are not equal. This will match
    data values, dimensions and coordinates, but not names or attributes
    (except for Dataset objects for which the variable names must match).
    Arrays with NaN in the same location are considered equal.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray or xarray.Variable
        The first object to compare.
    b : xarray.Dataset, xarray.DataArray or xarray.Variable
        The second object to compare.

    See Also
    --------
    assert_identical, assert_allclose, Dataset.equals, DataArray.equals
    numpy.testing.assert_array_equal
    Tequals& not supported by assertion comparisonN)
type
isinstancer   r   rO   r   diff_array_reprr	   diff_dataset_repr	TypeErrorabr%   s      r   r   r   ;   s    , 77d1gg!h	*++ Lxx{{FFJ6q!XFFFFFFF	Aw		 Lxx{{HHJ8AxHHHHHHH477JJJKKKr   c                 v   d}t          |           t          |          k    sJ t          | t                    r5|                     |          sJ t	          j        | |d                      dS t          | t                    rG| j        |j        k    sJ |                     |          sJ t	          j        | |d                      dS t          | t          t          f          r5|                     |          sJ t	          j	        | |d                      dS t          t          |            d          )a  Like :py:func:`xarray.testing.assert_equal`, but also matches the
    objects' names and attributes.

    Raises an AssertionError if two objects are not identical.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray or xarray.Variable
        The first object to compare.
    b : xarray.Dataset, xarray.DataArray or xarray.Variable
        The second object to compare.

    See Also
    --------
    assert_equal, assert_allclose, Dataset.equals, DataArray.equals
    T	identicalrP   N)rQ   rR   r   rZ   r   rS   r   namer	   rT   rU   rV   s      r   r   r   [   s0   $ 77d1gg!X L{{1~~LLz9!QLLLLLLL	Ay	!	! Lv{{1~~LLz9!QLLLLLLL	A*	+	+ L{{1~~NNz;Aq+NNNNNNN477JJJKKKr   c                    d}t          |           t          |          k    sJ t          j        t          |||          d_        fd}t          | t                    r/ || |          }|sJ t          j        | |                      dS t          | t                    rZt          j        | j        |j        |          o || j        |j                  }|sJ t          j        | |                      dS t          | t                    rT| j        |j        k    o t          j        | j        |j        |          }|sJ t          j        | |                      dS t%          t          |            d          )a-  Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects.

    Raises an AssertionError if two objects are not equal up to desired
    tolerance.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray or xarray.Variable
        The first object to compare.
    b : xarray.Dataset, xarray.DataArray or xarray.Variable
        The second object to compare.
    rtol : float, optional
        Relative tolerance.
    atol : float, optional
        Absolute tolerance.
    decode_bytes : bool, optional
        Whether byte dtypes should be decoded to strings as UTF-8 or not.
        This is useful for testing serialization methods on Python 3 that
        return saved strings as bytes.

    See Also
    --------
    assert_identical, assert_equal, numpy.testing.assert_allclose
    T)rE   rF   rL   allclosec                     t          | d|           } t          |d|          }| j        |j        k    o#| j        |j        u p | j        |j                  S )Nvariable)getattrdims_datar4   )rW   rX   equivs     r   compat_variablez(assert_allclose.<locals>.compat_variable   sW    Az1%%Az1%%vQQW%7%P55;P;PQr   )compatrP   N)rQ   r(   partialrM   __name__rR   r   r   rS   r   r   
dict_equivcoordsr_   r	   _coord_names	variablesrT   rU   )	rW   rX   rE   rF   rL   r%   rd   r]   rc   s	           @r   r   r   z   s   4 77d1ggdL  E  ENR R R R R !X L"?1a((GG3AqGGGGGGGG	Ay	!	! L#Hah
 
 
 6oaj!*55 	 GG3AqGGGGGGGG	Aw		 L>Q^3 
8HK_9
 9
 9
 II5a5IIIIIIII477JJJKKKr   c                 (   | |z
  }t          t          |                    }d}t          j        |          }|j        }| d| }	t          ||z  dz            }
d|d|	 d|
ddd	| d
| g}|r|d| d|gz  }d                    |          S )Nznot implementedz / d   zArrays are not equalzMismatched elements: z (z.0fz%)zMax absolute difference: zMax relative difference: z x: z y: 
)maxabsr0   count_nonzerosizefloatjoin)xyerr_msgverbosediffabs_diffrel_diffn_diffn_totalfraction
percentagepartss               r   _format_messager      s    q5D3t99~~H Hd##FiG&&W&&Hv'#-..J 	>>>J>>>>.H...H..E  
1LL1LL
 	

 99Ur   Hz> c                 t    d}t          j        | |||          }|sJ t          | |||                      dS )z6Like `np.testing.assert_allclose`, but for duckarrays.TrD   rw   rx   N)r   rI   r   )actualdesiredrE   rF   rw   rx   r%   r]   s           r   r   r      sO    
 /dQUVVVHWW_VWgwWWWWWWWWr   c                 0   d}t          j        |           s(t          j        |           st          j        |           } t          j        |          s(t          j        |          st          j        |          }t          j        |           rt          j        |          s(t          j        |           r-t          j        |          r| |k                                    }nt          j        | |          }|sJ t          | |||                      dS )z8Like `np.testing.assert_array_equal`, but for duckarraysTr   N)	r   is_duck_array	is_scalarr0   asarrayallr   rH   r   )ru   rv   rw   rx   r%   rc   s         r   r   r      s    q!! %/!*<*< JqMMq!! %/!*<*< JqMMA 15?1#5#5 11$21551 a*1a00II/!QIIIIIIIIr   c                     t          | t                    t          |t                    k    rt          d          |                                 }|                                }|j        |j        k    sJ dS )a  
    Assert that chunksizes along chunked dimensions are equal.

    Parameters
    ----------
    a : xarray.Dataset or xarray.DataArray
        The first object to compare.
    b : xarray.Dataset or xarray.DataArray
        The second object to compare.
    za and b have mismatched typesN)rR   r   rU   unify_chunkschunks)rW   rX   leftrights       r   r   r      sk     !Y:a#;#;;;7888>>DNNE;%,&&&&&&r   c                   
 t          | t                    s
J |             t          d |                                 D                       s&J d |                                 D                         d |                                D             }|                                 |k    sJ t          |           |f            |                                 D ]\  }}t          |t                    r|j        }||         }|j	        f|j
        k    sJ ||f            ||j	        k    r&|j        |j        k    sJ |j        |j        f            t          |j        j        t          j                  sJ |j        j                    |                    |j        j                  sJ ||f            t          |t$                    r |j        }|j        j        D ]}	|	|v sJ ||f            ||	         }|j	        f|j
        k    sJ ||f            |j        |	         |j        k    sJ |j        |	         |j        f            t          |j        j        t          j                  sJ |j        j                    |                    |j        j                  sJ ||f            |	| v sJ |	t          |           f            || |	         u sJ || |	         j        f            |rt-          ||          
|                                 
                                k    s&J t          |           t          
          f            t          
fd|                                 D                       sJ | 
f            d S d S )Nc              3   @   K   | ]}t          |t                    V  d S rB   )rR   r
   r   vs     r   r<   z4_assert_indexes_invariants_checks.<locals>.<genexpr>  s,      >>z!U##>>>>>>r   c                 4    i | ]\  }}|t          |          S r   rQ   r   kr   s      r   
<dictcomp>z5_assert_indexes_invariants_checks.<locals>.<dictcomp>  s7     A A Aq!477A A Ar   c                 B    h | ]\  }}t          |t                    |S r   )rR   r   r   s      r   	<setcomp>z4_assert_indexes_invariants_checks.<locals>.<setcomp>	  s;       a*Q:V:V	  r   c              3   T   K   | ]"\  }}|                     |                   V  #d S rB   )rO   )r   r   r   defaultss      r   r<   z4_assert_indexes_invariants_checks.<locals>.<genexpr>.  s7      EETQ188HQK((EEEEEEr   )rR   dictr   valuesitemskeyssetr   indexdimra   coord_dtyper.   rb   arraypdr
   rO   r   nameslevel_coords_dtype
MultiIndexr   )indexespossible_coord_variablesra   check_default
index_varsr   r   pd_indexvarr[   r   s             @r   !_assert_indexes_invariants_checksr     s    gt$$--g--->>W^^-=-=>>>>>   A A&}}A A A    .4466  J <<>>Z'''#g,,
)C''' MMOO O O5e[)) 		E{H*1-CI<38+++h_+++EI~~(CI5558I397U555ciorx88II#)/III??39?33DDh_DDDe-.. 	O{H) O O7777(J9O777.t4	|sx///(C////5BBB,T2IEBBB "#)/2=AARR39?RRRsy77HH(CHHHws7||(<---'$-:M/N---- 
"#;TBB||~~0003w<<X2O000EEEEW]]__EEEEE 	
 	
H
 	
 	
 	

 
	
 	
r   r   r[   c                    |d}n|f}t          | j        t                    sJ || j        fz               t          | j                  t          | j        j                  k    sJ || j        | j        j        fz               t          | j        t          d           t          f          sJ || j        fz               t          | j	        t          d           t          f          sJ || j	        fz               d S )Nr   )
rR   _dimstuplelenrb   shape	_encodingrQ   r   _attrs)r   r[   name_or_emptys      r   _assert_variable_invariantsr   4  s   |!ci''EE#))EEEEsy>>S11111=		D 4111 cmd4jj$%788  -K ;    cj4::t"455TT}
}7TTTTTTr   dacheck_default_indexesc                     t           j        t                    sJ  j                    t           j                   t           j        t
                    sJ  j                    t          d  j                                        D                       sJ  j                    t           fd j                                        D                       s2J  j        d  j        	                                D             f            t          d  j        	                                D                       s+J d  j        	                                D                          j        	                                D ]\  }}t          ||            j
        $t           j
         j         j        |           d S d S )Nc              3   @   K   | ]}t          |t                    V  d S rB   rR   r   r   s     r   r<   z/_assert_dataarray_invariants.<locals>.<genexpr>I  s,      DD1z!X&&DDDDDDr   c              3   l   K   | ].}t          |j                  t          j                  k    V  /d S rB   )r   ra   )r   r   r   s     r   r<   z/_assert_dataarray_invariants.<locals>.<genexpr>J  s8      HHqs16{{c"'ll*HHHHHHr   c                 $    i | ]\  }}||j         S r   )ra   r   s      r   r   z0_assert_dataarray_invariants.<locals>.<dictcomp>L  s     222tq!AF222r   c              3   ^   K   | ](\  }}|j         |fk    t          |t                    V  )d S rB   ra   rR   r   r   s      r   r<   z/_assert_dataarray_invariants.<locals>.<genexpr>N  sC        )/!QTUSW
1m$$ r   c                 4    i | ]\  }}|t          |          S r   r   r   s      r   r   z0_assert_dataarray_invariants.<locals>.<dictcomp>P  s$    222tq!477222r   r   )rR   	_variabler   r   _coordsr   r   r   ra   r   _indexesr   )r   r   r   r   s   `   r   _assert_dataarray_invariantsr   D  s   blH--;;r|;;;---bj$''33333DD
0A0A0C0CDDDDDPPbjPPPHHHHBJ4E4E4G4GHHHHH  
22rz//11222K      35:3C3C3E3E     3 322rz//112223 3 3 
  "" * *1#Aq))))	{)KRW<Q	
 	
 	
 	
 	
 	
 r   dsc                 6    t           j        t                    sJ t           j                              t	          d  j                                        D                       sJ  j                     j                                        D ]\  }}t          ||           t           j        t                    sJ  j                     j         j        
                                k    s#J  j        t           j                  f            t           j                  t          u sJ  j                    t	          d  j                                        D                       sJ  j                    t                      } j                                        D ]}|                    |j                    j        
                                |k    sJ t           j                  |f            t	           fd j                                        D                       s2J  j        d  j                                        D             f            t	          d  j                                        D                       s+J d  j                                        D                         t	           fd j                                        D                       s-J  fd j                                        D                          j        "t           j         j         j        |	           t           j        t          d           t          f          sJ t           j        t          d           t          f          sJ d S )
Nc              3   @   K   | ]}t          |t                    V  d S rB   r   r   s     r   r<   z-_assert_dataset_invariants.<locals>.<genexpr>\  s,      GG1z!X&&GGGGGGr   c              3   @   K   | ]}t          |t                    V  d S rB   )rR   intr   s     r   r<   z-_assert_dataset_invariants.<locals>.<genexpr>g  s,      ==az!S!!======r   c              3   d   K   | ]*}|j         D ] }j        |         |j         |         k    V  !+d S rB   )sizesr   )r   r   r   r   s      r   r<   z-_assert_dataset_invariants.<locals>.<genexpr>l  s[        &'17 FGqwqz!      r   c                 $    i | ]\  }}||j         S r   )r   r   s      r   r   z._assert_dataset_invariants.<locals>.<dictcomp>n  s     @@@$!Q1ag@@@r   c              3   ^   K   | ](\  }}|j         |fk    t          |t                    V  )d S rB   r   r   s      r   r<   z-_assert_dataset_invariants.<locals>.<genexpr>o  sF        Q6aT>> 	1m$$>>> r   c                 L    i | ]!\  }}|j         |fk    |t          |          "S r   )ra   rQ   r   s      r   r   z._assert_dataset_invariants.<locals>.<dictcomp>s  s-    GGGtq!1$477r   c              3   H   K   | ]\  }}|j         v |j        |fk    V  d S rB   r   ra   r   r   r   r   s      r   r<   z-_assert_dataset_invariants.<locals>.<genexpr>t  s5      SS&1aQ"(]]qv!~]]]]SSr   c                 8    i | ]\  }}|j         v ||j        S r   r   r   s      r   r   z._assert_dataset_invariants.<locals>.<dictcomp>t  s2     V V VaqBH}}16}}}r   r   )rR   
_variablesr   rQ   r   r   r   r   rj   r   r   r   updatera   r   r   r   r   )r   r   r   r   var_dimss   `    r   _assert_dataset_invariantsr   Z  s   bmT**??D,?,????GG0D0D0F0FGGGGGVVVVV##%% * *1#Aq))))bos++<<R_<<<?bm0022222
BM5222
 >>T!!!28!!!==28??+<+<=====GGrxGGG!eeH]!!##    8==??h&&&RX(A&&&    +-=+?+?+A+A     B B
@@"-*=*=*?*?@@@AB B B   m))++     H H HGr}2244GGG	H H H
 SSSSBM,?,?,A,ASSSSS   V V V V m1133V V V    
{)K@U	
 	
 	
 	
 blT$ZZ$677777bi$t**d!34444444r   
xarray_objc                 L   t          | t                    rt          |            dS t          | t                    rt	          | |           dS t          | t
                    rt          | |           dS t          d                    t          |                               )zValidate that an xarray object satisfies its own internal invariants.

    This exists for the benefit of xarray's own test suite, but may be useful
    in external projects if they (ill-advisedly) create objects using xarray's
    private APIs.
    )r   z6{} is not a supported type for xarray invariant checksN)
rR   r   r   r   r   r	   r   rU   formatrQ   )r   r   s     r   _assert_internal_invariantsr     s     *h'' 
#J/////	J		*	* 
$.C	
 	
 	
 	
 	
 	
 
J	(	( 	
".C	
 	
 	
 	
 	
 	
 DKKZ   
 
 	
r   )r6   r7   T)r   r   r   T)r   T)TrB   ),__doc__r(   r    collections.abcr   typingr   numpyr0   pandasr   xarray.corer   r   r   xarray.core.dataarrayr   xarray.core.datasetr	   xarray.core.indexesr
   r   r   r   xarray.core.variabler   r   __all__r*   r5   rM   r   r   r   r   r   r   r   r   r   boolr   r   r   r   r   r   <module>r      s   / /      $ $ $ $ $ $               9 9 9 9 9 9 9 9 9 9 + + + + + + ' ' ' ' ' ' U U U U U U U U U U U U 8 8 8 8 8 8 8 8     R R R R L L L> L L L< 5L 5L 5L 5Lp  6 =AX X X X J J J J&' ' '* <@0
 0
 0
 0
fU UX UX U U U U 
Y 
t 
 
 
 
,$57 $54 $5 $5 $5 $5N
i(23
LP
 
 
 
 
 
r   