Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[hooke.git] / hooke / driver / hemingclamp.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 """Library for interpreting Hemingway force spectroscopy files.
21 """
22 __version__='2007_02_15_devel'
23
24 __changelog__='''
25 2007_02_15: fixed time counter with my counter
26 2007_02_07: Initial implementation
27 '''
28 import string
29 from .. import curve as lhc
30
31 class DataChunk(list):
32     '''Dummy class to provide ext and ret methods to the data list.
33     In this case ext and self can be equal.
34     '''
35
36     def ext(self):
37         return self
38
39     def ret(self):
40         return self
41
42 class hemingclampDriver(lhc.Driver):
43
44     def __init__(self, filename):
45
46         self.filedata = open(filename,'r')
47         self.data = self.filedata.readlines()[6:]
48         self.filedata.close()
49
50         self.filetype = 'hemingclamp'
51         self.experiment = 'clamp'
52
53         self.filename=filename
54
55     def __del__(self):
56         self.filedata.close()
57
58     def is_me(self):
59         '''
60         we define our magic heuristic for HemingClamp files
61         '''
62         myfile=file(self.filename)
63         headerlines=myfile.readlines()[0:3]
64         myfile.close()
65         if headerlines[0][0:10]=='#Hemingway' and headerlines[1][0:19]=='#Experiment: FClamp':
66             return True
67         else:
68             return False
69
70     def _getdata_all(self):
71         time = []
72         phase = []
73         zpiezo = []
74         defl = []
75         imposed = []
76         trim_indexes = []
77         trim_counter = 0.0
78
79         for i in self.data:
80             temp = string.split(i)
81             #time.append(float(temp[0])*(1.0e-3)) # This is managed differently now, since each data point = 1ms: see below
82             phase.append(float(temp[1])*(1.0e-7)) # The nonsensical (e-7) multiplier is just there to make phase data nicely plottable along other data
83             zpiezo.append(float(temp[2])*(1.0e-9))
84             defl.append(float(temp[3])*(1.0e-9))
85             imposed.append(float(temp[4])*(1.0e-9))
86
87         for x in range (0,len(phase)):
88             if phase[x] != trim_counter:
89                 trim_indexes.append(x)
90                 trim_counter = phase[x]
91
92         #we rebuild the time counter assuming 1 point = 1 millisecond
93         c=0.0
94         for z in zpiezo:
95             time.append(c)
96             c+=(1.0e-3)
97
98         return time,phase,zpiezo,defl,imposed,trim_indexes
99
100     def time(self):
101         return DataChunk(self._getdata_all()[0])
102
103     def phase(self):
104         return DataChunk(self._getdata_all()[1])
105
106     def zpiezo(self):
107         return DataChunk(self._getdata_all()[2])
108
109     def deflection(self):
110         return DataChunk(self._getdata_all()[3])
111
112     def imposed(self):
113         return DataChunk(self._getdata_all()[4])
114
115     def trimindexes(self):
116         return DataChunk(self._getdata_all()[5])
117
118     def close_all(self):
119         '''
120         Explicitly closes all files
121         '''
122         self.filedata.close()
123
124     def default_plots(self):
125         main_plot=lhc.PlotObject()
126         defl_plot=lhc.PlotObject()
127
128         time=self.time()
129         phase=self.phase()
130         zpiezo=self.zpiezo()
131         deflection=self.deflection()
132         imposed=self.imposed()
133
134         main_plot.vectors=[[time,zpiezo],[time,phase]]
135         main_plot.units=['seconds','meters']
136         main_plot.destination=0
137         main_plot.title=self.filename
138
139         defl_plot.vectors=[[time,deflection],[time,imposed]]
140         defl_plot.units=['seconds','Newtons']
141         defl_plot.destination=1
142
143         return [main_plot, defl_plot]
144