1 # calibcant - tools for thermally calibrating AFM cantilevers
3 # Copyright (C) 2012-2013 W. Trevor King <wking@tremily.us>
5 # This file is part of calibcant.
7 # calibcant is free software: you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free Software
9 # Foundation, either version 3 of the License, or (at your option) any later
12 # calibcant is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # calibcant. If not, see <http://www.gnu.org/licenses/>.
19 """Temperature analysis.
21 Separate the more general `temperature.analyze()` from the other
22 `temperature.*()` functions in calibcant.
24 The relevant physical quantities are:
26 * `T` Temperature at which thermal vibration measurements were acquired
29 >>> from pprint import pprint
32 >>> from .config import TemperatureConfig
33 >>> from h5config.storage.hdf5 import pprint_HDF5, HDF5_Storage
35 >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
38 >>> config = TemperatureConfig()
41 >>> processed = analyze(config=config, temperature=raw)
42 >>> plot(raw=raw, processed=processed)
43 >>> save(filename=filename, group='/',
44 ... config=config, raw=raw, processed=processed)
46 >>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF
50 <HDF5 dataset "sleep": shape (), type "<i4">
53 <HDF5 dataset "data": shape (), type "<f8">
55 <HDF5 dataset "units": shape (), type "|S1">
58 <HDF5 dataset "data": shape (), type "<f8">
60 <HDF5 dataset "units": shape (), type "|S1">
63 >>> data = load(filename=filename, group='/')
65 >>> pprint(data) # doctest: +ELLIPSIS
66 {'config': {'temperature': <TemperatureConfig ...>},
70 >>> print(data['config']['temperature'].dump())
79 >>> os.remove(filename)
85 import matplotlib as _matplotlib
86 import matplotlib.pyplot as _matplotlib_pyplot
87 import time as _time # for timestamping lines on plots
88 except (ImportError, RuntimeError), e:
90 _matplotlib_import_error = e
92 from h5config.storage.hdf5 import HDF5_Storage as _HDF5_Storage
93 from h5config.storage.hdf5 import h5_create_group as _h5_create_group
95 from . import LOG as _LOG
96 from . import package_config as _package_config
97 from .config import TemperatureConfig as _TemperatureConfig
98 from .util import SaveSpec as _SaveSpec
99 from .util import save as _save
100 from .util import load as _load
103 def analyze(config, temperature, units='Kelvin'):
104 """Convert measured temperature to Kelvin.
106 `temperature` should be a numpy ndarray or scalar. `config`
107 should be a `config._temperatureemperatureConfig` instance.
109 The `units` option is just for fun. The AFM's `get_temperature`
110 method always returns temperatures in Kelvin.
112 if units == 'Kelvin':
114 elif units == 'Celsius':
115 return _C2K(temperature)
117 raise NotImplementedError()
120 def save(filename=None, group='/', config=None, raw=None, processed=None):
122 _SaveSpec(item=config, relpath='config/temperature',
123 config=_TemperatureConfig),
124 _SaveSpec(item=raw, relpath='raw', units='K'),
125 _SaveSpec(item=processed, relpath='processed', units='K'),
127 _save(filename=filename, group=group, specs=specs)
129 def load(filename=None, group='/'):
131 _SaveSpec(key=('config', 'temperature',), relpath='config/temperature',
132 config=_TemperatureConfig),
133 _SaveSpec(key=('processed',), relpath='processed', units='K'),
134 _SaveSpec(key=('raw',), relpath='raw', units='K'),
136 return _load(filename=filename, group=group, specs=specs)
138 def plot(raw=None, processed=None):
140 raise _matplotlib_import_error
141 figure = _matplotlib_pyplot.figure()
142 timestamp = _time.strftime('%H%M%S')
144 if processed is None:
145 return # nothing to plot
147 axes2 = figure.add_subplot(1, 1, 1)
148 elif processed is None:
149 axes1 = figure.add_subplot(1, 1, 1)
152 axes1 = figure.add_subplot(2, 1, 1)
153 axes2 = figure.add_subplot(2, 1, 2)
155 axes1.set_title('Raw Temperatures %s' % timestamp)
156 axes1.plot(raw, label='raw')
158 axes2.set_title('Processed Temperatures %s' % timestamp)
159 axes2.plot(processed, label='processed')
160 if hasattr(figure, 'show'):