Hooke(GUI)
[hooke.git] / drivers / 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 Copyright 2008 by Massimo Sandal
15 with modifications by Dr. Rolf Schmidt (Concordia University, Canada)
16
17 This program is released under the GNU General Public License version 2.
18 '''
19
20 import csv
21 import os.path
22
23 import lib.curve
24 import lib.driver
25 import lib.libhooke
26 import lib.plot
27
28 class csvdriverDriver(lib.driver.Driver):
29
30     def __init__(self, filename):
31
32         self.filedata = open(filename,'r')
33         self.data = list(self.filedata)
34         self.filedata.close()
35
36         self.filetype = 'generic'
37         self.experiment = ''
38
39         self.filename=filename
40
41     def close_all(self):
42         self.filedata.close()
43
44     def default_plots(self):
45         rrows=csv.reader(self.data)
46         rows=list(rrows) #transform the csv.reader iterator into a normal list
47         columns=lib.libhooke.transposed2(rows[1:])
48
49         for index in range(0, len(columns), 2):
50             temp_x=columns[index]
51             temp_y=columns[index+1]
52             #convert to float (the csv gives strings)
53             temp_x=[float(item) for item in temp_x]
54             temp_y=[float(item) for item in temp_y]
55
56             curve = lib.curve.Curve()
57
58             curve.destination.row = index + 1
59             curve.label = 'curve ' + str(index)
60             curve.style = 'plot'
61             curve.units.x = 'x'
62             curve.units.y = 'y'
63             curve.x = temp_x
64             curve.y = temp_y
65
66             plot = lib.plot.Plot()
67             plot.title = os.path.basename(self.filename)
68             plot.curves.append(curve)
69
70         #TODO: is normalization helpful or detrimental here?
71         #plot.normalize()
72         return plot
73
74     def is_me(self):
75         myfile=file(self.filename)
76         headerline=myfile.readlines()[0]
77         myfile.close()
78
79         #using a custom header makes things much easier...
80         #(looking for raw CSV data is at strong risk of confusion)
81         if headerline[:-1]=='Hooke data':
82             return True
83         else:
84             return False
85