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