a
    $OOA                     @   s   d dl Z d dlZdd Zdd Zdd Zdd	 Zd
d Zd/ddZdd Zdd Z	dd Z
d0ddZd1ddZd2ddZd3ddZd4ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd5d-d.ZdS )6    Nc                 C   s`   | dkr |dkrt j}q\t j}n<zt| |}W n,   tt j? d| |f f t d Y n0 |S )N-rz&Couldn't open file "%s" for mode "%s".   )sysstdinstdoutopenprintstderrexit)	file_namemodefile_handle r   Q/homepages/25/d321765456/htdocs/pub_http_internet/grad-school/python/tabutil_m.pyopen_file_or_die   s    r   c           	      C   s  t | d}g }d}d}| }|dkr*q|d7 }|d dkrJ|dd }tdd|}tdd|}td	d|}td
|rq| }t|}|dkr|}td|D ]}|g  qn(||krt	t
j? d| |f f t
d td|D ]}|| ||  qq| dkr|  |S )a9  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   r    r   
z#.*^\s+\s+$z^$z%Ragged input in file "%s" at line %d.r   )r   readlineresubZmatchsplitlenrangeappendr	   r   r
   r   close)	r   r   columnsZ
num_fieldsZline_numberlineZfieldsZcurr_num_fieldsjr   r   r   text_columns_from_file    s>    



r"   c                 C   sr   t | }t|}t|d }td|D ]F}td|D ]6}|| | }|dkrZd|| |< q4t||| |< q4q&|S )a(  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]]
	r   _N)r"   r   r   float)r   r   num_rowsZnum_colsir!   entryr   r   r   float_columns_from_file]   s    r(   c                 C   s   t | }|d |dd gS )aS  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]] ]
	r   r   N)r(   )r   r   r   r   r   xy_columns_from_filex   s    r)   c                 C   s   t | }|d }g }g }|dd }t|d dkrPttj? d|  f td ttt|d }td|D ],}|	|d|   |	|d| d   qn|||gS )a  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]]
	]
	r   r   N   z@xye_columns_from_file:  need odd number of columns in file '%s'.)
r(   r   r	   r   r
   r   introundr   r   )r   r   Z	x_columns	y_columns	e_columnsZtempnum_columnsr!   r   r   r   xye_columns_from_file   s"    
r0   %11.7fc                 C   sJ   | g}t |}td|D ] }|||  |||  qt||| d S Nr   )r   r   r   float_columns_to_file)Zx_columnr-   r.   r   formatZall_columnsZ
num_seriesr!   r   r   r   xye_columns_to_file   s    r5   c                 C   s   t | d}| }|dkr2ttj? df td | dkrB|  tdd|}|d dkr`g S td	d|}|d
 dkr|dd
 }tdd|}tdd|}|	 }|S )Nr   r   z2tabutil_m.labels_from_file:  couldn't read line 1.r   r   r   r   #z^#+r   r   r   )
r   r   r	   r   r
   r   r   r   r   r   )r   r   r    labelsr   r   r   labels_from_file   s&    

r8   c                 C   s\   t | }t | d }g }td|D ]4}g }td|D ]}|| | |  q4|| q"|S r2   )r   r   r   )r   r/   Z
num_tuplesZtuplesr&   tupler!   r   r   r   columns_to_tuples   s    r:   c                 C   s   t t| S )a%  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   r   r   r   float_rows_from_file  s    r;   c           
      C   s   t | }t | d }g }td|D ]P}g }td|D ]2}| | | }	|	dkrX|d q4|||	  q4|| q"t|| dS )zGiven 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
	r   Nr#   )r   r   r   text_columns_to_file)
