a
    ߙfbD                     @   s   d dl Z d dlZd dl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mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ ddlmZ G dd deZdS )    N)assert_array_equal)fits)units)Table)	printdiff)ColumnAttribute)REMOVE_KEYWORDS)AstropyUserWarning)_assert_attr_col   )FitsTestCasec                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
jdedd Zdd Zdd Zdd Zdd Zdd Ze
jdddgdd  Zd!d" Ze
jd#g d$d%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7S )8TestConveniencec                 C   s0   t dt t| d}t| d}d S )Nalways
test0.fits)warningssimplefilterResourceWarningr   getdatadata	getheader)self_ r   Elib/python3.9/site-packages/astropy/io/fits/tests/test_convenience.pytest_resource_warning   s    z%TestConvenience.test_resource_warningc                 C   sN   t | dd}t|}|jr$J |d t|}|jrBJ |  dS )z
        Tests that file-like objects are not closed after being passed
        to convenience functions.

        Regression test for https://github.com/astropy/astropy/issues/5063
        r   rbr   N)openr   r   r   closedseekr   close)r   fr   r   r   r   test_fileobj_not_closed   s    




z'TestConvenience.test_fileobj_not_closedc                 C   s   t g dg dg dgg dg dd}d|d _d|d	 _tjtjd
d}t|}W d    n1 sl0    Y  t|dksJ |j	
d|j	
dk sJ t|tjsJ | d}|j|dd d S )Nr         abcgffffff@g      @g@iU1r    namesdtypezm/sr&   z
not-a-unitr'   z''not-a-unit' did not parse as fits unitmatchr   ZTUNIT1ZTTYPE2test_table_to_hdu.fitsT	overwrite)r   unitpytestwarnsuZUnitsWarningr   table_to_hdulenheaderindex
isinstanceBinTableHDUtempwriteto)r   tablewhdufilenamer   r   r   test_table_to_hdu1   s    

(
z!TestConvenience.test_table_to_hduc                 C   st  t jjg dg dd}t jjg dg dd}t jjg dg dd}t jjg dg dd}t||||gg d	d
}t|}t|tjsJ |jd |j	ksJ t
|jd |  t
|jd |d t
|jd |t j t
|jd j|jt j t
|jd j|jt j t
|jd |tt jt j t
|jd |t j | d}|j|dd d S )Nr"   )TFF)maskr%   )FTT)yffffff@      ?y      @        y@      )TFTr)   )r+   sr(   r    )r.   ZTNULL1r+   rG    r(   r    r2   Tr3   )npZmaZMaskedArrayr   r   r9   r=   r>   r;   Z
fill_valuer   r   Zfillednanrealimagcomplexr?   r@   )r   r+   rG   r(   r    rA   rC   rD   r   r   r   test_masked_table_to_hduD   s"    
 
z(TestConvenience.test_masked_table_to_hduc                 C   s   t g dg dg dgg dg dd}tjd|d _tjtdd	}t	| W d    n1 sh0    Y  t
|d
ksJ d S )Nr"   r%   r)   r*   r-   testr&   z"The unit 'test' could not be savedr0   r   )r   r8   coreZIrreducibleUnitr5   r6   r7   r	   r   r9   r:   )r   rA   rB   r   r   r   (test_table_non_stringifyable_unit_to_hduX   s    (z8TestConvenience.test_table_non_stringifyable_unit_to_hduc                 C   s   t g dg dg dgg dg dd}g d|jd< t|}|jdg dksXJ tt |j	d W d	   n1 s0    Y  d	S )
zT
        Regression test for https://github.com/astropy/astropy/issues/6079
        r"   r%   r)   r*   r-   )ZThisisr&   commentZcommentsrS   N)
r   metar   r9   r;   getr6   raises
ValueErrorr<   r   rA   rC   r   r   r   ,test_table_to_hdu_convert_comment_conventiona   s    
z<TestConvenience.test_table_to_hdu_convert_comment_conventionc                 C   sb  d}dddddddd	}t g d
g dg dgg dg dd}|j| tjtd| d}t|}W d   n1 s0    Y  t|t|d ksJ t	t
|dd D ]$\}}d| t|| jv sJ q|jddksJ |jddksJ |jddksJ |jddks2J |jddksHJ tj|jdd dS )zT
        Regression test for https://github.com/astropy/astropy/issues/9387
        :be ignored since it conflicts with a FITS reserved keywordg@@ZNEWTABLEr   r$   	   *   ZAdams)EXPTIMEXTENSIONNAXISNAXIS1NAXIS2PCOUNTZOBSERVERr"   r%   r)   Zi4r,   f8r-   zMeta-data keyword \w+ will r0   Nr#   Meta-data keyword r^   BINTABLEr_   r`      ra   rb   r   r]   )r   rT   updater6   r7   r	   r   r9   r:   	enumerateliststrmessager;   rU   rI   testingZassert_almost_equal)r   diag	ins_cardsrA   rB   rC   r+   keyr   r   r   !test_table_to_hdu_filter_reservedn   s.    (z1TestConvenience.test_table_to_hdu_filter_reservedcardc              
   C   s   d}ddddddddd	d
	}dddddddddd
	}t g dg dgddgddgd}d|jd< || |j|< |j||| ksJ tjtd| d| d t|}W d	   n1 s0    Y  |j||| ksJ |jddksJ d	S )zT
        Test warning for each keyword in ..connect.REMOVE_KEYWORDS, 1 by 1
        rZ   rg      r#      r$   r   r   N)	r^   ZBITPIXr_   r`   ra   rb   ZGCOUNTZTFIELDSZTHEAPZTABLE         $   )g      ?g       @g      @r)   Z
