
    HR-eF                         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  G d de          ZdS )    N)assert_array_equal)units)fits)	printdiff)REMOVE_KEYWORDS)_assert_attr_col)Table)AstropyUserWarning   )FitsTestCasec                   l   e Zd Zd Zd Zd Zd Zd Zd Zd Z	e
j                            de          d	             Zd
 Zd Zd Zd Zd Ze
j                            dddg          d             Zd Ze
j                            dg d          d             Zd Zd Zd Zd Zd Zd Zd Zd ZdS ) TestConveniencec                     t          j        dt                     t          j        |                     d                    }t          j        |                     d                    }d S )Nalways
test0.fits)warningssimplefilterResourceWarningr   getdatadata	getheader)self_s     Flib/python3.11/site-packages/astropy/io/fits/tests/test_convenience.pytest_resource_warningz%TestConvenience.test_resource_warning   sO    h888L<0011N499\2233    c                    t          |                     d          d          }t          j        |          }|j        rJ |                    d           t          j        |          }|j        rJ |                                 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   s      r   test_fileobj_not_closedz'TestConvenience.test_fileobj_not_closed   sw     <(($//LOO8	q			N18						r   c                 :   t          g dg dg dgg dg d          }d|d         _        d|d	         _        t          j        t          j        d
          5 }t          j        |          }d d d            n# 1 swxY w Y   t          |          dk    sJ |j	        
                    d          |j	        
                    d          k     sJ t          |t          j                  s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   TUNIT1TTYPE2test_table_to_hdu.fitsT	overwrite)r	   unitpytestwarnsuUnitsWarningr   table_to_hdulenheaderindex
isinstanceBinTableHDUtempwriteto)r   tablewhdufilenames        r   test_table_to_hduz!TestConvenience.test_table_to_hdu/   se   YY9!//"""
 
 

  c
&c
\N"K
 
 
 	+#E**C	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 1vv{{{{ z))CJ,<,<X,F,FFFFF#t/0000099566H-----s   A;;A?A?c           	         t           j                            g dg d          }t           j                            g dg d          }t           j                            g dg d          }t           j                            g dg d          }t          ||||gg d	
          }t	          j        |          }t          |t          j                  sJ |j        d         |j	        k    sJ 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          }|                    |d           d S )Nr&   )TFF)maskr)   )FTT)yffffff@      ?y      @        y@      )TFTr-   )r/   sr,   r#   )r2   TNULL1r/   rO    r,   r#   r8   Tr9   )npmaMaskedArrayr	   r   r@   rD   rE   rB   
