-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheigertest2.py
442 lines (379 loc) · 14.8 KB
/
eigertest2.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""
EigerTest extends the functionality of DEigerClient for convenient
interactive work in a python shell. It should be used only for this
intended purpose or as an example how to use DEigerClient in your own
code. The ipython shell is highly recommended.
!Do NOT use this code in non-interactive scripts, python modules
or subclasses! Kittens will die and the detector may behave erratic!
For such purposes, please use DEigerClient or EIGER's REST API directly.
This code is
- NOT official DECTRIS code but written for personal use by the author
- NOT part of the software of EIGER detector systems.
- NOT well tested.
- only provided as is. Do not expect bug fixes, new features or any
support or maintanence by DECTRIS.
- NOT to be distributed without consent of the author!
- not particularly well documented. Be prepared to read the code
and simply try things out.
Modified: SteB
Version: 20150513
Author: Marcus Mueller
Contact: [email protected]
Version: 20140922
"""
import os
import re
import sys
import time
sys.path.insert(0,"/usr/local/dectris/python")
from eigerclient import DEigerClient
try:
import dectris.albula
from quickstart import Monitor
MONITOR_AVAILABLE = True
except ImportError:
MONITOR_AVAILABLE = False
#############################################################
### Configure IP and PORT of the EIGER system that you are
### accessing with this code below.
# fixed IP, direct connection
IP = "10.114.11.111"
PORT = "80"
# for printing detector config and values
LISTED_DETECTOR_PARAMS = [
u'description',
u'detector_number',
u'data_collection_date',
u'frame_time',
u'nimages',
u'ntrigger',
u'trigger_mode',
u'photon_energy',
u'threshold_energy',
u'element',
u'count_time',
u'detector_readout_time',
u'nframes_sum',
u'frame_count_time',
u'frame_period',
#u'sub_image_count_time',
u'auto_summation',
u'summation_nimages',
u'bit_depth_readout',
u'efficiency_correction_applied',
u'flatfield_correction_applied',
u'number_of_excluded_pixels',
u'calibration_type',
u'countrate_correction_applied',
u'countrate_correction_bunch_mode',
u'pixel_mask_applied',
#u'pixel_mask',
u'virtual_pixel_correction_applied',
u'software_version',
u'sensor_material',
u'sensor_thickness',
u'x_pixels_in_detector',
u'y_pixels_in_detector',
u'x_pixel_size',
u'y_pixel_size',
u'wavelength',
u'detector_distance',
u'beam_center_x',
u'beam_center_y',
u'detector_translation',
u'detector_orientation']
CHECK_DOWNLOAD="/tmp/check_download"
MAIN_DOWNLOAD_PATH="/tmp/main_download_path"
# for filewriter config to have data bundles of ~ 1GB
# 16M = 20 (45MB x 20)
# 4M = 80 (12MB x 80)
# 1M = 320 ( ?MB x 320)
NIMAGES_PER_FILE=100
class EigerTest(DEigerClient):
def setEnergy(self, photon_energy, threshold_ratio=0.5):
self.setDetectorConfig("photon_energy", photon_energy)
self.setDetectorConfig("threshold_energy", photon_energy * threshold_ratio)
def setElement(self, element='Cu'):
self.setDetectorConfig(u'element', element)
def setBeam_xy(self, beam_center_x=0, beam_center_y=0):
self.setDetectorConfig(u'beam_center_x', beam_center_x)
self.setDetectorConfig(u'beam_center_y', beam_center_y)
def setDetector_distance(self, detector_distance=0):
self.setDetectorConfig(u'detector_distance',detector_distance)
def imageSeries(self, expp, nimages, auto_sum=False, ff_corr=True,
pixel_mask=True):
self.setDetectorConfig("auto_summation", auto_sum)
self.setDetectorConfig("frame_time", expp)
# to avoid warm pixels in center of module
if expp > 0.099:
readout_time = 5.0e-04 # no simultaneous read/write
else:
# simultaneous read/write will give high duty cycle but also warm pixels
readout_time = self.detectorConfig('detector_readout_time')['value']
self.setDetectorConfig("count_time", expp - readout_time)
self.setDetectorConfig("nimages", nimages)
self.setDetectorConfig("flatfield_correction_applied", ff_corr)
self.setDetectorConfig("pixel_mask_applied", pixel_mask)
self.printConf('time|image')
def printConf(self, regex='', full=0):
for param in LISTED_DETECTOR_PARAMS:
if full:
if re.search(regex, param):
try:
print str(param).ljust(35), ' = ', str(e.detectorConfig(param)['value']).ljust(35),' ', str(e.detectorConfig(param)['min']).ljust(35),' ', str(e.detectorConfig(param)['max']).ljust(35)
except:
print ""
pass
#print str(param).ljust(35), ' = ', str(e.detectorConfig(param)['value']).ljust(35)
else:
if re.search(regex, param):
print str(param).ljust(35), ' = ', e.detectorConfig(param)['value']
def setFileNameBase(self, fnBase):
self.setFileWriterConfig("name_pattern", fnBase)
def setImagesPerFile(self, nimages=1000):
self.setFileWriterConfig("nimages_per_file", nimages)
def pHello(self, z="today"):
print "Hello " + z
def printFileWriterConfig(self):
for param in self.fileWriterConfig():
try:
print param, ' = ', self.fileWriterConfig(param)['value']
except RuntimeError as e:
print "RuntimeError accessing %s: %s" % (param, e)
def printFileWriterStatus(self):
for param in self.fileWriterStatus():
try:
print param, ' = ', self.fileWriterStatus(param)['value']
except RuntimeError as e:
print "RuntimeError accessing %s: %s" % (param, e)
def printTempHum(self, all_data=0):
for param in self.detectorStatus():
if all_data:
if re.search("temp|humidity", param):
print (param).ljust(35), ' = ', self.detectorStatus(param)['value']
else:
if re.search("th0", param):
print (param).ljust(35), ' = ', self.detectorStatus(param)['value']
def printFW(self):
for param in self.detectorStatus():
if re.search("fw", param):
print (param).ljust(35), ' = ', self.detectorStatus(param)['value']
def printDetectorState(self):
print "State: %s" % self.detectorStatus('state')['state']
print "Error: %s" % self.detectorStatus('error')
def setDetConMultiple(self, **params):
"""
Convenience function to set a single or multiple detector
configuration parameters in the form (parameter=value[, ...]).
Multiple parameters are set in arbitrary order!
You have to check the print output of this function whether
you obtain the desired detector configuration
"""
for p, data in params.items():
changeList = self.setDetectorConfig(param=p, value=data, dataType=None)
changes = ''
for changed_param in changeList:
changed_value = self.detectorConfig(changed_param)['value']
changes += '%s = %s ; ' % (changed_param, str(changed_value))
print "Setting: %s = %s" % (p, str(data))
print "Changing: " + changes[:-2]
def purgeFiles(self, force=False):
f_list = self.fileWriterFiles()
if not force:
print "Files on the detector control unit:"
#~ [print(f) for f in f_list]
for f in f_list:
print f
if force == True or raw_input('Do you really want to purge all '
'these files? \n'
'Then enter y. ') == 'y':
[self.fileWriterFiles(f, 'DELETE') for f in f_list]
#~ for f in f_list:
#~ self.fileWriterFiles(f, 'DELETE')
else:
print "Aborting without deleting files."
def startMonitor(self):
m = None
if not MONITOR_AVAILABLE:
print "Monitor nor available. Check dependencies."
print "Returning None."
else:
m = Monitor(self)
m.start()
print "Monitor started and monitor object returned."
self._monitor = m
return m
def arm(self):
return self.sendDetectorCommand(u'arm')
def trigger(self):
self.sendDetectorCommand(u'trigger')
def disarm(self):
self.sendDetectorCommand(u'disarm')
def initialize(self, element=None, energy=None):
self.sendDetectorCommand(u'initialize')
if element is not None and energy is not None:
print "You cannot give element AND energy."
elif element is not None:
self.setElement(element)
elif energy is not None:
self.setEnergy(energy)
def exposure(self, fnBase=None):
"""
tvx style exposure command
"""
if fnBase is not None:
self.setFileNameBase(fnBase=fnBase)
self.setDetectorConfig('data_collection_date',
self.fileWriterStatus('time')['value'])
print "Arm ..."
self.arm()
print "Trigger ..."
self.trigger()
print "Disarm ..."
time.sleep(1)
self.disarm()
print "DONE!"
def download(self, downloadpath="/tmp"):
try:
matching = self.fileWriterFiles()
except:
print "could not get file list"
if len(matching):
try:
[self.fileWriterSave(i, downloadpath) for i in matching]
except:
print "error saveing - noting deleted"
else:
print "Downloaded ..."
for i in matching:
print i + " to " + str(downloadpath)
[self.fileWriterFiles(i, method = 'DELETE') for i in matching]
print "Deleted " + str(len(matching)) + " file(s)"
def downloadD(self, downloadpath="/tmp"):
open(CHECK_DOWNLOAD, 'a').close()
with open(MAIN_DOWNLOAD_PATH, 'w') as the_file:
the_file.write(downloadpath)
if not os.path.exists(downloadpath):
os.makedirs(downloadpath)
while os.path.exists(CHECK_DOWNLOAD):
try:
matching = self.fileWriterFiles()
except:
print "could not get file list"
time.sleep(1)
if len(matching)>0:
try:
[self.fileWriterSave(i, downloadpath ) for i in matching]
except:
print "error saveing - noting deleted"
else:
print "Downloaded ..."
for i in matching:
print i
[self.fileWriterFiles(i, method = 'DELETE') for i in matching]
print "Deteted " + str(len(matching)) + " file(s)"
def stopDownloadD(self):
os.remove(CHECK_DOWNLOAD)
def expPF(self,frame_time=1,\
nimages=1,\
base_file_name="series",\
photon_energy="11271.1",\
beam_center_x=0,\
beam_center_y=0,\
detector_distance=0):
"""
tvx style exposure command
"""
#detector config
ntrigger=1000
self.setDetectorConfig("ntrigger",ntrigger)
self.setDetectorConfig("frame_time", frame_time)
readout_time = self.detectorConfig('detector_readout_time')['value']
self.setDetectorConfig("count_time", frame_time - readout_time)
self.setDetectorConfig("nimages", nimages)
self.setEnergy(photon_energy)
# set beamline parameters
self.setBeam_xy(beam_center_x,beam_center_y)
self.setDetector_distance(detector_distance)
# filewriter config
if nimages < NIMAGES_PER_FILE:
nipf = nimages
else:
nipf = NIMAGES_PER_FILE
self.setFileWriterConfig("nimages_per_file",nipf+1)
path_name,file_name=os.path.split(base_file_name)
full_file_name=file_name+"_$id"
self.setFileWriterConfig("name_pattern",base_file_name+"_$id")
# take path_name (subdir) from base_file name
with open(MAIN_DOWNLOAD_PATH, 'r') as fl:
base_download_path = fl.readline()
directory=os.path.join(base_download_path,path_name)
if not os.path.exists(directory):
os.makedirs(directory)
print "Arm ..."
self.arm()
print "Trigger ..."
print "Recording %s images of %f s at %d eV." % (nimages,frame_time,self.detectorConfig("photon_energy")['value'])
self.trigger()
print "Disarm ..."
time.sleep(1)
self.disarm()
print "DONE!"
self.printConf('nimages|frame_time')
def exp(self,frame_time=1,nimages=1,ntrigger=1000):
"""
tvx style exposure command
"""
self.setDetectorConfig("frame_time", frame_time)
readout_time = self.detectorConfig('detector_readout_time')['value']
self.setDetectorConfig("count_time", frame_time - readout_time)
self.setDetectorConfig("nimages", nimages)
self.setDetectorConfig("ntrigger",ntrigger)
if nimages < NIMAGES_PER_FILE:
nipf = nimages
else:
nipf = NIMAGES_PER_FILE
self.setFileWriterConfig("nimages_per_file",nipf)
print "Arm ..."
self.arm()
print "Trigger ..."
self.trigger()
print "Disarm ..."
time.sleep(1)
self.disarm()
print "DONE!"
self.printConf('time|image')
def testLoop(self, nloops, purge=True, **kwargs):
print "#" * 72 + "\nRunning test loop with %d series.\n" % nloops
self.imageSeries(**kwargs)
print "-" * 72
for n in range(nloops):
print "Running series %d" % (n + 1)
self.exposure()
if purge:
print "Purging files."
time.sleep(1)
self.purgeFiles(force=True)
print "-" * 72
print "Finished %d series." % nloops
print "#" * 72 + "\n"
if __name__ == '__main__':
print "run init"
e = EigerTest(host=IP, port=PORT, verbose=True)
e.printDetectorState()
a = e.fileWriterConfig()
print a
#~ e.setDetectorConfig('auto_summation', False)
#~ e.setDetectorConfig('countrate_correction_applied', True)
#~ e.setDetectorConfig('element', 'Cu')
#~ e.sendDetectorCommand(u'initialize')
#~ e.setEnergy(12400)
#~ e.imageSeries(expp=0.2, nimages=5)
#~ e.printTempHum()
# ~ e.setImagesPerFile(100)
#~ e.printDetectorConfig()
#~ e.printFileWriterConfig()
#~ print "Exposure ..."
#~ e.exposure(fnBase='test')
#~ print "... done"
#~ e.fileWriterSave(filename='test*', targetDir=os.path.curdir)