r   r   r4   r/   r%   text_columnsr!   text_columnr&   r'   r   r   r   r3     s    r3   c                 C   s   t | d|| dS )zGiven 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)float_rows_and_labels_to_file)rowsr   r4   r   r   r   float_rows_to_file0  s    rA   c                 C   s   t | }t | d }g }td|D ]P}g }td|D ]2}	| | |	 }
|
dkrX|d q4|||
  q4|| q"t||| dS )a9  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

	r   Nr#   )r   r   r   text_rows_and_labels_to_file)r@   r7   r   r4   r%   r/   Z	text_rowsr&   Ztext_rowr!   r'   r   r   r   r?   A  s    r?   c                 C   s   t | d| d S )Nr   )r3   )r   r4   r   r   r   print_float_columnse  s    rC   c                 C   s   t | }t | d }g }td|D ]P}g }td|D ]2}	| | |	 }
|
dkrX|d q4|||
  q4|| q"t||| dS )aL  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
	r   Nr#   )r   r   r   text_columns_and_labels_to_file)r   r7   r   r4   r/   r%   r=   r!   r>   r&   r'   r   r   r    float_columns_and_labels_to_filei  s    rE   c                 C   s   t | d | d S N)rD   )r   r   r   r   r   r<     s    r<   c                 C   s  t | }t | d }dg| }td|D ]f}| | }d}|D ]}	t |	}
|
|kr<|
}q<|d krjt || }
|dkrz|
d7 }
|
|kr|
}|||< q(t|d}|d krltd|D ]F}|dkr|d|| d || f  q|d|| || f  q|d td|D ]V}|dkr>|d d|| d  }|| n |d	 d||  }|| q
|d td|D ]T}td|D ]8}|dkr|d	 |d
|| | | | f  q|d qv|dkr|  d S Nr   r   wz#%-*sz %-*sr   r6   r    z%-*sr   r   r   writer   )r   r7   r   r/   r%   
max_widthsr!   Zcolumn	max_widthr'   widthr   dashesr&   r   r   r   rD     sR    



 






"
rD   c                 C   s   t | d | d S rF   )rB   )r@   r   r   r   r   text_rows_to_file  s    rP   c                 C   s  t | }t | d }dg| }td|D ]p}d}td|D ]$}| | | }	t |	}
|
|kr:|
}q:|d krt || }
|dkr|
d7 }
|
|kr|
}|||< q(t|d}|d krvtd|D ]F}|dkr|d|| d || f  q|d|| || f  q|d td|D ]V}|dkrH|d d|| d  }|| n |d	 d||  }|| q|d td|D ]T}td|D ]8}|dkr|d	 |d
|| | | | f  q|d q|dkr|  d S rG   rJ   )r@   r7   r   r%   r/   rL   r!   rM   r&   r'   rN   r   rO   r   r   r   rB     sR    



 






"
rB   c                 C   s\   i }t dt| D ]D}| | }i ||< t dt|D ] }|| }|| | || |< q4q|S r2   )r   r   )Zleft_labelsZ
top_labelsr   hashr&   Z
left_labelr!   Z	top_labelr   r   r   table_to_hashD  s    rR   c                 C   s    g }| D ]}| t| q|S rF   )r   r$   )Z
text_arrayZfloat_arrayr'   r   r   r   text_array_to_float_arrayP  s    rS   F%.7fc	                 C   s   |dkr~g }	t | }
td|
D ]$}| | }|| g| }|	| q|g}|D ]}|| }|d||f  qNt|	||| nB|g|  }|g}|D ]}|| }|d||f  qt|||| d S )NFr   z%s=%s)r   r   r   r?   rE   )matrixrow_index_namerow_index_valuescol_index_namecol_index_valuesr   	transposematrix_formatlabel_formatZ	long_rowsr%   r&   Z	short_rowZlong_rowr7   Zcol_index_valueZcol_index_stringZaugmented_columnsZrow_index_valueZrow_index_stringr   r   r   matrix_and_labels_to_filen  s,    
r]   )r1   )r1   )r1   )r1   )r1   )r1   )Fr1   rT   )r   r   r   r"   r(   r)   r0   r5   r8   r:   r;   r3   rA   r?   rC   rE   r<   rD   rP   rB   rR   rS   r]   r   r   r   r   <module>   s2   =& 
,


$
 
+@V   