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