ó
šßÈ[c           @` sÜ   d  Z  d d l m Z m Z m Z m 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 d d	 l m Z m Z d
 d l m Z d d l m Z d d l m Z d e f d „  ƒ  YZ d S(   u)   
Handles the CDS string format for units
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   six(   t   zipi   (   t   Base(   t   coret   utilsi   (   t   is_effectively_unity(   t   classproperty(   t   did_you_meant   CDSc        	   B` sé   e  Z d  Z d Z e d
 e ƒ d „  ƒ Z e d
 e ƒ d „  ƒ Z e d
 e ƒ d „  ƒ Z e	 d „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e e d „ ƒ Z e e d „ ƒ Z e	 d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   ua  
    Support the `Centre de DonnÃ©es astronomiques de Strasbourg
    <http://cds.u-strasbg.fr/>`_ `Standards for Astronomical
    Catalogues 2.0 <http://cds.u-strasbg.fr/doc/catstd-3.2.htx>`_
    format, and the `complete set of supported units
    <http://vizier.u-strasbg.fr/cgi-bin/Unit>`_.  This format is used
    by VOTable up to version 1.2.
    u   PRODUCTu   DIVISIONu
   OPEN_PARENu   CLOSE_PARENu   Xu   SIGNu   UINTu   UFLOATu   UNITt   lazyc         C` s
   |  j  ƒ  S(   N(   t   _generate_unit_names(   t   cls(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   _units6   s    c         C` s
   |  j  ƒ  S(   N(   t   _make_parser(   R   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   _parser:   s    c         C` s
   |  j  ƒ  S(   N(   t   _make_lexer(   R   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   _lexer>   s    c          C` si   d d l  m }  d d l  m } i  } x< |  j j ƒ  D]+ \ } } t | | j ƒ r6 | | | <q6 q6 W| S(   Ni   (   t   cdsi   (   t   units(   t    R   R   t   __dict__t   itemst
   isinstancet   UnitBase(   R   t   ut   namest   keyt   val(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR   B   s    c      	   ` s°   d d l  m } ˆ  j } d } d } d } d } d „  } d „  } d	 „  }	 d
 „  }
 ‡  f d †  } d } d „  } | j d t d t d ƒ d t j j t ƒ d t	 j
 ƒ } | S(   Ni   (   t   lexu   \.u   /u   \(u   \)c         S` sI   t  j d |  j ƒ s3 d |  _ t |  j ƒ |  _ n t |  j ƒ |  _ |  S(   u$   ((\d+\.?\d+)|(\.\d+))([eE][+-]?\d+)?u   [eE\.]u   UINT(   t   ret   searcht   valuet   typet   intt   float(   t   t(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_UFLOAT\   s
    	c         S` s   t  |  j ƒ |  _ |  S(   u   \d+(   R%   R#   (   R'   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_UINTe   s    c         S` s   t  |  j d ƒ |  _ |  S(   u
   [+-](?=\d)u   1(   R&   R#   (   R'   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_SIGNj   s    c         S` s   |  S(   u   [xÃ—](    (   R'   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_Xo   s    c         ` s   ˆ  j  |  ƒ |  _ |  S(   u   \%|Â°|\\h|((?!\d)\w)+(   t	   _get_unitR#   (   R'   (   R   (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_UNITs   s    u    c         S` s   t  d j |  j ƒ ƒ ‚ d  S(   Nu   Invalid character at col {0}(   t
   ValueErrort   formatt   lexpos(   R'   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   t_error{   s    t   optimizet   lextabu
   cds_lextabt	   outputdirt   reflags(   t
   extern.plyR    t   _tokenst   Truet   strt   ost   patht   dirnamet   __file__R!   t   UNICODE(   R   R    t   tokenst	   t_PRODUCTt
   t_DIVISIONt   t_OPEN_PARENt   t_CLOSE_PARENR(   R)   R*   R+   R-   t   t_ignoreR1   t   lexer(    (   R   s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR   O   s"    							c      	   C` s¿   d d l  m } |  j } d „  } 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 `Standards
        for Astronomical Catalogues 2.0
        <http://cds.u-strasbg.fr/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://bitbucket.org/nxg/unity/>`_.
        i   (   t   yaccc         S` sY   d d l  m } t |  ƒ d k rA | |  d |  d ƒ |  d <n | |  d ƒ |  d <d S(   ur   
            main : factor combined_units
                 | combined_units
                 | factor
            i   (   t   Uniti   i   i    N(   R   RG   t   len(   t   pRG   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_main•   s    c         S` s   |  d |  d <d S(   uj   
            combined_units : product_of_units
                           | division_of_units
            i   i    N(    (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_combined_units¡   s    c         S` s=   t  |  ƒ d k r+ |  d |  d |  d <n |  d |  d <d S(   u‚   
            product_of_units : unit_expression PRODUCT combined_units
                             | unit_expression
            i   i   i   i    N(   RH   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_product_of_units¨   s    c         S` sA   t  |  ƒ d k r' |  d d |  d <n |  d |  d |  d <d S(   uŽ   
            division_of_units : DIVISION unit_expression
                              | unit_expression DIVISION combined_units
            i   i   iÿÿÿÿi    i   N(   RH   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_division_of_units²   s    c         S` s5   t  |  ƒ d k r# |  d |  d <n |  d |  d <d S(   u   
            unit_expression : unit_with_power
                            | OPEN_PAREN combined_units CLOSE_PAREN
            i   i   i    N(   RH   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_unit_expression¼   s    c         S` s»   t  |  ƒ d k rN |  d d k r1 t d ƒ ‚ n  |  d d |  d |  d <ni t  |  ƒ d k r” |  d d k r t d ƒ ‚ n  d |  d	 |  d <n# t  |  ƒ d	 k r· |  d |  d <n  d
 S(   uÎ   
            factor : signed_float X UINT signed_int
                   | UINT X UINT signed_int
                   | UINT signed_int
                   | UINT
                   | signed_float
            i   i   i
   u*   Only base ten exponents are allowed in CDSi   g      $@i   i    i   N(   RH   R.   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_factorÆ   s    c         S` s=   t  |  ƒ d k r# |  d |  d <n |  d |  d |  d <d S(   ua   
            unit_with_power : UNIT numeric_power
                            | UNIT
            i   i   i    N(   RH   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_unit_with_powerÛ   s    c         S` s   |  d |  d |  d <d S(   u3   
            numeric_power : sign UINT
            i   i   i    N(    (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.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(   RH   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_signë   s    c         S` s   |  d |  d |  d <d S(   u0   
            signed_int : SIGN UINT
            i   i   i    N(    (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_signed_intõ   s    c         S` s   |  d |  d |  d <d S(   uY   
            signed_float : sign UINT
                         | sign UFLOAT
            i   i   i    N(    (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_signed_floatû   s    c         S` s   t  ƒ  ‚ d  S(   N(   R.   (   RI   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   p_error  s    t   debugt	   tabmoduleu   cds_parsetabR4   t   write_tables(
   R6   RF   R7   t   FalseR9   R:   R;   R<   R=   R8   (   R   RF   R?   RJ   RK   RL   RM   RN   RO   RP   RQ   RR   RS   RT   RU   t   parser(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR   †   s$    				
	
	
		
		
				c         C` sR   y |  j  | j ƒ SWn7 t k
 rM } t d j | j t j | ƒ ƒ ƒ ‚ n Xd  S(   Nu   At col {0}, {1}(   t   _parse_unitR#   R.   R/   R0   R   t	   text_type(   R   R'   t   e(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR,     s    c         C` sS   | |  j  k rH | r< t d j | t | |  j  ƒ ƒ ƒ ‚ qH t ƒ  ‚ n  |  j  | S(   Nu5   Unit '{0}' not supported by the CDS SAC standard. {1}(   R   R.   R/   R   (   R   t   unitt   detailed_exception(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR[     s    c         C` sÚ   d | k r t  d ƒ ‚ n  t | t j ƒ s? | j d ƒ } n  y |  j | d t ƒSWn} t  k
 rÕ y# |  j j | d |  j	 d | ƒSWqÖ t  k
 rÑ } t j | ƒ rÂ t  t j | ƒ ƒ ‚ qÒ t  d ƒ ‚ qÖ Xn Xd  S(   Nu    u$   CDS unit must not contain whitespaceu   asciiR_   RE   RV   u   Syntax error(
   R.   R   R   R\   t   decodeR[   RY   R   t   parseR   (   R   t   sRV   R]   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyRa   #  s    #c         C` s   |  j  d ƒ S(   Nu   cds(   t   get_format_name(   R^   (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   _get_unit_name8  s    c         C` sw   g  } xa | D]Y \ } } | d k r> | j  |  j | ƒ ƒ q | j  d j |  j | ƒ t | ƒ ƒ ƒ q Wd j | ƒ S(   Ni   u   {0}{1}u   .(   t   appendRd   R/   R%   t   join(   R   R   t   outt   baset   power(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   _format_unit_list<  s     c         C` sv  t  j | |  j ƒ } t | t j ƒ rN| j d k rM t | j d ƒ rM d S| j d k re d } n„ t  j	 | j ƒ \ } } g  } | d k rŸ | j
 | ƒ n  | rÚ | j d ƒ sÁ d | } n  | j
 d	 j | ƒ ƒ n  d
 j | ƒ } t t | j | j ƒ ƒ } t | ƒ d k rr| j d t j d ƒ d t ƒ | |  j | ƒ 7} qrn$ t | t j ƒ rr|  j | ƒ } n  | S(   Nu   dimensionlessg      Y@u   %i   u    u   1u   -u   +u   10{0}u   xi    R   t   reverse(   u    u   1(   R   t   decompose_to_known_unitsRd   R   R   t   CompositeUnitt   physical_typeR	   t   scalet   split_mantissa_exponentRe   t
   startswithR/   Rf   t   listR   t   basest   powersRH   t   sortt   operatort
   itemgetterR8   Rj   t	   NamedUnit(   R   R^   Rb   t   mR]   t   partst   pairs(    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt	   to_stringG  s.    	(	   u   PRODUCTu   DIVISIONu
   OPEN_PARENu   CLOSE_PARENu   Xu   SIGNu   UINTu   UFLOATu   UNIT(   t   __name__t
   __module__t   __doc__R7   R
   R8   R   R   R   t   staticmethodR   t   classmethodR   R   R,   R[   RY   Ra   Rd   Rj   R|   (    (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyR       s0           7†	(   R   t
   __future__R    R   R   R   Rv   R:   R!   t   externR   t   extern.six.movesR   Rh   R   R   R   R   R	   R
   t
   utils.miscR   R   (    (    (    s7   lib/python2.7/site-packages/astropy/units/format/cds.pyt   <module>   s   "