8239fc37f0e54da15d41414ae3b4de463f8d8877
[hooke.git] / hooke / driver / mfp1dexport.py
1 '''
2 mfp1dexport.py
3
4 Driver for text-exported MFP 1D files
5
6 Massimo Sandal (c) 2009
7 '''
8
9 import os
10
11 from .. import libhooke as lh
12 from .. import curve as lhc
13
14
15 __version__='0.0.0.20090923'
16
17
18 class mfp1dexportDriver(lhc.Driver):
19
20     def __init__(self, filename):
21         '''
22         This is a driver to import Asylum Research MFP 1D data.
23         Status: experimental
24         '''
25         self.filename = filename
26         self.filedata = open(filename,'rU')
27         self.lines = list(self.filedata.readlines())
28         self.filedata.close()
29
30         self.filetype='mfp1dexport'
31         self.experiment='smfs'
32
33     def close_all(self):
34         self.filedata.close()
35
36     def is_me(self):
37         try:
38             self.raw_header = self.lines[0:38]
39         except:
40             #Not enough lines for a header; not a good file
41             return False
42
43         #FIXME: We want a more reasonable header recognition
44         if self.raw_header[0].startswith('Wave'):
45             return True
46         else:
47             return False
48
49     def _read_columns(self):
50
51         self.raw_columns=self.lines[39:]
52
53         kline=None
54         for line in self.lines:
55             if line[:7]=='SpringC':
56                 kline=line
57                 break
58
59         kline=kline.split(':')
60
61         #self.k=float(self.raw_header[23][8:])
62         self.k=float(kline[1])
63
64         xext=[]
65         xret=[]
66         yext=[]
67         yret=[]
68         for line in self.raw_columns:
69             spline=line.split()
70             xext.append(float(spline[0]))
71             yext.append(float(spline[1]))
72             xret.append(float(spline[2]))
73             yret.append(float(spline[3]))
74
75         return [[xext,yext],[xret,yret]]
76
77     def deflection(self):
78         self.data = self._read_columns()
79         return self.data[0][1], self.data[1][1]
80
81     def default_plots(self):
82         main_plot = lhc.PlotObject()
83         defl_ext,defl_ret = self.deflection()
84         yextforce = [i*self.k for i in defl_ext]
85         yretforce = [i*self.k for i in defl_ret]
86         main_plot.add_set(self.data[0][0], yextforce)
87         main_plot.add_set(self.data[1][0], yretforce)
88         main_plot.normalize_vectors()
89         #main_plot.units = ['Z','force']  #FIXME: if there's an header saying something about the time count, should be used
90         main_plot.units = ['m','N']
91         main_plot.destination = 0
92         main_plot.filename = self.filename
93         main_plot.title = os.path.basename(self.filename)
94         main_plot.colors = ['red','blue']
95         main_plot.style = ['plot','plot']
96         return [main_plot]