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