wavelengthZfluxrd   Zf4r-   zMin.Silly WalksZORIGINrf   z will r0   )	r   rT   rU   r6   r7   r	   r   r9   r;   )r   rs   ro   Z	res_cardsrp   rA   rC   r   r   r   test_table_to_hdu_warn_reserved   s*    
(z/TestConvenience.test_table_to_hdu_warn_reservedc                 C   s   t g dg dg dgg dg dd}|jdtddd	d
d tjtdd t	|}W d   n1 sv0    Y  |j
ddksJ d|j
vsJ d|j
vsJ dS )zD
        Test removal of unsupported data types from header
        r"   r%   r)   rc   r-   z
2001-05-26   r   r$   )ZPRIMARYZSECONDAR)OBSDATERAMPTARGETSzBAttribute \S+ of type .+ cannot be added to FITS Header - skippingr0   Nr|   r}   r~   )r   rT   ri   rI   aranger6   r7   r	   r   r9   r;   rU   rX   r   r   r   %test_table_to_hdu_filter_incompatible   s    (z5TestConvenience.test_table_to_hdu_filter_incompatiblec                 C   sh   t jddtfdtfgd}t }d|d< | d}tj|||dd	 tj|d
d}|d dksdJ dS )zT
        Regression test for https://github.com/astropy/astropy/issues/5988
        )r{   xyr/   )g      E@zLTU&EZANSWERztabhdr42.fitsT)r   r;   r4   r   extr\   N)	rI   zerosfloatintr   Headerr?   r@   r   )r   r   Zh_inrD   h_outr   r   r   test_table_writeto_header   s    
z)TestConvenience.test_table_writeto_headerc                 C   s|   |  d}ttdttdg}t|| tj|tdt	dgdd tj
|dd}|d dksxJ d	S )
z
        Test that _makehdu correctly includes the header. For example in the
        fits.update convenience function.
        ztwoextension.fits)
   r   )WHATd   r   )r;   r   r   r   r   N)r?   r   
PrimaryHDUrI   r   ImageHDUHDUListr@   ri   r   r   )r   rD   hdusr   r   r   r   "test_image_extension_update_header   s    

z2TestConvenience.test_image_extension_update_headerc              
   C   s  t | d| ddu sJ t | d| ddddu s@J t | d| ddddu sbJ tt t dd	dd W d   n1 s0    Y  tj| d
dd}tj| ddd}t |d |d du sJ tt& t |d |d dd W d   n1 s0    Y  t ||du s6J tt t ||d W d   n1 sd0    Y  W d   n1 s0    Y  W d   n1 s0    Y  dS )zS
        Test that FITSDiff can run the different inputs without crashing.
        zarange.fitsz
blank.fitsNr   r   zo4sp040b0_raw.fitsZsci)Zextnamezfakefile.fitszstddata.fitsreadonly)modezchecksum.fits)	r   r   r6   rV   OSErrorr   r   rW   NotImplementedError)r   Zin1Zin2r   r   r   test_printdiff   s4    



,6zTestConvenience.test_printdiffc                 C   s  d}d}|  d t| d tj| ds8J tj| d| dd tj| dshJ | d}| d}t| d|| tj|sJ t| }| |ksJ W d	   n1 s0    Y  t| }| |ksJ W d	   n1 s0    Y  d	S )
z
        A simple test of the dump method.
        Also regression test for https://github.com/astropy/astropy/issues/6937
        z"                    1" "abc" "     3.70000007152557" "                    0"
"                    2" "xy " "     6.69999971389771" "                    1"
a  c1 1J I11              ""               ""               -2147483647      ""               ""              
c2 3A A3               ""               ""               ""               ""               ""              
c3 1E G15.7            ""               ""               ""               3                0.4             
c4 1L L6               ""               ""               ""               ""               ""              
tb.fitsztb_1.txtztest_tb.txt)datafiledata.txtcoldefs.txtN)		copy_filer   	tabledumpr?   ospathisfiler   read)r   ZdatastrZcdstrr   cdfiler   Zcoldefsr   r   r   test_tabledump   s"    



.
zTestConvenience.test_tabledump	tablenamez
table.fitsr   c                 C   s   |  | | d}| d}| d}t| |||| t|||}t| |}t||d  W d   n1 s0    Y  dS )z
        A simple test of the dump/load methods; dump the data, column, and
        header files and try to reload the table from them.
        r   r   z
header.txtr   N)r   r?   r   r   Z	tableloadr   r
   )r   r   r   r   ZhfileZ	new_tbhduhdulr   r   r   test_dump_load_round_trip  s    



