1 # Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
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.
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.
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/>.
20 """Simple driver to read general comma-separated values in Hooke
22 Columns are read this way:
24 X1 , Y1 , X2 , Y2 , X3 , Y3 ...
26 If the number of columns is odd, the last column is ignored.
29 from .. import curve as lhc
30 from .. import libhooke as lh
33 class csvdriverDriver(lhc.Driver):
35 def __init__(self, filename):
37 self.filedata = open(filename,'r')
38 self.data = list(self.filedata)
41 self.filetype = 'generic'
44 self.filename=filename
47 myfile=file(self.filename)
48 headerline=myfile.readlines()[0]
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':
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:])
66 main_plot=lhc.PlotObject()
69 for index in range(0,len(columns),2):
70 main_plot.vectors.append([])
72 temp_y=columns[index+1]
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]
78 main_plot.vectors[-1].append(temp_x)
79 main_plot.vectors[-1].append(temp_y)
81 main_plot.units=['x','y']
82 main_plot.title=self.filename
83 main_plot.destination=0