U
    e»{j�6  ã                   @   sf   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„ 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                   @   s’   e Zd ZdZdZedd„ ƒZedd„ ƒZedd„ ƒZ	edd
d„ƒZ
eddd„ƒZeddd„ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd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/>`__.
    )ÚDIVISIONZ
OPEN_PARENZCLOSE_PARENÚ
WHITESPACEZSTARSTARÚSTARZSIGNZUFLOATZLIT10ZUINTÚUNKNOWNZUNITc                  C   s°  ddl m}  i }tƒ }ddddddd	d
dddddddddddddddddddg}g }ddd d!d"d#d$dd%d&d'd(d)d*d+d,dd-d.d/d0g}|| D ]0}|D ]&}|| }t |¡rªq’t| |ƒ||< q’qŠ|D ]}|D ]}| || ¡ qÈqÀd1d2d3d4d5d6d7d8d9d:d;d<d,d)d=d>d?d@dAdBdCg}	|	D ]}
t| |
ƒ||
< �q| jdDgdEdFdG�}|  dH| ¡}||dD< ||dI< dDdIg}|D ]}
| |
¡ �qbdJdKdLdMdNdOdPdQdRdSdTdUdVg}|D ]}|||< �q–|||fS )WNr   )ÚunitsÚAÚCÚcdZeVÚFÚgÚHZHzÚJZJyÚKZlmÚlxÚmZmolÚNZohmZPaZpcZradÚsÚSÚsrÚTÚVÚWZWbÚyÚzÚaÚfÚpÚnÚuÚcÚdÚ ÚdaÚhÚkÚMÚGÚPÚEÚZÚYZangstromZarcminZarcsecZAUZbarnÚbinÚbyteZchanÚcountÚdayÚdegZergZlyrÚmagÚminZphotonZpixelZvoxelÚyrÚCrabFzCrab (X-ray flux))ÚprefixesÚdocgü©ñÒMbP?ÚmCrabÚlogÚlnÚexpÚsqrtÚsinÚcosÚtanÚasinÚacosÚatanÚsinhÚcoshÚtanh)	Zastropyr   ÚsetÚkeywordÚ	iskeywordÚgetattrÚaddZdef_unitÚUnit)r%   ÚnamesZdeprecated_namesÚbasesZdeprecated_basesr;   ÚbaseÚprefixÚkeyZsimple_unitsÚunitr:   r=   Zdeprecated_unitsZ	functionsÚname© rX   úX/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/units/format/ogip.pyÚ_generate_unit_names4   sÞ                            ý                   þ
                 ü           þzOGIP._generate_unit_namesc                    st   ˆ j }d}d}d}d}d}d}dd„ }d	d
„ }	dd„ }
dd„ }dd„ }dd„ }‡ fdd„}d}dd„ }tjddd�S )Nú/z\(z\)z[ 	]+z\*\*z\*c                 S   s   t | jƒ| _| S )zK(((\d+\.?\d*)|(\.\d+))([eE][+-]?\d+))|(((\d+\.\d*)|(\.\d+))([eE][+-]?\d+)?)©ÚfloatÚvalue©ÚtrX   rX   rY   Út_UFLOATw   s    z"OGIP._make_lexer.<locals>.t_UFLOATc                 S   s   t | jƒ| _| S )z\d+)Úintr^   r_   rX   rX   rY   Út_UINT|   s    z OGIP._make_lexer.<locals>.t_UINTc                 S   s   t | jd ƒ| _| S )z
[+-](?=\d)Ú1r\   r_   rX   rX   rY   Út_SIGN�   s    z OGIP._make_lexer.<locals>.t_SIGNc                 S   s   | S )u   [xÃ—]rX   r_   rX   rX   rY   Út_X†   s    zOGIP._make_lexer.<locals>.t_Xc                 S   s   dS )Ú10é
   rX   r_   rX   rX   rY   Út_LIT10Š   s    z!OGIP._make_lexer.<locals>.t_LIT10c                 S   s   dS )z[Uu][Nn][Kk][Nn][Oo][Ww][Nn]NrX   r_   rX   rX   rY   Ú	t_UNKNOWNŽ   s    z#OGIP._make_lexer.<locals>.t_UNKNOWNc                    s   ˆ   | ¡| _| S )z[a-zA-Z][a-zA-Z_]*)Z	_get_unitr^   r_   ©ÚclsrX   rY   Út_UNIT’   s    z OGIP._make_lexer.<locals>.t_UNITr(   c                 S   s   t d| j› �ƒ‚d S )NzInvalid character at col )Ú
ValueErrorZlexposr_   rX   rX   rY   Út_error›   s    z!OGIP._make_lexer.<locals>.t_errorZogip_lextabúastropy/units)ZlextabÚpackage)Ú_tokensr   Úlex)rl   ÚtokensZ
t_DIVISIONZt_OPEN_PARENZt_CLOSE_PARENZt_WHITESPACEZ
t_STARSTARZt_STARra   rc   re   rf   ri   rj   rm   Zt_ignorero   rX   rk   rY   Ú_make_lexerj   s"    zOGIP._make_lexerc                    sˆ   ˆ j }dd„ }dd„ }dd„ }‡ fdd„}d	d
„ }dd„ }dd„ }dd„ }	dd„ }
dd„ }dd„ }dd„ }dd„ }dd„ }tjd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                 S   sT   t | ƒdkr"| d | d  | d< n.t | ƒdkrD| d | d  | d< n| d | d< dS )zÂ
            main : UNKNOWN
                 | complete_expression
                 | scale_factor complete_expression
                 | scale_factor WHITESPACE complete_expression
            é   r   é   r   é   N©Úlen©r#   rX   rX   rY   Úp_main­   s
    z!OGIP._make_parser.<locals>.p_mainc                 S   s   | d | d< dS )z@
            complete_expression : product_of_units
            r   r   NrX   r{   rX   rX   rY   Úp_complete_expression»   s    z0OGIP._make_parser.<locals>.p_complete_expressionc                 S   sr   t | ƒdkrD| d dkr.| d | d  | d< qn| d | d  | d< n*t | ƒdkrb| d d | d< n| 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
            rv   rx   r	   r   rw   r   éÿÿÿÿNry   r{   rX   rX   rY   Úp_product_of_unitsÁ   s    z-OGIP._make_parser.<locals>.p_product_of_unitsc                    s  t | d ƒ}|ˆ jkr2|dkr2td| d › d�ƒ‚t| ƒdkr†|dkrh| d | d d| d    | d	< n| d | d | d   | d	< n„t| ƒdkr¨| d
 | d  | d	< nbt| ƒdkrä|dkrÎ| d d | d	< n| d | d  | d	< n&t| ƒdkrþ| d
 | d	< n| 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.é   rw   g      à?é   r   rx   é   rv   N)ÚstrZ
_functionsrn   rz   )r#   Zp1_strrk   rX   rY   Úp_unit_expressionÒ   s$    ÿ"z,OGIP._make_parser.<locals>.p_unit_expressionc                 S   sh   t | ƒdkrd| d  | d< n| d | d< t | d ¡d dkrdddlm} t d	| d › d
�|¡ dS )a  
            scale_factor : LIT10 power numeric_power
                         | LIT10
                         | signed_float
                         | signed_float power numeric_power
                         | signed_int power numeric_power
            rv   rh   rw   r   r   ç      ð?ç        )ÚUnitsWarningú'ú.' scale should be a power of 10 in OGIP formatN)rz   ÚmathÚlog10Zastropy.units.corer‡   ÚwarningsÚwarn)r#   r‡   rX   rX   rY   Úp_scale_factorø   s    þz)OGIP._make_parser.<locals>.p_scale_factorc                 S   s   d| d< dS )z¹
            division : DIVISION
                     | WHITESPACE DIVISION
                     | WHITESPACE DIVISION WHITESPACE
                     | DIVISION WHITESPACE
            r	   r   NrX   r{   rX   rX   rY   Ú
p_division  s    z%OGIP._make_parser.<locals>.p_divisionc                 S   s   d| d< dS )zÆ
            product : WHITESPACE
                    | STAR
                    | WHITESPACE STAR
                    | WHITESPACE STAR WHITESPACE
                    | STAR WHITESPACE
            ZPRODUCTr   NrX   r{   rX   rX   rY   Ú	p_product  s    z$OGIP._make_parser.<locals>.p_productc                 S   s   d| d< dS )z*
            power : STARSTAR
            ZPOWERr   NrX   r{   rX   rX   rY   Úp_power   s    z"OGIP._make_parser.<locals>.p_powerc                 S   s2   t | ƒdkr"| d | d  | d< n| d | d< dS )zQ
            unit : UNIT
                 | UNIT power numeric_power
            rv   r   rw   r   Nry   r{   rX   rX   rY   Úp_unit&  s    z!OGIP._make_parser.<locals>.p_unitc                 S   sV   t | ƒdkr,tt| d ƒt| d ƒƒ| d< n&t | ƒdkrF| d | d< n| 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�   rx   rv   r   r   N)rz   r   rb   r{   rX   rX   rY   Úp_numeric_power0  s
     z*OGIP._make_parser.<locals>.p_numeric_powerc                 S   s&   t | ƒdkr| d | d< nd| d< dS )z8
            sign : SIGN
                 |
            rx   r   r   r…   Nry   r{   rX   rX   rY   Úp_sign?  s    z!OGIP._make_parser.<locals>.p_signc                 S   s   | d | d  | d< dS )z0
            signed_int : SIGN UINT
            r   rx   r   NrX   r{   rX   rX   rY   Úp_signed_intI  s    z'OGIP._make_parser.<locals>.p_signed_intc                 S   s   | d | d  | d< dS )zY
            signed_float : sign UINT
                         | sign UFLOAT
            r   rx   r   NrX   r{   rX   rX   rY   Úp_signed_floatO  s    z)OGIP._make_parser.<locals>.p_signed_floatc                 S   s
   t ƒ ‚d S )N)rn   r{   rX   rX   rY   Úp_errorV  s    z"OGIP._make_parser.<locals>.p_errorZogip_parsetabrp   )Z	tabmodulerq   )rr   r   Zyacc)rl   rt   r|   r}   r   r„   rŽ   r�   r�   r‘   r’   r“   r”   r•   r–   r—   rX   rk   rY   Ú_make_parser    s     &	


zOGIP._make_parserTc                 C   sb   || j kr<|r6td|› d�t || j | j| j¡ ƒ‚ntƒ ‚|| jkr^t || j | d| j¡ d S )NzUnit 'z&' not supported by the OGIP standard. r   )Ú_unitsrn   r   Zdid_you_mean_unitsZ_deprecated_unitsÚ_to_decomposed_alternativeZunit_deprecation_warning©rl   rV   Údetailed_exceptionrX   rX   rY   Ú_validate_unit[  s(    

üÿÿ

   ÿzOGIP._validate_unitc                 C   s   | j ||d� | j| S )N©rœ   )r�   r™   r›   rX   rX   rY   Ú_parse_unitp  s    zOGIP._parse_unitFc                 C   s’   |  ¡ }z| j|dd�W S  tk
rŒ   z t | jj|| j|d�¡W  Y S  tk
r† } z t|ƒrf‚ ntd|› d�ƒ‚W 5 d }~X Y nX Y nX d S )NFrž   )ÚlexerÚdebugzSyntax error parsing unit 'rˆ   )	ÚstriprŸ   rn   r   rP   Ú_parserÚparseÚ_lexerrƒ   )rl   r   r¡   ÚerX   rX   rY   r¤   u  s     z
OGIP.parsec                 C   s   |  d¡}|  |¡ |S )NZogip)Zget_format_namer�   )rl   rV   rW   rX   rX   rY   Ú_get_unit_name…  s    

zOGIP._get_unit_namec                    s”   g }|j ‡ fdd„d� |D ]l\}}|dkr>| ˆ  |¡¡ qt |¡}d|krn| ˆ  |¡› d|› d�¡ q| ˆ  |¡› d|› �¡ qd	 |¡S )
Nc                    s   ˆ   | d ¡ ¡ S )Nr   )r§   Úlower)Úxrk   rX   rY   Ú<lambda>Ž  ó    z(OGIP._format_unit_list.<locals>.<lambda>)rU   r   r[   z**(ú)z**ú )ÚsortÚappendr§   r   Zformat_powerÚjoin)rl   r   ÚoutrS   ÚpowerrX   rk   rY   Ú_format_unit_list‹  s    
zOGIP._format_unit_listc                 C   sR   t  || j¡}t|tjƒrFt |j¡d dkrFt	 
d|j› d�tj¡ t | |¡S )Nr…   r†   rˆ   r‰   )r   Údecompose_to_known_unitsr§   Ú
isinstancer   ÚCompositeUnitrŠ   r‹   ÚscalerŒ   r�   r‡   r   Ú
_to_string)rl   rV   rX   rX   rY   Ú	to_string›  s    þzOGIP.to_stringc                 C   sf   t  || j¡}t|tjƒr\t |j¡d dkr\|j}t	 	|¡}d|_
t | |¡› d|› d�S t |¡S )Nr…   r†   z (with data multiplied by r¬   )r   r´   r§   rµ   r   r¶   rŠ   r‹   r·   ÚcopyZ_scaler   r¸   )rl   rV   r·   rX   rX   rY   rš   ª  s    
ÿzOGIP._to_decomposed_alternativeN)T)T)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rr   ÚstaticmethodrZ   Úclassmethodru   r˜   r�   rŸ   r¤   r§   r³   r¹   rš   rX   rX   rX   rY   r      s.   
5
5
 ;


r   )r¾   rº   rL   rŠ   rŒ   Ú	fractionsr   Zastropy.utilsr   r(   r   r   r   ÚGenericr   rX   rX   rX   rY   Ú<module>   s   