3ec4cbe054abe49178b273249a20a8c4e9d2a7e6
[hooke.git] / hooke / driver / csvdriver.py
1 # Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """Simple driver to read general comma-separated values in Hooke
21
22 Columns are read this way:
23
24 X1 , Y1 , X2 , Y2 , X3 , Y3 ...
25
26 If the number of columns is odd, the last column is ignored.
27 """
28
29 import csv
30 import os.path
31
32 import lib.curve
33 import lib.driver
34 import lib.libhooke
35 import lib.plot
36
37 class csvdriverDriver(lib.driver.Driver):
38
39     def __init__(self, filename):
40
41         self.filedata = open(filename,'r')
42         self.data = list(self.filedata)
43         self.filedata.close()
44
45         self.filetype = 'generic'
46         self.experiment = ''
47
48         self.filename=filename
49
50     def close_all(self):
51         self.filedata.close()
52
53     def default_plots(self):
54         rrows=csv.reader(self.data)
55         rows=list(rrows) #transform the csv.reader iterator into a normal list
56         columns=lib.libhooke.transposed2(rows[1:])
57
58         for index in range(0, len(columns), 2):
59             temp_x=columns[index]
60             temp_y=columns[index+1]
61             #convert to float (the csv gives strings)
62             temp_x=[float(item) for item in temp_x]
63             temp_y=[float(item) for item in temp_y]
64
65             curve = lib.curve.Curve()
66
67             curve.destination.row = index + 1
68             curve.label = 'curve ' + str(index)
69             curve.style = 'plot'
70             curve.units.x = 'x'
71             curve.units.y = 'y'
72             curve.x = temp_x
73             curve.y = temp_y
74
75             plot = lib.plot.Plot()
76             plot.title = os.path.basename(self.filename)
77             plot.curves.append(curve)
78
79         #TODO: is normalization helpful or detrimental here?
80         #plot.normalize()
81         return plot
82
83     def is_me(self):
84         myfile=file(self.filename)
85         headerline=myfile.readlines()[0]
86         myfile.close()
87
88         #using a custom header makes things much easier...
89         #(looking for raw CSV data is at strong risk of confusion)
90         if headerline[:-1]=='Hooke data':
91             return True
92         else:
93             return False