Correct indentation and add h5config dependency comment to README.
[pyafm.git] / pyafm / temperature.py
1 # Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of unfold_protein.
4 #
5 # Unfold_protein is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Unfold_protein is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with unfold_protein.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 from scipy.constants import C2K as _C2K
20 from pypid.backend.melcor import MelcorBackend as _TemperatureBackend
21
22 from . import LOG as _LOG
23 from .config import Celsius, Kelvin
24
25
26 class Temperature (object):
27     """A temperature monitor based on the Melcor controller.
28
29     >>> from pyafm.config import TemperatureConfig
30
31     >>> config = TemperatureConfig()
32     >>> t = Temperature(config=config)
33     >>> t.load_from_config()
34     >>> t.get_temperature()  # doctest: +SKIP
35     297.37
36     >>> t.cleanup()
37     """
38     def __init__(self, config, backend=None):
39         _LOG.debug('setup temperature monitor')
40         self.config = config
41         self.backend = backend
42
43     def load_from_config(self):
44         c = self.config  # reduce verbosity
45         if self.backend is None:
46             self.backend = _TemperatureBackend(
47                 controller=c['controller'],
48                 device=c['device'],
49                 baudrate=c['baudrate'])
50             self.backend.set_max_mv(max=c['max-current'])  # amp
51         self.name = self.config['name']
52
53     def setup_config(self):
54         self.config['controller'] = self.backend._controller
55         self.config['device'] = self.backend._client.port
56         self.config['baudrate'] = self.backend._client.baudrate
57         self.config['max-current'] = self.backend.get_max_mv()
58
59     def cleanup(self):
60         try:
61             self.backend.cleanup()
62         except Exception:
63             pass
64         self.backend = None
65
66     def get_temperature(self):
67         temp = self.backend.get_pv()
68         unit = self.config['units']
69         if unit == Kelvin:  # convert K -> K
70             pass
71         elif unit == Celsius:  # convert C -> K
72             temp = _C2K(temp)
73         else:
74             raise NotImplementedError(unit)
75         _LOG.info('measured temperature of {:g} K'.format(temp))
76         return temp