4222255f32d9a0b966c2d5f5d4a0560da06fc8c1
[hooke.git] / hooke / driver / csvdriver.py
1 #!/usr/bin/env python
2
3 '''
4 csvdriver.py
5
6 Simple driver to read general comma-separated values in Hooke
7
8 Columns are read this way:
9     
10 X1 , Y1 , X2 , Y2 , X3 , Y3 ...
11
12 If the number of columns is odd, the last column is ignored.
13
14 (c)Massimo Sandal, 2008
15 '''
16
17 import libhookecurve as lhc
18 import libhooke as lh
19 import csv
20
21 class csvdriverDriver(lhc.Driver):
22     
23         def __init__(self, filename):
24         
25             self.filedata = open(filename,'r')
26             self.data = list(self.filedata) 
27             self.filedata.close()
28         
29             self.filetype = 'generic'
30             self.experiment = ''
31             
32             self.filename=filename
33         
34         def is_me(self):
35             myfile=file(self.filename)
36             headerline=myfile.readlines()[0]
37             myfile.close()
38             
39             #using a custom header makes things much easier...
40             #(looking for raw CSV data is at strong risk of confusion)
41             if headerline[:-1]=='Hooke data':
42                 return True
43             else:
44                 return False
45         
46         def close_all(self):
47             self.filedata.close()
48         
49         def default_plots(self):
50             rrows=csv.reader(self.data)
51             rows=list(rrows) #transform the csv.reader iterator in a normal list
52             columns=lh.transposed2(rows[1:])
53             
54             main_plot=lhc.PlotObject()
55             main_plot.vectors=[]
56             
57             for index in range(0,len(columns),2):
58                 main_plot.vectors.append([])
59                 temp_x=columns[index]
60                 temp_y=columns[index+1]
61                 
62                 #convert to float (the csv gives strings)
63                 temp_x=[float(item) for item in temp_x]
64                 temp_y=[float(item) for item in temp_y]
65                 
66                 main_plot.vectors[-1].append(temp_x)
67                 main_plot.vectors[-1].append(temp_y)
68                 
69             main_plot.units=['x','y']
70             main_plot.title=self.filename
71             main_plot.destination=0
72             
73             return [main_plot]
74             
75