
    >qj[L                        d Z ddlZddlmZmZ ddlZddlZddlZddl	m
Z
 ddlmZ ddlmZmZ ddlZddlmZmZmZ ddlZddlmZ dd	lmZ  e ee      d
      ZdZdZej<                  j>                  Z ed        Z!ed        Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d  Z4d! Z5d" Z6d# Z7d$ Z8d% Z9d& Z:d' Z;d( Z<d) Z=y)*z Tests for netcdf     N)joindirname)BytesIO)glob)chdircontextmanager)assert_assert_allcloseassert_equal)raises)netcdf_filedata   bc               #      K   t        j                         5 } t        |       5  |  ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY ww)a   Create, return, and change directory to a temporary directory

    Examples
    --------
    >>> import os
    >>> my_cwd = os.getcwd()
    >>> with in_tempdir() as tmpdir:
    ...     _ = open('test.txt', 'wt').write('some text')
    ...     assert os.path.isfile('test.txt')
    ...     assert os.path.isfile(os.path.join(tmpdir, 'test.txt'))
    >>> os.path.exists(tmpdir)
    False
    >>> os.getcwd() == my_cwd
    True
    N)tempfileTemporaryDirectoryr   )tds    Y/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/scipy/io/tests/test_netcdf.py
in_tempdirr      sP     " 
	$	$	& "2Y 	H	 	 	 s.   AA9A	AA	AA
Ac               /     K   t        | i |}d|_        |j                  dt               |j	                  dt
        d      }t        j                  t              |d d  d|_        |j                          | |j                          y w)NCreated for a testtimer   zdays since 2008-01-01)r   historycreateDimensionN_EG_ELScreateVariable
VARTYPE_EGnparangeunitsflushclose)argskwargsfr   s       r   make_simpler(   2   sp     T$V$A$AIfh'FJ	:Dii!DG(DJGGI
GGGIs   BB
c                     t        | j                  d       | j                  d   }t        |j                  d       t        |j                  t
        f       t        |d   t
        dz
         y)zExample fileobj tests    Created for a testr   s   days since 2008-01-01   N)r   r   	variablesr"   shaper   )	ncfileobjr   s     r   check_simpler0   ?   sT    ""$9:v&D56h[)b8A:&    c                 Z    t         j                  j                  |       }t        ||       y)a  
    Asserts that the mask of arr is effectively the same as expected_mask.

    In contrast to numpy.ma.testutils.assert_mask_equal, this function allows
    testing the 'mask' of a standard numpy array (the mask in this case is treated
    as all False).

    Parameters
    ----------
    arr : ndarray or MaskedArray
        Array to test.
    expected_mask : array_like of booleans
        A list giving the expected mask.
    N)r    magetmaskarrayr   )arrexpected_maskmasks      r   assert_mask_matchesr8   G   s"      55c"D}%r1   c                  d   t        j                         } 	 t        j                         }t        j                  |       t        dd      5 }	 d d d        t        dd      5 }t        |       d|j                  d<   d d d        t        d      5 }|j                  sJ t        |       t        |j                  d   d       d d d        t        dd      5 }t        |j                          t        |       t        |j                  d   d       d d d        t        dd      5 }t        |j                          t        |       d d d        t        dd      5 }t        |      5 }t        |j                          t        |       d d d        d d d        t        j                         5  t        dd      5 }t        |d	      5 }t        |j                         t        |       d d d        d d d        d d d        t        dd
      5 }t        |d      5 }t        |j                          t        |       |j                  dd       |j!                  ddd      }d|d d  d d d        d d d        t        d      5 }t        |       t        |j"                  d   d d  d       d d d        t        j                  |        t%        j&                  |       y # 1 sw Y   xY w# 1 sw Y   pxY w# 1 sw Y   7xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   PxY w# 1 sw Y   UxY w# 1 sw Y   ZxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        j                  |        t%        j&                         w xY w)Nz	simple.ncwar,   	appendRanFmmaprbTzr+bapp_dimapp_vari)r@   *   )osgetcwdr   mkdtempr   r(   r   r0   _attributesuse_mmapr   r	   openwarningscatch_warningsr   r   r-   shutilrmtree)cwdtmpdirr'   fobjvars        r   test_read_write_filesrR   [   s   
))+C@!!#
c* 	a	 c* 	+aO)*AMM+&	+ % 	8:::O{3Q7		8 c* 	8a

N#O{3Q7	8 51 	Q

N#O	 +t$ 	 T"  aAJJ'Q 	  $$& 	$k4( $D D1 $QAJJ' O$$	$ +u% 	T3' 1AJJ'Q!!)Q/&&y#|DA	 % 	8OY/2B7	8
 	f{	 		+ 	+	8 	8	8 	8	 	   	  	 $ $$ $	$ 	$ 	 		8 	8
 	fsD  5N KN !K*<N 3K7N ;LN &"LN L+("L
L+N .M;M	!L8*M2M:N M+AM'M+/N (M7*N K'"N *K4/N 7L<N L	N LN L(	#L++L50N 8M=MM	
MMN M(	$M++M40N 7N <N ,N/c                     t               } t        | d      5  | j                         }d d d        t              }t        |      5 }t	        |       d d d        t        |      }t        t        t        |dd       t               }t        |dd      5 }|j                         }d d d        t        |      }t        |      5 }t	        |       t        |j                  d       d d d        t        |      }t        |d      5 }t	        |       t        |j                  d       d d d        y # 1 sw Y   
xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   qxY w# 1 sw Y   y xY w)Nr:   rT   )version)	r   r(   getvaluer   r0   assert_raises
ValueErrorr   version_byte)eg_sio1str_valeg_sio2f2eg_sio3	eg_sio_64f_64s          r   test_read_write_siorb      sJ   iG	Wc	" %""$% gG	W	 R gG*k7C>	I	YQ	/ '4$$&'  I	Y	 +4TT&&*+  I	Y	* +dTT&&*+ ++% % ' '+ +
+ +s;   D0D=E	"E"E!0D:=E	EE!E*c                     t               } t        | d      }d|_        |j                  dd       |j	                  dt
        j                  d      }d|d	<   d
