Ran update_copyright.py
[hooke.git] / hooke / driver / mfp1dexport.py
1 # Copyright (C) 2009-2010 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """Driver for text-exported MFP 1D files
21 """
22
23 import os
24
25 from .. import libhooke as lh
26 from .. import curve as lhc
27
28
29 __version__='0.0.0.20090923'
30
31
32 class mfp1dexportDriver(lhc.Driver):
33
34     def __init__(self, filename):
35         '''
36         This is a driver to import Asylum Research MFP 1D data.
37         Status: experimental
38         '''
39         self.filename = filename
40         self.filedata = open(filename,'rU')
41         self.lines = list(self.filedata.readlines())
42         self.filedata.close()
43
44         self.filetype='mfp1dexport'
45         self.experiment='smfs'
46
47     def close_all(self):
48         self.filedata.close()
49
50     def is_me(self):
51         try:
52             self.raw_header = self.lines[0:38]
53         except:
54             #Not enough lines for a header; not a good file
55             return False
56
57         #FIXME: We want a more reasonable header recognition
58         if self.raw_header[0].startswith('Wave'):
59             return True
60         else:
61             return False
62
63     def _read_columns(self):
64
65         self.raw_columns=self.lines[39:]
66
67         kline=None
68         for line in self.lines:
69             if line[:7]=='SpringC':
70                 kline=line
71                 break
72
73         kline=kline.split(':')
74
75         #self.k=float(self.raw_header[23][8:])
76         self.k=float(kline[1])
77
78         xext=[]
79         xret=[]
80         yext=[]
81         yret=[]
82         for line in self.raw_columns:
83             spline=line.split()
84             xext.append(float(spline[0]))
85             yext.append(float(spline[1]))
86             xret.append(float(spline[2]))
87             yret.append(float(spline[3]))
88
89         return [[xext,yext],[xret,yret]]
90
91     def deflection(self):
92         self.data = self._read_columns()
93         return self.data[0][1], self.data[1][1]
94
95     def default_plots(self):
96         main_plot = lhc.PlotObject()
97         defl_ext,defl_ret = self.deflection()
98         yextforce = [i*self.k for i in defl_ext]
99         yretforce = [i*self.k for i in defl_ret]
100         main_plot.add_set(self.data[0][0], yextforce)
101         main_plot.add_set(self.data[1][0], yretforce)
102         main_plot.normalize_vectors()
103         #main_plot.units = ['Z','force']  #FIXME: if there's an header saying something about the time count, should be used
104         main_plot.units = ['m','N']
105         main_plot.destination = 0
106         main_plot.filename = self.filename
107         main_plot.title = os.path.basename(self.filename)
108         main_plot.colors = ['red','blue']
109         main_plot.style = ['plot','plot']
110         return [main_plot]