a
    ReqK                     @   s  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 ddlZddlmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ eeedZdZ dZ!edd Z"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*d d! Z+d"d# Z,d$d% Z-d&d' Z.d(d) Z/d*d+ Z0d,d- Z1d.d/ Z2d0d1 Z3d2d3 Z4d4d5 Z5d6d7 Z6d8d9 Z7d:d; Z8d<d= Z9d>d? Z:d@dA Z;dBdC Z<dDdE Z=dS )Fz Tests for netcdf     N)joindirname)BytesIO)glob)contextmanager)assert_assert_allcloseassert_equalbreak_cyclessuppress_warningsIS_PYPY)raises)netcdf_file)
in_tempdirdata   bc                  o   s`   t | i |}d|_|dt |dtd}tt|d d < d|_|	  |V  |
  d S )NCreated for a testtimer   zdays since 2008-01-01)r   historycreateDimensionN_EG_ELScreateVariable
VARTYPE_EGnpZarangeunitsflushclose)argskwargsfr    r"   N/var/www/sunrise/env/lib/python3.9/site-packages/scipy/io/tests/test_netcdf.pymake_simple   s    r$   c                 C   sF   t | jd | jd }t |jd t |jtf t |d td  dS )zExample fileobj tests    Created for a testr   s   days since 2008-01-01   N)r	   r   	variablesr   shaper   )Z	ncfileobjr   r"   r"   r#   check_simple&   s
    
r*   c                 C   s   t j| }t|| dS )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   maZgetmaskarrayr	   )ZarrZexpected_maskmaskr"   r"   r#   assert_mask_matches.   s    r-   c               
   C   s  t  } znt }t | tdd}W d    n1 s>0    Y  tdd"}t| d|jd< W d    n1 sz0    Y  td6}t	|j
t  t| t	|jd d W d    n1 s0    Y  tdd4}t|j
  t| t	|jd d W d    n1 s0    Y  tddd$}t|j
  t| W d    n1 sZ0    Y  tddN}t|$}t|j
  t| W d    n1 s0    Y  W d    n1 s0    Y  t }tr|td	 tddP}t|d
d"}t|j
 t| W d    n1 s*0    Y  W d    n1 sJ0    Y  W d    n1 sj0    Y  tddv}t|dJ}t|j
  t| |dd |ddd}d|d d < W d    n1 s0    Y  W d    n1 s0    Y  td0}t| t	|jd d d  d W d    n1 sF0    Y  W trdt  t  t |  t| n(trt  t  t |  t| 0 d S )Nz	simple.ncwar'   Z	appendRanFmmaprbz2Cannot close a netcdf_file opened with mmap=True.*Tzr+bapp_dimZapp_vari)r3   *   )osgetcwdtempfilemkdtempchdirr$   r   r*   _attributesr	   Zuse_mmapr   r   openr   filterRuntimeWarningr   r   r(   r
   shutilrmtree)cwdtmpdirr!   Zfobjsupvarr"   r"   r#   test_read_write_filesB   sj    
(
.0(
H
hL
:

rE   c                  C   sV  t  } t| d |  }W d    n1 s.0    Y  t |}t|}t| W d    n1 sf0    Y  t |}ttt|dd t  }t|ddd}| }W d    n1 s0    Y  t |}t|$}t| t|jd W d    n1 s0    Y  t |}t|dd$}t| t|jd W d    n1 sH0    Y  d S )Nr.   rT   )version)	r   r$   getvaluer   r*   assert_raises
ValueErrorr	   Zversion_byte)Zeg_sio1Zstr_valZeg_sio2f2Zeg_sio3Z	eg_sio_64Zf_64r"   r"   r#   test_read_write_sio   s&    &
&&
*rM   c                  C   sd   t  } t| dd}d|_|dd |dtjd}d|d	< d
|_|  | 	 }d}t
|| d S )Nr.   moder   dimr'   rD   )rP   ir   ds|   CDF       
      dim             a         b            var                 c         d            x)r   r   r/   r   r   r   int16csyncrI   r	   )Zraw_filer!   rD   actualexpectedr"   r"   r#   
test_bytes   s     rW   c                  C   s   t t dd`} | dd | ddd}t| d d	|_t| d	 d
|_t| d W d    n1 st0    Y  d S )Nr.   rN   xr'   rD   ZS1rX          s     )r   r   r   r   r	   Z_get_encoded_fill_valueZ
_FillValue)r!   rD   r"   r"   r#   test_encoded_fill_value   s    r\   c               	   C   sp   t ttdD ]\} t| d W d    n1 s20    Y  t| ddd W d    q1 s`0    Y  qd S )Nz*.ncrF   Fr0   )r   pjoinTEST_DATA_PATHr   )fnamer"   r"   r#   test_read_example_data   s
    r`   c               	   C   s   t td} t T}|td t| ddd}|jd }W d    n1 sL0    Y  W d    n1 sj0    Y  tt|j	d d S )Nexample_1.ncsCannot close a netcdf_file opened with mmap=True, when netcdf_variables or arrays referring to its data still existrF   Tr0   r   r5   )
r]   r^   r   r=   r>   r   r(   rJ   RuntimeErrorZassignValue)filenamerC   r!   Ztime_varr"   r"   r#   $test_itemset_no_segfault_on_readonly   s    
Fre   c                  C   s   t  } t| ddH}|dd |dtd d|jd d< |  |  }W d    n1 s`0    Y  t |} t| dd}d|jd d< W d    n1 s0    Y  d S )	Nr.   rN   rX   rG   rY   r'   .r/   )	r   r$   r   r   floatr(   r   rI   r   )streamr!   contentsr"   r"   r#   test_appending_issue_gh_8625   s    &ri   c                  C   s   ddg} t djdkr"| d t djdkr<| d tt d8}|dt | D ]}tt	|j
d|d qZW d    n1 s0    Y  d S )	Nint64Zuint64int   Zuintr.   r   r   )r   dtypeitemsizeappendr   r   r   r   rJ   rK   r   )Zdtypesr!   dtr"   r"   r#   test_write_invalid_dtype  s    

