a
    ߙfb&S                     @   s   d dl Z d dlZddlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZ d dlmZ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   )FitsTestCase)GLOBAL_TIME_INFOtime_to_fitsis_time_column_keyword)EarthLocation)fits)TableQTableColumn)Time	TimeDelta)BARYCENTRIC_SCALES)FITS_DEPRECATED_SCALES)AstropyUserWarningc                   @   sL  e Zd Zdd Zdd Zejdee	fdd Z
ejdd	d
d	gfejdddd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd Zejdee	fdd  Zd!S )"TestFitsTimec                 C   s4   t ddg| _t ddgddgddggg| _d S )Nz1999-01-01T00:00:00.123456789z2010-01-01T00:00:00r            )nparraytimetime_3dself r   Blib/python3.9/site-packages/astropy/io/fits/tests/test_fitstime.pysetup_class   s    zTestFitsTime.setup_classc                 C   s4   t ddu sJ t ddu s J t ddu s0J d S )NZTRPOSFTIMESYSZTRPOS12T)r   r   r   r   r   test_is_time_column_keyword   s    z(TestFitsTime.test_is_time_column_keywordtable_typesc           
   	   C   sR  | }t | jddd|d< t | jddd|d< tddgdd	gd	d
gdd|d _tjtdd t|\}}W d   n1 s0    Y  |d |d jjj	ddk
 sJ |d |d jjj	ddk
 sJ |d |d jjj	ddk
 sJ tjtdd& |j| dddd W d   n1 s20    Y  t| dd}|dddu sdJ tjtdd& |j| dddd}W d   n1 s0    Y  |d j|d jk
 sJ |d j|d jksJ tddd|d _tddd|d _tt0}t|\}}dt|jv s2J W d   n1 sH0    Y  d|d _tjtdd}t|\}}W d   n1 s0    Y  t|dksJ tddd|d _t|\}}tD ]j}|dt|d | tjtd d}t|\}}W d   n1 s0    Y  t|dksJ q| }tddgdd	gd	d
gggddgdd	gd	d
gggddgdd	gd	d
gggdd}	t | jd!|	d"|d< t|\}}|d |d jjj	ddk
 sJ |d |d jjj	ddk
 sJ |d |d jjj	ddk
 sJ |j| dddd |j| dddd}|d j|d jk
 sfJ t | jtdgggdgggd	gggddd#|d< t|\}}|d |d jjj	ddksJ |d |d jjj	ddksJ |d |d jjj	ddksJ |j| dddd |j| dddd}|d j|d jksNJ dS )$zk
        Test all the unusual conditions for locations of ``Time``
        columns in a ``Table``.
        isotutcformatscaleattbg      ?g       @g      @g      @ZMmunitNTime Column "b" has no specified location, but global Time Position is presentmatchNOBSGEO-XmOBSGEO-YOBSGEO-Z	time.fitsr   Tr$   	overwriter   ZTRPOS2zTime column reference position "TRPOSn" is not specified. The default value for it is "TOPOCENTER", and the observatory position has been specified.r$   astropy_nativer   r   r   z/Multiple Time Columns with different geocentricz+Earth Location "TOPOCENTER" for Time ColumnZjd)r$   location)r7   )r   r   r   r7   pytestwarnsr   r   xto_valueallyzwritetempr   Z	getheadergetreadZraises
