ó
$Oc           @   sô   d  d l  Z  d  d l Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z	 d „  Z
 d d „ Z d d „ Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d d d „ Z d S(   iÿÿÿÿNc         C   su   |  d k r0 | d k r$ t  j } qq t  j } nA y t |  | ƒ } Wn+ t  j d |  | f IJt  j d ƒ n X| S(   Nt   -t   rs&   Couldn't open file "%s" for mode "%s".i   (   t   syst   stdint   stdoutt   opent   stderrt   exit(   t	   file_namet   modet   file_handle(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   open_file_or_die   s    c   	      C   s—  t  |  d ƒ } g  } d } d } xV| j ƒ  } | d k r@ Pn  | d 7} | d d k rj | d d !} n  t j d d | ƒ } t j d d | ƒ } t j d	 d | ƒ } t j d
 | ƒ rÁ q$ n  | j ƒ  } t | ƒ } | d k r| } xZ t d | ƒ D] } | j g  ƒ qû Wn3 | | k rHt	 j
 d |  | f IJt	 j d ƒ n  x, t d | ƒ D] } | | j | | ƒ qXWq$ |  d k r“| j ƒ  n  | S(   s9  Given an input file containing tabular data, returns a list of columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9
	a b c

	then this routine returns
	[['1', '4', '7', 'a'], ['2', '5', '8', 'b'], ['3', '6', '9', 'c']].
	R   iÿÿÿÿi    t    i   s   
s   #.*s   ^\s+s   \s+$s   ^$s%   Ragged input in file "%s" at line %d.R    (   R   t   readlinet   ret   subt   matcht   splitt   lent   ranget   appendR   R   R   t   close(	   R   R
   t   columnst
   num_fieldst   line_numbert   linet   fieldst   curr_num_fieldst   j(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   text_columns_from_file    s>    

c         C   sŸ   t  |  ƒ } t | ƒ } t | d ƒ } xp t d | ƒ D]_ } xV t d | ƒ D]E } | | | } | d k r d | | | <qN t | ƒ | | | <qN Wq8 W| S(   s(  Given an input file containing float tabular data, returns a list of columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9

	then this routine returns
	[[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]
	i    t   _N(   R   R   R   t   Nonet   float(   R   R   t   num_rowst   num_colst   iR   t   entry(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   float_columns_from_file]   s    c         C   s   t  |  ƒ } | d | d g S(   sS  Given an input file containing float tabular data, returns a list containing
	the x column, and a list of y columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9

	then this routine returns
	[ [1.0, 4.0, 7.0], [[2.0, 5.0, 8.0], [3.0, 6.0, 9.0]] ]
	i    i   (   R%   (   R   R   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   xy_columns_from_filex   s    c         C   sÔ   t  |  ƒ } | d } g  } g  } | d } t | ƒ d d k rc t j d |  IJt j d ƒ n  t t t | ƒ d ƒ ƒ } xE t d | ƒ D]4 } | j | d | ƒ | j | d | d ƒ q W| | | g S(   s°  Given an input file containing float tabular data, returns a list containing
	the x column, a list of y columns, and a list of error-bar columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 0.2 3 0.3
	4 5 0.2 6 0.3 # Comment here

	7 8 0.2 9 0.3

	then this routine returns
	[
	[1.0, 4.0, 7.0],
	[[2.0, 5.0, 8.0], [3.0, 6.0, 9.0]],
	[[0.2, 0.2, 0.2], [0.3, 0.3, 0.3]]
	]
	i    i   i   s@   xye_columns_from_file:  need odd number of columns in file '%s'.(	   R%   R   R   R   R   t   intt   roundR   R   (   R   R   t	   x_columnst	   y_columnst	   e_columnst   tempt   num_columnsR   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   xye_columns_from_file‹   s    

s   %11.7fc         C   se   |  g } t  | ƒ } x9 t d | ƒ D]( } | j | | ƒ | j | | ƒ q% Wt | | | ƒ d  S(   Ni    (   R   R   R   t   float_columns_to_file(   t   x_columnR*   R+   R   t   formatt   all_columnst
   num_seriesR   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   xye_columns_to_file°   s    	c         C   sõ   t  |  d ƒ } | j ƒ  } | d k rD t j d IJt j d ƒ n  |  d k r] | j ƒ  n  t j d d | ƒ } | d d k r† g  St j d	 d | ƒ } | d
 d k r» | d d
 !} n  t j d d | ƒ } t j d d | ƒ } | j ƒ  } | S(   NR   R   s2   tabutil_m.labels_from_file:  couldn't read line 1.i   R    s   ^\s+i    t   #s   ^#+iÿÿÿÿs   
s   \s+$(	   R   R   R   R   R   R   R   R   R   (   R   R
   R   t   labels(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   labels_from_fileÊ   s$    c         C   s‚   t  |  ƒ } t  |  d ƒ } g  } xY t d | ƒ D]H } g  } x, t d | ƒ D] } | j |  | | ƒ qN W| j | ƒ q2 W| S(   Ni    (   R   R   R   (   R   R-   t
   num_tuplest   tuplesR#   t   tupleR   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   columns_to_tuplesö   s    c         C   s   t  t |  ƒ ƒ S(   s%  Given an input file containing float tabular data, returns a list of rows.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9

	then this routine returns
	[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
	(   R;   R%   (   R   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   float_rows_from_file  s    c   
      C   sµ   t  |  ƒ } t  |  d ƒ } g  } x t d | ƒ D]n } g  } xR t d | ƒ D]A } |  | | }	 |	 d k r~ | j d ƒ qN | j | |	 ƒ qN W| j | ƒ q2 Wt | | ƒ d S(   sñ   Given a list of columns, write the data to a table file.
	For example, if the input is

	[[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]

	then the output file contains

	1.0000 2.0000 3.0000
	4.0000 5.0000 6.0000
	7.0000 8.0000 9.0000
	i    R   N(   R   R   R   R   t   text_columns_to_file(
   R   R   R1   R-   R!   t   text_columnsR   t   text_columnR#   R$   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyR/     s    c         C   s   t  |  d | | ƒ d S(   sï   Given a list of rows, write the data to a table file.
	For example, if the input is

	[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

	then the output file contains

	1.0000 2.0000 3.0000
	4.0000 5.0000 6.0000
	7.0000 8.0000 9.0000

	N(   t   float_rows_and_labels_to_fileR   (   t   rowsR   R1   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   float_rows_to_file0  s    c         C   s¸   t  |  ƒ } t  |  d ƒ } g  } x t d | ƒ D]n } g  } xR t d | ƒ D]A }	 |  | |	 }
 |
 d k r~ | j d ƒ qN | j | |
 ƒ qN W| j | ƒ q2 Wt | | | ƒ d S(   s9  Given a list of rows, write the data to a table file.
	For example, if the input is

	[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

	and the labels are

	['x', 'y', 'z'],

	then the output file contains

	#x     y      z
	#-     -      -
	1.0000 2.0000 3.0000
	4.0000 5.0000 6.0000
	7.0000 8.0000 9.0000

	i    R   N(   R   R   R   R   t   text_rows_and_labels_to_file(   RA   R6   R   R1   R!   R-   t	   text_rowsR#   t   text_rowR   R$   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyR@   A  s    c         C   s   t  |  d | ƒ d  S(   NR    (   R/   (   R   R1   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   print_float_columnse  s    c         C   s¸   t  |  ƒ } t  |  d ƒ } g  } x t d | ƒ D]n } g  } xR t d | ƒ D]A }	 |  | |	 }
 |
 d k r~ | j d ƒ qN | j | |
 ƒ qN W| j | ƒ q2 Wt | | | ƒ d S(   sL  Given a list of columns, write the data to a table file.
	For example, if the columns input is

	[[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]

	and the labels input is

	['x', 'y1', 'y2'],

	then the output file contains

	#x     y1     y2
	#-     --     --
	1.0000 2.0000 3.0000
	4.0000 5.0000 6.0000
	7.0000 8.0000 9.0000
	i    R   N(   R   R   R   R   t   text_columns_and_labels_to_file(   R   R6   R   R1   R-   R!   R>   R   R?   R#   R$   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt    float_columns_and_labels_to_filei  s    c         C   s   t  |  d  | ƒ d  S(   N(   RG   R   (   R   R   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyR=   ”  s    c         C   s™  t  |  ƒ } t  |  d ƒ } d g | } x° t d | ƒ D]Ÿ } |  | } d } x/ | D]' }	 t  |	 ƒ }
 |
 | k rV |
 } qV qV W| d  k r  t  | | ƒ }
 n  | d k r¹ |
 d 7}
 n  |
 | k rÎ |
 } n  | | | <q9 Wt | d ƒ } | d  k rüxh t d | ƒ D]W } | d k r?| j d | | d | | f ƒ q| j d | | | | f ƒ qW| j d ƒ xz t d | ƒ D]i } | d k rÀ| j d ƒ d | | d } | j | ƒ q| j d	 ƒ d | | } | j | ƒ qW| j d ƒ n  x} t d | ƒ D]l } xV t d | ƒ D]E } | d k rD| j d	 ƒ n  | j d
 | | |  | | f ƒ q"W| j d ƒ qW| d k r•| j ƒ  n  d  S(   Ni    i   t   ws   #%-*ss    %-*ss   
