Run update-copyright.py.
[hooke.git] / hooke / driver / mfp1dexport.py
1 # Copyright (C) 2009-2012 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 under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
18
19 """Driver for text-exported MFP 1D files
20 """
21
22 import os
23 import os.path
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     def close_all(self):
45         self.filedata.close()
46
47     def is_me(self):
48         if os.path.isdir(path):
49             return False
50         try:
51             self.raw_header = self.lines[0:38]
52         except:
53             #Not enough lines for a header; not a good file
54             return False
55
56         #FIXME: We want a more reasonable header recognition
57         if self.raw_header[0].startswith('Wave'):
58             return True
59         else:
60             return False
61
62     def _read_columns(self):
63
64         self.raw_columns=self.lines[39:]
65
66         kline=None
67         for line in self.lines:
68             if line[:7]=='SpringC':
69                 kline=line
70                 break
71
72         kline=kline.split(':')
73
74         #self.k=float(self.raw_header[23][8:])
75         self.k=float(kline[1])
76
77         xext=[]
78         xret=[]
79         yext=[]
80         yret=[]
81         for line in self.raw_columns:
82             spline=line.split()
83             xext.append(float(spline[0]))
84             yext.append(float(spline[1]))
85             xret.append(float(spline[2]))
86             yret.append(float(spline[3]))
87
88         return [[xext,yext],[xret,yret]]
89
90     def deflection(self):
91         self.data = self._read_columns()
92         return self.data[0][1], self.data[1][1]
93
94     def default_plots(self):
95         main_plot = lhc.PlotObject()
96         defl_ext,defl_ret = self.deflection()
97         yextforce = [i*self.k for i in defl_ext]
98         yretforce = [i*self.k for i in defl_ret]
99         main_plot.add_set(self.data[0][0], yextforce)
100         main_plot.add_set(self.data[1][0], yretforce)
101         main_plot.normalize_vectors()
102         #main_plot.units = ['Z','force']  #FIXME: if there's an header saying something about the time count, should be used
103         main_plot.units = ['m','N']
104         main_plot.destination = 0
105         main_plot.filename = self.filename
106         main_plot.title = os.path.basename(self.filename)
107         main_plot.colors = ['red','blue']
108         main_plot.style = ['plot','plot']
109         return [main_plot]