
    HR-el                        d Z ddlm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mZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ g dZd Zd Zd Zd Zd Zd Z e g dg dg dg d          Z!d Z"d Z#d Z$d Z%d Z&d Z'dS )z
This module tests some methods related to ``CDS`` format
reader/writer.
Requires `pyyaml <https://pyyaml.org/>`_ to be installed.
    )StringION)units)SkyCoord)ascii)ColumnMaskedColumnTable)Time)get_pkg_data_filename)AstropyWarning   )assert_almost_equal)znames e d s izHD81809 1E-7 22.25608 +2 67z#HD103095 -31.6e5 +27.2500 -9E34 -30c                  @   g d} t          dd          }t          j        |d          }t                      }|                    |d           |                                                                }|                    d          }||d         }|| k    sJ dS )	a  
    Tests whether or not the CDS writer can roundtrip a table,
    i.e. read a table to ``Table`` object and write it exactly
    as it is back to a file. Since, presently CDS uses a
    MRT format template while writing, only the Byte-By-Byte
    and the data section of the table can be compared between
    original and the newly written table.

    Further, the CDS Reader does not have capability to recognize
    column format from the header of a CDS/MRT table, so this test
    can work for a limited set of simple tables, which don't have
    whitespaces in the column values or mix-in columns. Because of
    this the written table output cannot be directly matched with
    the original file and have to be checked against a list of lines.
    Masked columns are read properly though, and thus are being tested
    during round-tripping.

    The difference between ``cdsFunctional2.dat`` file and ``exp_output``
    is the following:
        * Metadata is different because MRT template is used for writing.
        * Spacing between ``Label`` and ``Explanations`` column in the
            Byte-By-Byte.
        * Units are written as ``[cm.s-2]`` and not ``[cm/s2]``, since both
            are valid according to CDS/MRT standard.
    )P================================================================================+Byte-by-byte Description of file: table.datP--------------------------------------------------------------------------------+ Bytes Format Units  Label     Explanationsr   zE 1- 7  A7       ---    ID       Star ID                              zE 9-12  I4       K      Teff     [4337/4654] Effective temperature    zE14-17  F4.2   [cm.s-2] logg     [0.77/1.28] Surface gravity          zE19-22  F4.2     km.s-1 vturb    [1.23/1.82] Micro-turbulence velocityzE24-28  F5.2     [-]    [Fe/H]   [-2.11/-1.5] Metallicity             zE30-33  F4.2     [-]    e_[Fe/H] ? rms uncertainty on [Fe/H]          r   Notes:r   z!S05-5   4337 0.77 1.80 -2.07     z!S08-229 4625 1.23 1.23 -1.50     z!S05-10  4342 0.91 1.82 -2.11 0.14z!S05-47  4654 1.28 1.74 -1.64 0.16zdata/cdsFunctional2.datzastropy.io.ascii.tests)package	ascii.mrtformatr   N)r   r	   readr   writegetvalue
splitlinesindex)
exp_outputdattoutlinesi_bbbs         ?lib/python3.11/site-packages/astropy/io/ascii/tests/test_cds.pytest_roundtrip_mrt_tabler%      s    4  J(  !+C  C 	
3{+++A
**CGGCG$$$LLNN%%''EKK!!E%&&MEJ    c                  >   t          j        t                    } d t          j        t          j        t          j        t          j        z  t          j        g}| 	                    d|           t          j
        d          t          j
        d          g| d<   |                    t          j                   t                      }|                     |d           t          j        |                                d          fdj        D             |k    sJ d S )	Nunit   i	magnituder   r   cdsc                 *    g | ]}|         j         S  r(   ).0coltReads     r$   
