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