Fix real-T handling in T_acquire().
[calibcant.git] / calibcant / T.py
1 # T family.
2 # Fairly stubby, since a one shot Temp measurement is a common thing.
3 # We just wrap that to provide a consistent interface.
4
5 from . import LOG as _LOG
6 from . import base_config as _base_config
7 from .T_analyze import T_analyze as _T_analyze
8 from .T_analyze import T_save as _T_save
9
10
11 def T_acquire(get_T=None):
12     """Measure the current temperature of the sample, 
13
14     If `get_T` is `None`, fake it by returning
15     `base_config['temperature']`.
16     """
17     if get_T:
18         _LOG.info('measure temperature')
19         T = get_T()
20     else:
21         T = None
22     if T is None:
23         _LOG.info('fake temperature %g' % _base_config['temperature'])
24         T = _base_config['temperature']
25     return T
26
27 def T(get_T, temperature_config, filename, group='/'):
28     """Wrapper around T_acquire(), T_analyze(), T_save().
29
30     >>> import os
31     >>> import tempfile
32     >>> from pypiezo.config import pprint_HDF5
33     >>> from .config import HDF5_TemperatureConfig
34
35     >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
36     >>> os.close(fd)
37
38     >>> temperature_config = HDF5_TemperatureConfig(
39     ...     filename=filename, group='/T/config/')
40     >>> def get_T():
41     ...     return 19.2
42
43     >>> t = T(get_T=get_T, temperature_config=temperature_config,
44     ...     filename=filename, group='/T/')
45     >>> pprint_HDF5(filename)  # doctest: +ELLIPSIS, +REPORT_UDIFF
46     /
47       /T
48         /T/config
49           <HDF5 dataset "default": shape (), type "|S2">
50             no
51           <HDF5 dataset "units": shape (), type "|S7">
52             Celsius
53         <HDF5 dataset "processed": shape (), type "<f8">
54           292.35
55         <HDF5 dataset "raw": shape (), type "<f8">
56           19.2
57     >>> t = T(get_T=None, temperature_config=temperature_config,
58     ...     filename=filename, group='/T/')
59     >>> pprint_HDF5(filename)  # doctest: +ELLIPSIS, +REPORT_UDIFF
60     /
61       /T
62         /T/config
63           <HDF5 dataset "default": shape (), type "|S3">
64             yes
65           <HDF5 dataset "units": shape (), type "|S7">
66             Celsius
67         <HDF5 dataset "processed": shape (), type "<f8">
68           295.15
69         <HDF5 dataset "raw": shape (), type "<i4">
70           22
71
72     Cleanup our temporary config file.
73
74     >>> os.remove(filename)
75     """
76     T_raw = T_acquire(get_T)
77     T_ret = _T_analyze(T_raw, temperature_config)
78     temperature_config['default'] = not get_T
79     _T_save(filename, group=group, raw_T=T_raw,
80             temperature_config=temperature_config, processed_T=T_ret)
81     return T_ret