Run update-copyright.py.
[hooke.git] / hooke / driver / hemingway.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Library for interpreting Hemingway force spectroscopy files.
19 """
20
21 import os.path
22 import numpy
23
24 from .. import curve as curve
25 from ..util.util import Closing as Closing
26 from . import Driver as Driver
27
28
29 class HemingwayDriver (Driver):
30     """Handle Hemingway force spectroscopy files.
31     """
32     def __init__(self):
33         super(HemingwayDriver, self).__init__(name='hemingway')
34
35     def is_me(self, path):
36         if os.path.isdir(path):
37             return False
38         headlines = []
39         with Closing(file(path, 'r')) as f:
40             for i in range(2):
41                 headlines.append(f.readline())
42         return (headlines[0].startswith('#Hemingway')
43                 and headlines[1].startswith('#Experiment: FClamp'))
44
45     def read(self, path, info=None):
46         file_info = {}
47         with Closing(file(path, 'r')) as f:
48             while True:
49                 line = f.readline().strip()
50                 if line == '#END':
51                     break
52                 fields = line.split(':', 1)
53                 if len(fields) == 2:
54                     file_info[fields[0]] = fields[1]
55             data = numpy.loadtxt(f, dtype=numpy.float)
56         ret = curve.Data(
57             shape=data.shape,
58             dtype=data.dtype,
59             buffer=data,
60             info=file_info,
61             )
62         ret.info['columns'] = [
63             'time (s)',  # first data column in file just increasing index
64             'phase (m?rad)',
65             'z piezo (m)',
66             'deflection (N)',
67             'imposed (N)',
68             ]
69         ret.info['name'] = 'force clamp'
70         # assume 1 ms timestep
71         ret[:,0] = numpy.arange(0, 1e-3*data.shape[0], 1e-3, dtype=ret.dtype)
72
73         return ([ret,], file_info)