Added README and reorganized directory structure (breaks code)
[hooke.git] / hooke / driver / 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) 2008 Massimo Sandal, Marco Brucale (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 class DataChunk(list):
22     '''Dummy class to provide ext and ret methods to the data list.
23     In this case ext and self can be equal.
24     '''
25     
26     def ext(self):
27         return self
28         
29     def ret(self):
30         return self
31
32 class hemingclampDriver(lhc.Driver):
33     
34     def __init__(self, filename):
35         
36         self.filedata = open(filename,'r')
37         self.data = self.filedata.readlines()[6:]
38         self.filedata.close()
39         
40         self.filetype = 'hemingclamp'
41         self.experiment = 'clamp'
42         
43         self.filename=filename
44        
45     def __del__(self):
46         self.filedata.close()   
47     
48     def is_me(self):
49         '''
50         we define our magic heuristic for HemingClamp files
51         '''
52         myfile=file(self.filename)
53         headerlines=myfile.readlines()[0:3]
54         myfile.close()
55         if headerlines[0][0:10]=='#Hemingway' and headerlines[1][0:19]=='#Experiment: FClamp':
56             return True
57         else:
58             return False
59         
60     def _getdata_all(self):
61         time = []
62         phase = []
63         zpiezo = []
64         defl = []
65         imposed = []
66         trim_indexes = []
67         trim_counter = 0.0
68                         
69         for i in self.data:
70             temp = string.split(i)
71             #time.append(float(temp[0])*(1.0e-3)) # This is managed differently now, since each data point = 1ms: see below
72             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
73             zpiezo.append(float(temp[2])*(1.0e-9))
74             defl.append(float(temp[3])*(1.0e-9))
75             imposed.append(float(temp[4])*(1.0e-9))
76
77         for x in range (0,len(phase)):
78             if phase[x] != trim_counter:
79                 trim_indexes.append(x)
80                 trim_counter = phase[x]
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,phase,zpiezo,defl,imposed,trim_indexes
89         
90     def time(self):
91         return DataChunk(self._getdata_all()[0])
92
93     def phase(self):
94         return DataChunk(self._getdata_all()[1])
95     
96     def zpiezo(self):
97         return DataChunk(self._getdata_all()[2])
98      
99     def deflection(self):
100         return DataChunk(self._getdata_all()[3])
101
102     def imposed(self):
103         return DataChunk(self._getdata_all()[4])
104
105     def trimindexes(self):
106         return DataChunk(self._getdata_all()[5])
107     
108     def close_all(self):
109         '''
110         Explicitly closes all files
111         '''
112         self.filedata.close()
113     
114     def default_plots(self):
115         main_plot=lhc.PlotObject()
116         defl_plot=lhc.PlotObject()
117         
118         time=self.time()
119         phase=self.phase()
120         zpiezo=self.zpiezo()
121         deflection=self.deflection()
122         imposed=self.imposed()
123                 
124         main_plot.vectors=[[time,zpiezo],[time,phase]]
125         main_plot.units=['seconds','meters']
126         main_plot.destination=0
127         main_plot.title=self.filename
128         
129         defl_plot.vectors=[[time,deflection],[time,imposed]]
130         defl_plot.units=['seconds','Newtons']
131         defl_plot.destination=1
132  
133         return [main_plot, defl_plot]
134