rq   c                  C   s   t  } t| dd`}|dd |dddg}d|d d < |  t|  }|  t|  }W d    n1 sx0    Y  t||k d S )Nr.   rN   rX      vi2r'   )r   r$   r   r   r   lenrI   r   )rg   r!   rs   Z
len_singleZ
len_doubler"   r"   r#   test_flush_rewind  s    *rv   c                  C   sx   t t ddV} | dd | dddg | dtjdg | dttjdg W d    n1 sj0    Y  d S )	Nr.   rN   rX   rr   Zv1rt   Zv2Zv3)r$   r   r   r   r   rR   rm   r!   r"   r"   r#   test_dtype_specifiers*  s
    rx   c                  C   s   t  } g d}t| dR}d|_|dd |ddd}||d d < d|_|  |  }W d    n1 sp0    Y  t |} t| d	P}t|jd
 |j	d }t|jd t|j
d t|d d  | W d    n1 s0    Y  d S )N)
r   g?g?g333333?g?g      ?g333333?gffffff?g?g?r.   r   	float_var
   r!   )ry   ZmetresrF   r%   s   metres)rz   )r   r   r   r   r   r   r   rI   r	   r(   r)   r   )ioitemsr!   ry   rh   r"   r"   r#   test_ticket_17205  s"    &
r}   c               	      s   t td tst Z td t dd$} | jd d d  }~W d    n1 sX0    Y  W d    n1 sv0    Y   fdd}t "}|	t
d | }W d    n1 s0    Y  |  d S )	Nra   errorTr0   latc                     sB   t  dd"} | jd d d  W  d    S 1 s40    Y  d S )NTr0   r   )r   r(   rw   rd   r"   r#   doitW  s    z!test_mmaps_segfault.<locals>.doitrb   )r]   r^   r   warningscatch_warningssimplefilterr   r(   r   r=   r>   sum)r!   rX   r   rC   r"   r   r#   test_mmaps_segfaultL  s    


>$r   c                  C   s^   t  } t| d:}|ddg }|jdu s4J |j|  W d    n1 sP0    Y  d S )Nr.   Zzerodimrt   F)r   r$   r   Zisrecr   )r{   r!   rs   r"   r"   r#   test_zero_dimensional_varc  s
    r   c                  C   s   t  l d} t| d}d|jd< d|jd< |  t| d}t|jd d t|jd d |  W d    n1 sx0    Y  d S )	Nzg_byte_atts.ncr.   s   grailZholyZfloatsZwitchrF   s   floatsr   r   r;   r   r	   rd   r!   r"   r"   r#   test_byte_gattsm  s    



r   c                  C   s   t   d} t| d}d|jd< |  t| d}t|jd d d|jd< |  t| d	}t|jd d t|jd d |  W d    n1 s0    Y  d S )
Nzappend_dat.ncr.   zwas hereZKilroyr/   s   was heres   ZootZnaughtyrF   r   r   r"   r"   r#   test_open_append|  s    




r   c               
   C   s  d} t  | tdd}|dd  |ddd |d|  |ddd}tt| |d d < |d	|  |d	dd
}tt| |d d < |ddd |  |  W d    n1 s0    Y  tdD ]$}tdd\}t	|j
d j||j
d _t| | f||j
d |d d d d f< |  W d    n1 sL0    Y  td}t|j
d d | t|j
d dd d d d f  t| | f| t|j
d jjd |d  t|j
d jjd |d  W d    q1 s0    Y  qtd`}tt }|j
d jd  W d    n1 s:0    Y  |j}t|jd d W d    n1 sp0    Y  W d    n1 s0    Y  d S )Nd   zwithRecordDimension.ncr.   r   rQ   r   rX   rY   y)r   ZtestDatar4   )r   rX   r   rG   r/   r&   r   r'   r   )r   r   r   r   r   arrayranger   r   ro   r(   r   fullr	   copyr)   rJ   KeyErrorr;   valuer   )ZdataSizer!   rX   r   r4   arexr"   r"   r#   test_append_recordDimension  s:    
&((
2>

0r   c            	   	   C   s  t ddd} d| d< t j| d}ttd}t|dd	p}|jd
 }t|j	d t|j
