ó
šßÈ[c           @` s½   d  Z  d d l m Z m Z m Z m Z d d l m Z d d l 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 m Z m Z d	 e j f d
 „  ƒ  YZ d S(   u›   
Handles units in `Office of Guest Investigator Programs (OGIP)
FITS files
<https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/>`__.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixN(   t   Fractioni   (   t   coret   generict   utilst   OGIPc           B` sÂ   e  Z d  Z d Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e e
 d „ ƒ Z e e
 d „ ƒ Z e e d „ ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   u¯   
    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/>`__.
    u   DIVISIONu
   OPEN_PARENu   CLOSE_PARENu
   WHITESPACEu   STARSTARu   STARu   SIGNu   UFLOATu   LIT10u   UINTu   UNKNOWNu   UNITc          C` sy  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/ d0 g } xU | | D]I } x@ | D]8 } | | } t j | ƒ rþ qÙ n  t |  | ƒ | | <qÙ WqÌ Wx0 | D]( } x | D] } | j | | ƒ q-Wq Wd1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d, d) d= d> d? d@ dA dB dC g }	 x! |	 D] }
 t |  |
 ƒ | |
 <q˜W|  j dD g dE t dF dG ƒ} |  j	 dX | ƒ } | | dD <| | dJ <dD dJ g } x | D] }
 | j |
 ƒ qWdK dL dM dN dO dP dQ dR dS dT dU dV dW g } x | D] } | | | <qXW| | | f S(Y   Ni   (   t   unitsu   Au   Cu   cdu   eVu   Fu   gu   Hu   Hzu   Ju   Jyu   Ku   lmu   lxu   mu   molu   Nu   ohmu   Pau   pcu   radu   su   Su   sru   Tu   Vu   Wu   Wbu   yu   zu   au   fu   pu   nu   uu   cu   du    u   dau   hu   ku   Mu   Gu   Pu   Eu   Zu   Yu   angstromu   arcminu   arcsecu   AUu   barnu   binu   byteu   chanu   countu   dayu   degu   ergu   lyru   magu   minu   photonu   pixelu   voxelu   yru   Crabt   prefixest   docu   Crab (X-ray flux)i
   iýÿÿÿu   mCrabu   logu   lnu   expu   sqrtu   sinu   cosu   tanu   asinu   acosu   atanu   sinhu   coshu   tanhgü©ñÒMbP?(
   t    R
   t   sett   keywordt	   iskeywordt   getattrt   addt   def_unitt   Falset   Unit(   t   ut   namest   deprecated_namest   basest   deprecated_basesR   t   baset   prefixt   keyt   simple_unitst   unitt   Crabt   mCrabt   deprecated_unitst	   functionst   name(    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _generate_unit_names3   sH    	!'


c         ` sÅ   d d l  m } ˆ  j } d } d } d } d } d } d } d	 „  }	 d
 „  }
 d „  } d „  } d „  } d „  } ‡  f d †  } d } d „  } | j d t d t d ƒ d t j j t ƒ ƒ } | S(   Ni   (   t   lexu   /u   \(u   \)u   [ 	]+u   \*\*u   \*c         S` s   t  |  j ƒ |  _ |  S(   uK   (((\d+\.?\d*)|(\.\d+))([eE][+-]?\d+))|(((\d+\.\d*)|(\.\d+))([eE][+-]?\d+)?)(   t   floatt   value(   t   t(    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_UFLOAT~   s    c         S` s   t  |  j ƒ |  _ |  S(   u   \d+(   t   intR(   (   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_UINTƒ   s    c         S` s   t  |  j d ƒ |  _ |  S(   u
   [+-](?=\d)u   1(   R'   R(   (   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_SIGNˆ   s    c         S` s   |  S(   u   [xÃ—](    (   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_X   s    c         S` s   d S(   u   10i
   (    (   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_LIT10‘   s    c         S` s   d S(   u   [Uu][Nn][Kk][Nn][Oo][Ww][Nn]N(   t   None(   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt	   t_UNKNOWN•   s    c         ` s   ˆ  j  |  ƒ |  _ |  S(   u   [a-zA-Z][a-zA-Z_]*(   t	   _get_unitR(   (   R)   (   t   cls(    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_UNIT™   s    u    c         S` s   t  d j |  j ƒ ƒ ‚ d  S(   Nu   Invalid character at col {0}(   t
   ValueErrort   formatt   lexpos(   R)   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   t_error¢   s    t   optimizet   lextabu   ogip_lextabt	   outputdir(	   t
   extern.plyR&   t   _tokenst   Truet   strt   ost   patht   dirnamet   __file__(   R3   R&   t   tokenst
   t_DIVISIONt   t_OPEN_PARENt   t_CLOSE_PARENt   t_WHITESPACEt
   t_STARSTARt   t_STARR*   R,   R-   R.   R/   R1   R4   t   t_ignoreR8   t   lexer(    (   R3   s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _make_lexero   s(    								c      	   ` s×   d d l  m } ˆ  j } d „  } d „  } d „  } ‡  f d †  } d „  } d „  } d	 „  }	 d
 „  }
 d „  } d „  } d „  } d „  } d „  } d „  } | j d t d t d ƒ d t j j t ƒ d t	 ƒ } | S(   uˆ  
        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/>`_.
        i   (   t   yaccc         S` sh   t  |  ƒ d k r+ |  d |  d |  d <n9 t  |  ƒ d k rV |  d |  d |  d <n |  d |  d <d S(   uÂ   
            main : UNKNOWN
                 | complete_expression
                 | scale_factor complete_expression
                 | scale_factor WHITESPACE complete_expression
            i   i   i   i    i   N(   t   len(   t   p(    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_main»   s
    c         S` s   |  d |  d <d S(   u@   
            complete_expression : product_of_units
            i   i    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_complete_expressionÉ   s    c         S` s   t  |  ƒ d k rT |  d d k r; |  d |  d |  d <q‰ |  d |  d |  d <n5 t  |  ƒ d k r{ |  d d |  d <n |  d |  d <d S(	   u  
            product_of_units : unit_expression
                             | division unit_expression
                             | product_of_units product unit_expression
                             | product_of_units division unit_expression
            i   i   u   DIVISIONi   i   i    iÿÿÿÿN(   RO   (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_product_of_unitsÏ   s    c         ` sW  t  |  d ƒ } | ˆ  j k rG | d k rG t d j |  d ƒ ƒ ‚ n  t |  ƒ d k r« | d k rŠ |  d |  d d |  d |  d <qS|  d |  d |  d |  d <n¨ t |  ƒ d k rÖ |  d	 |  d
 |  d <n} t |  ƒ d
 k r"| d k r	|  d d |  d <qS|  d |  d |  d <n1 t |  ƒ d k rE|  d	 |  d <n |  d |  d <d S(   u†  
            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
            i   u   sqrtuI   The function '{0}' is valid in OGIP, but not understood by astropy.units.i   i   g      à?i   i    i   i   i   N(   R?   t
   _functionsR5   R6   RO   (   RP   t   p1_str(   R3   (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_unit_expressionà   s$    %!c         S` s†   t  |  ƒ d k r' d |  d |  d <n |  d |  d <t j |  d ƒ d d k r‚ d d	 l m } t j d
 j |  d ƒ | ƒ n  d S(   u  
            scale_factor : LIT10 power numeric_power
                         | LIT10
                         | signed_float
                         | signed_float power numeric_power
                         | signed_int power numeric_power
            i   i
   i   i    i   g      ð?g        i   (   t   UnitsWarningu2   '{0}' scale should be a power of 10 in OGIP formatN(   RO   t   matht   log10R   RW   t   warningst   warnR6   (   RP   RW   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_scale_factor  s    c         S` s   d |  d <d S(   u¹   
            division : DIVISION
                     | WHITESPACE DIVISION
                     | WHITESPACE DIVISION WHITESPACE
                     | DIVISION WHITESPACE
            u   DIVISIONi    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt
   p_division  s    c         S` s   d |  d <d S(   uÆ   
            product : WHITESPACE
                    | STAR
                    | WHITESPACE STAR
                    | WHITESPACE STAR WHITESPACE
                    | STAR WHITESPACE
            u   PRODUCTi    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt	   p_product"  s    c         S` s   d |  d <d S(   u*   
            power : STARSTAR
            u   POWERi    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_power,  s    c         S` s=   t  |  ƒ d k r+ |  d |  d |  d <n |  d |  d <d S(   uQ   
            unit : UNIT
                 | UNIT power numeric_power
            i   i   i   i    N(   RO   (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_unit2  s    c         S` sq   t  |  ƒ d k r< t t |  d ƒ t |  d ƒ ƒ |  d <n1 t  |  ƒ d k r_ |  d |  d <n |  d |  d <d S(   u#  
            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
            i   i   i   i    i   N(   RO   R   R+   (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_numeric_power<  s
    *c         S` s1   t  |  ƒ d k r# |  d |  d <n
 d |  d <d S(   u8   
            sign : SIGN
                 |
            i   i   i    g      ð?N(   RO   (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_signK  s    c         S` s   |  d |  d |  d <d S(   u0   
            signed_int : SIGN UINT
            i   i   i    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_signed_intU  s    c         S` s   |  d |  d |  d <d S(   uY   
            signed_float : sign UINT
                         | sign UFLOAT
            i   i   i    N(    (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_signed_float[  s    c         S` s   t  ƒ  ‚ d  S(   N(   R5   (   RP   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   p_errorb  s    t   debugt	   tabmoduleu   ogip_parsetabR;   t   write_tables(
   R<   RN   R=   R   R?   R@   RA   RB   RC   R>   (   R3   RN   RD   RQ   RR   RS   RV   R\   R]   R^   R_   R`   Ra   Rb   Rc   Rd   Re   t   parser(    (   R3   s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _make_parser¬   s(    				&				
		
		
				c         C` sX   y |  j  | j ƒ SWn= t k
 rS } t d j | j | j t j | ƒ ƒ ƒ ‚ n Xd  S(   Nu   At col {0}, '{1}': {2}(   t   _parse_unitR(   R5   R6   R7   R   t	   text_type(   R3   R)   t   e(    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyR2   l  s    c         C` s   | |  j  k rW | rK t d j | t j | |  j  |  j |  j ƒ ƒ ƒ ‚ qW t ƒ  ‚ n  | |  j k r‰ t j | |  j  | d |  j ƒ n  d  S(   Nu2   Unit '{0}' not supported by the OGIP standard. {1}u   OGIP(   t   _unitsR5   R6   R   t   did_you_mean_unitst   _deprecated_unitst   _to_decomposed_alternativet   unit_deprecation_warning(   R3   R   t   detailed_exception(    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _validate_unitu  s    	c         C` s   |  j  | d | ƒ|  j | S(   NRs   (   Rt   Rn   (   R3   R   Rs   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyRk   ‡  s    c         C` s§   | j  ƒ  } y |  j | d t ƒSWn} t k
 r¢ y, t j |  j j | d |  j d | ƒƒ SWq£ t k
 rž } t	 j
 | ƒ r† ‚  qŸ t d j | ƒ ƒ ‚ q£ Xn Xd  S(   NRs   RL   Rf   u   Syntax error parsing unit '{0}'(   t   stripRk   R   R5   R   R   t   _parsert   parset   _lexerR   Rl   R6   (   R3   t   sRf   Rm   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyRw   Œ  s    &c         C` s    | j  d ƒ } |  j | ƒ | S(   Nu   ogip(   t   get_format_nameRt   (   R3   R   R$   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _get_unit_namež  s    c         ` sÊ   g  } | j  d ‡  f d †  ƒ x› | D]“ \ } } | d k rW | j ˆ  j | ƒ ƒ q& t j | ƒ } d | k r— | j d j ˆ  j | ƒ | ƒ ƒ q& | j d j ˆ  j | ƒ | ƒ ƒ q& Wd j | ƒ S(   NR   c         ` s   ˆ  j  |  d ƒ j ƒ  S(   Ni    (   R{   t   lower(   t   x(   R3   (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   <lambda>§  s    i   u   /u
   {0}**({1})u   {0}**{1}u    (   t   sortt   appendR{   R   t   format_powerR6   t   join(   R3   R
   t   outR   t   power(    (   R3   s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   _format_unit_list¤  s    c         C` sx   t  j | |  j ƒ } t | t j ƒ rh t j | j ƒ d d k rh t	 j
 d j | j ƒ t j ƒ qh n  t j |  | ƒ S(   Ng      ð?g        u2   '{0}' scale should be a power of 10 in OGIP format(   R   t   decompose_to_known_unitsR{   t
   isinstanceR   t   CompositeUnitRX   RY   t   scaleRZ   R[   R6   RW   R   t
   _to_string(   R3   R   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt	   to_string¶  s    	c         C` s   t  j | |  j ƒ } t | t j ƒ rƒ t j | j ƒ d d k rƒ | j } t	 j	 | ƒ } d | _
 d j t j |  | ƒ | ƒ Sn  t j | ƒ S(   Ng      ð?g        u!   {0} (with data multiplied by {1})(   R   R†   R{   R‡   R   Rˆ   RX   RY   R‰   t   copyt   _scaleR6   R   RŠ   (   R3   R   R‰   (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyRq   Æ  s    		(   u   DIVISIONu
   OPEN_PARENu   CLOSE_PARENu
   WHITESPACEu   STARSTARu   STARu   SIGNu   UFLOATu   LIT10u   UINTu   UNKNOWNu   UNIT(   t   __name__t
   __module__t   __doc__R=   t   staticmethodR%   t   classmethodRM   Rj   R2   R>   Rt   Rk   R   Rw   R{   R…   R‹   Rq   (    (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyR	      s6              <=À	(   R   t
   __future__R    R   R   R   t   externR   R   RX   R@   RŒ   RZ   t	   fractionsR   R   R   R   R   t   GenericR	   (    (    (    s8   lib/python2.7/site-packages/astropy/units/format/ogip.pyt   <module>   s   "