Fix existing Driver.is_me's crashes if path is a directory.
[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 import os.path
25
26 from .. import libhooke as lh
27 from .. import curve as lhc
28
29
30 __version__='0.0.0.20090923'
31
32
33 class mfp1dexportDriver(lhc.Driver):
34
35     def __init__(self, filename):
36         '''
37         This is a driver to import Asylum Research MFP 1D data.
38         Status: experimental
39         '''
40         self.filename = filename
41         self.filedata = open(filename,'rU')
42         self.lines = list(self.filedata.readlines())
43         self.filedata.close()
44
45         self.filetype='mfp1dexport'
46         self.experiment='smfs'
47
48     def close_all(self):
49         self.filedata.close()
50
51     def is_me(self):
52         if os.path.isdir(path):
53             return False
54         try:
55             self.raw_header = self.lines[0:38]
56         except:
57             #Not enough lines for a header; not a good file
58             return False
59
60         #FIXME: We want a more reasonable header recognition
61         if self.raw_header[0].startswith('Wave'):
62             return True
63         else:
64             return False
65
66     def _read_columns(self):
67
68         self.raw_columns=self.lines[39:]
69
70         kline=None
71         for line in self.lines:
72             if line[:7]=='SpringC':
73                 kline=line
74                 break
75
76         kline=kline.split(':')
77
78         #self.k=float(self.raw_header[23][8:])
79         self.k=float(kline[1])
80
81         xext=[]
82         xret=[]
83         yext=[]
84         yret=[]
85         for line in self.raw_columns:
86             spline=line.split()
87             xext.append(float(spline[0]))
88             yext.append(float(spline[1]))
89             xret.append(float(spline[2]))
90             yret.append(float(spline[3]))
91
92         return [[xext,yext],[xret,yret]]
93
94     def deflection(self):
95         self.data = self._read_columns()
96         return self.data[0][1], self.data[1][1]
97
98     def default_plots(self):
99         main_plot = lhc.PlotObject()
100         defl_ext,defl_ret = self.deflection()
101         yextforce = [i*self.k for i in defl_ext]
102         yretforce = [i*self.k for i in defl_ret]
103         main_plot.add_set(self.data[0][0], yextforce)
104         main_plot.add_set(self.data[1][0], yretforce)
105         main_plot.normalize_vectors()
106         #main_plot.units = ['Z','force']  #FIXME: if there's an header saying something about the time count, should be used
107         main_plot.units = ['m','N']
108         main_plot.destination = 0
109         main_plot.filename = self.filename
110         main_plot.title = os.path.basename(self.filename)
111         main_plot.colors = ['red','blue']
112         main_plot.style = ['plot','plot']
113         return [main_plot]