z)TestConvenience.test_dump_load_round_tripc                 C   s   t d}| d}tj||dd tj||d ddd tj||d ddd	 tj|dd
T}t j|d j| t j|d j|d  t j|d j|d  W d   n1 s0    Y  dS )z<
        Test fits.append with a filename argument.
        rw   test_append_1.fitsT)r   checksumr#   )r   r   Zignore_blankr$   F)r   r   Zverifyr   r   r   N)	rI   r   r?   r   appendr   rn   r   r   )r   r   testfileZhdu1r   r   r   test_append_filename1  s    

z$TestConvenience.test_append_filenamer   )wbzwb+Zabzab+c                 C   sL   | d}||$}tj|tdd W d   n1 s>0    Y  dS )z?
        Test fits.append with a file handle argument.
        zappend.fits)rx   rx   )rD   r   N)joinr   r   r   rI   ones)r   Ztmpdirr   Zappend_fileZhandler   r   r   test_append_filehandleG  s    
z&TestConvenience.test_append_filehandlec                 C   s   |  d}t| d0}|D ]}tj||j|jdd q W d   n1 sP0    Y  tj|dd }t|dkszJ W d   n1 s0    Y  dS )z
        Test fits.append with a fits Header, which triggers detection of the
        HDU class. Regression test for
        https://github.com/astropy/astropy/issues/8660
        r   r   Tr   Nr{   )r?   r   r   r   r   r;   r:   )r   r   r   rC   r   r   r   test_append_with_headerP  s    
6z'TestConvenience.test_append_with_headerc                 C   sx   t | d}td}tt|g}|| t	|$}tj
|d j| W d    n1 sj0    Y  d S )Nz	test.fitsr   r   )pathlibPathr?   rI   r   r   r   r   r@   r   rn   r   r   )r   r   r   hdulistr   r   r   r   test_pathlib^  s    

zTestConvenience.test_pathlibc                 C   s   t jtjdtdd}t jtjdtdd}t jdtjdtd d}t |||g}t	 }|
| dD ],}|d t j||d}|d |kslJ qld S )	Nr{   r{   r   r   r#   )r   r   r#   r   r   r   r   )r   r   rI   r   r   r   r   r   ioBytesIOr@   r   r   )r   prihduexthdu1exthdu2r   bufr   r   r   r   r   test_getdata_ext_giveng  s    

z&TestConvenience.test_getdata_ext_givenc                 C   s   t jtjdtdd}t jtjdtdd}t jd d}t |||g}t	 }|
| |d tjtdd t j|dd W d    n1 s0    Y  d S )	Nr   r   r   r   zNo data in HDU #2.r0   r#   r   )r   r   rI   r   r   r   r   r   r   r   r@   r   r6   rV   
IndexErrorr   r   r   r   r   r   r   r   r   r   test_getdata_ext_given_nodatat  s    

z-TestConvenience.test_getdata_ext_given_nodatac                 C   sx   t jtjdtdd}t jd d}t jd d}t |||g}t }|	| |
d t |}|d dkstJ d S )Nr   r   r   r   r   )r   r   rI   r   r   r   r   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   /test_getdata_ext_not_given_with_data_in_primary  s    


z?TestConvenience.test_getdata_ext_not_given_with_data_in_primaryc                 C   sx   t jd d}t jtjdtdd}t jd d}t |||g}t }|	| |
d t |}|d dkstJ d S )Nr   r   r   r   r   r   )r   r   r   rI   r   r   r   r   r   r@   r   r   r   r   r   r   +test_getdata_ext_not_given_with_data_in_ext  s    


z;TestConvenience.test_getdata_ext_not_given_with_data_in_extc                 C   s   t jd d}t jd d}t jtjdtdd}t |||g}t }|	| |
d tjtdd t | W d    n1 s0    Y  d S )Nr   r   r   r   z2No data in either Primary or first extension HDUs.r0   )r   r   r   rI   r   r   r   r   r   r@   r   r6   rV   r   r   r   r   r   r   %test_getdata_ext_not_given_nodata_any  s    

z5TestConvenience.test_getdata_ext_not_given_nodata_anyc                 C   sp   t jd d}t |g}t }|| |d tjt	dd t 
| W d    n1 sb0    Y  d S )Nr   r   z2No data in Primary HDU and no extension HDU found.r0   )r   r   r   r   r   r@   r   r6   rV   r   r   )r   r   r   r   r   r   r   'test_getdata_ext_not_given_nodata_noext  s    

z7TestConvenience.test_getdata_ext_not_given_nodata_noextN)__name__
__module____qualname__r   r!   rE   rN   rQ   rY   rr   r6   ZmarkZparametrizer   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s6   	
!(

	r   )r   r   r   r   r6   ZnumpyrI   Znumpy.testingr   Z
astropy.ior   Zastropyr   r8   Zastropy.tabler   Zastropy.io.fitsr   Zastropy.io.fits.columnr   Zastropy.io.fits.connectr   Zastropy.utils.exceptionsr	   Z astropy.io.fits.tests.test_tabler
   rH   r   r   r   r   r   r   <module>   s    