Use numpy.loadtxt instead of newer numpy.genfromtxt in Hemingway driver.
[hooke.git] / hooke / driver / hemingway.py
1 # Copyright (C) 2008-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
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General 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 """Library for interpreting Hemingway force spectroscopy files.
21 """
22
23 import numpy
24
25 from .. import curve as curve
26 from .. import experiment as experiment
27 from ..config import Setting
28 from ..util.util import Closing as Closing
29 from . import Driver as Driver
30
31
32 class HemingwayDriver (Driver):
33     """Handle Hemingway force spectroscopy files.
34     """
35     def __init__(self):
36         super(HemingwayDriver, self).__init__(name='hemingway')
37
38     def is_me(self, path):
39         headlines = []
40         with Closing(file(path, 'r')) as f:
41             for i in range(2):
42                 headlines.append(f.readline())
43         return (headlines[0].startswith('#Hemingway')
44                 and headlines[1].startswith('#Experiment: FClamp'))
45
46     def read(self, path, info=None):
47         file_info = {}
48         with Closing(file(path, 'r')) as f:
49             while True:
50                 line = f.readline().strip()
51                 if line == '#END':
52                     break
53                 fields = line.split(':', 1)
54                 if len(fields) == 2:
55                     file_info[fields[0]] = fields[1]
56             data = numpy.loadtxt(f, dtype=numpy.float)
57         ret = curve.Data(
58             shape=data.shape,
59             dtype=data.dtype,
60             buffer=data,
61             info=file_info,
62             )
63         ret.info['columns'] = [
64             'time (s)',  # first data column in file just increasing index
65             'phase (m?rad)',
66             'z piezo (m)',
67             'deflection (N)',
68             'imposed (N)',
69             ]
70         # assume 1 ms timestep
71         ret[:,0] = numpy.arange(0, 1e-3*data.shape[0], 1e-3, dtype=ret.dtype)
72         
73         file_info['filetype'] = self.name
74         file_info['experiment'] = experiment.ForceClamp
75         return ([ret,], file_info)