~janusz-martyniak/mcdb/mice.cdb.client.api-python

« back to all changes in this revision

Viewing changes to src/cdb/_calibration.py

  • Committer: Antony Wilson
  • Date: 2011-09-01 08:10:55 UTC
  • Revision ID: antony.wilson@stfc.ac.uk-20110901081055-5hcu1wbeh9f75sdd
add calibration, fixes to cabling, pylint stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"Calibration module."
 
2
 
 
3
from xml.sax import parseString
 
4
from xml.sax.handler import ContentHandler
 
5
 
 
6
from cdb._base import _CdbBase
 
7
from cdb._base import _get_string_from_date
 
8
from cdb._exceptions import CdbPermanentError
 
9
from cdb._exceptions import CdbTemporaryError
 
10
 
 
11
 
 
12
__all__ = ["Calibration"]
 
13
 
 
14
 
 
15
class Calibration(_CdbBase):
 
16
 
 
17
    """
 
18
The Calibration class is used to retrieve calibration data.
 
19
 
 
20
For the detectors and trackers data is described by its location with reference
 
21
to to bank, board and channel. Old versions of the data are stored for
 
22
diagnostic purposes. Data can be retrieved for a given time using
 
23
get_calibration_for_date and for a given run using get_calibration_for_run.
 
24
 
 
25
 
 
26
    """
 
27
 
 
28
    def __init__(self, wsdl_dir="/cdb/calibration?wsdl"):
 
29
        """
 
30
Construct a Calibration.
 
31
 
 
32
@param wsdl_dir: the path to the wsdl on the server. The default is
 
33
"/cdb/calibration?wsdl".
 
34
 
 
35
@exception CdbPermanentError: Unable to contact CDB server or invalid URL
 
36
 
 
37
        """
 
38
        super(Calibration, self).__init__(wsdl_dir)
 
39
        self._calibration_handler = _CalibrationHandler()
 
40
 
 
41
    def __str__(self):
 
42
        return "Calibration \
 
43
        \n\tset_url(string url) \
 
44
        \n\tget_status() \
 
45
        \n\tget_calibration_for_date(string device, datetime timestamp) \
 
46
        \n\tget_calibration_for_run(string device, int run_number) \
 
47
        \n\tget_current_calibration(string device) \
 
48
        \n\tlist_devices()"
 
49
 
 
50
    def set_url(self, url):
 
51
        """
 
52
Set the client to use the given CDB server.
 
53
 
 
54
@param url: the URL of the CDB wsdl
 
55
 
 
56
@exception CdbTemporaryError: Unable to contact CDB server
 
57
@exception CdbPermanentError: Invalid URL
 
58
 
 
59
        """
 
60
        super(Calibration, self).set_url(url)
 
61
 
 
62
    def get_status(self):
 
63
        """
 
64
Get the status of the service.
 
65
 
 
66
@return a string containing the status of the service
 
67
 
 
68
@exception CdbTemporaryError: The problem maybe transient and retrying the
 
69
request MAY succeed
 
70
@exception CdbPermanentError: An unexpected internal error
 
71
 
 
72
        """
 
73
        return super(Calibration, self).get_status()
 
74
 
 
75
    def get_calibration_for_date(self, device, timestamp):
 
76
        """
 
77
Get the calibration data of the given device, where the name of the device may be
 
78
that of a detector or tracker, that was valid at the given timestamp.
 
79
 
 
80
@param device: a string containing the name of the detector or tracker
 
81
@param timestamp: a datetime in UTC
 
82
 
 
83
@return a list of dictionaries
 
84
    
 
85
@exception CdbTemporaryError: The problem maybe transient and retrying the
 
86
request MAY succeed
 
87
@exception CdbPermanentError: Maybe due to to bad data being passed in or an
 
88
unexpected internal error
 
89
 
 
90
        """
 
91
        timestamp = _get_string_from_date(timestamp)
 
92
        xml = str(self._client.getCalibrationForDate(str(device), timestamp))
 
93
        return self._parse_calibration_xml(xml)
 
94
 
 
95
    def get_calibration_for_run(self, device, run_number):
 
96
        """
 
97
Get the calibration data of the given device, where the name of the device may be
 
98
that of a detector or tracker, that was valid for the given run number.
 
99
 
 
100
@param device: a string containing the name of the detector or tracker
 
101
@param run_number:  a long containing the number of a run
 
102
 
 
103
@return a list of dictionaries
 
104
 
 
105
@exception CdbTemporaryError: The problem maybe transient and retrying the
 
106
request MAY succeed
 
107
@exception CdbPermanentError: Maybe due to to bad data being passed in or an
 
108
unexpected internal error
 
109
 
 
110
        """
 