ValueErrorstrvaluelenr   Zreplace_columngetattrr   )
r   r    ttablehdrtmerrwr%   r7   r   r   r   test_time_to_fits_loc"   s    *$$$6&4
,,&&&
"""z"TestFitsTime.test_time_to_fits_locmaskFTserialize_method)jd1_jd2formatted_valuec                 C   s  t tjj| j|d}t tjjddgddggt|dddd}|j|j  u r^|d	uu sdn J t||gd
dgd}|j| 	ddd|d tj
| 	dddd}|d	urt|d
 j|jksJ t|d j|jksJ |dkr>t|d
 t rt|d
 |ksJ t|d t r8t|d |ksJ nTt|d
 trdt|d
 |jkshJ t|d trt|d |jksJ dS )zO
        Test the data returned by ``time_to_fits`` for masked values.
        )rO   r   r   r   r   )r   r   cxcsecr$   Fr&   r(   )namesr2   r   T)r$   r4   rP   r5   rQ   N)r   r   ZmaZMaskedArrayr   Zbroadcast_toZmaskedr
   r?   r@   rB   r<   rO   
isinstancer   rE   )r   rP   rO   r&   r(   rH   rK   r   r   r   "test_time_to_fits_serialize_method   s$    $"
(**z/TestFitsTime.test_time_to_fits_serialize_methodc           
   
   C   s  | }t | jddtdddddd|d	< t d
dgddd|d< |d	 jjj|d	 jjj|d	 jjjd}tj	t
dd t|\}}W d   n1 s0    Y  t D ]<\}}|| |d ksJ |j| |d
 ksJ || q| D ]$\}}|| |ks
J || q|jd }|D ]<}	||	 d ||	 j ksHJ ||	 d dks$J q$|d	 d dksxJ |d d dksJ t|dksJ dS )zL
        Test the header and metadata returned by ``time_to_fits``.
        r!   r"   隧@ 9> r/   r)   r$   r%   r7   r&   r   r   rS   r'   r#   r(   )r.   r0   r1   r+   r,   Nr   Z__coordinate_columns__
coord_type
coord_unitdtime_ref_pos
TOPOCENTER)r   r   r   r7   r:   rE   r=   r>   r8   r9   r   r   r   itemsZcommentsremovemetar%   upperrF   )
r   r    rH   Zideal_col_hdrrI   rJ   keyrE   Z
coord_infocolnamer   r   r   test_time_to_fits_header   s6    

*
 z%TestFitsTime.test_time_to_fits_headerc                 C   s  | }t | jddd|d< d|jd< d|jd< |j| d	d
dd |j| d	d
dd}t|jd t snJ |jd j|jd ksJ |jd jd
ksJ |jd j	dksJ t|jd t sJ |jd j|jd ksJ |jd jdksJ |jd j	dksJ d|jd< |j| d	d
dd |j| d	d
dd}t|jd t sTJ |jd j|jd kspJ |jd j	dksJ t|jd t sJ |jd j|jd ksJ |jd j	t
|jd  ksJ d|d jjd
< |j| d	d
dd |j| d	d
d}t|jd t r(J |jd |jd ksBJ t|jd t rXJ |jd |jd ksrJ |d |d jk sJ dS )zn
        Test that the relevant global time metadata is read into
        ``Table.meta`` as ``Time``.
        r!   r"   r#   r&   z1999-01-01T00:00:00ZDATEi^  zMJD-OBSr2   r   Tr3   r5   mjdZETr   rR   rT   N)r   r   rc   r?   r@   rB   rV   rE   r$   r%   r   inforP   r<   )r   r    rH   rK   r   r   r   test_fits_to_time_meta   sF    


 z#TestFitsTime.test_fits_to_time_metac              
   C   sX  | }t | jddtdddddd|d	< t|\}}|d
 |d	 jjjddksTJ |d |d	 jjjddkstJ |d |d	 jjjddksJ |j	| 
dddd |j| 
dddd}|d	 j|d	 jk sJ |d	 jjj|d	 jjjddksJ |d	 jjj|d	 jjjddks,J |d	 jjj|d	 jjjddksTJ dS )z
        Test that ``location`` specified by using any valid unit
        (length/angle) in ``Time`` columns gets stored in FITS
        as ITRS Cartesian coordinates (X, Y, Z), each in m.
        Test that it round-trips through FITS.
        r!   r"   r   r   r   Zkmr)   r[   r&   r.   r/   r0   r1   r2   r   Tr3   r5   N)r   r   r   r   r7   r:   r;   r=   r>   r?   r@   rB   r<   rE   )r   r    rH   rI   rJ   rK   r   r   r   test_time_loc_unit
  s     

   ((zTestFitsTime.test_time_loc_unitc                 C   s   | }t | jddd|d< ddg|d< dd	g|d
< |d |d |j| dddd |j| dddd}t|d t sJ t|jdksJ t|jdksJ dD ]4}t|| j	jdksJ t|| j	jdksJ qdS )zg
        Ensure that fits_to_time works correctly if the time column is also
        an index.
        r!   r"   r#   r&   r   r   r(   r   r   cr2   r   Tr3   r5   r   )r&   r(   N)
r   r   Z	add_indexr?   r@   rB   rV   rF   indicesri   )r   r    rH   rK   namer   r   r   test_fits_to_time_index'  s     

z$TestFitsTime.test_fits_to_time_indexc                 C   s   |  d}tjtdd |j|dd}W d   n1 s<0    Y  t|d tsXJ |d jdksjJ |d jd	ks|J ||}t|j	d
 d	|j	d 
 d}t|d }|| |d k sJ dD ]}t|| trJ qdS )a  
        Test that FITS table with time columns (standard compliant)
        can be read by io.fits as a table with Time columns.
        This tests the following:

        1. The special-case where a column has the name 'TIME' and a
           time unit
        2. Time from Epoch (Reference time) is appropriately converted.
        3. Coordinate columns (corresponding to coordinate keywords in the header)
           other than time, that is, spatial coordinates, are not mistaken
           to be time.
        zchandra_time.fitsz5Time column "time" reference position will be ignoredr,   Tr6   Nr   r'   rh   ZMJDREFr   r#   )ZchipxZchipyZdetxZdetyr:   r=   )datar8   r9   r   rB   rV   r   r%   r$   rc   lowerr   r<   )r   r    filenamerK   Z
non_nativeZref_timeZ
delta_timerf   r   r   r   test_io_time_read_fitsG  s    
,
z#TestFitsTime.test_io_time_read_fitsc                 C   s   t jdddd| jd}t j|g}|j| ddd |j| ddd	}t|d t	s^J |d j
d
kspJ |d jdksJ |d | jk sJ dS )zP
        Test that ISO-8601 Datetime String Columns are read correctly.
        datetimeA29ZTCGZ	GEOCENTERrn   r$   r\   r_   r   r2   Tr4   rp   Ztcgr   N)r   r   r   BinTableHDUfrom_columnswritetor@   rB   rV   r   r%   r$   r<   )r   r    rl   bhdurK   r   r   r   test_io_time_read_fits_datetimek  s    
z,TestFitsTime.test_io_time_read_fits_datetimec                 C   sd  t jdddd| jd}g d}t jj|gt |d}|j| dd	d
 |j| dd	d}t	|d t
spJ |d jjjdksJ |d jjjdksJ |d jjjdksJ g d}t jj|gt |d}|j| dd	d
 |j| dd	d}t	|d t
sJ |d jjjdks&J |d jjjdks>J tj|d jjjdddds`J dS )z{
        Test that geocentric/geodetic observatory position is read
        properly, as and when it is specified.
        ru   rv   TTr`   rw   ))r.   rX   )r0   rY   )r1   rZ   headerr2   Trx   rp   rX   rY   rZ   )zOBSGEO-Lr   )zOBSGEO-Br   )zOBSGEO-Hr   r   g&.>)ZrtolZatolN)r   r   r   ry   rz   Headerr{   r@   rB   rV   r   r7   r:   rE   r=   r>   ZlonZlatr   ZiscloseZheight)r   r    rl   cardsr|   rK   r   r   r   test_io_time_read_fits_location  s*    
z,TestFitsTime.test_io_time_read_fits_locationc           	   	   C   s  t ddg}tjdddddd|d}g d	}tjj|gt|d
}|j| ddd t	j
tdd$}|j| ddd}W d   n1 s0    Y  t|dksJ t|d tsJ |d jdksJ |d jdksJ |d j|k sJ t ddg}tjdddddd|d}tj|g}|j| ddd |j| ddd}t|d tshJ |d jdks|J |d jdksJ |d j|k sJ dS )z
        Test handling of 'GPS' and 'LOCAL' time scales which are
        recognized by the FITS standard but are not native to astropy.
        i%i%gps_timeDsZGPSr`   )rn   r$   r*   r\   r]   r_   r   r   r   r2   Trx   z&FITS recognized time scale value "GPS"r,   rp   Nr   ZgpsZtair   
local_timer^   ZLOCALZRELOCATABLErh   local)r   r   r   r   ry   rz   r   r{   r@   r8   r9   r   rB   rF   rV   r   r$   r%   rE   r<   )	r   r    r   rl   r   r|   rM   rK   r   r   r   r   test_io_time_read_fits_scale  s4    2
z)TestFitsTime.test_io_time_read_fits_scalec                 C   s   t jdddd| jd}t j|g}|j| ddd tjt	d	d
$}|j
| ddd W d   n1 sp0    Y  t|dksJ t jddd| jd}t j|g}|j| ddd |j
| ddd dS )zC
        Test warnings for time column reference position.
        ru   rv   r~   r`   rw   r2   Trx   z.observatory position is not properly specifiedr,   rp   Nr   )rn   r$   r\   r   )r   r   r   ry   rz   r{   r@   r8   r9   r   rB   rF   )r   r    rl   r|   rM   r   r   r   (test_io_time_read_fits_location_warnings  s    
2
z5TestFitsTime.test_io_time_read_fits_location_warningsN)__name__
__module____qualname__r   r   r8   ZmarkZparametrizer	   r
   rN   rW   rg   rj   rk   ro   rt   r}   r   r   r   r   r   r   r   r      s2   

e
'
?


#

'
)r   )r8   Znumpyr    r   Zastropy.io.fits.fitstimer   r   r   Zastropy.coordinatesr   Z
astropy.ior   Zastropy.tabler	   r
   r   Zastropy.timer   r   Zastropy.time.corer   Zastropy.time.formatsr   Zastropy.utils.exceptionsr   r   r   r   r   r   <module>   s   