a
    BCCfK                     @   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"   V/var/www/html/django/DPS/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)cwdZtmpdirr!   Zfobjsupvarr"   r"   r#   test_read_write_filesB   sj    
(
.0(
H
hL
:

rD   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&    &
&&
*rL   c                  C   sd   t  } t| dd}d|_|dd |dtjd}d|d	< d
|_|  | 	 }d}t
|| d S )Nr.   moder   dimr'   rC   )rO   ir   ds|   CDF       
      dim             a         b            var                 c         d            x)r   r   r/   r   r   r   int16csyncrH   r	   )Zraw_filer!   rC   actualexpectedr"   r"   r#   
test_bytes   s     rV   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.   rM   xr'   rC   ZS1rW          s     )r   r   r   r   r	   Z_get_encoded_fill_valueZ
_FillValue)r!   rC   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*.ncrE   Fr0   )r   pjoinTEST_DATA_PATHr   )fnamer"   r"   r#   test_read_example_data   s
    r_   c               	   C   s   t td} t X}d}|t| t| ddd}|jd }W d    n1 sP0    Y  W d    n1 sn0    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 existrE   Tr0   r   r5   )
r\   r]   r   r=   r>   r   r(   rI   RuntimeErrorZassignValue)filenamerB   messager!   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.   rM   rW   rF   rX   r'   .r/   )	r   r$   r   r   floatr(   r   rH   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 )	NZint64Zuint64int   Zuintr.   r   r   )r   dtypeitemsizeappendr   r   r   r   rI   rJ   r   )Zdtypesr!   dtr"   r"   r#   test_write_invalid_dtype  s    

rp   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.   rM   rW      vi2r'   )r   r$   r   r   r   lenrH   r   )rg   r!   rr   Z
len_singleZ
len_doubler"   r"   r#   test_flush_rewind  s    *ru   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.   rM   rW   rq   v1rs   v2Zv3)r$   r   r   r   r   rQ   rl   r!   r"   r"   r#   test_dtype_specifiers+  s
    ry   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!   )rz   ZmetresrE   r%   s   metres)r{   )r   r   r   r   r   r   r   rH   r	   r(   r)   r   )ioitemsr!   rz   rh   r"   r"   r#   test_ticket_17206  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 &}d}|	t
| | }W d    n1 s0    Y  |  d S )	Nr`   errorTr0   latc                     sB   t  dd"} | jd d d  W  d    S 1 s40    Y  d S )NTr0   r   )r   r(   rx   rc   r"   r#   doitX  s    z!test_mmaps_segfault.<locals>.doitra   )r\   r]   r   warningscatch_warningssimplefilterr   r(   r   r=   r>   sum)r!   rW   r   rB   rd   r"   r   r#   test_mmaps_segfaultM  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zerodimrs   F)r   r$   r   Zisrecr   )r|   r!   rr   r"   r"   r#   test_zero_dimensional_vare  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witchrE   s   floatsr   r   r;   r   r	   rc   r!   r"   r"   r#   test_byte_gattso  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naughtyrE   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   rP   r   rW   rX   y)r   ZtestDatar4   )r   rW   r   rF   r/   r&   r   r'   r   )r   r   r   r   r   arrayranger   r   rn   r(   r   fullr	   copyr)   rI   KeyErrorr;   valuer   )ZdataSizer!   rW   r   r4   arexr"   r"   r#   test_append_recordDimension  s>    
&((
 >

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?rF   zms.ncr.   r4   )r   )r   Zlinspacer+   Zmasked_greaterr\   r]   r   r(   r	   Zmissing_valueZ
add_offsetZscale_factorZfloat32
compressedroundr   r   r   rt   r   r   )	ttmr^   r!   ZTempfoundrU   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'   rF   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)TFFrF   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   rI   Zscipy.ior   Zscipy._lib._tmpdirsr   __file__r]   r   r   r$   r*   r-   rD   rL   rV   r[   r_   re   ri   rp   ru   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   sZ    
N1	
))	
