Added modular directory structure.
[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 from .. import libhookecurve as lhc
10 from .. import libhooke as lh
11
12 class mfp1dexportDriver(lhc.Driver):
13
14     def __init__(self, filename):
15
16         self.filename=filename
17         self.filedata=open(filename,'rU')
18         self.lines=list(self.filedata.readlines())
19         self.filedata.close()
20
21         self.filetype='mfp1dexport'
22         self.experiment='smfs'
23
24     def close_all(self):
25         self.filedata.close()
26
27     def is_me(self):
28         try:
29             self.raw_header=self.lines[0:38]
30         except:
31             #Not enough lines for a header; not a good file
32             return False
33
34         #FIXME: We want a more reasonable header recognition
35         if self.raw_header[0][0:4]=='Wave':
36             return True
37         else:
38             return False
39
40     def _read_columns(self):
41
42         self.raw_columns=self.lines[39:]
43
44         kline=None
45         for line in self.lines:
46             if line[:7]=='SpringC':
47                 kline=line
48                 break
49
50         kline=kline.split(':')
51
52         #self.k=float(self.raw_header[23][8:])
53         self.k=float(kline[1])
54
55
56         xext=[]
57         xret=[]
58         yext=[]
59         yret=[]
60         for line in self.raw_columns:
61             spline=line.split()
62             xext.append(float(spline[0]))
63             yext.append(float(spline[1]))
64             xret.append(float(spline[2]))
65             yret.append(float(spline[3]))
66
67         return [[xext,yext],[xret,yret]]
68
69     def deflection(self):
70         self.data=self._read_columns()
71         return self.data[0][1],self.data[1][1]
72
73
74     def default_plots(self):
75         main_plot=lhc.PlotObject()
76         defl_ext,defl_ret=self.deflection()
77         yextforce=[i*self.k for i in defl_ext]
78         yretforce=[i*self.k for i in defl_ret]
79         main_plot.add_set(self.data[0][0],yextforce)
80         main_plot.add_set(self.data[1][0],yretforce)
81         main_plot.normalize_vectors()
82         main_plot.units=['Z','force']  #FIXME: if there's an header saying something about the time count, should be used
83         main_plot.destination=0
84         main_plot.title=self.filename
85         #main_plot.colors=['red','blue']
86         return [main_plot]