ó
±éRc           @   sv   d  Z  d d l Z d d l Z d d l Z d d l j Z d d l j	 Z	 d „  Z
 i  e
 _ d d d d d d „ Z d S(   sÆ  
Some io tools for excel -- requires xlwt

Example usage:

    import matplotlib.mlab as mlab
    import mpl_toolkits.exceltools as exceltools

    r = mlab.csv2rec('somefile.csv', checkrows=0)

    formatd = dict(
        weight = mlab.FormatFloat(2),
        change = mlab.FormatPercent(2),
        cost   = mlab.FormatThousands(2),
        )


    exceltools.rec2excel(r, 'test.xls', formatd=formatd)
    mlab.rec2csv(r, 'test.csv', formatd=formatd)

iÿÿÿÿNc         C   s-  t  |  ƒ } t j j | ƒ } | d k	 r. | St j |  ƒ }  t j ƒ  } t	 |  t
 j ƒ r“ d j d g |  j ƒ } d | | f | _ d |  _ n€ t	 |  t
 j ƒ rï |  j d k rã d j d g |  j ƒ } d | | f | _ qd | _ n$ t	 |  t
 j ƒ rd | _ n d } | |  _ |  t j | <|  S(	   sn   
    copy the format, perform any overrides, and attach an xlstyle instance
    copied format is returned
    t    t   0s   0.%s%%;[RED]-0.%s%%g      ð?i    s   #,##0.%s;[RED]-#,##0.%ss   #,##;[RED]-#,##N(   t   hasht   xlformat_factoryt   created_formatst   gett   Nonet   copyt   deepcopyt   excelt   XFStylet
   isinstancet   mlabt   FormatPercentt   joint	   precisiont   num_format_strt   scalet   FormatFloatt	   FormatIntt   xlstyle(   t   formatt   keyt   fmt_R   t   zeros(    (    sQ   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/exceltools.pyR      s*    		i    t   NaNt   Infc         C   sz  t  } t j | ƒ r? | } t j ƒ  }	 |	 j d ƒ } t } n  | d k rW t ƒ  } n  g  }
 t j	 ƒ  } t | _
 t j ƒ  } | | _ xœ t |  j j ƒ D]ˆ \ } } |  j | } | j | ƒ } | d k rï t j j | j t j ƒ  ƒ } n  t | ƒ } | j | | | | | ƒ |
 j | ƒ qš W| d 7} t j t |  j j ƒ ƒ } x|  D]
} x÷ | D]ï } | | } |
 | } | j | ƒ } t j | ƒ r±| j | | | | ƒ q_t j | ƒ rt j | ƒ } | d k rä| } n
 d | } | j | | | | ƒ q_| j d k r1| j | | | | ƒ q_| j | | | | | j ƒ q_W| d 7} qRW| rv|	 j  | ƒ n  | S(   sv  
    save record array r to excel xlwt worksheet ws
    starting at rownum.  if ws is string like, assume it is a
    filename and save to it

    start writing at rownum, colnum

    formatd is a dictionary mapping dtype name -> mlab.Format instances

    nanstr is the string that mpl will put into excel for np.nan value
    The next rownum after writing is returned
    t	   worksheeti   i    s   -%sN(!   t   Falset   cbookt   is_string_likeR	   t   Workbookt	   add_sheett   TrueR   t   dictt   Fontt   boldR
   t   fontt	   enumeratet   dtypet   namesR   R   t   defaultformatdt   typet	   FormatObjR   t   writet   appendt   npt   aranget   lent   tovalt
   safe_isnant
   safe_isinft   signR   t   save(   t   rt   wst   formatdt   rownumt   colnumt   nanstrt   infstrt   autosavet   filenamet   wbt   formatsR%   t   stylehdrt   it   namet   dtR   t   indt   rowt   valt   sgnt   s(    (    sQ   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/exceltools.pyt	   rec2excelG   sV    			!


 	
!(   t   __doc__R   t   numpyR.   t   xlwtR	   t   matplotlib.cbookR   t   matplotlib.mlabR   R   R   R   RJ   (    (    (    sQ   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/exceltools.pyt   <module>   s   	&	