
    IR-e5                     |    d 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
mZmZ  G d dej                  ZdS )	z
Handles units in `Office of Guest Investigator Programs (OGIP)
FITS files
<https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/>`__.
    N)Fraction)parsing   )coregenericutilsc                       e Zd ZdZdZed             Zed             Zed             Z	edd            Z
edd            Zedd
            Ze fd            Zed             Zed fd	            Ze fd            Z xZS )OGIPz
    Support the units in `Office of Guest Investigator Programs (OGIP)
    FITS files
    <https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/>`__.
    )DIVISION
OPEN_PARENCLOSE_PAREN
WHITESPACESTARSTARSTARSIGNUFLOATLIT10UINTUNKNOWNUNITc                  
   ddl m}  i }t                      }g d}g }g d}||z   D ]4}|D ]/}||z   }t          j        |          rt          | |          ||<   05|D ]}|D ]}|                    ||z               g d}	|	D ]}
t          | |
          ||
<   |                     dgdd	          }|                     d
|z            }||d<   ||d<   ddg}|D ]}
|                    |
           g d}|D ]}|||<   |||fS )Nr   )units)ACcdeVFgHHzJJyKlmlxmmolNohmPapcradsSsrTVWWb)yzafpnur&   cd dahkMGr0   PEZY)angstromarcminarcsecAUbarnbinbytechancountdaydegergrB   r?   lyrmagminphotonpixelvoxelyrCrabFzCrab (X-ray flux))prefixesdocgMbP?mCrab)loglnexpsqrtsincostanasinacosatansinhcoshtanh)	astropyr   setkeyword	iskeywordgetattradddef_unitUnit)r:   namesdeprecated_namesbasesdeprecated_basesr[   baseprefixkeysimple_unitsunitrZ   r]   deprecated_units	functionsnames                   9lib/python3.11/site-packages/astropy/units/format/ogip.py_generate_unit_nameszOGIP._generate_unit_names4   s   &&&&&&55
 
 

 
 
 

 ,, 	- 	-D" - -tm$S)) $Q__c

	-
 % 	4 	4D" 4 4 $$Vd]33334
 
 
 ! 	+ 	+D!!T**E$KK zz6(U8KzLLv}%%fg"G,$ 	' 	'D  &&&&
 
 
	  	 	DE$KK&	11    c                       j         }d}d}d}d}d}d}d }d }	d	 }
d
 }d }d } fd}d}d }t          j        dd          S )N/z\(z\)z[ 	]+z\*\*z\*c                 8    t          | j                  | _        | S )zK(((\d+\.?\d*)|(\.\d+))([eE][+-]?\d+))|(((\d+\.\d*)|(\.\d+))([eE][+-]?\d+)?)floatvaluets    r   t_UFLOATz"OGIP._make_lexer.<locals>.t_UFLOATw   s    AGnnAGHr   c                 8    t          | j                  | _        | S )z\d+)intr   r   s    r   t_UINTz OGIP._make_lexer.<locals>.t_UINT|   s    !'llAGHr   c                 >    t          | j        dz             | _        | S )z
[+-](?=\d)1r   r   s    r   t_SIGNz OGIP._make_lexer.<locals>.t_SIGN   s    AGcM**AGHr   c                     | S )u   [x×] r   s    r   t_XzOGIP._make_lexer.<locals>.t_X   s    Hr   c                     dS )10
   r   r   s    r   t_LIT10z!OGIP._make_lexer.<locals>.t_LIT10   s    2r   c                     dS )z[Uu][Nn][Kk][Nn][Oo][Ww][Nn]Nr   r   s    r   	t_UNKNOWNz#OGIP._make_lexer.<locals>.t_UNKNOWN   s    4r   c                 <                         |           | _        | S )z[a-zA-Z][a-zA-Z_]*)	_get_unitr   )r   clss    r   t_UNITz OGIP._make_lexer.<locals>.t_UNIT   s    mmA&&AGHr   r=   c                 0    t          d| j                   )NzInvalid character at col )
