3
Pd                 @   s   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m	Z	m
Z
 ddlZddlZddlmZmZmZmZ ejeje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S )z$ Tests for fortran sequential files     N)path)iglob)assert_equalassert_allclose)FortranFile_test_fortranFortranEOFErrorFortranFormattingErrordatac              C   s   xt tjtdD ]} tjd| tj}|s6td|  t|j	dt|j	dt|j	df}|j	dj
dd	}t| d
d}|j|dj|dd}|j  tjtj|j|j|}t|| qW d S )Nzfortran-*-*x*x*.datz%fortran-([^-]+)-(\d+)x(\d+)x(\d+).datz#Couldn't match %s filename to regex            s<rz<u4)dtypeF)order)r   r   join	DATA_PATHresearchIRuntimeErrorintgroupreplacer   read_recordreshapeclosenparangeprodastyper   )filenamemdimsr   fr
   expected r*   O/var/www/html/virt/lib64/python3.6/site-packages/scipy/io/tests/test_fortran.pytest_fortranfiles_read   s    (r,   c              C   s~   t jtd} t| dd}|jd}W d Q R X t|d d d t|d d d	 t|d
 d d t|d d ddg d S )Nzfortran-mixed.datr   z<u4z<i4,<f4,<i8,(2)<f8Zf0r   r   f1gffffff@f2r   Zf3gffffff@g333333@)r   r   r   r   r   r   r   )r%   r(   recordr*   r*   r+   test_fortranfiles_mixed_record(   s    r0   c        
      C   s  xt tjtdD ] } tjd| tj}|s:td|  t|j	dt|j	dt|j	df}|j	dj
dd	}tjtj|j|j|}tj }zptj|tj| }t|d
d}|j|j |j  t| d}t|d}	t|j |	j | d |j  |	j  W d tj| X qW d S )Nzfortran-*-*x*x*.datz%fortran-([^-]+)-(\d+)x(\d+)x(\d+).datz#Couldn't match %s filename to regexr   r   r   r   r   r   wz<u4rb)err_msg)r   r   r   r   r   r   r   r   r   r   r   r!   r"   r#   r   r$   tempfilemkdtempbasenamer   write_recordTr    openr   readshutilrmtree)
r%   r&   r'   r   r
   tmpdirZtestFiler(   Zoriginalfilenewfiler*   r*   r+   test_fortranfiles_write3   s(    (

r?   c              C   s   t jtd} t| dd}|jdd}W d Q R X tjdjddjtj	}tj
ddgtjd	}t|d
 |j t|d |j d S )Nzfortran-3x3d-2i.datr   z<u4z(3,3)f82i4r   r   r   )r   r   	   )r   r   r   r   r   r!   r"   r   r$   doublearrayint32r   r8   )r%   r(   r/   ZaxZbxr*   r*   r+   "test_fortranfile_read_mixed_recordM   s    rG   c       	      C   s  t jt| d}dtjdtjdtjdffdtjjd
dgdjtjtjjd
ddgdjtjtjjd
dgdjtjffg}x|D ]|\}}t	|d}|j
|  W d Q R X t	|d}|j| }W d Q R X tt|t| x t||D ]\}}t|| qW qW d S )Nztest.datf4i4r   r   d   4f4(3,3)f48i4   r   )size   r1   r   )rH   rH   rI   )rK   rL   rM   )r   r   strr!   Zfloat32rF   randomrandintr$   r   r7   r   r   lenzip)	r=   tfrecordsr   ar(   bZaaZbbr*   r*   r+   #test_fortranfile_write_mixed_recordd   s     rZ   c          -   C   s  t jt| d}tjjd d\}}}tjj|||}t|d}|j|j	 W d Q R X t
j||||}t|d}|jdj	}W d Q R X t|| t|| d\}}}tjj|||jtj}t|d}|j|j	 W d Q R X t
j||||}t|d}|jd	j	}W d Q R X t|| t|| d\}}}tjj||}tjj|jtj}	t|d}|j|j	|	j	 W d Q R X t
j||||\}}
t|d}|jd
d\}}|j	}W d Q R X t|| t|| t|
|	 t||	 d S )Nztest.datr      r   r   r1   r   z	(2,3,5)f8z	(2,3,5)i4z(3,5)f8r@   )r[   r   r   )r[   r   r   )r[   r   r   )r   r   rQ   r!   rR   seedrandnr   r7   r8   r   Zread_unformatted_doubler   r   r$   rF   Zread_unformatted_intZintcZread_unformatted_mixed)r=   r%   r&   nkrX   r(   Za2a3rY   Zb2Zb3r*   r*   r+   test_fortran_roundtrip{   sB    









ra   c             C   s   t jt| d}tjjd t|d*}|jtjjd |jtjjd W d Q R X t|dL}t	|j
 dksvtt	|j
 dksttjt |j
  W d Q R X W d Q R X d S )Nscratchr   r1   r[   r   r   )r   r   rQ   r!   rR   r\   r   r7   r]   rT   
read_realsAssertionErrorpytestraisesr   )r=   r%   r(   r*   r*   r+   test_fortran_eof_ok   s    rg   c             C   s   t jt| d}tjjd t|d*}|jtjjd |jtjjd W d Q R X t	|d}|j
d W d Q R X t|dL}t|j dkstt|j dksttjt |j  W d Q R X W d Q R X d S )	Nrb   r   r1   r[   r   ab   r   )r   r   rQ   r!   rR   r\   r   r7   r]   r9   writerT   rc   rd   re   rf   r	   )r=   r%   r(   r*   r*   r+   test_fortran_eof_broken_size   s    rk   c             C   s   t jt| d}tjjd t|d*}|jtjjd |jtjjd W d Q R X t	|d}|j
d W d Q R X t|d$}tjt |j  W d Q R X W d Q R X d S )	Nrb   r   r1   r[   r   zw+bs   r   )r   r   rQ   r!   rR   r\   r   r7   r]   r9   rj   re   rf   r	   rc   )r=   r%   r(   r*   r*   r+   test_fortran_bogus_size   s    rl   c             C   s   t jt| d}tjjd t|d*}|jtjjd |jtjjd W d Q R X t	|d}|j
t j|d  W d Q R X t|d8}t|j dksttjt |j  W d Q R X W d Q R X d S )	Nrb   r   r1   r[   r   rh      r   )r   r   rQ   r!   rR   r\   r   r7   r]   r9   truncategetsizerT   rc   rd   re   rf   r	   )r=   r%   r(   r*   r*   r+   test_fortran_eof_broken_record   s    rp   c             C   s  t jt| d}d\}}}tjdtj||ffg}tj||d}t|d(}|j|d  |j| |j| W d Q R X t	|d	}|j
t j|d
  W d Q R X t|dX}t|j|ddkstt|j|d|ksttjt |j|d W d Q R X W d Q R X d S )Nrb   r   r[      field)r   r1   r   rh   rm   r   r   )r   r[   rq   )r   r   rQ   r!   r   Zfloat64Zzerosr   r7   r9   rn   ro   rT   r   rd   re   rf   r	   )r=   r%   r^   r&   qdtrX   r(   r*   r*   r+   !test_fortran_eof_multidimensional   s    

ru   )"__doc__r4   r;   osr   globr   r   Znumpy.testingr   r   Znumpyr!   re   Zscipy.ior   r   r   r	   r   dirname__file__r   r,   r0   r?   rG   rZ   ra   rg   rk   rl   rp   ru   r*   r*   r*   r+   <module>   s*   +