<listcomp>z1test_write_byte_by_byte_units.<locals>.<listcomp>d   s    555E#JO555r&   )r   r   test_datuCkgmsyear_set_column_attribute	Magnitudeappendmagr   r   r   columns)r    	col_unitsr!   r1   s      @r$   test_write_byte_by_byte_unitsr@   X   s    
8AqsAD!#)QV4IFI... k"ooq{27AkNQU
**CGGCG$$$Js||~~e444E5555u}555BBBBBBr&   c                      g d} t          j        t                    }t                      }|                    |d           |                                                                | k    sJ d S )N)zTitle:zAuthors:zTable:r   r   r   r   r   ? 1- 8  A8     ---    names   Description of names              ?10-14  E5.1   ---    e       [-3160000.0/0.01] Description of e?16-23  F8.5   ---    d       [22.25/27.25] Description of d    ?25-31  E7.1   ---    s       [-9e+34/2.0] Description of s     ?33-35  I3     ---    i       [-30/67] Description of i         r   r   r   z$HD81809  1e-07  22.25608   2e+00  67z$HD103095 -3e+06 27.25000  -9e+34 -30r   r   )r   r   r3   r   r   r   r   )r   r    r!   s      r$   &test_write_readme_with_default_optionsrG   g   sn      J( 	
8A
**CGGCG$$$<<>>$$&&*444444r&   c                      t                      } dd l} |j        t                    5  t	                                          | d           d d d            d S # 1 swxY w Y   d S )Nr   r   r   )r   pytestraisesNotImplementedErrorr	   r   )r!   rI   s     r$   test_write_empty_tablerL      s    
**CMMM	*	+	+ / /c+.../ / / / / / / / / / / / / / / / / /s   $AA Ac                  t   g d} t          j        t                    }|                    g dg d           t	                      }|                    |d           |                                                                }d t          |          D             }||d         d	z   d          }|| k    sJ d S )
N)z%HD81809  1e-07  22.25608  2.0e+00  67z%HD103095 -3e+06 27.25000 -9.0e+34 -30z%Sun                       5.3e+27    )Sunz3.250z5.3e272)FTTFTmaskr   r   c                 B    g | ]\  }}|                     d           |S )z------z=======
startswithr/   ir8   s      r$   r2   z/test_write_null_data_values.<locals>.<listcomp>   .    TTTDAq=R0S0STaTTTr&   r   )	r   r   r3   add_rowr   r   r   r   	enumerater   r    r!   r"   i_secss        r$   test_write_null_data_valuesr_      s      J
 	
8AII+++2R2R2R     **CGGCG$$$LLNN%%''ETTIe,,TTTF&*q.""#EJr&   c                     g d} t          j        t                    }|                    ddgd           |                    ddgd           t	          |d         dd	g
          |d<   t	          |d         d	d	g
          |d<   t                      }|                    |d           |                                                                }|	                    d          }||d         }|| k    sJ dS )a	  
    This test differs from the ``test_write_null_data_values``
    above in that it tests the column value limits in the Byte-By-Byte
    description section for columns whose values are masked.
    It also checks the description for columns with same values.
    )r   r   r   r   r   z; 1- 8  A8     ---    names   Description of names          z;10-14  E5.1   ---    e       [0.0/0.01]? Description of e  z;16-17  F2.0   ---    d       ? Description of d            z;19-25  E7.1   ---    s       [-9e+34/2.0] Description of s z;27-29  I3     ---    i       [-30/67] Description of i     z;31-33  F3.1   ---    sameF   [5.0/5.0] Description of sameFz;35-36  I2     ---    sameI   [20] Description of sameI     r   r   r   z"HD81809  1e-07    2e+00  67 5.0 20z"HD103095         -9e+34 -30 5.0 20      @sameFname   sameIeFTrQ   dr   r   r   N)