ValueErrorlexposr   s    r   t_errorz!OGIP._make_lexer.<locals>.t_error   s    CCCDDDr   ogip_lextabastropy/units)lextabpackage)_tokensr   lex)r   tokens
t_DIVISIONt_OPEN_PARENt_CLOSE_PARENt_WHITESPACE
t_STARSTARt_STARr   r   r   r   r   r   r   t_ignorer   s   `                r   _make_lexerzOGIP._make_lexerj   s    

	 	 	
	 	 	
	 	 	
	 	 		 	 		 	 		 	 	 	 	 	E 	E 	E {-IIIIr   c                       j         }d }d }d } f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
        `Specification of Physical Units within OGIP FITS files
        <https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/>`__,
        which is not terribly precise.  The exact grammar is here is
        based on the YACC grammar in the `unity library
        <https://bitbucket.org/nxg/unity/>`_.
        c                     t          |           dk    r| d         | d         z  | d<   dS t          |           dk    r| d         | d         z  | d<   dS | d         | d<   dS )z
            main : UNKNOWN
                 | complete_expression
                 | scale_factor complete_expression
                 | scale_factor WHITESPACE complete_expression
               r      r      Nlenr8   s    r   p_mainz!OGIP._make_parser.<locals>.p_main   se     1vv{{tad{!Q1tad{!t!r   c                     | d         | d<   dS )z@
            complete_expression : product_of_units
            r   r   Nr   r   s    r   p_complete_expressionz0OGIP._make_parser.<locals>.p_complete_expression   s     Q4AaDDDr   c                     t          |           dk    r8| d         dk    r| d         | d         z  | d<   dS | d         | d         z  | d<   dS t          |           dk    r| d         dz  | d<   dS | d         | d<   dS )	a  
            product_of_units : unit_expression
                             | division unit_expression
                             | product_of_units product unit_expression
                             | product_of_units division unit_expression
            r   r   r   r   r   r   Nr   r   s    r   p_product_of_unitsz-OGIP._make_parser.<locals>.p_product_of_units   s     1vv{{Q4:%%Q4!A$;AaDDDQ4!A$;AaDDDQ1trz!t!r   c                 \   t          | d                   }|j        v r|dk    rt          d| d          d          t          |           dk    rG|dk    r"| d         | d         d| d         z  z  z  | d	<   dS | d         | d         | d         z  z  | d	<   dS t          |           dk    r| d
         | d         z  | d	<   dS t          |           dk    r,|dk    r| d         dz  | d	<   dS | d         | d         z  | d	<   dS t          |           dk    r| d
         | d	<   dS | d         | d	<   dS )a  
            unit_expression : unit
                            | UNIT OPEN_PAREN complete_expression CLOSE_PAREN
                            | OPEN_PAREN complete_expression CLOSE_PAREN
                            | UNIT OPEN_PAREN complete_expression CLOSE_PAREN power numeric_power
                            | OPEN_PAREN complete_expression CLOSE_PAREN power numeric_power
            r   ra   zThe function 'z8' is valid in OGIP, but not understood by astropy.units.   r   g      ?   r   r      r   N)str
_functionsr   r   )r8   p1_strr   s     r   p_unit_expressionz,OGIP._make_parser.<locals>.p_unit_expression   s]    1YYF''Ff,<,< (QqT ( ( (  
 1vv{{V##Q4!A$31:"66AaDDDQ4!A$!A$,.AaDDDQ1tqt|!Q1V##Q43;AaDDDQ4!A$;AaDDDQ1t!t!r   c                     t          |           dk    rd| d         z  | d<   n| d         | d<   t          j        | d                   dz  dk    r'ddlm} t          j        d	| d          d
|           dS dS )a  
            scale_factor : LIT10 power numeric_power
                         | LIT10
                         | signed_float
                         | signed_float power numeric_power
                         | signed_int power numeric_power
            r   r   r   r   r         ?        )UnitsWarning'.' scale should be a power of 10 in OGIP formatN)r   mathlog10astropy.units.corer   warningswarn)r8   r   s     r   p_scale_factorz)OGIP._make_parser.<locals>.p_scale_factor   s     1vv{{QqTz!t!z!A$#%,,;;;;;;L!LLL      -,r   c                     d| d<   dS )z
            division : DIVISION
                     | WHITESPACE DIVISION
                     | WHITESPACE DIVISION WHITESPACE
                     | DIVISION WHITESPACE
            r   r   Nr   r   s    r   
p_divisionz%OGIP._make_parser.<locals>.p_division  s     AaDDDr   c                     d| d<   dS )z
            product : WHITESPACE
                    | STAR
                    | WHITESPACE STAR
                    | WHITESPACE STAR WHITESPACE
                    | STAR WHITESPACE
            PRODUCTr   Nr   r   s    r   	p_productz$OGIP._make_parser.<locals>.p_product  s     AaDDDr   c                     d| d<   dS )z*
            power : STARSTAR
            POWERr   Nr   r   s    r   p_powerz"OGIP._make_parser.<locals>.p_power  s     AaDDDr   c                 n    t          |           dk    r| d         | d         z  | d<   dS | d         | d<   dS )zQ
            unit : UNIT
                 | UNIT power numeric_power
            r   r   r   r   Nr   r   s    r   p_unitz!OGIP._make_parser.<locals>.p_unit$  s=    
 1vv{{tqt|!t!r   c                     t          |           dk    r;t          t          | d                   t          | d                             | d<   dS t          |           dk    r| d         | d<   dS | d         | d<   dS )a#  
            numeric_power : UINT
                          | signed_float
                          | OPEN_PAREN signed_int CLOSE_PAREN
                          | OPEN_PAREN signed_float CLOSE_PAREN
                          | OPEN_PAREN signed_float division UINT CLOSE_PAREN
            r   r   r   r   r   N)r   r   r   r   s    r   p_numeric_powerz*OGIP._make_parser.<locals>.p_numeric_power.  so     1vv{{AaD		3qt9955!Q1t!t!r   c                 P    t          |           dk    r| d         | d<   dS d| d<   dS )z8
            sign : SIGN
                 |
            r   r   r   r   Nr   r   s    r   p_signz!OGIP._make_parser.<locals>.p_sign=  s1    
 1vv{{t!!r   c                 .    | d         | d         z  | d<   dS )z0
            signed_int : SIGN UINT
            r   r   r   Nr   r   s    r   p_signed_intz'OGIP._make_parser.<locals>.p_signed_intG  s     Q4!A$;AaDDDr   c                 .    | d         | d         z  | d<   dS )zY
            signed_float : sign UINT
                         | sign UFLOAT
            r   r   r   Nr   r   s    r   p_signed_floatz)OGIP._make_parser.<locals>.p_signed_floatM  s    
 Q4!A$;AaDDDr   c                     t                      N)r   r   s    r   p_errorz"OGIP._make_parser.<locals>.p_errorT  s    ,,r   ogip_parsetabr   )	tabmoduler   )r   r   yacc)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s   `               r   _make_parserzOGIP._make_parser   s     	 	 		 	 		 	 	"#	 #	 #	 #	 #	J	 	 	*	 	 		 	 		 	 		 	 		 	 		 	 		 	 		 	 		 	 	 |oOOOOr   Tc           	         || j         vrJ|r:t          d| dt          j        || j         | j        | j                  z             t                      || j        v r)t          j        || j         |         d| j                   d S d S )NzUnit 'z&' not supported by the OGIP standard. r
   )_unitsr   r   did_you_mean_units_deprecated_units_to_decomposed_alternativeunit_deprecation_warningr   r{   detailed_exceptions      r   _validate_unitzOGIP._validate_unitY  s    sz!!! # ITIII.
-6	    !ll"3(((*cj&0N     )(r   c                 J    |                      ||           | j        |         S )Nr   )r   r   r   s      r   _parse_unitzOGIP._parse_unitn  s)    44FGGGz$r   Fc                 N   |                                 }	 |                     |d          S # t          $ rm 	 t          j        | j                            || j        |                    cY S # t          $ r(}t          |          r t          d| d          d }~ww xY ww xY w)NFr   )lexerdebugzSyntax error parsing unit 'r   )	stripr   r   r   rr   _parserparse_lexerr   )r   r-   r   es       r   r   z
OGIP.parses  s    GGII	I ??1???? 	I 	I 	IIy!2!21CJe!2!T!TUUUUU I I Iq66 I$%G1%G%G%GHHH	I	Is,   - 
B$3A.+B$.
B 8#BB  B$c                 t    t                                          |          }|                     |           |S r   )super_get_unit_namer   )r   r{   r~   	__class__s      r   r  zOGIP._get_unit_name  s3    ww%%d++4   r   c                      d|v rd| dnd| S )Nr   z**()z**r   )r   numbers     r   _format_superscriptzOGIP._format_superscript  s"    "%--V]&]]Br   inlinec                 B   t          j        || j                  }t          |t          j                  rHt          j        |j                  dz  dk    r(t          j
        d|j         dt          j                   t                                          ||          S )Nr   r   r   r   )fraction)r   decompose_to_known_unitsr  
