
    IR-e%                     r    d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ  G d	 d
e          ZdS )z(Handles the CDS string format for units.    N)is_effectively_unity)classpropertyparsing)did_you_mean   )coreutils)Basec                   x    e Zd ZdZdZdZdZdZ ed          d             Z	 ed          d	             Z
 ed          d
             Zed             Zed             Zed             Zed             Zedd            Zedd            Zedd            Zed             Zed fd	            Z xZS )CDSuh  
    Support the `Centre de Données astronomiques de Strasbourg
    <https://cds.unistra.fr/>`_ `Standards for Astronomical
    Catalogues 2.0 <https://vizier.unistra.fr/vizier/doc/catstd-3.2.htx>`_
    format, and the `complete set of supported units
    <https://vizier.unistra.fr/viz-bin/Unit>`_.  This format is used
    by VOTable up to version 1.2.
    .x )PRODUCTDIVISION
OPEN_PARENCLOSE_PARENOPEN_BRACKETCLOSE_BRACKETXSIGNUINTUFLOATUNITDIMENSIONLESST)lazyc                 *    |                                  S N)_generate_unit_namesclss    8lib/python3.11/site-packages/astropy/units/format/cds.py_unitsz
CDS._units6   s    '')))    c                 *    |                                  S r   )_make_parserr    s    r"   _parserzCDS._parser:   s    !!!r$   c                 *    |                                  S r   )_make_lexerr    s    r"   _lexerz
CDS._lexer>   s       r$   c                      ddl m}  ddlm} i }|j                                        D ]\  }}t          || j                  r|||<    |S )Nr   )units)cds)astropyr,   astropy.unitsr-   __dict__items
isinstanceUnitBase)ur-   nameskeyvals        r"   r   zCDS._generate_unit_namesB   so    &&&&&&%%%%%%**,, 	! 	!HC#qz** ! c
r$   c                       j         }d}d}d}d}d}d}d }d }	d	 }
d
 } fd} fd}d}d }t          j        ddt          t          j                            S )Nz\./z\(z\)z\[z\]c                     t          j        d| j                  s!d| _        t	          | j                  | _        nt          | j                  | _        | S )z$((\d+\.?\d+)|(\.\d+))([eE][+-]?\d+)?z[eE\.]r   )researchvaluetypeintfloatts    r"   t_UFLOATz!CDS._make_lexer.<locals>.t_UFLOAT]   sD    9Y00 )ag,,..Hr$   c                 8    t          | j                  | _        | S )z\d+)r?   r=   rA   s    r"   t_UINTzCDS._make_lexer.<locals>.t_UINTf   s    !'llAGHr$   c                 >    t          | j        dz             | _        | S )z