r   r   r3   
add_columnr   r   r   r   r   r   r   r    r!   r"   r#   s        r$   )test_write_byte_by_byte_for_masked_columnrk      s     J& 	
8ALL#s'L***LL"bL(((!C&t}555AcF!C&d|444AcF
**CGGCG$$$LLNN%%''EKK!!E%&&MEJr&   )r   r   r   r   r   rB   rC   rD   rE   rF   ?37-39  F3.1   ---    sameF   [5.0/5.0] Description of sameF    ?41-42  I2     ---    sameI   [20] Description of sameI         ?44-45  I2     h      RAh     Right Ascension (hour)            ?47-48  I2     min    RAm     Right Ascension (minute)          ?50-62  F13.10 s      RAs     Right Ascension (second)          ?   64  A1     ---    DE-     Sign of Declination               ?65-66  I2     deg    DEd     Declination (degree)              ?68-69  I2     arcmin DEm     Declination (arcmin)              ?71-82  F12.9  arcsec DEs     Declination (arcsec)              r   r   r   zSHD81809  1e-07  22.25608   2e+00  67 5.0 20 22 02 15.4500000000 -61 39 34.599996000SHD103095 -3e+06 27.25000  -9e+34 -30 5.0 20 12 48 15.2244072000 +17 46 26.496624000)r   r   r   r   r   rB   rC   rD   rE   rF   rl   rm   rn   ro   rp   rq   rr   rs   rt   r   r   r   zSHD81809  1e-07  22.25608   2e+00  67 5.0 20 12 48 15.2244072000 +17 46 26.496624000ru   )r   r   r   r   r   rB   rC   rD   rE   rF   rl   rm   z?44-59  F16.12 deg    GLON    Galactic Longitude                z?61-76  F16.12 deg    GLAT    Galactic Latitude                 r   r   r   zMHD81809  1e-07  22.25608   2e+00  67 5.0 20 330.071639591690 -45.548080484609zMHD103095 -3e+06 27.25000  -9e+34 -30 5.0 20 330.071639591690 -45.548080484609)r   r   r   r   r   zH 1- 8  A8     ---    names   Description of names                       zH10-14  E5.1   ---    e       [-3160000.0/0.01] Description of e         zH16-23  F8.5   ---    d       [22.25/27.25] Description of d             zH25-31  E7.1   ---    s       [-9e+34/2.0] Description of s              zH33-35  I3     ---    i       [-30/67] Description of i                  zH37-39  F3.1   ---    sameF   [5.0/5.0] Description of sameF             zH41-42  I2     ---    sameI   [20] Description of sameI                  zH44-59  F16.12 deg    ELON    Ecliptic Longitude (geocentrictrueecliptic)zH61-76  F16.12 deg    ELAT    Ecliptic Latitude (geocentrictrueecliptic) r   r   r   zMHD81809  1e-07  22.25608   2e+00  67 5.0 20 306.224208650096 -45.621789850825zMHD103095 -3e+06 27.25000  -9e+34 -30 5.0 20 306.224208650096 -45.621789850825)genericpositive_degalacticeclipticc                     t          j        t                    } |                     ddgd           |                     ddgd           t	          ddt
          j                  }t	          d	d
t
          j                  }t          ||g          ||j        |j	        g}t          |t                    D ]\  }}t          |         }|| d<   t                      }|                     |d           |                                                                }|                    d          }	||	d         }||k    sJ | j        g dk    sJ dS )a  
    There can only be one such coordinate column in a single table,
    because division of columns into individual component columns requires
    iterating over the table columns, which will have to be done again
    if additional such coordinate columns are present.
    ra   rb   rc   re   rf   zt@#nNr.   ghh@g|&1@coordr   r   r   N)namesrg   rh   r8   rX   rb   rf   r}   )r   r   r3   ri   r   r4   degr   rx   geocentrictrueeclipticzipexp_coord_cols_outputr   r   r   r   r   colnames)
r    r}   coordpcolsr0   
coord_typer   r!   r"   r#   s
             r$   test_write_coord_colsr   '  s`    	
