forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractCollect.py
644 lines (558 loc) · 21.6 KB
/
AbstractCollect.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE 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 3 of the License, or
# (at your option) any later version.
#
# MXCuBE 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 MXCuBE. If not, see <http://www.gnu.org/licenses/>.
"""
AbstractMulticollect
Defines a sequence how data collection is executed.
"""
import os
import sys
import logging
import time
import errno
import abc
import collections
import gevent
from HardwareRepository.TaskUtils import *
__copyright__ = "Copyright 2012, ESRF"
__credits__ = ["MXCuBE colaboration"]
__version__ = "2.2."
__status__ = "Draft"
BeamlineConfig = collections.namedtuple('BeamlineConfig',
['synchrotron_name',
'directory_prefix',
'default_exposure_time',
'minimum_exposure_time',
'detector_fileext',
'detector_type',
'detector_manufacturer',
'detector_model',
'detector_px',
'detector_py',
'undulators',
'focusing_optic',
'monochromator_type',
'beam_divergence_vertical',
'beam_divergence_horizontal',
'polarisation',
'input_files_server'])
class AbstractCollect(object, metaclass=abc.ABCMeta):
def __init__(self):
self.bl_config = BeamlineConfig(*[None]*17)
self.data_collect_task = None
self.current_dc_parameters = None
self.autoprocessing_hwobj = None
self.beam_info_hwobj = None
self.detector_hwobj = None
self.diffractometer_hwobj = None
self.energy_hwobj = None
self.lims_client_hwobj = None
self.machine_info__hwobj = None
self.resolution_hwobj = None
self.sample_changer_hwobj = None
self.transmission_hwobj = None
self.ready_event = gevent.event.Event()
def set_beamline_configuration(self, **configuration_parameters):
self.bl_config = BeamlineConfig(**configuration_parameters)
def collect(self, owner, dc_parameters_list):
"""
Main collect method.
"""
self.ready_event.clear()
self.current_dc_parameters = dc_parameters_list[0]
self.data_collect_task = gevent.spawn(self.do_collect, owner)
self.ready_event.wait()
self.ready_event.clear()
return self.data_collect_task
def do_collect(self, owner):
"""
Actual collect sequence
"""
logging.getLogger("user_level_log").info("Collect started")
self.emit("collectReady", (False, ))
self.emit('collectOscillationStarted', (owner, None, \
None, None, self.current_dc_parameters, None))
# ----------------------------------------------------------------
self.open_detector_cover()
self.open_safety_shutter()
self.open_fast_shutter()
# ----------------------------------------------------------------
self.current_dc_parameters["status"] = 'Running'
self.current_dc_parameters["collection_start_time"] = \
time.strftime("%Y-%m-%d %H:%M:%S")
self.current_dc_parameters["synchrotronMode"] = \
self.get_machine_fill_mode()
self.store_data_collection_in_lims()
self.create_file_directories()
self.get_sample_info()
self.store_sample_info_in_lims()
if all(item == None for item in list(self.current_dc_parameters['motors'].values())):
# No centring point defined
# create point based on the current position
current_diffractometer_position = self.diffractometer_hwobj.getPositions()
for motor in list(self.current_dc_parameters['motors'].keys()):
self.current_dc_parameters['motors'][motor] = \
current_diffractometer_position[motor]
self.move_to_centered_position()
self.take_crystal_snapshots()
self.move_to_centered_position()
if "transmission" in self.current_dc_parameters:
logging.getLogger("user_level_log").info(\
"Setting transmission to %.3f",
self.current_dc_parameters["transmission"])
self.set_transmission(self.current_dc_parameters["transmission"])
if "wavelength" in self.current_dc_parameters:
logging.getLogger("user_level_log").info(\
"Setting wavelength to %.3f", \
self.current_dc_parameters["wavelength"])
self.set_wavelength(self.current_dc_parameters["wavelength"])
elif "energy" in self.current_dc_parameters:
logging.getLogger("user_level_log").info(\
"Setting energy to %.3f",
self.current_dc_parameters["energy"])
self.set_energy(self.current_dc_parameters["energy"])
if "resolution" in self.current_dc_parameters:
resolution = self.current_dc_parameters["resolution"]["upper"]
logging.getLogger("user_level_log").info("Setting resolution to %.3f", resolution)
self.set_resolution(resolution)
elif 'detdistance' in self.current_dc_parameters:
logging.getLogger("user_level_log").info("Moving detector to %f",
self.current_dc_parameters["detdistance"])
self.move_detector(self.current_dc_parameters["detdistance"])
if self.lims_client_hwobj:
self.current_dc_parameters["flux"] = self.get_flux()
self.current_dc_parameters["wavelength"] = self.get_wavelength()
self.current_dc_parameters["detectorDistance"] = self.get_detector_distance()
self.current_dc_parameters["resolution"] = self.get_resolution()
self.current_dc_parameters["transmission"] = self.get_transmission()
beam_centre_x, beam_centre_y = self.get_beam_centre()
self.current_dc_parameters["xBeam"] = beam_centre_x
self.current_dc_parameters["yBeam"] = beam_centre_y
und = self.get_undulators_gaps()
i = 1
for jj in self.bl_config.undulators:
key = jj.type
if key in und:
self.current_dc_parameters["undulatorGap%d" % (i)] = und[key]
i += 1
self.current_dc_parameters["resolutionAtCorner"] = self.get_resolution_at_corner()
beam_size_x, beam_size_y = self.get_beam_size()
self.current_dc_parameters["beamSizeAtSampleX"] = beam_size_x
self.current_dc_parameters["beamSizeAtSampleY"] = beam_size_y
self.current_dc_parameters["beamShape"] = self.get_beam_shape()
hor_gap, vert_gap = self.get_slit_gaps()
self.current_dc_parameters["slitGapHorizontal"] = hor_gap
self.current_dc_parameters["slitGapVertical"] = vert_gap
self.data_collection_hook()
# ----------------------------------------------------------------
self.close_fast_shutter()
self.close_safety_shutter()
self.close_detector_cover()
def data_collection_cleanup(self, owner="MXCuBE"):
"""
Method called when an error is raised during the collectin.
"""
logging.exception("Data collection failed")
self.current_dc_parameters["status"] = 'failed'
exc_type, exc_value, exc_tb = sys.exc_info()
failed_msg = 'Data collection failed!\n%s' % exc_value
self.emit("collectOscillationFailed", (owner, False, failed_msg,
self.current_dc_parameters.get('collection_id'), 1))
def stop_collect(self, owner):
"""
Stops data collection
"""
if self.data_collect_task is not None:
self.data_collect_task.kill(block = False)
def open_detector_cover(self):
"""
Descript. :
"""
pass
def open_safety_shutter(self):
"""
Descript. :
"""
pass
def open_fast_shutter(self):
"""
Descript. :
"""
pass
def close_fast_shutter(self):
"""
Descript. :
"""
pass
def close_safety_shutter(self):
"""
Descript. :
"""
pass
def close_detector_cover(self):
"""
Descript. :
"""
pass
def set_transmission(self, value):
"""
Descript. :
"""
pass
def set_wavelength(self, value):
"""
Descript. :
"""
pass
def set_energy(self, value):
"""
Descript. :
"""
pass
def set_resolution(self, value):
"""
Descript. :
"""
pass
def move_detector(self, value):
"""
Descript. :
"""
pass
def get_flux(self):
"""
Descript. :
"""
return
def get_wavelength(self):
"""
Descript. :
"""
if self.energy_hwobj is not None:
return self.energy_hwobj.getCurrentWavelength()
def get_detector_distance(self):
"""
Descript. :
"""
if self.detector_hwobj is not None:
return self.detector_hwobj.get_distance()
def get_resolution(self):
"""
Descript. :
"""
if self.resolution_hwobj is not None:
return self.resolution_hwobj.getPosition()
def get_transmission(self):
"""
Descript. :
"""
if self.transmission_hwobj is not None:
return self.transmission_hwobj.getAttFactor()
def get_beam_centre(self):
if self.detector_hwobj is not None:
return self.detector_hwobj.get_beam_centre()
else:
return None, None
def get_resolution_at_corner(self):
"""
Descript. :
"""
return
def get_beam_size(self):
"""
Descript. :
"""
if self.beam_info_hwobj is not None:
return self.beam_info_hwobj.get_beam_size()
else:
return None, None
def get_slit_gaps(self):
"""
Descript. :
"""
if self.beam_info_hwobj is not None:
return self.beam_info_hwobj.get_slits_gap()
return None, None
def get_undulators_gaps(self):
"""
Descript. :
"""
return {}
def get_beam_shape(self):
"""
Descript. :
"""
if self.beam_info_hwobj is not None:
return self.beam_info_hwobj.get_shape()
def get_machine_current(self):
"""
Descript. :
"""
if self.machine_info_hwobj:
return self.machine_info_hwobj.get_current()
else:
return 0
def get_machine_message(self):
"""
Descript. :
"""
if self.machine_info_hwobj:
return self.machine_info_hwobj.get_message()
else:
return ''
def get_machine_fill_mode(self):
"""
Descript. :
"""
if self.machine_info_hwobj:
fill_mode = str(self.machine_info_hwobj.get_message())
return fill_mode[:20]
else:
return ''
def get_measured_intensity(self):
"""
Descript. :
"""
return
def get_cryo_temperature(self):
"""
Descript. :
"""
return
def create_file_directories(self):
"""
Method create directories for raw files and processing files.
Directorie names for xds, mosflm and hkl are created
"""
logging.getLogger("user_level_log").info(\
"Creating directories for raw images and processing files")
self.create_directories(\
self.current_dc_parameters['fileinfo']['directory'],
self.current_dc_parameters['fileinfo']['process_directory'])
xds_directory, mosflm_directory, hkl2000_directory = \
self.prepare_input_files()
if xds_directory:
self.current_dc_parameters['xds_dir'] = xds_directory
def create_directories(self, *args):
"""
Descript. :
"""
for directory in args:
try:
os.makedirs(directory)
except os.error as e:
if e.errno != errno.EEXIST:
raise
def prepare_input_files(self):
"""
Prepares input files for xds, mosflm and hkl2000
returns: 3 strings
"""
return None, None, None
def store_data_collection_in_lims(self):
"""
Descript. :
"""
if self.lims_client_hwobj:
logging.getLogger("user_level_log").info("Storing data collection in LIMS")
try:
(collection_id, detector_id) = self.lims_client_hwobj.\
store_data_collection(self.current_dc_parameters,
self.bl_config)
self.current_dc_parameters['collection_id'] = collection_id
if detector_id:
self.current_dc_parameters['detector_id'] = detector_id
except:
logging.getLogger("HWR").exception("Could not store data collection in LIMS")
def update_data_collection_in_lims(self):
"""
Descript. :
"""
if self.lims_client_hwobj:
logging.getLogger("user_level_log").info("Updating data collection in LIMS")
try:
self.lims_client_hwobj.update_data_collection(self.current_dc_parameters)
except:
logging.getLogger("HWR").exception("Could not update data collection in LIMS")
def store_sample_info_in_lims(self):
"""
Descript. :
"""
if self.lims_client_hwobj:
logging.getLogger("user_level_log").info("Storing sample info in LIMS")
def store_image_in_lims(self, frame_number, motor_position_id=None):
"""
Descript. :
"""
if self.lims_client_hwobj:
file_location = self.current_dc_parameters["fileinfo"]["directory"]
image_file_template = self.current_dc_parameters['fileinfo']['template']
filename = image_file_template % frame_number
lims_image = {'dataCollectionId': self.current_dc_parameters["collection_id"],
'fileName': filename,
'fileLocation': file_location,
'imageNumber': frame_number,
'measuredIntensity': self.get_measured_intensity(),
'synchrotronCurrent': self.get_machine_current(),
'machineMessage': self.get_machine_message(),
'temperature': self.get_cryo_temperature()}
archive_directory = self.current_dc_parameters['fileinfo']['archive_directory']
if archive_directory:
jpeg_filename = "%s.jpeg" % os.path.splitext(image_file_template)[0]
thumb_filename = "%s.thumb.jpeg" % os.path.splitext(image_file_template)[0]
jpeg_file_template = os.path.join(archive_directory, jpeg_filename)
jpeg_thumbnail_file_template = os.path.join(archive_directory, thumb_filename)
jpeg_full_path = jpeg_file_template % frame_number
jpeg_thumbnail_full_path = jpeg_thumbnail_file_template % frame_number
lims_image['jpegFileFullPath'] = jpeg_full_path
lims_image['jpegThumbnailFileFullPath'] = jpeg_thumbnail_full_path
if motor_position_id:
lims_image['motorPositionId'] = motor_position_id
image_id = self.lims_client_hwobj.store_image(lims_image)
return image_id
def get_sample_info(self):
"""
Descript. :
"""
logging.getLogger("user_level_log").info("Getting sample info from parameters")
sample_info = self.current_dc_parameters.get("sample_reference")
try:
sample_id = int(sample_info["blSampleId"])
except:
sample_id = None
try:
sample_code = sample_info["code"]
except:
sample_code = None
sample_location = None
try:
sample_container_number = int(sample_info['container_reference'])
except:
pass
else:
try:
vial_number = int(sample_info["sample_location"])
except:
pass
else:
sample_location = (sample_container_number, vial_number)
self.current_dc_parameters['blSampleId'] = sample_id
if self.sample_changer_hwobj:
try:
self.current_dc_parameters["actualSampleBarcode"] = \
self.sample_changer_hwobj.getLoadedSample().getID()
self.current_dc_parameters["actualContainerBarcode"] = \
self.sample_changer_hwobj.getLoadedSample().getContainer().getID()
logging.getLogger("user_level_log").info("Getting loaded sample coords")
basket, vial = self.sample_changer_hwobj.getLoadedSample().getCoords()
self.current_dc_parameters["actualSampleSlotInContainer"] = vial
self.current_dc_parameters["actualContainerSlotInSC"] = basket
except:
self.current_dc_parameters["actualSampleBarcode"] = None
self.current_dc_parameters["actualContainerBarcode"] = None
else:
self.current_dc_parameters["actualSampleBarcode"] = None
self.current_dc_parameters["actualContainerBarcode"] = None
def move_to_centered_position(self):
"""
Descript. :
"""
logging.getLogger("user_level_log").info("Moving to centred position")
positions_str = ""
for motor, position in self.current_dc_parameters['motors'].items():
if position:
if isinstance(motor, str):
positions_str += " %s=%f" % (motor, position)
else:
positions_str += " %s=%f" % (motor.getMotorMnemonic(), position)
self.current_dc_parameters['actualCenteringPosition'] = positions_str
self.move_motors(self.current_dc_parameters['motors'])
@abc.abstractmethod
@task
def move_motors(self, motor_position_dict):
"""
Descript. :
"""
return
def take_crystal_snapshots(self):
"""
Descript. :
"""
number_of_snapshots = self.current_dc_parameters["take_snapshots"]
if number_of_snapshots > 0:
snapshot_directory = self.current_dc_parameters["fileinfo"]["archive_directory"]
if not os.path.exists(snapshot_directory):
try:
logging.getLogger("user_level_log").info(\
"Creating snapshots directory: %r",
snapshot_directory)
self.create_directories(snapshot_directory)
except:
logging.getLogger("HWR").exception("Error creating snapshot directory")
logging.getLogger("user_level_log").info(\
"Taking %d sample snapshot(s)" % number_of_snapshots)
for snapshot_index in range(number_of_snapshots):
snapshot_filename = os.path.join(\
snapshot_directory,
"%s_%s_%s.snapshot.jpeg" % (\
self.current_dc_parameters["fileinfo"]["prefix"],
self.current_dc_parameters["fileinfo"]["run_number"],
(snapshot_index + 1)))
self.current_dc_parameters['xtalSnapshotFullPath%i' % \
(snapshot_index + 1)] = snapshot_filename
self._take_crystal_snapshot(snapshot_filename)
if number_of_snapshots > 1:
self.diffractometer_hwobj.move_omega_relative(90)
@abc.abstractmethod
@task
def _take_crystal_snapshot(self, snapshot_filename):
"""
Depends on gui version how this method is implemented.
In Qt3 diffractometer has a function,
In Qt4 graphics_manager is making crystal snapshots
"""
pass
@abc.abstractmethod
def data_collection_hook(self):
"""
Descript. :
"""
pass
@abc.abstractmethod
def trigger_auto_processing(self, process_event, frame_number):
"""
Descript. :
"""
pass
def set_helical(self, arg):
"""
Descript. :
"""
pass
def set_helical_pos(self, arg):
"""
Descript. :
"""
pass
def setMeshScanParameters(self, num_lines, num_images_per_line, mesh_range):
"""
Descript. :
"""
pass
def setCentringStatus(self, status):
pass