Added illysam branch
[hooke.git] / drivers / mfp1dexport.py
1 #!/usr/bin/env python
2
3 '''
4 mfp1dexport.py
5
6 Driver for text-exported MFP 1D files.
7
8 Copyright 2009 by Massimo Sandal
9 with modifications by Dr. Rolf Schmidt (Concordia University, Canada)
10
11 This program is released under the GNU General Public License version 2.
12 '''
13
14 import os.path
15
16 import lib.driver
17 import lib.curve
18 import lib.plot
19
20 __version__='0.0.0.20090923'
21
22 class mfp1dexportDriver(lib.driver.Driver):
23
24     def __init__(self, filename):
25         '''
26         This is a driver to import Asylum Research MFP 1D data.
27         Status: experimental
28         '''
29         self.filename = filename
30         self.filedata = open(filename,'rU')
31         self.lines = list(self.filedata.readlines())
32         self.filedata.close()
33
34         self.filetype = 'mfp1dexport'
35         self.experiment = 'smfs'
36
37     def _read_columns(self):
38
39         self.raw_columns=self.lines[39:]
40
41         kline=None
42         for line in self.lines:
43             if line[:7]=='SpringC':
44                 kline=line
45                 break
46
47         kline=kline.split(':')
48
49         #self.k=float(self.raw_header[23][8:])
50         self.k=float(kline[1])
51
52         #find retract velocity to calculate loading rate
53         retract_velocity = None
54         for line in self.lines:
55             if line.startswith('RetractVelocity:'):
56                 retract_velocity = line.split(':')
57                 self.retract_velocity = float(retract_velocity[1])
58                 break
59
60         xext=[]
61         xret=[]
62         yext=[]
63         yret=[]
64         for line in self.raw_columns:
65             spline=line.split()
66             xext.append(float(spline[0]))
67             yext.append(float(spline[1]))
68             xret.append(float(spline[2]))
69             yret.append(float(spline[3]))
70
71         return [[xext,yext],[xret,yret]]
72
73     def close_all(self):
74         self.filedata.close()
75
76     def is_me(self):
77         try:
78             self.raw_header = self.lines[0:38]
79         except:
80             #Not enough lines for a header; not a good file
81             return False
82
83         #FIXME: We want a more reasonable header recognition
84         if self.raw_header[0].startswith('Wave'):
85             return True
86         else:
87             return False
88
89     def default_plots(self):
90         '''
91         loads the curve data
92         '''
93         defl_ext, defl_ret = self.deflection()
94         yextforce = [i * self.k for i in defl_ext]
95         yretforce = [i * self.k for i in defl_ret]
96
97         extension = lib.curve.Curve()
98         retraction = lib.curve.Curve()
99
100         extension.color = 'red'
101         extension.label = 'extension'
102         extension.style = 'plot'
103         extension.title = 'Force curve'
104         extension.units.x = 'm'
105         extension.units.y = 'N'
106         extension.x = self.data[0][0]
107         extension.y = yextforce
108         retraction.color = 'blue'
109         retraction.label = 'retraction'
110         retraction.style = 'plot'
111         retraction.title = 'Force curve'
112         retraction.units.x = 'm'
113         retraction.units.y = 'N'
114         retraction.x = self.data[1][0]
115         retraction.y = yretforce
116
117         plot = lib.plot.Plot()
118         plot.title = os.path.basename(self.filename)
119         plot.curves.append(extension)
120         plot.curves.append(retraction)
121
122         plot.normalize()
123         return plot
124
125     def deflection(self):
126         self.data = self._read_columns()
127         return self.data[0][1], self.data[1][1]