isinstancer   CompositeUnitr   r   scaler   r   r   r  	to_string)r   r{   r
  r  s      r   r  zOGIP.to_string  s     -dC4FGGdD.// 	z$*%%+s22R
RRR%  
 ww   999r   c                 h   t          j        || j                  }t          |t          j                  r]t          j        |j                  dz  dk    r=|j        }t          j	        |          }d|_
        t          j        | |           d| dS t                                          |          S )Nr   r   z (with data multiplied by r  )r   r  r  r  r   r  r   r   r  copy_scaler   
_to_stringr  r  )r   r{   r  r  s      r   r   zOGIP._to_decomposed_alternative  s     -dC4FGGdD.// 	z$*%%+s22
y!)#t44XXPUXXX ww  &&&r   )T)F)r  )__name__
__module____qualname____doc__r   staticmethodr   classmethodr   r   r   r   r   r  r  r  r   __classcell__)r  s   @r   r
   r
      s        G 32 32 \32j 3J 3J [3Jj vP vP [vPp    [(       [  I I I [I     [
 C C [C : : : : : [: ' ' ' ' [' ' ' ' 'r   r
   )r  r  rm   r   r   	fractionsr   astropy.utilsr   r=   r   r   r   Genericr
   r   r   r   <module>r     s               ! ! ! ! ! ! " " " " " " " " " "M' M' M' M' M'7? M' M' M' M' M'r   