forked from ap--/python-seabreeze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrometers.py
265 lines (217 loc) · 10.2 KB
/
spectrometers.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import seabreeze.backends
# get the backend and add some functions/classes to this module
lib = seabreeze.backends.get_backend()
list_devices = lib.device_list_devices
SeaBreezeError = lib.SeaBreezeError
SeaBreezeDevice = lib.SeaBreezeDevice
import numpy
class _HelperFeatureAdder(object):
def __init__(self, other):
self._other = other
def add(self, feature):
ffunc = getattr(lib, "device_get_%s_feature_id" % feature)
fids = ffunc(self._other._dev)
if fids:
return fids[0]
else:
return -1 # It seems as if negative numbers are not used for featureIDs
class LightSource(object):
def __init__(self, device, featureId, index):
ident = device, featureId, index
self._ident = ident
self._has_enable = lib.light_source_has_enable(*ident)
self._has_varint = lib.light_source_has_variable_intensity(*ident)
def set_enable(self, enable):
if self._has_enable:
d, f, i = self._ident
lib.light_source_set_enable(d, f, i, enable)
else:
raise SeaBreezeError("Light source #%d can't be enabled or disabled.")
def set_intensity(self, intensity):
if self._has_varint:
d, f, i = self._ident
lib.light_source_set_intensity(d, f, i, intensity)
else:
raise SeaBreezeError("Light source #%d intensity can't be set.")
def get_intensity(self):
try:
return lib.light_source_get_intensity(*self._ident)
except SeaBreezeError:
raise
def __repr__(self):
d, f, i = self._ident
return "<LightSource #%d at %s:%s>" % (i, d.model, d.serial)
class Spectrometer(object):
def __init__(self, device):
self._open_device(device)
@classmethod
def from_serial_number(cls, serial=None):
if serial is None: # pick first spectrometer
for dev in lib.device_list_devices():
if not lib.device_is_open(dev):
return cls(dev)
else:
raise SeaBreezeError("No unopened device found.")
else: # pick spectrometer with correct serial
for dev in lib.device_list_devices():
if dev.serial == str(serial):
if lib.device_is_open(dev):
raise SeaBreezeError("Device already opened.")
else:
return cls(dev)
else:
raise SeaBreezeError("No device attached with serial number '%s'." % serial)
def _open_device(self, device):
if not isinstance(device, SeaBreezeDevice):
raise SeaBreezeError("Not a SeaBreezeDevice")
if lib.device_is_open(device):
raise SeaBreezeError("Device already opened.")
if hasattr(self, '_dev') and lib.device_is_open(getattr(self, '_dev')):
lib.device_close(getattr(self, '_dev'))
self._dev = device
lib.device_open(self._dev)
# get default information
self._serial = self._dev.serial
self._model = self._dev.model
# get features
feature = _HelperFeatureAdder(self)
self._fidsp = feature.add('spectrometer')
self._fidsh = feature.add('shutter')
self._fidls = feature.add('light_source')
self._fidcs = feature.add('continuous_strobe')
self._fidee = feature.add('eeprom')
self._fidic = feature.add('irrad_calibration')
self._fidla = feature.add('lamp')
self._fidte = feature.add('tec')
self._fidnc = feature.add('nonlinearity_coeffs') # Added
self._fidsl = feature.add('stray_light_coeffs')
self._fidspp = feature.add('spectrum_processing') # Not implemented in pyseabreeze
# get additional information
self._pixels = lib.spectrometer_get_formatted_spectrum_length(self._dev, self._fidsp)
self._minimum_integration_time_micros = (
lib.spectrometer_get_minimum_integration_time_micros(self._dev, self._fidsp))
# get wavelengths
self._wavelengths = numpy.zeros((self._pixels,), dtype=numpy.double)
transfered_N = 0
while True:
transfered_N += lib.spectrometer_get_wavelengths(self._dev, self._fidsp,
self._wavelengths[transfered_N:])
if transfered_N >= self._pixels:
break
# get dark pixel indices
self._dark = lib.spectrometer_get_electric_dark_pixel_indices(self._dev, self._fidsp)
self._has_dark_pixels = True if len(self._dark) > 0 else False
# get nonlinearity coefficients
try:
sbnc = lib.nonlinearity_coeffs_get(self._dev, self._fidnc)
self._nc = numpy.poly1d(sbnc[::-1])
self._has_nonlinearity_coeffs = True
except SeaBreezeError:
self._has_nonlinearity_coeffs = False
# if lightsources
try:
N_light_sources = lib.light_source_get_count(self._dev, self._fidls)
self._light_sources = tuple(LightSource(self._dev, self._fidls, i)
for i in range(N_light_sources))
except SeaBreezeError:
self._light_sources = tuple()
def wavelengths(self):
return self._wavelengths
def intensities(self, correct_dark_counts=False, correct_nonlinearity=False):
if correct_dark_counts and not self._has_dark_pixels:
raise SeaBreezeError("This device does not support dark count correction.")
if correct_nonlinearity and not self._has_nonlinearity_coeffs:
raise SeaBreezeError("This device does not support nonlinearity correction.")
# Get the intensities
out = numpy.empty((self._pixels,), dtype=numpy.double)
transfered_N = 0
while True:
transfered_N += lib.spectrometer_get_formatted_spectrum(self._dev, self._fidsp,
out[transfered_N:])
if transfered_N >= self._pixels:
break
# Do corrections if requested
if (correct_nonlinearity or correct_dark_counts):
dark_offset = numpy.mean(out[self._dark]) if self._has_dark_pixels else 0.
out -= dark_offset
if (correct_nonlinearity):
out = out / numpy.polyval(self._nc, out)
if correct_nonlinearity and (not correct_dark_counts):
out += dark_offset
return out
def spectrum(self, correct_dark_counts=False, correct_nonlinearity=False):
return numpy.vstack((self._wavelengths,
self.intensities(correct_dark_counts, correct_nonlinearity)))
def integration_time_micros(self, integration_time_micros):
# Protect against a bug in libseabreeze:
# If integration time is out of bounds, libseabreeze returns Undefined Error
# (Probably only for devices with a non micro second time base...)
try:
lib.spectrometer_set_integration_time_micros(self._dev,
self._fidsp, integration_time_micros)
except SeaBreezeError as e:
if getattr(e, 'error_code', None) == 1:
# Only replace if 'Undefined Error'
raise SeaBreezeError("FIX: Specified integration time is out of range.")
else:
raise e
def trigger_mode(self, mode):
lib.spectrometer_set_trigger_mode(self._dev, self._fidsp, mode)
def boxcar_width(self, boxcar_width):
lib.spectrum_processing_set_boxcar_width(self._dev, self._fidspp, boxcar_width) # Not implemented in pyseabreeze
def scans_to_average(self, scans_to_average):
lib.spectrum_processing_set_scans_to_average(self._dev, self._fidspp, scans_to_average) # Not implemented in pyseabreeze
def get_boxcar_width(self):
lib.spectrum_processing_get_boxcar_width(self._dev, self._fidspp) # Not implemented in pyseabreeze
def get_scans_to_average(self):
lib.spectrum_processing_get_scans_to_average(self._dev, self._fidspp) # Not implemented in pyseabreeze
@property
def serial_number(self):
return self._serial
@property
def model(self):
return self._model
@property
def pixels(self):
return self._pixels
@property
def minimum_integration_time_micros(self):
return self._minimum_integration_time_micros
@property
def light_sources(self):
return self._light_sources
def eeprom_read_slot(self, slot):
return lib.eeprom_read_slot(self._dev, self._fidee, slot)
def tec_set_enable(self, enable):
lib.tec_set_enable(self._dev, self._fidte, enable)
def tec_set_temperature_C(self, set_point_C):
lib.tec_set_temperature_setpoint_degrees_C(self._dev, self._fidte, set_point_C)
def tec_get_temperature_C(self):
return lib.tec_read_temperature_degrees_C(self._dev, self._fidte)
def lamp_set_enable(self, enable):
lib.lamp_set_lamp_enable(self._dev, self._fidla, enable)
def shutter_set_open(self, state):
lib.shutter_set_shutter_open(self._dev, self._fidsh, state)
def stray_light_coeffs(self):
return lib.stray_light_coeffs_get(self._dev, self._fidsl)
def irrad_calibration(self):
out = numpy.empty((self._pixels,), dtype=numpy.float32)
lib.irrad_calibration_read(self._dev, self._fidic, out)
return out
def irrad_calibration_collection_area(self):
if lib.irrad_calibration_has_collection_area(self._dev, self._fidic):
return lib.irrad_calibration_read_collection_area(self._dev, self._fidic)
else:
raise SeaBreezeError("Device does not store irrad calibration area.")
def continuous_strobe_set_enable(self, enable):
lib.continuous_strobe_set_enable(self._dev, self._fidcs, enable)
def continuous_strobe_set_period_micros(self, period_micros):
lib.continuous_strobe_set_period_micros(self._dev, self._fidcs, period_micros)
def close(self):
lib.device_close(self._dev)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def __repr__(self):
return "<Spectrometer %s:%s>" % (self.model, self.serial_number)