Don't specify curve or data block types. See doc/standards.txt.
[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         self.filename=filename
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 into a normal list
52         columns=lib.libhooke.transposed2(rows[1:])
53
54         for index in range(0, len(columns), 2):
55             temp_x=columns[index]
56             temp_y=columns[index+1]
57             #convert to float (the csv gives strings)
58             temp_x=[float(item) for item in temp_x]
59             temp_y=[float(item) for item in temp_y]
60
61             curve = lib.curve.Curve()
62
63             curve.destination.row = index + 1
64             curve.label = 'curve ' + str(index)
65             curve.style = 'plot'
66             curve.units.x = 'x'
67             curve.units.y = 'y'
68             curve.x = temp_x
69             curve.y = temp_y
70
71             plot = lib.plot.Plot()
72             plot.title = os.path.basename(self.filename)
73             plot.curves.append(curve)
74
75         #TODO: is normalization helpful or detrimental here?
76         #plot.normalize()
77         return plot
78
79     def is_me(self):
80         myfile=file(self.filename)
81         headerline=myfile.readlines()[0]
82         myfile.close()
83
84         #using a custom header makes things much easier...
85         #(looking for raw CSV data is at strong risk of confusion)
86         if headerline[:-1]=='Hooke data':
87             return True
88         else:
89             return False