|_        |j                          | j                         }d}t        ||       y )Nr:   moder   dimr,   rQ   )rf   ir   ds|   CDF       
      dim             a         b            var                 c         d            x)r   r   r;   r   r   r    int16csyncrW   r   )raw_filer'   rQ   actualexpecteds        r   
test_bytesrn      s    yHH3'A ACeQ


5"((H
5CCFCEFFH F$H@ "r1   c                  Z   t        t               d      5 } | j                  dd       | j                  ddd      }t	        |j                         d       d	|_        t	        |j                         d	       d
|_        t	        |j                         d       d d d        y # 1 sw Y   y xY w)Nr:   rd   xr,   rQ   S1rp          s     )r   r   r   r   r   _get_encoded_fill_value
_FillValue)r'   rQ   s     r   test_encoded_fill_valuerw      s    	WYS	) =Q	#q!udF3S002G< S002G<$S002G<= = =s   BB!!B*c                      t        t        t        d            D ]0  } t        | d      5  	 d d d        t        | dd      5  	 d d d        2 y # 1 sw Y   $xY w# 1 sw Y   IxY w)Nz*.ncrT   Fr=   )r   pjoinTEST_DATA_PATHr   )fnames    r   test_read_example_datar|      sh    eNF34 $ 		%0 		 		 		 	s   AAA	A#	c                  J   t        t        d      } t        j                         5  d}t        j                  d|t
               t        | dd      5 }|j                  d   }d d d        d d d        t        t        j                  d       y # 1 sw Y   -xY w# 1 sw Y   1xY w)	Nexample_1.ncsCannot close a netcdf_file opened with mmap=True, when netcdf_variables or arrays referring to its data still existignorerT   Tr=   r   rC   )ry   rz   rJ   rK   filterwarningsRuntimeWarningr   r-   rX   RuntimeErrorassignValue)filenamemessager'   time_vars       r   $test_itemset_no_segfault_on_readonlyr     s     ^^4H		 	 	" +R'>B3T2 	+a{{6*H	+	+ , 4 4b9		+ 	+	+ +s#   ,BB!BB	BB"c                     t               } t        | d      5 }|j                  dd       |j                  dt        d       d|j
                  d   d<   |j                          | j                         }d d d        t              } t        | d      5 }d|j
                  d   d<   d d d        y # 1 sw Y   =xY w# 1 sw Y   y xY w)	Nr:   rd   rp   rU   rr   r,   .r;   )	r   r(   r   r   floatr-   r#   rW   r   )streamr'   contentss      r   test_appending_issue_gh_8625r     s    YF	V#	& %!	#q!	eV, !C		??$% XF	V#	& "! !C" "% %" "s   AB0B<0B9<Cc            	         ddg} t        j                  d      j                  dk(  r| j                  d       t        j                  d      j                  dk(  r| j                  d       t	        t               d      5 }|j                  dt               | D ]  }t        t        |j                  d|d       ! 	 d d d        y # 1 sw Y   y xY w)	Nint64uint64int   uintr:   r   r   )r    dtypeitemsizeappendr   r   r   r   rX   rY   r   )dtypesr'   dts      r   test_write_invalid_dtyper   !  s    x F	xx1$e	xx  A%f	WY	$ O	&(+ 	OB*a&6&6IN	OO O Os   ?;CCc                  r   t               } t        | d      5 }|j                  dd       |j                  dddg      }d|d d  |j	                          t        | j                               }|j	                          t        | j                               }d d d        t        k(         y # 1 sw Y   xY w)Nr:   rd   rp      vi2r,   )r   r(   r   r   r#   lenrW   r	   )r   r'   r   