111
        xml = str(self._client.getCalibrationForRun(device, run_number))
 
112
        return self._parse_calibration_xml(xml)
 
113
 
 
114
    def get_current_calibration(self, device):
 
115
        """
 
116
Get the calibration data of the given device, where the name of the device may be
 
117
that of a detector or tracker.
 
118
 
 
119
@param device: a string containing the name of the detector or tracker
 
120
 
 
121
@return a list of dictionaries
 
122
    
 
123
@exception CdbTemporaryError: The problem maybe transient and retrying the
 
124
request MAY succeed
 
125
@exception CdbPermanentError: Maybe due to to bad data being passed in or an
 
126
unexpected internal error
 
127
 
 
128
        """
 
129
        xml = str(self._client.getCurrentCalibration(device))
 
130
        return self._parse_calibration_xml(xml)
 
131
    
 
132
    def list_devices(self):
 
133
        """
 
134
Get a list of known devices. These are the device names that are recognised by this API.
 
135
 
 
136
@return a list of device names
 
137
    
 
138
@exception CdbTemporaryError: The problem maybe transient and retrying the
 
139
request MAY succeed
 
140
@exception CdbPermanentError: Maybe due to to bad data being passed in or an
 
141
unexpected internal error
 
142
 
 
143
        """
 
144
        xml = str(self._client.listDevices())
 
145
        return self._parse_calibration_xml(xml)
 
146
    
 
147
 
 
148
    def _parse_calibration_xml(self, xml):
 
149
        """ Parser for calibration data. """
 
150
        parseString(xml, self._calibration_handler)
 
151
        return self._calibration_handler.get_data()
 
152
 
 
153
 
 
154
class _CalibrationHandler(ContentHandler):
 
155
 
 
156
    " ContentHandler for calibration data. "
 
157
 
 
158
    def __init__ (self):
 
159
        ContentHandler.__init__(self)
 
160
        self._device_type = ""
 
161
        self._board = ""
 
162
        self._bank = ""
 
163
        self._data = []
 
164
        self._message = ""
 
165
 
 
166
    def get_data(self):
 
167
        """
 
168
        Get a list containing the parsed xml.
 
169
 
 
170
        @return the list containing the parsed xml
 
171
 
 
172
        """
 
173
        return self._data
 
174
 
 
175
    def startElement(self, name, attrs): #pylint: disable-msg=C0103
 
176
        """ Method required for ContentHandler. """
 
177
        if name == 'error':
 
178
            self._message = ""
 
179
        elif name == 'warning':
 
180
            self._message = ""
 
181
        elif name == 'calibration':
 
182
            self._reset()
 
183
            self._device_type = str(attrs.get('type', ""))
 
184
        elif name == 'device':
 
185
            self._data.append(str(attrs.get('name', "")))
 
186
        elif name == 'board':
 
187
            self._board = int(attrs.get('number', ""))
 
188
        elif name == 'bank':
 
189
            self._bank = int(attrs.get('number', ""))
 
190
        elif name == 'channel':
 
191
            if self._device_type == "TRACKER":
 
192
                self._add_tracker(attrs)
 
193
        return
 
194
 
 
195
    def characters(self, message):
 
196
        """ Method required for ContentHandler. """
 
197
        self._message = self._message + message
 
198
 
 
199
    def endElement(self, name): #pylint: disable-msg=C0103
 
200
        """ Method required for ContentHandler. """
 
201
        if name == 'error':
 
202
            raise CdbPermanentError(self._message)
 
203
        elif name == 'warning':
 
204
            raise CdbTemporaryError(self._message)
 
205
 
 
206
    def _add_tracker(self, attrs):
 
207
        """ Populate a dictionary with data from the xml. """
 
208
        _cable_data = {}
 
209
        _cable_data["bank"] = self._board
 
210
        _cable_data["board"] = self._bank
 
211
        _cable_data["channel"] = int(attrs.get('number', ""))
 
212
        _cable_data["adc_pedestal"] = int(attrs.get('adcPedestal', ""))
 
213
        _cable_data["adc_gain"] = int(attrs.get('adcGain', ""))
 
214
        _cable_data["tdc_pedestal"] = int(attrs.get('tdcPedestal', ""))
 
215
        _cable_data["tdc_gain"] = int(attrs.get('tdcGain', ""))
 
216
        self._data.append(_cable_data)
 
217
 
 
218
    def _reset(self):
 
219
        """ Reset self values. """
 
220
        self._device_type = ""
 
221
        self._board = ""
 
222
        self._bank = ""
 
223
        self._data = []
 
224
        self._message = ""
 
225