fill_valuer   r   fillednanrealimagcomplexrF   rG   )r   r/   rO   r,   r#   rH   rJ   rK   s           r   test_masked_table_to_hduz(TestConvenience.test_masked_table_to_hduF   s3   Eiii.B.B.BCCEooo4G4G4GHHE<<<CVCVCVWWEooo4G4G4GHHq!Ql*>*>*>???&&#t/00000z(#q|333338C=!((**55538C=!((2,,77738C=!((26*:*:;;;38C=-qv}}RV/D/DEEE38C=-qv}}RV/D/DEEE38C=!((726263J3J*K*KLLL38C=!((26*:*:;;;99566H-----r   c                 P   t          g dg dg dgg dg d          }t          j                            d          |d         _        t          j        t          d	          5 }t          j	        |           d d d            n# 1 swxY w Y   t          |          d
k    sJ d S )Nr&   r)   r-   r.   r1   testr*   z"The unit 'test' could not be savedr4   r   )r	   r>   coreIrreducibleUnitr;   r<   r=   r
   r   r@   rA   )r   rH   rI   s      r   (test_table_non_stringifyable_unit_to_hduz8TestConvenience.test_table_non_stringifyable_unit_to_hduZ   s    YY9!//"""
 
 

 &0088c
\&J
 
 
 	%e$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 1vv{{{{{{s   %BB
B
c                 `   t          g dg dg dgg dg d          }g d|j        d<   t          j        |          }|j                            d          g dk    sJ t          j        t                    5  |j        	                    d           d	d	d	           d	S # 1 swxY w Y   d	S )
zT
        Regression test for https://github.com/astropy/astropy/issues/6079
        r&   r)   r-   r.   r1   )Thisisr*   commentcommentsrd   N)
r	   metar   r@   rB   getr<   raises
ValueErrorrC   r   rH   rJ   s      r   ,test_table_to_hdu_convert_comment_conventionz<TestConvenience.test_table_to_hdu_convert_comment_conventionh   s    YY9!//"""
 
 

 "@!?!?
:&&z~~i((,J,J,JJJJJ]:&& 	) 	)JZ(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   ;B##B'*B'c                    d}dddddddd	}t          g d
g dg dgg dg d          }|j                            |           t          j        t
          d|           5 }t          j        |          }ddd           n# 1 swxY w Y   t          |          t          |          dz
  k    sJ t          t          |          dd                   D ]&\  }}d| t          ||         j                  v sJ '|j                            d          dk    sJ |j                            d          dk    sJ |j                            d          dk    sJ |j                            d          dk    sJ |j                            d          dk    sJ 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@@NEWTABLEr   r(   	   *   Adams)EXPTIMEXTENSIONNAXISNAXIS1NAXIS2PCOUNTOBSERVERr&   r)   r-   i4r0   f8r1   zMeta-data keyword \w+ will r4   Nr'   Meta-data keyword rs   BINTABLErt   ru      rv   rw   r   rr   )r	   rf   updater<   r=   r
   r   r@   rA   	enumerateliststrmessagerB   rg   rR   testingassert_almost_equal)r   diag	ins_cardsrH   rI   rJ   r/   keys           r   !test_table_to_hdu_filter_reservedz1TestConvenience.test_table_to_hdu_filter_reservedx   sH    L"
 
	 YY9!//$$$
 
 

 	
)$$$\&KT&K&K
 
 
 	+#E**C	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 1vvY!+++++Y" 566 	C 	CFAs---QqT\1B1BBBBBBz~~j))Z7777z~~g&&!++++z~~h''2----z~~h''1,,,,z~~h''1,,,,

&&sz~~i'@'@&IIIIIs   $BB	B	cardc           
         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|j        d<   ||         |j        |<   |j                            |          ||         k    sJ t          j        t
          d| d|           5  t          j        |          }d	d	d	           n# 1 swxY w Y   |j                            |          ||         k    sJ |j                            d          dk    sJ d	S )zT
        Test warning for each keyword in ..connect.REMOVE_KEYWORDS, 1 by 1
        rm   r~      r'      r(   r   r   N)	rs   BITPIXrt   ru   rv   rw   GCOUNTTFIELDSTHEAPTABLE         $   )g      ?g       @g      @r-   
wavelengthfluxr{   f4r1   zMin.Silly WalksORIGINr}   z will r4   )	r	   rf   rg   r<   r=   r
   r   r@   rB   )r   r   r   	res_cardsr   rH   rJ   s          r   test_table_to_hdu_warn_reservedz/TestConvenience.test_table_to_hdu_warn_reserved   s   
 L"

 

	  

 

	 __ooo.(,
 
 

  1
8$T?
4z~~d##y6666\&M4&M&Mt&M&M
 
 
 	+ 	+ #E**C	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+
 z~~d##y6666z~~h''+<<<<<<<s   B99B= B=c                    t          g dg dg dgg dg d          }|j                            dt          j        d          dd	d
d           t          j        t          d          5  t          j	        |          }ddd           n# 1 swxY w Y   |j
                            d          dk    sJ d|j
        vsJ d|j
        vsJ dS )zD
        Test removal of unsupported data types from header
        r&   r)   r-   ry   r1   z
2001-05-26   r   r(   )PRIMARYSECONDAR)OBSDATERAMPTARGETSzBAttribute \S+ of type .+ cannot be added to FITS Header - skippingr4   Nr   r   r   )r	   rf   r   rR   aranger<   r=   r
   r   r@   rB   rg   rj   s      r   %test_table_to_hdu_filter_incompatiblez5TestConvenience.test_table_to_hdu_filter_incompatible   sR    YY9!//$$$
 
 

 	
'	!'(a88 	
 	
 	
 \<
 
 
 	+ 	+
 #E**C	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ z~~i((L8888SZ''''
******s   .BBBc                 $   t          j        ddt          fdt          fg          }t	          j                    }d|d<   |                     d          }t	          j        |||d	           t	          j        |d
          }|d         dk    sJ dS )zT
        Regression test for https://github.com/astropy/astropy/issues/5988
        )r   xyr3   )g      E@zLTU&EANSWERztabhdr42.fitsT)r   rB   r:   r   extrp   N)	rR   zerosfloatintr   HeaderrF   rG   r   )r   r   h_inrK   h_outs        r   test_table_writeto_headerz)TestConvenience.test_table_writeto_header   s     xc5\C:$>???{}}(X99_--XDFFFFxQ///X"$$$$$$r   c                    |                      d          }t          j        t          j        d                    t          j        t          j        d                    g}t          j        t          d          5  t          j	        |t          j        d          t          j
        dg          d           ddd           n# 1 swxY w Y   t          j        |                              |           t          j	        |t          j        d          t          j
        dg          d           t          j        |d	          }|d
         dk    sJ dS )z
        Test that _makehdu correctly includes the header. For example in the
        fits.update convenience function.
        ztwoextension.fits)
   r   zNo such filer4   )WHATd   r   )rB   r   Nr   r   r   )rF   r   
PrimaryHDUrR   r   ImageHDUr<   rh   FileNotFoundErrorr   r   HDUListrG   r   )r   home_is_temprK   hdusr   s        r   "test_image_extension_update_headerz2TestConvenience.test_image_extension_update_header   sx   
 99011 2 233T]28HCUCU5V5VW ],NCCC 	 	K"(8,,T[-5Q5QWX   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	T""8,,,bhx((m_1M1MST	
 	
 	
 	
 xQ///V}######s   =>CCCc                 n   t          |                     d          |                     d                    J t          |                     d          |                     d          d          J t          |                     d          |                     d          d          	 J t          j        t                    5  t          dd	d           ddd           n# 1 swxY w Y   t          j        |                     d
          d          5 }t          j        |                     d          d          5 }t          |d         |d                   J t          j        t                    5  t          |d         |d         d           ddd           n# 1 swxY w Y   t          ||          J t          j        t                    5  t          ||d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zS
        Test that FITSDiff can run the different inputs without crashing.
        zarange.fitsz
blank.fitsNr   r   zo4sp040b0_raw.fitssci)extnamezfakefile.fitszstddata.fitsreadonly)modezchecksum.fits)	r   r   r<   rh   OSErrorr   r   ri   NotImplementedError)r   in1in2s      r   test_printdiffzTestConvenience.test_printdiff  se    =11499\3J3JKKSSSdii..		,0G0GQOOOWWW 		.//		.//  
    ]7## 	L 	L*OUKKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L Ytyy00zBBB 
	+c499_55JGGG 	+3 QQ00888]:.. 5 5c!fc!f!44445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 !c**222]#677 + +c3***+ + + + + + + + + + + + + + +	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+ 
	+s   C&&C*-C**H*8H<F'H'F++H.F+/.HG;/H;G??HG?HH*H	H*H	H**H.1H.c                    d}d}|                      d           t          j        |                     d                     t          j                            |                     d                    sJ t          j        |                     d          |                     d                     t          j                            |                     d                    sJ |                     d          }|                     d          }t          j        |                     d          ||           t          j                            |          sJ t          |          5 }|                                |k    sJ 	 d	d	d	           n# 1 swxY w Y   t          |          5 }|                                |k    sJ 	 d	d	d	           d	S # 1 swxY w 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   	tabledumprF   ospathisfiler   read)r   datastrcdstrr   cdfiler   coldefss          r   test_tabledumpzTestConvenience.test_tabledump0  s&   ^ 	
R 	 	y!!! 	tyy++,,,w~~dii
3344444 	tyy++dii6N6NOOOOw~~dii6677777 99Z((=))tyy++Xv>>>w~~h'''''(^^ 	*t99;;')))))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*&\\ 	+W<<>>U*****	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s$   (FFF*GGG	tablenamez
table.fitsr   c                    |                      |           |                     d          }|                     d          }|                     d          }t          j        |                     |          |||           t          j        |||          }t          j        |                     |                    5 }t          ||d                    ddd           dS # 1 swxY w 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   rF   r   r   	tableloadr   r   )r   r   r   r   hfile	new_tbhduhduls          r   test_dump_load_round_tripz)TestConvenience.test_dump_load_round_tripX  s    	y!!!99Z((=))		,''tyy++XvuEEEN8VU;;	Ytyy++,, 	1YQ000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   <C  C$'C$c                 ^   t          j        d          }|                     d          }t          j        ||d           t          j        ||dz  dd           t          j        ||dz  dd	           t          j        |d
          5 }t           j                            |d         j        |           t           j                            |d         j        |dz             t           j                            |d         j        |dz             ddd           dS # 1 swxY w Y   dS )z<
        Test fits.append with a filename argument.
        r   test_append_1.fitsT)r   checksumr'   )r   r   ignore_blankr(   F)r   r   verifyr   r   r   N)	rR   r   rF   r   appendr   r   r   r   )r   r   r   testfilehdu1s        r   test_append_filenamez$TestConvenience.test_append_filenamel  se    y||99122 	H4$7777 	H4!8dNNNN 	H4!8d5IIIIYx$/// 	B4J))$q',===J))$q',qAAAJ))$q',qAAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	Bs   BD""D&)D&r   )wbzwb+abzab+c                     |dz  }|                     |          5 }t          j        |t          j        d                     ddd           dS # 1 swxY w Y   dS )z?
        Test fits.append with a file handle argument.
        zappend.fits)r   r   )rK   r   N)r   r   r   rR   ones)r   tmp_pathr   append_filehandles        r   test_append_filehandlez&TestConvenience.test_append_filehandle  s    
 .d## 	?vKbgfoo>>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?s   )AAAc                    |                      d          }t          j        |                     d                    5 }|D ]$}t          j        ||j        |j        d           %	 ddd           n# 1 swxY w Y   t          j        |d          5 }t          |          dk    sJ 	 ddd           dS # 1 swxY w 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   )rF   r   r   r   r   rB   rA   )r   r   r   rJ   s       r   test_append_with_headerz'TestConvenience.test_append_with_header  sX    99122Ytyy..// 	K4 K KHch
TJJJJJK	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K Yx$/// 	"4t99>>>>>	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s#   (A22A69A6B77B;>B;c                    t          j        |                     d                    }t          j        d          }t          j        t          j        |          g          }|                    |           t          j	        |          5 }t          j
                            |d         j        |           d d d            d S # 1 swxY w Y   d S )Nz	test.fitsr   r   )pathlibPathrF   rR   r   r   r   r   rG   r   r   r   r   )r   r   r   hdulistr   s        r   test_pathlibzTestConvenience.test_pathlib  s    <		+ 6 677y}}, 5 5677!!!Yx   	>DJ))$q',===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   ,CC	C	c                    t          j        t          j        dt                              }t          j        t          j        dt                              }t          j        dt          j        dt                    z            }t          j        |||g          }t          j	                    }|
                    |           dD ];}|                    d           t          j        ||          }|d         |k    sJ <d S )	Nr   r   r   r   r'   )r   r   r'   r   r   r   r   )r   r   rR   r   r   r   r   r   ioBytesIOrG   r!   r   )r   prihduexthdu1exthdu2r   bufr   r   s           r   test_getdata_ext_givenz&TestConvenience.test_getdata_ext_given  s    bhvS&A&A&ABBB-RWV3%?%?%?@@@-Qs)C)C)C%CDDD,9::jll 	% 	%CHHQKKK<---D:$$$$$	% 	%r   c                 $   t          j        t          j        dt                              }t          j        t          j        dt                              }t          j        d           }t          j        |||g          }t          j	                    }|
                    |           |                    d           t          j        t          d          5  t          j        |d           d d d            d S # 1 swxY w Y   d S )	Nr   r   r   r   zNo data in HDU #2.r4   r'   r   )r   r   rR   r   r   r   r   r   r   r   rG   r!   r<   rh   
IndexErrorr   r   r   r  r  r   r  s         r   test_getdata_ext_given_nodataz-TestConvenience.test_getdata_ext_given_nodata  s'   bhvS&A&A&ABBB-RWV3%?%?%?@@@-T***,9::jll]:-ABBB 	% 	%L!$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   !DD	D	c                    t          j        t          j        dt                              }t          j        d           }t          j        d           }t          j        |||g          }t          j                    }|	                    |           |
                    d           t          j        |          }|d         dk    sJ d S )Nr   r   r   r   r   )r   r   rR   r   r   r   r   r   r   rG   r!   r   r   r   r  r  r   r  r   s          r   /test_getdata_ext_not_given_with_data_in_primaryz?TestConvenience.test_getdata_ext_not_given_with_data_in_primary  s    bhvS&A&A&ABBB-T***-T***,9::jll|C  DzQr   c                    t          j        d           }t          j        t          j        dt
                              }t          j        d           }t          j        |||g          }t          j                    }|	                    |           |
                    d           t          j        |          }|d         dk    sJ d S )Nr   r   r   r   r   r   )r   r   r   rR   r   r   r   r   r   rG   r!   r   r
  s          r   +test_getdata_ext_not_given_with_data_in_extz;TestConvenience.test_getdata_ext_not_given_with_data_in_ext  s    d+++-RWV3%?%?%?@@@-T***,9::jll|C  DzQr   c                    t          j        d           }t          j        d           }t          j        t          j        dt
                              }t          j        |||g          }t          j                    }|	                    |           |
                    d           t          j        t          d          5  t          j        |           d d d            d S # 1 swxY w Y   d S )Nr   r   r   r   z2No data in either Primary or first extension HDUs.r4   )r   r   r   rR   r   r   r   r   r   rG   r!   r<   rh   r  r   r  s         r   %test_getdata_ext_not_given_nodata_anyz5TestConvenience.test_getdata_ext_not_given_nodata_any  s    d+++-T***-RWV3%?%?%?@@@,9::jll]R
 
 
 	 	 L	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   C**C.1C.c                 d   t          j        d           }t          j        |g          }t          j                    }|                    |           |                    d           t          j        t          d          5  t          j
        |           d d d            d S # 1 swxY w Y   d S )Nr   r   z2No data in Primary HDU and no extension HDU found.r4   )r   r   r   r   r   rG   r!   r<   rh   r  r   )r   r   r   r  s       r   'test_getdata_ext_not_given_nodata_noextz7TestConvenience.test_getdata_ext_not_given_nodata_noext  s     d+++,x((jll]R
 
 
 	 	 L	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B%%B),B)N)__name__
__module____qualname__r   r$   rL   r[   r`   rk   r   r<   markparametrizer   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r   r   r   r   r      s       4 4 4
  $. . ... . .(  ) ) ) %J %J %JN [V_55+= += 65+=Z+ + +6
% 
% 
%$ $ $.#+ #+ #+J&+ &+ &+P [[<*CDD1 1 ED1&B B B, [V%?%?%?@@? ? A@?" " "> > >% % %
% 
% 
%
 
 
         r   r   )r   r   r   r   numpyrR   r<   numpy.testingr   astropyr   r>   
astropy.ior   astropy.io.fitsr   astropy.io.fits.connectr    astropy.io.fits.tests.test_tabler   astropy.tabler	   astropy.utils.exceptionsr
   conftestr   r   r  r   r   <module>r"     s    
			 				        , , , , , ,             % % % % % % 3 3 3 3 3 3 = = = = = =       7 7 7 7 7 7 " " " " " "Y Y Y Y Yl Y Y Y Y Yr   