len_single
len_doubles        r   test_flush_rewindr   .  s    YF	V#	& ,!	#a S$.!		*+
		*+
, J*$%, ,s   A>B--B6c                  R   t        t               d      5 } | j                  dd       | j                  dddg       | j                  dt        j
                  dg       | j                  dt	        j                  t        j
                        dg       d d d        y # 1 sw Y   y xY w)	Nr:   rd   rp   r   v1r   v2v3)r(   r   r   r   r    rh   r   )r'   s    r   test_dtype_specifiersr   <  s     
WYS	) :Q	#a 	tcU+	rxx#/	rxx1C59	: : :s   A>BB&c                  *   t               } g d}t        | d      5 }d|_        |j                  dd       |j	                  ddd      }||d d  d|_        |j                          | j                         }d d d        t              } t        | d	      5 }t        |j                  d
       |j                  d   }t        |j
                  d       t        |j                  d       t        |d d  |       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)N)
r   g?g?g333333?g?g      ?g333333?gffffff?g?g?r:   r   	float_var
   r'   )r   metresrT   r*   s   metres)r   )r   r   r   r   r   r"   r#   rW   r   r-   r.   r
   )ioitemsr'   r   r   s        r   test_ticket_1720r   G  s    	B3E	R	 !(		+r*$$[#~F		!"			;;=! 
	B	R	 -QYY 56KK,	Y__i0Y__e,	!e,- -! !- -s   AC=A!D	=D	Dc                     t        t        d      t        j                         5  t        j                  d       t        d      5 } | j                  d   d d  }~d d d        d d d        fd}t        j                         5  d}t        j                  d|t                |       }d d d        j                          y # 1 sw Y   hxY w# 1 sw Y   lxY w# 1 sw Y   2xY w)	Nr~   errorTr=   latc                  n    t        d      5 } | j                  d   d d  cd d d        S # 1 sw Y   y xY w)NTr=   r   )r   r-   )r'   r   s    r   doitz!test_mmaps_segfault.<locals>.doith  s5    - 	);;u%a(	) 	) 	)s   +4r   r   )
