-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_kromek.py
137 lines (107 loc) · 4.21 KB
/
plugin_kromek.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Detector plugin for kromek gamma and neutron detectors
# Copyright (C) 2016 Norwegain Radiation Protection Authority
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Authors: Dag Robole,
import sys, time
from ctypes import *
from gc_exceptions import ProtocolError
TOTAL_RESULT_CHANNELS = 4096
_so = CDLL('/usr/lib/libSpectrometerDriver.so')
_so.kr_Initialise.argtypes = [c_void_p, c_void_p]
_so.kr_Initialise.restype = c_int
_so.kr_GetNextDetector.argtypes = [c_uint]
_so.kr_GetNextDetector.restype = c_uint
_so.kr_GetDeviceSerial.argtypes = [c_uint, POINTER(c_char), c_int, POINTER(c_int)]
_so.kr_GetDeviceSerial.restype = c_int
_so.kr_ClearAcquiredData.argtypes = [c_uint]
_so.kr_ClearAcquiredData.restype = c_int
_so.kr_BeginDataAcquisition.argtypes = [c_uint, c_uint, c_uint]
_so.kr_BeginDataAcquisition.restype = c_int
_so.kr_IsAcquiringData.argtypes = [c_uint]
_so.kr_IsAcquiringData.restype = c_int
_so.kr_GetAcquiredDataEx.argtypes = [c_uint, POINTER(c_uint), POINTER(c_uint), POINTER(c_uint), POINTER(c_uint), c_uint]
_so.kr_GetAcquiredDataEx.restype = c_int
_did = c_uint(0)
def _setDetector(serialname):
global _did
_did = c_uint(0)
serial = (c_char * 200)()
serial_size = c_int(0)
while True:
_did = _so.kr_GetNextDetector(_did)
if _did == 0:
break
_so.kr_GetDeviceName(_did, serial, 200, byref(serial_size))
if serial.value == serialname:
print "Using detector %s\n" % (serial.value)
return
raise ProtocolError('detector_config_error', "Detector not found: %s" % (serialname))
def initializePlugin():
_so.kr_Initialise(c_void_p(None), c_void_p(None))
def finalizePlugin():
_so.kr_Destruct()
def initializeDetector(config):
global _did
if set(config) < set(('serialnumber', 'voltage', 'lld')):
raise ProtocolError('detector_config_error', "Unable to initialize detector: missing configuration items")
_setDetector(config['serialnumber'])
def finalizeDetector(config):
pass
def initializeSession(config):
pass
def finalizeSession(config):
pass
def acquireSpectrum(args):
global _did
if set(args) < set(('session_name', 'livetime')):
raise ProtocolError('error', "Unable to acquire spectrum: Missing arguments")
if _did == 0:
raise ProtocolError('error', "Unable to acquire spectrum: Invalid detector id")
lt = int(args['livetime'] * 1000.0)
# _so.kr_ClearAcquiredData(_did)
_so.kr_BeginDataAcquisition(_did, c_uint(0), c_uint(lt))
while _so.kr_IsAcquiringData(_did):
time.sleep(0.1)
total_count = c_uint(0)
livetime = c_uint(0)
realtime = c_uint(0)
spectrum = (c_uint * TOTAL_RESULT_CHANNELS)()
flags = c_uint(1)
_so.kr_GetAcquiredDataEx(_did, spectrum, byref(total_count), byref(realtime), byref(livetime), flags)
# Add spectrum data to response message
msg = {
'command': 'spectrum',
'session_name': args['session_name'],
'channels': ' '.join(map(str, spectrum)),
'num_channels': TOTAL_RESULT_CHANNELS,
'total_count': int(total_count.value),
'livetime': float(livetime.value) * 1000.0,
'realtime': float(realtime.value) * 1000.0
}
return msg
if __name__ == "__main__":
try:
initializePlugin()
config = {'serialnumber':'GR1A', 'voltage':700, 'lld':32}
initializeDetector(config)
args = {'session_name':'01012000_121212', 'livetime':2}
msg = acquireSpectrum(args)
print msg
except ProtocolError as err:
print "Exception: ", err
finally:
finalizePlugin()