d t|jt d |d d   }~t | d}t|| W d    n1 s0    Y  t  d}t|ddd	}|d
t| |d
dd}d|_	d|_d|_
||d d < |  t|dd	p}|jd
 }t|j	d t|j
d t|jt d t | d}|d d   }~t|| W d    n1 s0    Y  W d    n1 s0    Y  d S )N         r      c   zexample_2.ncTmaskandscaleTemperature'  g{Gz?rG   zms.ncr.   r4   )r   )r   Zlinspacer+   Zmasked_greaterr]   r^   r   r(   r	   Zmissing_valueZ
add_offsetZscale_factorfloat32
compressedroundr   r   r   ru   r   r   )	ttmr_   r!   ZTempfoundrV   Znewfnametempr"   r"   r#   test_maskandscale  s@    

(
r   c                  C   sZ   t td} t| dd0}|jd d d  }t|g d W d    n1 sL0    Y  d S )Nexample_3_maskedvals.ncTr   Zvar1_fillval0FTFr]   r^   r   r(   r-   r_   r!   vardatar"   r"   r#   !test_read_withValuesNearFillValue  s    
r   c                  C   sh   t td} t| dd>}|jd d d  }t|g d t|g d W d    n1 sZ0    Y  d S )Nr   Tr   Zvar2_noFillvalFFFr'   rG   r   r]   r^   r   r(   r-   r	   r   r"   r"   r#   test_read_withNoFillValue  s
    
r   c                  C   sn   d} t td}t|dd@}|jd d d  }t|g d t|| ddg W d    n1 s`0    Y  d S )	Nr   r   Tr   var3_fillvalAndMissingValue)TFFrG   r   r   )ZIRRELEVANT_VALUEr_   r!   r   r"   r"   r#   &test_read_withFillValueAndMissingValue  s    
r   c                  C   sZ   t td} t| dd0}|jd d d  }t|g d W d    n1 sL0    Y  d S )Nr   Tr   Zvar4_missingValuer   r   r   r"   r"   r#   test_read_withMissingValue  s    
r   c                  C   sZ   t td} t| dd0}|jd d d  }t|g d W d    n1 sL0    Y  d S )Nr   Tr   Zvar5_fillvalNaNr   r   r   r"   r"   r#   test_read_withFillValNaN  s    
r   c                  C   sZ   t td} t| dd0}|jd d d  }t|g d W d    n1 sL0    Y  d S )Nr   Tr   Z	var6_charr   r   r   r"   r"   r#   test_read_withChar
  s    
r   c                  C   sh   t td} t| dd>}|jd d d  }t|ddgddgddgg W d    n1 sZ0    Y  d S )Nr   Tr   Zvar7_2dFr   r   r"   r"   r#   test_read_with2dVar  s    
r   c                  C   sj   t td} t| ddd>}|jd d d  }t|g d t|g d W d    n1 s\0    Y  d S )Nr   F)r   r1   r   r   r   r   r   r"   r"   r#   test_read_withMaskAndScaleFalse  s
    
r   )>__doc__r6   os.pathr   r]   r   r?   r8   r   r{   r   r   
contextlibr   numpyr   Znumpy.testingr   r   r	   r
   r   r   Zpytestr   rJ   Zscipy.ior   Zscipy._lib._tmpdirsr   __file__r^   r   r   r$   r*   r-   rE   rM   rW   r\   r`   re   ri   rq   rv   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   sZ    
N1	
()	
