ó
šßÈ[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 Z d d l m Z d d l m Z e e ƒ Z e d d	 e j ƒ Z e d d	 e j ƒ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d d „ Z  d S(   u}   
Miscellaneous utilities for `astropy.units`.

None of the functions in the module are meant for use outside of the
package.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Fraction(   t   finfoi   (   t   sixg      ð?g      @c         C` s@   t  j d |  ƒ } | d k	 r0 | j d ƒ }  n  |  j d d ƒ S(   uS   
    Get the first sentence from a string and remove any carriage
    returns.
    u	   .*?\S\.\si    u   
u    N(   t   ret   matcht   Nonet   groupt   replace(   t   st   x(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   _get_first_sentence   s    c   
      c` sg  d d l  m } g  } t ƒ  } x† t j |  ƒ D]u \ } } t | | j ƒ sS q/ n  | | j k rh q/ n  t | | j ƒ r— | j	 | j
 j d j ƒ q/ | j | ƒ q/ W| j d d „  ƒ x¥ | D] } t | j ƒ j ƒ  } d } t | | j ƒ rd j | j
 j d ƒ d d	 !ƒ } n  d
 j d „  | j Dƒ ƒ }	 | | | |	 | j | k rWd n d f VqÂ Wd S(   u‘   
    Generates the ``(unit, doc, represents, aliases, prefixes)``
    tuple used to format the unit summary docs in `generate_unit_summary`.
    i   (   t   corei    t   keyc         S` s   |  j  j ƒ  S(   N(   t   namet   lower(   R   (    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   <lambda>G   s    u    u   :math:`{0}`u   latexiÿÿÿÿu   , c         s` s   |  ] } d  j  | ƒ Vq d S(   u   ``{0}``N(   t   format(   t   .0R   (    (    s2   lib/python2.7/site-packages/astropy/units/utils.pys	   <genexpr>O   s    u   Yesu   NoN(   t    R   t   setR   t	   iteritemst
   isinstancet   UnitBaseR   t
   PrefixUnitt   addt   _representst   basest   appendt   sortR   t   __doc__t   stript   UnitR   t	   to_stringt   joint   aliases(
   t	   namespaceR   t   unitst   has_prefixesR   t   valt   unitt   doct
   representsR&   (    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   _iter_unit_summary*   s(    	c         C` sP   t  j ƒ  } | j d ƒ x* t |  ƒ D] } | j d j | Œ  ƒ q& W| j ƒ  S(   u`  
    Generates a summary of units from a given namespace.  This is used
    to generate the docstring for the modules that define the actual
    units.

    Parameters
    ----------
    namespace : dict
        A namespace containing units.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    u¢   
.. list-table:: Available Units
   :header-rows: 1
   :widths: 10 20 20 20 1

   * - Unit
     - Description
     - Represents
     - Aliases
     - SI Prefixes
u<   
   * - ``{0}``
     - {1}
     - {2}
     - {3}
     - {4}
(   t   iot   StringIOt   writeR.   R   t   getvalue(   R'   t	   docstringt   unit_summary(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   generate_unit_summaryT   s    
c         C` s¥   d d l  m } i  } xI |  j ƒ  D]; \ } } t | | ƒ r# | j j d } | | | j <q# q# Wt j ƒ  } x* t	 | ƒ D] } | j
 d j | Œ  ƒ q{ W| j ƒ  S(   uù  
    Generates table entries for units in a namespace that are just prefixes
    without the base unit.  Note that this is intended to be used *after*
    `generate_unit_summary` and therefore does not include the table header.

    Parameters
    ----------
    namespace : dict
        A namespace containing units that are prefixes but do *not* have the
        base unit in their namespace.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    i   (   R   i    uS   
   * - Prefixes for ``{0}``
     - {1} prefixes
     - {2}
     - {3}
     - Only
(   R   R   t   itemsR   R-   R   R   R/   R0   R.   R1   R   R2   (   R'   R   t   faux_namespacet   nmR+   t	   base_unitR3   R4   (    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt    generate_prefixonly_unit_summary   s    c         C` so   y t  |  k o t k SSWnL t k
 rj t  |  j k oF t k n oi t  |  j d k og t k SSXd  S(   Ni   (   t   _JUST_BELOW_UNITYt   _JUST_ABOVE_UNITYt	   TypeErrort   realt   imag(   t   value(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   is_effectively_unity¦   s
    c         C` s¬   t  |  ƒ r d St j |  ƒ r¨ |  d k r/ d St |  j ƒ t |  j ƒ k rv t  |  j |  j d ƒ r¥ |  j }  q¥ q¨ t  |  j |  j d ƒ r¨ t d |  j ƒ }  q¨ n  |  S(   Ng      ð?g        i   (   RA   t   npt	   iscomplext   absR>   R?   t   complex(   t   scale(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   sanitize_scale°   s    c         C` sF  t  |  t j t f ƒ re |  j } | d k r? t |  j ƒ }  qB| | d @d k rBt |  ƒ }  qBnÝ y t |  ƒ }  Wn2 t k
 r© t	 j
 |  ƒ s£ t d ƒ ‚ qª ‚  n X|  d d k rÉ t |  ƒ }  ny |  d d d k rà nb x_ d D]W } |  t | ƒ } | d t j d d t j k  rç t t t | ƒ ƒ | ƒ }  Pqç qç W|  S(   u  Convert a power to a floating point value, an integer, or a Fraction.

    If a fractional power can be represented exactly as a floating point
    number, convert it to a float, to make the math much faster; otherwise,
    retain it as a `fractions.Fraction` object to avoid losing precision.
    Conversely, if the value is indistinguishable from a rational number with a
    low-numbered denominator, convert to a Fraction object.

    Parameters
    ----------
    p : float, int, Rational, Fraction
        Power to be converted
    i   i    u9   Quantities and Units may only be raised to a scalar powerg      ð?g        g       @i
   i	   i   i   g      @(   i
   i	   i   i   (   R   t   numberst   RationalR   t   denominatort   intt	   numeratort   floatt	   ExceptionRB   t   isscalart
   ValueErrort   _float_finfot   epst   round(   t   pt   support_tuplest   denomt   it   scaled(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   validate_powerÂ   s.    	c         C` s`   t  |  t ƒ } t  | t ƒ } | r: | r: t | ƒ } n | rV | rV t |  ƒ }  n  |  | f S(   u»   
    If either input is a Fraction, convert the other to a Fraction.
    This ensures that any operation involving a Fraction will use
    rational arithmetic and preserve precision.
    (   R   R   (   t   at   bt   a_is_fractiont   b_is_fraction(    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   resolve_fractionsø   s    c         ` sv   d d l  m ‰  t |  t j ƒ r_ t j |  ƒ r_ t ‡  f d †  |  Dƒ ƒ r_ ˆ  |  d | ƒSt j |  d | ƒSd  S(   Ni   (   t   Quantityc         3` s   |  ] } t  | ˆ  ƒ Vq d  S(   N(   R   (   R   R   (   R_   (    s2   lib/python2.7/site-packages/astropy/units/utils.pys	   <genexpr>	  s    t   dtype(   t   quantityR_   R   RB   t   ndarrayRO   t   anyt
   asanyarray(   RZ   R`   (    (   R_   s2   lib/python2.7/site-packages/astropy/units/utils.pyt   quantity_asanyarray  s    ?(!   R!   t
   __future__R    R   R   R   RH   R/   R   t	   fractionsR   t   numpyRB   R   t   externR   RM   RQ   t   epsnegR;   RR   R<   R   R.   R5   R:   RA   RG   t   FalseRY   R^   R	   Re   (    (    (    s2   lib/python2.7/site-packages/astropy/units/utils.pyt   <module>   s(   "		*	+	'	
	6	