Get calibcant working with the new load_from_config-based pyafm.
[calibcant.git] / calibcant / temperature.py
1 """Temperature measurement tools
2
3 Fairly stubby, since a one shot temperature measurement is a common
4 thing.  We just wrap that to provide a consistent interface.
5 """
6
7 from . import LOG as _LOG
8 from .temperature_analyze import analyze as _analyze
9 from .temperature_analyze import save as _save
10
11
12 def acquire(get=None):
13     """Measure the current temperature of the sample, 
14     """
15     if get:
16         _LOG.info('measure temperature')
17         temperature = get()
18     else:
19         temperature = None
20     return temperature
21
22 def run(get, config, filename, group='/'):
23     """Wrapper around acquire(), analyze(), save().
24
25     >>> import os
26     >>> import tempfile
27     >>> from h5config.storage.hdf5 import HDF5_Storage, pprint_HDF5
28     >>> from .config import TemperatureConfig
29
30     >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
31     >>> os.close(fd)
32
33     >>> config = TemperatureConfig()
34     >>> def get():
35     ...     return 19.2
36     >>> t = run(
37     ...     get=get, config=config, filename=filename, group='/')
38     >>> pprint_HDF5(filename)  # doctest: +ELLIPSIS, +REPORT_UDIFF
39     /
40       /config
41         /config/temperature
42           <HDF5 dataset "sleep": shape (), type "<i4">
43             1
44       /processed
45         <HDF5 dataset "data": shape (), type "<f8">
46           19.2
47         <HDF5 dataset "units": shape (), type "|S1">
48           K
49       /raw
50         <HDF5 dataset "data": shape (), type "<f8">
51           19.2
52         <HDF5 dataset "units": shape (), type "|S1">
53           K
54
55     Cleanup our temporary config file.
56
57     >>> os.remove(filename)
58     """
59     raw = acquire(get)
60     _LOG.debug('got temperature: {} K'.format(raw))
61     processed = _analyze(config=config, temperature=raw)
62     _save(filename=filename, group=group, config=config, raw=raw,
63           processed=processed)
64     return processed