Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[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
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General 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 from .. import curve as lhc
30 from .. import libhooke as lh
31 import csv
32
33 class csvdriverDriver(lhc.Driver):
34
35         def __init__(self, filename):
36
37             self.filedata = open(filename,'r')
38             self.data = list(self.filedata)
39             self.filedata.close()
40
41             self.filetype = 'generic'
42             self.experiment = ''
43
44             self.filename=filename
45
46         def is_me(self):
47             myfile=file(self.filename)
48             headerline=myfile.readlines()[0]
49             myfile.close()
50
51             #using a custom header makes things much easier...
52             #(looking for raw CSV data is at strong risk of confusion)
53             if headerline[:-1]=='Hooke data':
54                 return True
55             else:
56                 return False
57
58         def close_all(self):
59             self.filedata.close()
60
61         def default_plots(self):
62             rrows=csv.reader(self.data)
63             rows=list(rrows) #transform the csv.reader iterator in a normal list
64             columns=lh.transposed2(rows[1:])
65
66             main_plot=lhc.PlotObject()
67             main_plot.vectors=[]
68
69             for index in range(0,len(columns),2):
70                 main_plot.vectors.append([])
71                 temp_x=columns[index]
72                 temp_y=columns[index+1]
73
74                 #convert to float (the csv gives strings)
75                 temp_x=[float(item) for item in temp_x]
76                 temp_y=[float(item) for item in temp_y]
77
78                 main_plot.vectors[-1].append(temp_x)
79                 main_plot.vectors[-1].append(temp_y)
80
81             main_plot.units=['x','y']
82             main_plot.title=self.filename
83             main_plot.destination=0
84
85             return [main_plot]