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