R5   R    t    s   %-*s(   R   R   R   R   t   writeR   (   R   R6   R   R-   R!   t
   max_widthsR   t   columnt	   max_widthR$   t   widthR
   t   dashesR#   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyRG   ¡  sR    
	&#'c         C   s   t  |  d  | ƒ d  S(   N(   RC   R   (   RA   R   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   text_rows_to_fileá  s    c         C   s¦  t  |  ƒ } t  |  d ƒ } d g | } x½ t d | ƒ D]¬ } d } xF t d | ƒ D]5 } |  | | }	 t  |	 ƒ }
 |
 | k rU |
 } qU qU W| d  k rÛ t  | | ƒ }
 | d k rÃ |
 d 7}
 n  |
 | k rÛ |
 } qÛ n  | | | <q9 Wt | d ƒ } | d  k r	xh t d | ƒ D]W } | d k rL| j d | | d | | f ƒ q| j d | | | | f ƒ qW| j d ƒ xz t d | ƒ D]i } | d k rÍ| j d ƒ d | | d } | j | ƒ qŒ| j d	 ƒ d | | } | j | ƒ qŒW| j d ƒ n  x} t d | ƒ D]l } xV t d | ƒ D]E } | d k rQ| j d	 ƒ n  | j d
 | | |  | | f ƒ q/W| j d ƒ qW| d k r¢| j ƒ  n  d  S(   Ni    i   RI   s   #%-*ss    %-*ss   
R5   R    RJ   s   %-*s(   R   R   R   R   RK   R   (   RA   R6   R   R!   R-   RL   R   RN   R#   R$   RO   R
   RP   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyRC   î  sR    &#'c         C   s~   i  } xq t  d t |  ƒ ƒ D]Z } |  | } i  | | <x= t  d t | ƒ ƒ D]& } | | } | | | | | | <qL Wq W| S(   Ni    (   R   R   (   t   left_labelst
   top_labelsR   t   hashR#   t
   left_labelR   t	   top_label(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   table_to_hashD  s    


c         C   s.   g  } x! |  D] } | j  t | ƒ ƒ q W| S(   N(   R   R    (   t
   text_arrayt   float_arrayR$   (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   text_array_to_float_arrayP  s    s   %.7fc	         C   s  | t  k r± g  }	 t |  ƒ }
 x? t d |
 ƒ D]. } |  | } | | g | } |	 j | ƒ q. W| g } x/ | D]' } | | } | j d | | f ƒ qp Wt |	 | | | ƒ n[ | g |  } | g } x/ | D]' } | | } | j d | | f ƒ qÎ Wt | | | | ƒ d  S(   Ni    s   %s=%s(   t   FalseR   R   R   R@   RH   (   t   matrixt   row_index_namet   row_index_valuest   col_index_namet   col_index_valuesR   t	   transposet   matrix_formatt   label_formatt	   long_rowsR!   R#   t	   short_rowt   long_rowR6   t   col_index_valuet   col_index_stringt   augmented_columnst   row_index_valuet   row_index_string(    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   matrix_and_labels_to_filen  s(    
	

	
(   R   R   R   R   R%   R&   R.   R4   R7   R;   R<   R/   RB   R@   RF   RH   R=   RG   RQ   RC   RW   RZ   R[   Rl   (    (    (    sE   /homepages/25/d321765456/htdocs/pub_http_internet/python/tabutil_m.pyt   <module>   s,   		=			&	,		$*		@		V		 