Don't specify curve or data block types. See doc/standards.txt.
[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 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 """Library for interpreting Hemingway force spectroscopy files.
21 """
22
23 import os.path
24 import numpy
25
26 from .. import curve as curve
27 from ..util.util import Closing as Closing
28 from . import Driver as Driver
29
30
31 class HemingwayDriver (Driver):
32     """Handle Hemingway force spectroscopy files.
33     """
34     def __init__(self):
35         super(HemingwayDriver, self).__init__(name='hemingway')
36
37     def is_me(self, path):
38         if os.path.isdir(path):
39             return False
40         headlines = []
41         with Closing(file(path, 'r')) as f:
42             for i in range(2):
43                 headlines.append(f.readline())
44         return (headlines[0].startswith('#Hemingway')
45                 and headlines[1].startswith('#Experiment: FClamp'))
46
47     def read(self, path, info=None):
48         file_info = {}
49         with Closing(file(path, 'r')) as f:
50             while True:
51                 line = f.readline().strip()
52                 if line == '#END':
53                     break
54                 fields = line.split(':', 1)
55                 if len(fields) == 2:
56                     file_info[fields[0]] = fields[1]
57             data = numpy.loadtxt(f, dtype=numpy.float)
58         ret = curve.Data(
59             shape=data.shape,
60             dtype=data.dtype,
61             buffer=data,
62             info=file_info,
63             )
64         ret.info['columns'] = [
65             'time (s)',  # first data column in file just increasing index
66             'phase (m?rad)',
67             'z piezo (m)',
68             'deflection (N)',
69             'imposed (N)',
70             ]
71         ret.info['name'] = 'force clamp'
72         # assume 1 ms timestep
73         ret[:,0] = numpy.arange(0, 1e-3*data.shape[0], 1e-3, dtype=ret.dtype)
74
75         return ([ret,], file_info)