ry   rz   rJ   rK   simplefilterr   r-   r   r   sum)r'   rp   r   r   r   s       @r   test_mmaps_segfaultr   ^  s    ^^4H		 	 	" g&- 	E"1%A	)
 
	 	 	" R'>BF	
 EEG	 	  s/   #C	CC%CC		CCC%c                      t               } t        | d      5 }|j                  ddg       }|j                  du sJ |j                         |j	                          d d d        y # 1 sw Y   y xY w)Nr:   zerodimr   F)r   r(   r   isrecr#   )r   r'   r   s      r   test_zero_dimensional_varr   u  sd    	B	R	 Yb1 ww%((		  s   AA##A,c                  V   t               5  d} t        | d      }d|j                  d<   d|j                  d<   |j                          t        | d      }t	        |j                  d   d       t	        |j                  d   d       |j                          d d d        y # 1 sw Y   y xY w)	Nzg_byte_atts.ncr:   s   grailholyfloatswitchrT   s   floatsr   r   rG   r$   r   r   r'   s     r   test_byte_gattsr     s     
 	##& (f!)g		#&Q]]6*H5Q]]7+Y7			 	 	s   BBB(c                     t               5  d} t        | d      }d|j                  d<   |j                          t        | d      }t	        |j                  d   d       d|j                  d<   |j                          t        | d	      }t	        |j                  d   d       t	        |j                  d   d       |j                          d d d        y # 1 sw Y   y xY w)
Nzappend_dat.ncr:   zwas hereKilroyr;   s   was heres   ZootnaughtyrT   r   r   s     r   test_open_appendr     s    	 "#&",h		 #&Q]]8,k:#*i 		 #&Q]]8,k:Q]]9-w7		!  s   C CCc            
         d} t               5  t        dd      5 }|j                  dd        |j                  ddd       |j                  d|        |j                  ddd      }t	        j
                  t        |             |d d  |j                  d	|        |j                  d	dd
      }t	        j
                  t        |             |d d  |j                  ddd       |j                          |j                          d d d        t        d      D ]s  }t        dd      5 }t	        j                  |j                  d   j                  |      |j                  d   _        t	        j                  | | f|      |j                  d   |d d d d f<   |j                          d d d        t        d      5 }t        |j                  d   d   |       t        |j                  d   dd d d d f   j                         t	        j                  | | f|             t        |j                  d   j                  j                  d   |dz          t        |j                  d   j                  j                  d   |dz          d d d        v t        d      5 }t!        t"              5 }|j                  d   j$                  d    d d d        j&                  }t        |j(                  d   d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   mxY w# 1 sw Y   xY w# 1 sw Y   fxY w# 1 sw Y   ExY w# 1 sw Y   y xY w)Nd   zwithRecordDimension.ncr:   r   rg   r   rp   rr   y)r   testDatarB   )r   rp   r   rU   r;   r+   r   r,   r   )r   r   r   r   r    arrayranger#   r$   r   r-   r   fullr   copyr.   rX   KeyErrorrG   valuer%   )dataSizer'   rp   r   rB   arexs          r   test_append_recordDimensionr     s   H	 $-137 	1fd+VS)4c8,  c62A88E(O,AaDc8,  c62A88E(O,AaDZ.@AGGIGGI	 q 	IA5s; q+-99Q[[5H5M5Mq+QF#(3577Hh;OQR3SJ'1a0	 56 I!Q[[04a8Q[[4RAX>CCEWWh%91=?Q[[055;;A>!DQ[[499??BAaCHI I	I" 12 	-ax( <BJ'33F;<BV,		-A$- $-	 	 I I< <	- 	-A$- $-s   L>C%K??#L>"A?L!L>4C
L>L>L2$L&-L2.L>?L		L>LL>L#L>&L/+L22L;	7L>>Mc                     t        j                  ddd      } d| d<   t         j                  j                  | d      }t	        t
        d      }t        |d	      5 }|j                  d
   }t        |j                  d       t        |j                  d       t        |j                  t        j                  d             |d d  j                         }~t        j                  |j                         d      }t        ||       d d d        t!               5  d}t        |dd	      }|j#                  d