[+-](?=\d)1)r@   r=   rA   s    r"   t_SIGNzCDS._make_lexer.<locals>.t_SIGNk   s    AGcM**AGHr$   c                     | S )u   [x×] rA   s    r"   t_XzCDS._make_lexer.<locals>.t_Xp   s    Hr$   c                 <                         |           | _        | S )u   \%|°|\\h|((?!\d)\w)+	_get_unitr=   rB   r!   s    r"   t_UNITzCDS._make_lexer.<locals>.t_UNITt   s    mmA&&AGHr$   c                 <                         |           | _        | S )z---|-rM   rO   s    r"   t_DIMENSIONLESSz(CDS._make_lexer.<locals>.t_DIMENSIONLESSy   s     mmA&&AGHr$   r   c                 0    t          d| j                   )NzInvalid character at col )
ValueErrorlexposrA   s    r"   t_errorz CDS._make_lexer.<locals>.t_error   s    CCCDDDr$   
cds_lextabastropy/units)lextabpackagereflags)_tokensr   lexr?   r;   UNICODE)r!   tokens	t_PRODUCT
t_DIVISIONt_OPEN_PARENt_CLOSE_PARENt_OPEN_BRACKETt_CLOSE_BRACKETrC   rE   rH   rK   rP   rR   t_ignorerV   s   `               r"   r)   zCDS._make_lexerO   s    	

	 	 		 	 	
	 	 	
	 	 		 	 	 	 	
	 	 	 	 	 	E 	E 	E {#bj//
 
 
 	
r$   c                     | j         }d }d }d }d }d }d }d }d }	d	 }
d
 }d }d }t          j        dd          S )a\  
        The grammar here is based on the description in the `Standards
        for Astronomical Catalogues 2.0
        <https://vizier.unistra.fr/vizier/doc/catstd-3.2.htx>`_, which is not
        terribly precise.  The exact grammar is here is based on the
        YACC grammar in the `unity library <https://purl.org/nxg/dist/unity/>`_.
        c                     ddl m} ddlm} t	          |           dk    r || d         | d         z            | d<   dS t	          |           dk    r || d                   | d<   dS  || d                   | d<   dS )	a  
            main : factor combined_units
                 | combined_units
                 | DIMENSIONLESS
                 | OPEN_BRACKET combined_units CLOSE_BRACKET
                 | OPEN_BRACKET DIMENSIONLESS CLOSE_BRACKET
                 | factor
            r   )dex)Unit   r         N)r/   ri   astropy.units.corerj   len)pri   rj   s      r"   p_mainz CDS._make_parser.<locals>.p_main   s     *)))))//////1vv{{tAaD1Q4K((!Q1s1Q4yy!tAaDzz!r$   c                     | d         | d<   dS )zj
            combined_units : product_of_units
                           | division_of_units
            r   r   NrJ   rp   s    r"   p_combined_unitsz*CDS._make_parser.<locals>.p_combined_units   s    
 Q4AaDDDr$   c                 n    t          |           dk    r| d         | d         z  | d<   dS | d         | d<   dS )z
            product_of_units : unit_expression PRODUCT combined_units
                             | unit_expression
            rm   r   rk   r   Nro   rs   s    r"   p_product_of_unitsz,CDS._make_parser.<locals>.p_product_of_units   s=    
 1vv{{tad{!t!r$   c                 t    t          |           dk    r| d         dz  | d<   dS | d         | d         z  | d<   dS )z
            division_of_units : DIVISION unit_expression
                              | combined_units DIVISION unit_expression
            rk   rl   r   r   Nrv   rs   s    r"   p_division_of_unitsz-CDS._make_parser.<locals>.p_division_of_units   sA    
 1vv{{trz!tad{!r$   c                 \    t          |           dk    r| d         | d<   dS | d         | d<   dS )z
            unit_expression : unit_with_power
                            | OPEN_PAREN combined_units CLOSE_PAREN
            rl   r   r   Nrv   rs   s    r"   p_unit_expressionz+CDS._make_parser.<locals>.p_unit_expression   s5    
 1vv{{t!t!r$   c                 P   t          |           dk    r4| d         dk    rt          d          | d         d| d         z  z  | d<   d
S t          |           dk    r+| d         dk    rt          d          d| d	         z  | d<   d
S t          |           d	k    r| d         | d<   d
S d
S )z
            factor : signed_float X UINT signed_int
                   | UINT X UINT signed_int
                   | UINT signed_int
                   | UINT
                   | signed_float
               rk   
   z*Only base ten exponents are allowed in CDSr   g      $@rm   r   rl   N)ro   rT   rs   s    r"   p_factorz"CDS._make_parser.<locals>.p_factor   s     1vv{{Q42::$%QRRRtdadl*!Q1Q42::$%QRRRqt|!Q1t! r$   c                 n    t          |           dk    r| d         | d<   dS | d         | d         z  | d<   dS )za
            unit_with_power : UNIT numeric_power
                            | UNIT
            rl   r   r   Nrv   rs   s    r"   p_unit_with_powerz+CDS._make_parser.<locals>.p_unit_with_power   s=    
 1vv{{t!tqt|!r$   c                 .    | d         | d         z  | d<   dS )z3
            numeric_power : sign UINT
            r   rl   r   NrJ   rs   s    r"   p_numeric_powerz)CDS._make_parser.<locals>.p_numeric_power        Q4!A$;AaDDDr$   c                 P    t          |           dk    r| d         | d<   dS d| d<   dS )z8
            sign : SIGN
                 |
            rl   r   r   g      ?Nrv   rs   s    r"   p_signz CDS._make_parser.<locals>.p_sign   s1    
 1vv{{t!!r$   c                 .    | d         | d         z  | d<   dS )z0
            signed_int : SIGN UINT
            r   rl   r   NrJ   rs   s    r"   p_signed_intz&CDS._make_parser.<locals>.p_signed_int   r   r$   c                 .    | d         | d         z  | d<   dS )zY
            signed_float : sign UINT
                         | sign UFLOAT
            r   rl   r   NrJ   rs   s    r"   p_signed_floatz(CDS._make_parser.<locals>.p_signed_float   s    
 Q4!A$;AaDDDr$   c                     t                      r   )rT   rs   s    r"   p_errorz!CDS._make_parser.<locals>.p_error  s    ,,r$   cds_parsetabrX   )	tabmodulerZ   )r\   r   yacc)r!   r_   rq   rt   rw   rz   r|   r   r   r   r   r   r   r   s                 r"   r&   zCDS._make_parser   s     	" 	" 	"&	 	 		 	 		# 	# 	#	 	 		 	 	&	$ 	$ 	$	 	 		 	 		 	 		 	 		 	 	 |noNNNNr$   c           	         	 |                      |j                  S # t          $ rd}t          j                    }|j        |j        v r|j        |j                 cY d }~S t          d|j         dt          |                     d }~ww xY w)NzAt col z, )_parse_unitr=   rT   r   get_current_unit_registryaliasesrU   str)r!   rB   eregistrys       r"   rN   zCDS._get_unit  s    	=??17+++ 	= 	= 	=577Hw(***'0000000;qx;;3q66;;<<<	=s     
B
2BB
'BB
c           	          || j         vrF|r6t          d                    |t          || j                                       t                      | j         |         S )Nz3Unit '{}' not supported by the CDS SAC standard. {})r#   rT   formatr   )r!   unitdetailed_exceptions      r"   r   zCDS._parse_unit  sf    sz!!! # IPPl4<<    !ll"z$r$   Fc                    d|v rt          d          t          |t                    s|                    d          }	 |                     |d          S # t           $ rr 	 | j                            || j        |          cY S # t           $ r?}t          |          rt          t          |                    t          d          d }~ww xY ww xY w)N z$CDS unit must not contain whitespaceasciiF)r   )lexerdebugzSyntax error)rT   r2   r   decoder   r'   parser*   )r!   sr   r   s       r"   r   z	CDS.parse$  s    !88CDDD!S!! 	"!!A		5??1???? 	5 	5 	55{((#*E(JJJJJ 5 5 5q66 5$SVV,,,$^444	5	5s/   A 
C!!BC
C:C		CC.8gc                 (   t          j        |          \  }}g }|dvr|                    |           |rE|                    d          sd|z   }|                    d|                     |                      | j                            |          S )N)r   rG   -+10)r	   split_mantissa_exponentappend
startswith_format_superscript_timesjoin)r!   r7   format_specmexpartss         r"   format_exponential_notationzCDS.format_exponential_notation9  s    -c222ILLOOO 	===%% 2XLL;c55b99;;<<<zu%%%r$   c                     |S r   rJ   )r!   numbers     r"   r   zCDS._format_superscriptE  s    r$   c                     t          j        || j                  }|j        s&|j        dk    rdS t          |j        dz            rdS t                                          ||          S )Nr   z---g      Y@%)fraction)r	   decompose_to_known_units_get_unit_namebasesscaler   super	to_string)r!   r   r   	__class__s      r"   r   zCDS.to_stringI  sm     -dC4FGGz 	zQu%dj5&899 sww   999r$   )T)F)r   )__name__
__module____qualname____doc___spacer   _scale_unit_separatorr\   r   r#   r'   r*   staticmethodr   classmethodr)   r&   rN   r   r   r   r   r   __classcell__)r   s   @r"   r   r      s         FFG ]* * * ]" " " ]! ! ! 
 
 \
 7
 7
 [7
r O O [OB = = [=       [  5 5 5 [5( 	& 	& 	& [	&   [ 
: 
: 
: 
: 
: [
: 
: 
: 
: 
:r$   r   )r   r;   astropy.units.utilsr   astropy.utilsr   r   astropy.utils.miscr   r   r   r	   baser
   r   rJ   r$   r"   <module>r      s    / . 				 4 4 4 4 4 4 0 0 0 0 0 0 0 0 + + + + + +              {: {: {: {: {:$ {: {: {: {: {:r$   