8ALL#s'L***LL"bL((( ZAE:::ElKae<<<Fv$	D t%:;; V VZ*:6
'
jj	K((())++H%%eff
"""" zUUUUUUUUV Vr&   c                  8   g d} t          j        t                    }|                    ddgd           |                    ddgd           t	          ddt
          j        	          |d
<   ddg|d<   d|d         _        t                      }|	                    |d           |
                                                                }d t          |          D             }||d         |d                  }|                    d           || k    sJ dS )a  
    Tests the alignment of Byte counts with respect to hyphen
    in the Bytes column of Byte-By-Byte. The whitespace around the
    hyphen is govered by the number of digits in the total Byte
    count. Single Byte columns should have a single Byte count
    without the hyphen.
    )r   r   r   r   r   zE 1- 8  A8     ---    names         Description of names              zE10-21  E12.6  ---    e             [-3160000.0/0.01] Description of ezE23-30  F8.5   ---    d             [22.25/27.25] Description of d    zE32-38  E7.1   ---    s             [-9e+34/2.0] Description of s     zE40-42  I3     ---    i             [-30/67] Description of i         zE44-46  F3.1   ---    sameF         [5.0/5.0] Description of sameF    zE48-49  I2     ---    sameI         [20] Description of sameI         zE   51  I1     ---    singleByteCol [2] Description of singleByteCol  zE53-54  I2     h      RAh           Right Ascension (hour)            zE56-57  I2     min    RAm           Right Ascension (minute)          zE59-71  F13.10 s      RAs           Right Ascension (second)          zE   73  A1     ---    DE-           Sign of Declination               zE74-75  I2     deg    DEd           Declination (degree)              zE77-78  I2     arcmin DEm           Declination (arcmin)              zE80-91  F12.9  arcsec DEs           Declination (arcsec)              r   ra   rb   rc   re   rf   r{   r|   r.   r}      singleByteColz.5Erg   r   r   c                 B    g | ]\  }}|                     d           |S rT   rU   rW   s      r$   r2   z<test_write_byte_by_byte_bytes_col_format.<locals>.<listcomp>u  rY   r&   r   r   N)r   r   r3   ri   r   r4   r   r   r   r   r   r   r\   r<   r]   s        r$   (test_write_byte_by_byte_bytes_col_formatr   M  s     J. 	
8ALL#s'L***LL"bL(((*l???AgJQAoAcFM
**CGGCG$$$LLNN%%''ETTIe,,TTTF&)fRj()E	LLJr&   c                     d} t          j        t                    }|                    ddg           d}|dz  |d         _        d|d         _        t                      }|                    |d	           |                                	                                }d
 t          |          D             }||d         |d                  }|                    d           || 	                                k    sJ dS )ze
    Test line wrapping in the description column of the
    Byte-By-Byte section of the ReadMe.
    aF  ================================================================================
Byte-by-byte Description of file: table.dat
--------------------------------------------------------------------------------
 Bytes Format Units  Label     Explanations
--------------------------------------------------------------------------------
 1- 8  A8     ---    thisIsALongColumnLabel This is a tediously long
                                           description. But they do sometimes
                                           have them. Better to put extra
                                           details in the notes. This is a
                                           tediously long description. But they
                                           do sometimes have them. Better to put
                                           extra details in the notes.
10-14  E5.1   ---    e                      [-3160000.0/0.01] Description of e
16-23  F8.5   ---    d                      [22.25/27.25] Description of d
--------------------------------------------------------------------------------
r8   rX   zqThis is a tediously long description. But they do sometimes have them. Better to put extra details in the notes. r   r~   thisIsALongColumnLabelr   r   c                 B    g | ]\  }}|                     d           |S rT   rU   rW   s      r$   r2   z4test_write_byte_by_byte_wrapping.<locals>.<listcomp>  rY   r&   r   r   r   N)r   r   r3   remove_columnsdescriptionrd   r   r   r   r   r\   r<   )r   r    r   r!   r"   r^   s         r$    test_write_byte_by_byte_wrappingr   |  s    
J" 	
8Ac3Z   	8 
 )1_AgJ.AgJO
**CGGCG$$$LLNN%%''ETTIe,,TTTF&)fRj()E	LLJ))++++++++r&   c                     g d} t                      }dg|d<   t          ddt          j                  }t	          |          |d<   |                    dd	g           |j        |d
<   t          d          |d<   t                      }|	                    |d           |
                                                                }|                    d          }||d         }|| k    sJ dS )z
    Tests conversion to string values for ``mix-in`` columns other than
    ``SkyCoord`` and for columns with only partial ``SkyCoord`` values.
    )r   r   r   r   r   z5  1-  7  A7     ---    name    Description of name   z5  9- 74  A66    ---    Unknown Description of Unknownz5 76-114  A39    ---    Unknown Description of Unknownz5116-138  A23    ---    Unknown Description of Unknownr   r   r   z*HD81809 <SkyCoord (ICRS): (ra, dec) in degz_    (330.564375, -61.65961111)> (0.41342785, -0.23329341, -0.88014294)  2019-01-01 00:00:00.000zrandom  12                                                                 (0.41342785, -0.23329341, -0.88014294)  2019-01-01 00:00:00.000HD81809rd   r{   r|   r.   r}   random   cartz2019-1-1timer   r   r   N)r	   r   r4   r   r   r[   	cartesianr
   r   r   r   r   r   )r   r    r}   r!   r"   r#   s         r$    test_write_mixin_and_broken_colsr     s      J$ 	AAfIZAE:::EAgJIIxnAfIZ  AfI
**CGGCG$$$LLNN%%''EKK!!E%&&MEJr&   c            	      j   g d} t                      }ddg|d<   t          ddgddgt          j        	          |d
<   t          ddgddgt          j        	          |d<   t	                      }t          j        t          d          5  |                    |d           ddd           n# 1 swxY w Y   |	                                
                                }|                    d          }||d         }|dd         | dd         k    sJ t          |dd         | dd                   D ]}\  }}|dd         |dd         k    sJ |dd         |dd         k    sJ t          t          j        |dd         d          t          j        |dd         d                     ~dS )zS
    Tests output for cases when table contains multiple ``SkyCoord`` columns.
    )r   r   r   r   r   5 1- 7  A7     ---    name    Description of name     5 9-10  I2     h      RAh     Right Ascension (hour)  512-13  I2     min    RAm     Right Ascension (minute)z515-27  F13.10 s      RAs     Right Ascension (second)z5   29  A1     ---    DE-     Sign of Declination     z530-31  I2     deg    DEd     Declination (degree)    z533-34  I2     arcmin DEm     Declination (arcmin)    z536-47  F12.9  arcsec DEs     Declination (arcsec)    z549-62  A14    ---    coord2  Description of coord2   r   r   r   z>HD4760   0 49 39.9000000000 +06 24 07.999200000 12.4163 6.407 z>HD81809 22 02 15.4500000000 -61 39 34.599996000 330.564 -61.66HD4760r   rd   Q(@r{   F~@r|   r.   coord1gL
F%(@g]Kt@gT㥛Ġ@gGzNcoord2zMcolumn 2 is being skipped with designation of a string valued column `coord2`matchr   r   Nr   r         *   r    )sep)r	   r   r4   r   r   rI   warnsUserWarningr   r   r   r   r   r   np
fromstring)r   r    r!   r"   r#   abs          r$   test_write_extra_skycoord_colsr     s'     J* 	A9%AfIHj1Hl3KRSRWXXXAhKHj1E6?OOOAhK
**C	)
 
 
 ) )
 	
K((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) LLNN%%''EKK!!E%&&ME":CRC((((E"##J
23300 
 
1"v3B3Bx1RU8####M!ABB%S)))2=122C+H+H+H	
 	
 	
 	

 
s   	B--B14B1c                     g d} t                      }ddg|d<   t          ddgddgt          j        	          |d
<   t	                      }t          j        t          d          5  |                    |dddd           ddd           n# 1 swxY w Y   |	                                
                                }|                    d          }||d         }|| k    sJ dS )zM
    Tests output with custom setting for ``SkyCoord`` (second) columns.
    )r   r   r   r   r   r   r   r   z515-19  F5.2   s      RAs     Right Ascension (second)z5   21  A1     ---    DE-     Sign of Declination     z522-23  I2     deg    DEd     Declination (degree)    z525-26  I2     arcmin DEm     Declination (arcmin)    z528-31  F4.1   arcsec DEs     Declination (arcsec)    r   r   r   zHD4760   0 49 39.90 +06 24 08.0zHD81809 22 02 15.45 -61 39 34.6r   r   rd   r   r{   r   r|   r.   r}   zcThe key.s. {'[RD][AE]s', '[RD][AE]s'} specified in the formats argument do not match a column name.r   r   z05.2fz04.1f)RAsDEs)r   formatsNr   )r	   r   r4   r   r   rI   r   r   r   r   r   r   rj   s        r$   test_write_skycoord_with_formatr      sT     J( 	A9%AfI8Z08\2JQRQVWWWAgJ
**C 
<
 
 
 S S
 	
K1Q1QRRRS S S S S S S S S S S S S S S LLNN%%''EKK!!E%&&MEJs   &BBB)(__doc__ior   numpyr   rI   astropyr   r4   astropy.coordinatesr   
astropy.ior   astropy.tabler   r   r	   astropy.timer
   astropy.utils.datar   astropy.utils.exceptionsr   commonr   r3   r%   r@   rG   rL   r_   rk   dictr   r   r   r   r   r   r   r-   r&   r$   <module>r      s
   
                  ( ( ( ( ( (       5 5 5 5 5 5 5 5 5 5       4 4 4 4 4 4 3 3 3 3 3 3 ' ' ' ' ' '  7 7 7tC C C5 5 56/ / /  $$ $ $N   4  6  *  Ya a a H#V #V #VL, , ,^&, &, &,R% % %P0
 0
 0
f* * * * *r&   