t%        |             |j'                  d
dd      }d|_	        d|_        d|_
        ||d d  |j)                          t        |d	      5 }|j                  d
   }t        |j                  d       t        |j                  d       t        |j                  t        j                  d             t        j                  |j                         d      }|d d  j                         }~t        ||       d d d        d d d        y # 1 sw Y   DxY w# 1 sw Y   xY w# 1 sw Y   y xY w)N         r      c   zexample_2.ncTmaskandscaleTemperature'  g{Gz?rU   zms.ncr:   rB   )r   )r    linspacer3   masked_greaterry   rz   r   r-   r   missing_value
add_offsetscale_factorfloat32
compressedroundr
   r   r   r   r   r$   )	ttmr{   r'   Tempfoundrm   newfnametemps	            r   test_maskandscaler     s   
BBAAaD			a	$B..1E	U	. )!{{=)T''.T__b)T&&

4(89Q""$88BMMOQ/x() 
 -#D9	-R1s4DE! Q		5 	-;;}-D++T2"-**BJJt,<=xx3HG&&(EE8,	-- -) )*	- 	-- -s3   B)IA6IB)I4III	II'c                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |g d       d d d        y # 1 sw Y   y xY w)Nexample_3_maskedvals.ncTr   var1_fillval0FTFry   rz   r   r-   r8   r{   r'   vardatas      r   !test_read_withValuesNearFillValuer     sN    .";<E	U	. ;!++o.q1G%9:; ; ;   !AAc                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |g d       t        |g d       d d d        y # 1 sw Y   y xY w)Nr   Tr   var2_noFillvalFFFr,   rU   r   ry   rz   r   r-   r8   r   r   s      r   test_read_withNoFillValuer     s[     .";<E	U	. '!++./2G%:;Wg&' ' 's   /AAc                      d} t        t        d      }t        |d      5 }|j                  d   d d  }t	        |g d       t        || ddg       d d d        y # 1 sw Y   y xY w)	Nr   r   Tr   var3_fillvalAndMissingValue)TFFrU   r   r   )IRRELEVANT_VALUEr{   r'   r   s       r   &test_read_withFillValueAndMissingValuer     si     .";<E	U	. 8!++;<Q?G%9:W/A678 8 8s   0AA"c                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |g d       d d d        y # 1 sw Y   y xY w)Nr   Tr   var4_missingValuer   r   r   s      r   test_read_withMissingValuer    sQ     .";<E	U	. ;!++1215G%9:; ; ;r   c                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |g d       d d d        y # 1 sw Y   y xY w)Nr   Tr   var5_fillvalNaNr   r   r   s      r   test_read_withFillValNaNr    sO    .";<E	U	. ;!++/03G%9:; ; ;r   c                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |g d       d d d        y # 1 sw Y   y xY w)Nr   Tr   	var6_charr   r   r   s      r   test_read_withCharr    sN    .";<E	U	. ;!++k*1-G%9:; ; ;r   c                      t        t        d      } t        | d      5 }|j                  d   d d  }t	        |ddgddgddgg       d d d        y # 1 sw Y   y xY w)Nr   Tr   var7_2dFr   r   s      r   test_read_with2dVarr
  #  sd    .";<E	U	. U!++i(+GtUmeU^eT]%STU U Us   (AAc                      t        t        d      } t        | dd      5 }|j                  d   d d  }t	        |g d       t        |g d       d d d        y # 1 sw Y   y xY w)Nr   F)r   r>   r   r   r   r   r   s      r   test_read_withMaskAndScaleFalser  )  s_     .";<E 
UU	; )q++;<Q?G%:;Wi() ) )s   /AA )>__doc__rD   os.pathr   ry   r   rL   r   rJ   r   r   r   
contextlibr   r   numpyr    numpy.testingr	   r
   r   pytestr   rX   scipy.ior   __file__rz   r   r   markthread_unsafe
pytestmarkr   r(   r0   r8   rR   rb   rn   rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r   r1   r   <module>r     s    	 *      ,  @ @  *  wx(&1
 [[&&
  * 	 	'&(CL+8.#b=: "
O&:-..*'-R"-R;'8;;;U	)r1   