def unload( self, holderLength, sample_id=None, sample_location=None, sampleIsUnloadedCallback=None, failureCallback=None, ): unloaded = False with error_cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Alarm), failureCallback, ): with cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Ready)): with cleanup(self.unlockMinidiffMotors, wait=True, timeout=3): unloaded = self.__unloadSample(holderLength, sample_id, sample_location) if unloaded: logging.getLogger("HWR").debug( "%s: sample has been unloaded", self.name()) self.emit("statusChanged", "Ready") if callable(sampleIsUnloadedCallback): sampleIsUnloadedCallback()
def unload( self, holderLength, sample_id=None, sample_location=None, sampleIsUnloadedCallback=None, failureCallback=None, ): unloaded = False with error_cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Alarm), failureCallback, ): with cleanup( functools.partial( self.emit, "stateChanged", SC3.SampleChangerState.Ready ) ): with cleanup(self.unlockMinidiffMotors, wait=True, timeout=3): unloaded = self.__unloadSample( holderLength, sample_id, sample_location ) if unloaded: logging.getLogger("HWR").debug( "%s: sample has been unloaded", self.name() ) self.emit("statusChanged", "Ready") if callable(sampleIsUnloadedCallback): sampleIsUnloadedCallback()
def startXrfSpectrum(self, ct, directory, prefix, session_id = None, blsample_id = None): self.scanning = True self.emit('xrfScanStarted', ()) with cleanup(self.ready_event.set): self.spectrumInfo["sessionId"] = session_id self.spectrumInfo["blSampleId"] = blsample_id mcaData = [] calibrated_data = [] values = [0, 200, 300, 500, 600, 700, 800, 900, 1000, 1500, 1600, 1000, 700, 600, 500, 450, 300, 200, 100, 0, 0 ,0, 90] for n, value in enumerate(values): mcaData.append((n, value)) mcaCalib = [10,1, 21, 0] mcaConfig = {} mcaConfig["legend"] = "XRF test scan from XRF mockup" mcaConfig['htmldir'] = "html dir not defined" mcaConfig["min"] = values[0] mcaConfig["max"] = values[-1] mcaConfig["file"] = None time.sleep(3) self.emit('xrfScanFinished', (mcaData, mcaCalib, mcaConfig))
def startXrfSpectrum(self, ct, directory, archive_directory, prefix, session_id=None, blsample_id=None, adjust_transmission=False): self.scanning = True self.emit('xrfSpectrumStarted', ()) with cleanup(self.ready_event.set): self.spectrumInfo["sessionId"] = session_id self.spectrumInfo["blSampleId"] = blsample_id mcaData = [] calibrated_data = [] values = [ 0, 20, 340, 70, 100, 110, 120, 200, 200, 210, 1600, 210, 200, 200, 200, 250, 300, 200, 100, 0, 0, 0, 90 ] for n, value in enumerate(values): mcaData.append((n, value)) mcaCalib = [10, 1, 21, 0] mcaConfig = {} mcaConfig["legend"] = "XRF test scan from XRF mockup" mcaConfig['htmldir'] = "html dir not defined" mcaConfig["min"] = values[0] mcaConfig["max"] = values[-1] mcaConfig["file"] = "/Not/existing/Configure/file" time.sleep(3) self.emit('xrfSpectrumFinished', (mcaData, mcaCalib, mcaConfig))
def startXrfSpectrum(self, ct, directory, archive_directory, prefix, session_id=None, blsample_id=None, adjust_transmission=False): self.scanning = True self.emit('xrfSpectrumStarted', ()) with cleanup(self.ready_event.set): self.spectrumInfo["sessionId"] = session_id self.spectrumInfo["blSampleId"] = blsample_id mcaData = [] calibrated_data = [] values = [0, 20, 340, 70, 100, 110, 120, 200, 200, 210, 1600, 210, 200, 200, 200, 250, 300, 200, 100, 0, 0 ,0, 90] for n, value in enumerate(values): mcaData.append((n, value)) mcaCalib = [10,1, 21, 0] mcaConfig = {} mcaConfig["legend"] = "XRF test scan from XRF mockup" mcaConfig['htmldir'] = "html dir not defined" mcaConfig["min"] = values[0] mcaConfig["max"] = values[-1] mcaConfig["file"] = "/Not/existing/Configure/file" time.sleep(3) self.emit('xrfSpectrumFinished', (mcaData, mcaCalib, mcaConfig))
def scanCommandFinished(self, result, *args): logging.getLogger("HWR").debug("EnergyScan: energy scan result is %s" % result) with cleanup(self.ready_event.set): self.scanInfo["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") logging.getLogger("HWR").debug( "EnergyScan: energy scan result is %s" % result) self.scanning = False if result == -1: self.storeEnergyScan() self.emit("energyScanFailed", ()) return try: t = float(result["transmissionFactor"]) except Exception: pass else: self.scanInfo["transmissionFactor"] = t try: et = float(result["exposureTime"]) except Exception: pass else: self.scanInfo["exposureTime"] = et try: se = float(result["startEnergy"]) except Exception: pass else: self.scanInfo["startEnergy"] = se try: ee = float(result["endEnergy"]) except Exception: pass else: self.scanInfo["endEnergy"] = ee try: bsX = float(result["beamSizeHorizontal"]) except Exception: pass else: self.scanInfo["beamSizeHorizontal"] = bsX try: bsY = float(result["beamSizeVertical"]) except Exception: pass else: self.scanInfo["beamSizeVertical"] = bsY try: self.thEdge = float(result["theoreticalEdge"]) / 1000.0 except Exception: pass self.emit("energyScanFinished", (self.scanInfo, )) time.sleep(0.1) self.emit("energyScanFinished2", (self.scanInfo, ))
def scanCommandFinished(self, result, *args): logging.getLogger("HWR").debug("EnergyScan: energy scan result is %s" % result) with cleanup(self.ready_event.set): self.scanInfo["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") logging.getLogger("HWR").debug( "EnergyScan: energy scan result is %s" % result ) self.scanning = False if result == -1: self.storeEnergyScan() self.emit("energyScanFailed", ()) return try: t = float(result["transmissionFactor"]) except BaseException: pass else: self.scanInfo["transmissionFactor"] = t try: et = float(result["exposureTime"]) except BaseException: pass else: self.scanInfo["exposureTime"] = et try: se = float(result["startEnergy"]) except BaseException: pass else: self.scanInfo["startEnergy"] = se try: ee = float(result["endEnergy"]) except BaseException: pass else: self.scanInfo["endEnergy"] = ee try: bsX = float(result["beamSizeHorizontal"]) except BaseException: pass else: self.scanInfo["beamSizeHorizontal"] = bsX try: bsY = float(result["beamSizeVertical"]) except BaseException: pass else: self.scanInfo["beamSizeVertical"] = bsY try: self.thEdge = float(result["theoreticalEdge"]) / 1000.0 except BaseException: pass self.emit("energyScanFinished", (self.scanInfo,)) time.sleep(0.1) self.emit("energyScanFinished2", (self.scanInfo,))
def spectrumCommandFinished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info['endTime'] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False values = list(self.cmd_scan_start.get()) xmin = 0 xmax = 0 mcaCalib = self.chan_scan_consts.getValue()[::-1] mcaData = [] calibrated_data = [] for n, value in enumerate(values): mcaData.append((n / 1000.0, value)) energy = (mcaCalib[2] + mcaCalib[1] * n + mcaCalib[0] * n * n) / 1000 if value > xmax: xmax = value if value < xmin: xmin = value calibrated_data.append([energy, value]) calibrated_array = numpy.array(calibrated_data) self.spectrum_info[ "beamTransmission"] = self.transmission_hwobj.getAttFactor() self.spectrum_info["energy"] = self.getCurrentEnergy() beam_size = self.beam_info_hwobj.get_beam_size() self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] * 1000) self.spectrum_info["beamSizeVertical"] = int(beam_size[1] * 1000) mcaConfig = {} mcaConfig["legend"] = "Xfe spectrum" mcaConfig["min"] = xmin mcaConfig["max"] = xmax mcaConfig["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color": 'black'}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info( "Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"]) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi=80) #logging.getLogger().debug("Copying .fit file to: %s", a_dir) #tmpname=filename.split(".") logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit('xrfScanFinished', (mcaData, mcaCalib, mcaConfig))
def spectrumCommandFinished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False values = list(self.cmd_scan_start.get()) xmin = 0 xmax = 0 mcaCalib = self.chan_scan_consts.getValue()[::-1] mcaData = [] calibrated_data = [] for n, value in enumerate(values): mcaData.append((n / 1000.0, value)) energy = (mcaCalib[2] + mcaCalib[1] * n + mcaCalib[0] * n * n) / 1000 if value > xmax: xmax = value if value < xmin: xmin = value calibrated_data.append([energy, value]) calibrated_array = numpy.array(calibrated_data) self.spectrum_info["beamTransmission"] = self.transmission_hwobj.getAttFactor() self.spectrum_info["energy"] = self.getCurrentEnergy() beam_size = self.beam_info_hwobj.get_beam_size() self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] * 1000) self.spectrum_info["beamSizeVertical"] = int(beam_size[1] * 1000) mcaConfig = {} mcaConfig["legend"] = "Xfe spectrum" mcaConfig["min"] = xmin mcaConfig["max"] = xmax mcaConfig["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color": "black"}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info("Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"]) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi=80) # logging.getLogger().debug("Copying .fit file to: %s", a_dir) # tmpname=filename.split(".") logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit("xrfScanFinished", (mcaData, mcaCalib, mcaConfig))
def scanCommandFinished(self, *args): with cleanup(self.ready_event.set): self.scan_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") logging.getLogger("HWR").debug("Energy scan: finished") self.scanning = False self.scan_info["startEnergy"] = self.scan_data[-1][0] self.scan_info["endEnergy"] = self.scan_data[-1][1] self.emit("energyScanFinished", (self.scan_info,)) self.emit("progressStop", ()) if hasattr(self.energy_hwobj, "set_break_bragg"): self.energy_hwobj.set_break_bragg()
def scanCommandFinished(self, *args): """ Descript. : """ with cleanup(self.ready_event.set): self.scan_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") logging.getLogger("HWR").debug("Energy Scan: finished") self.scanning = False self.scan_info["startEnergy"] = self.scan_data[-1][0] / 1000.0 self.scan_info["endEnergy"] = self.scan_data[-1][1] / 1000.0 self.emit("energyScanFinished", self.scan_info)
def load( self, holderLength, sample_id=None, sample_location=None, sampleIsLoadedCallback=None, failureCallback=None, prepareCentring=True, ): loaded = False with error_cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Alarm), failureCallback, ): with cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Ready)): with cleanup(self.unlockMinidiffMotors, wait=True, timeout=3): loaded = self.__loadSample(holderLength, sample_id, sample_location) if loaded: logging.getLogger("HWR").debug("%s: sample is loaded", self.name()) if self.prepareCentringAfterLoad and prepareCentring: logging.getLogger("HWR").debug( "%s: preparing minidiff for sample centring", self.name()) self.emit("stateChanged", SC3.SampleChangerState.Moving) self.emit("statusChanged", "Preparing minidiff for sample centring") self.prepareCentring(wait=True, timeout=1000) self.emit("statusChanged", "Ready") if callable(sampleIsLoadedCallback): sampleIsLoadedCallback()
def load( self, holderLength, sample_id=None, sample_location=None, sampleIsLoadedCallback=None, failureCallback=None, prepareCentring=True, ): loaded = False with error_cleanup( functools.partial(self.emit, "stateChanged", SC3.SampleChangerState.Alarm), failureCallback, ): with cleanup( functools.partial( self.emit, "stateChanged", SC3.SampleChangerState.Ready ) ): with cleanup(self.unlockMinidiffMotors, wait=True, timeout=3): loaded = self.__loadSample(holderLength, sample_id, sample_location) if loaded: logging.getLogger("HWR").debug("%s: sample is loaded", self.name()) if self.prepareCentringAfterLoad and prepareCentring: logging.getLogger("HWR").debug( "%s: preparing minidiff for sample centring", self.name() ) self.emit("stateChanged", SC3.SampleChangerState.Moving) self.emit( "statusChanged", "Preparing minidiff for sample centring" ) self.prepareCentring(wait=True, timeout=1000) self.emit("statusChanged", "Ready") if callable(sampleIsLoadedCallback): sampleIsLoadedCallback()
def scanCommandFailed(self, *args): with cleanup(self.ready_event.set): self.scan_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") if self.scan_data: self.scan_info["startEnergy"] = self.scan_data[-1][0] self.scan_info["endEnergy"] = self.scan_data[-1][1] self.emit("energyScanFailed", ()) self.emit("progressStop", ()) self.scanning = False self.ready_event.set()
def spectrum_command_finished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info['endTime'] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False values = list(self.chan_scan_start.getValue()) mcaCalib = self.chan_scan_consts.getValue() mcaData = [] calibrated_data = [] for n, value in enumerate(values): mcaData.append((n, value)) energy = mcaCalib[0] + mcaCalib[1] * n + mcaCalib[2] * n * n calibrated_line = [energy, value] calibrated_data.append(calibrated_line) calibrated_array = numpy.array(calibrated_data) mcaConfig = {} if self.transmission_hwobj is not None: self.spectrum_info["beamTransmission"] = self.transmission_hwobj.getAttFactor() else: self.spectrum_info["beamTransmission"] = None self.spectrum_info["energy"] = self.get_current_energy() if self.beam_info_hwobj is not None: beam_size = self.beam_info_hwobj.get_beam_size() else: beam_size = (None, None) self.spectrum_info["beamSizeHorizontal"] = beam_size[0] self.spectrum_info["beamSizeVertical"] = beam_size[1] mcaConfig["legend"] = "test legend" mcaConfig["min"] = values[0] mcaConfig["max"] = values[-1] mcaConfig["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color" : 'black'}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info("Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"]) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi = 80) #logging.getLogger().debug("Copying .fit file to: %s", a_dir) #tmpname=filename.split(".") logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit('xrfScanFinished', (mcaData, mcaCalib, mcaConfig))
def scanCommandFinished(self, *args): logging.getLogger("HWR").debug("EnergyScan: finished") with cleanup(self.ready_event.set): self.scan_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False self.scan_info["startEnergy"] = self.scan_data[-1][0] self.scan_info["endEnergy"] = self.scan_data[-1][1] self.log.debug("Scan finished. data is: %s " % str(self.scan_data)) self.emit("energyScanFinished", (self.scan_info,)) self.emit("progressStop", ())
def scanCommandFinished(self, *args): logging.getLogger("HWR").debug("EnergyScan: finished") with cleanup(self.ready_event.set): self.scan_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False self.scan_info["startEnergy"] = self.scan_data[-1][0] self.scan_info["endEnergy"] = self.scan_data[-1][1] self.log.debug("Scan finished. data is: %s " % str(self.scan_data)) self.emit("energyScanFinished", (self.scan_info, )) self.emit("progressStop", ())
def scanCommandFailed(self, *args): with cleanup(self.ready_event.set): # error_msg = self.chan_scan_error.getValue() # print error_msg # logging.getLogger("GUI").error("Energy scan: %s" % error_msg) self.scan_info["endTime"] = str(time.strftime("%Y-%m-%d %H:%M:%S")) if self.scan_data: self.scan_info["startEnergy"] = self.scan_data[-1][0] / 1000.0 self.scan_info["endEnergy"] = self.scan_data[-1][1] / 1000.0 self.emit("energyScanFailed", ()) self.emit("progressStop", ()) if hasattr(self.energy_hwobj, "set_break_bragg"): self.energy_hwobj.set_break_bragg() self.scanning = False self.ready_event.set()
def spectrumCommandFinished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info['endTime'] = time.strftime("%Y-%m-%d %H:%M:%S") self.scanning = False values = list(self.cmd_scan_start.get()) xmin = 0 xmax = 0 mcaCalib = self.chan_scan_consts.getValue()[::-1] mcaData = [] calibrated_data = [] try: scan_file_raw = open(self.spectrum_info["scanFileFullPath"], "w") archive_file_raw = open(self.spectrum_info["scanFilePath"], "w") except: logging.getLogger("HWR").exception("EMBLXRFSpectrum: could not create spectrum result raw file %s" %self.spectrum_info["scanFileFullPath"]) for n, value in enumerate(values): energy = (mcaCalib[2] + mcaCalib[1] * n + mcaCalib[0] * n * n) / 1000 if energy < 13: if energy > xmax: xmax = value if energy < xmin: xmin = value calibrated_data.append([energy, value]) mcaData.append((n / 1000.0, value)) if scan_file_raw: scan_file_raw.write("%f,%f\r\n" % (energy, value)) if archive_file_raw: archive_file_raw.write("%f,%f\r\n" % (energy, value)) if scan_file_raw: scan_file_raw.close() if archive_file_raw: archive_file_raw.close() calibrated_array = numpy.array(calibrated_data) self.spectrum_info["beamTransmission"] = self.transmission_hwobj.getAttFactor() self.spectrum_info["energy"] = self.getCurrentEnergy() beam_size = self.beam_info_hwobj.get_beam_size() self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] * 1000) self.spectrum_info["beamSizeVertical"] = int(beam_size[1] * 1000) mcaConfig = {} mcaConfig["legend"] = self.spectrum_info["filename"] mcaConfig["file"] = self.config_filename mcaConfig["min"] = xmin mcaConfig["max"] = xmax mcaConfig["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") self.spectrum_info.pop("scanFilePath") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color" : 'black'}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info("Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"]) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi = 80) #logging.getLogger().debug("Copying .fit file to: %s", a_dir) #tmpname=filename.split(".") #logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit('xrfScanFinished', (mcaData, mcaCalib, mcaConfig))
def do_collect(self, owner, data_collect_parameters): if self.__safety_shutter_close_task is not None: self.__safety_shutter_close_task.kill() logging.getLogger("user_level_log").info("Closing fast shutter") self.close_fast_shutter() # reset collection id on each data collect self.collection_id = None # Preparing directory path for images and processing files # creating image file template and jpegs files templates file_parameters = data_collect_parameters["fileinfo"] file_parameters["suffix"] = self.bl_config.detector_fileext image_file_template = ( "%(prefix)s_%(run_number)s_%%04d.%(suffix)s" % file_parameters ) file_parameters["template"] = image_file_template archive_directory = self.get_archive_directory(file_parameters["directory"]) data_collect_parameters["archive_dir"] = 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 ) else: jpeg_file_template = None jpeg_thumbnail_file_template = None # database filling if self.bl_control.lims: data_collect_parameters["collection_start_time"] = time.strftime( "%Y-%m-%d %H:%M:%S" ) if self.bl_control.machine_current is not None: logging.getLogger("user_level_log").info( "Getting synchrotron filling mode" ) data_collect_parameters[ "synchrotronMode" ] = self.get_machine_fill_mode() data_collect_parameters["status"] = "failed" logging.getLogger("user_level_log").info("Storing data collection in LIMS") ( self.collection_id, detector_id, ) = self.bl_control.lims.store_data_collection( data_collect_parameters, self.bl_config ) data_collect_parameters["collection_id"] = self.collection_id if detector_id: data_collect_parameters["detector_id"] = detector_id # Creating the directory for images and processing information logging.getLogger("user_level_log").info( "Creating directory for images and processing" ) self.create_directories( file_parameters["directory"], file_parameters["process_directory"] ) self.xds_directory, self.mosflm_directory, self.hkl2000_directory = self.prepare_input_files( file_parameters["directory"], file_parameters["prefix"], file_parameters["run_number"], file_parameters["process_directory"], ) data_collect_parameters["xds_dir"] = self.xds_directory logging.getLogger("user_level_log").info("Getting sample info from parameters") sample_id, sample_location, sample_code = self.get_sample_info_from_parameters( data_collect_parameters ) data_collect_parameters["blSampleId"] = sample_id if self.bl_control.sample_changer is not None: try: data_collect_parameters[ "actualSampleBarcode" ] = self.bl_control.sample_changer.getLoadedSample().getID() data_collect_parameters["actualContainerBarcode"] = ( self.bl_control.sample_changer.getLoadedSample() .getContainer() .getID() ) logging.getLogger("user_level_log").info("Getting loaded sample coords") basket, vial = ( self.bl_control.sample_changer.getLoadedSample().getCoords() ) data_collect_parameters["actualSampleSlotInContainer"] = vial data_collect_parameters["actualContainerSlotInSC"] = basket except BaseException: data_collect_parameters["actualSampleBarcode"] = None data_collect_parameters["actualContainerBarcode"] = None else: data_collect_parameters["actualSampleBarcode"] = None data_collect_parameters["actualContainerBarcode"] = None centring_info = {} try: logging.getLogger("user_level_log").info("Getting centring status") centring_status = self.diffractometer().getCentringStatus() except BaseException: pass else: centring_info = dict(centring_status) # Save sample centring positions positions_str = "" motors = centring_info.get( "motors", {} ) # .update(centring_info.get("extraMotors", {})) motors_to_move_before_collect = data_collect_parameters.setdefault("motors", {}) for motor, pos in motors.iteritems(): if motor in motors_to_move_before_collect: continue motors_to_move_before_collect[motor] = pos current_diffractometer_position = self.diffractometer().getPositions() for motor in motors_to_move_before_collect.keys(): if motors_to_move_before_collect[motor] is None: del motors_to_move_before_collect[motor] try: if current_diffractometer_position[motor] is not None: positions_str += "%s=%f " % ( motor, current_diffractometer_position[motor], ) except BaseException: pass # this is for the LIMS positions_str += " ".join( [ motor + ("=%f" % pos) for motor, pos in motors_to_move_before_collect.iteritems() ] ) data_collect_parameters["actualCenteringPosition"] = positions_str self.move_motors(motors_to_move_before_collect) # take snapshots, then assign centring status (which contains images) to # centring_info variable take_snapshots = data_collect_parameters.get("take_snapshots", False) if take_snapshots: logging.getLogger("user_level_log").info("Taking sample snapshosts") self._take_crystal_snapshots(take_snapshots) centring_info = self.bl_control.diffractometer.getCentringStatus() # move *again* motors, since taking snapshots may change positions logging.getLogger("user_level_log").info( "Moving motors: %r", motors_to_move_before_collect ) self.move_motors(motors_to_move_before_collect) if self.bl_control.lims: try: if self.current_lims_sample: self.current_lims_sample[ "lastKnownCentringPosition" ] = positions_str logging.getLogger("user_level_log").info( "Updating sample information in LIMS" ) self.bl_control.lims.update_bl_sample(self.current_lims_sample) except BaseException: logging.getLogger("HWR").exception( "Could not update sample information in LIMS" ) if centring_info.get("images"): # Save snapshots snapshot_directory = self.get_archive_directory( file_parameters["directory"] ) try: logging.getLogger("user_level_log").info( "Creating snapshosts directory: %r", snapshot_directory ) self.create_directories(snapshot_directory) except BaseException: logging.getLogger("HWR").exception("Error creating snapshot directory") else: snapshot_i = 1 snapshots = [] for img in centring_info["images"]: img_phi_pos = img[0] img_data = img[1] snapshot_filename = "%s_%s_%s.snapshot.jpeg" % ( file_parameters["prefix"], file_parameters["run_number"], snapshot_i, ) full_snapshot = os.path.join(snapshot_directory, snapshot_filename) try: f = open(full_snapshot, "w") logging.getLogger("user_level_log").info( "Saving snapshot %d", snapshot_i ) f.write(img_data) except BaseException: logging.getLogger("HWR").exception("Could not save snapshot!") try: f.close() except BaseException: pass data_collect_parameters[ "xtalSnapshotFullPath%i" % snapshot_i ] = full_snapshot snapshots.append(full_snapshot) snapshot_i += 1 try: data_collect_parameters["centeringMethod"] = centring_info["method"] except BaseException: data_collect_parameters["centeringMethod"] = None if self.bl_control.lims: try: logging.getLogger("user_level_log").info( "Updating data collection in LIMS" ) if "kappa" in data_collect_parameters["actualCenteringPosition"]: data_collect_parameters["oscillation_sequence"][0][ "kappaStart" ] = current_diffractometer_position["kappa"] data_collect_parameters["oscillation_sequence"][0][ "phiStart" ] = current_diffractometer_position["kappa_phi"] self.bl_control.lims.update_data_collection(data_collect_parameters) except BaseException: logging.getLogger("HWR").exception( "Could not update data collection in LIMS" ) oscillation_parameters = data_collect_parameters["oscillation_sequence"][0] sample_id = data_collect_parameters["blSampleId"] subwedge_size = oscillation_parameters.get("reference_interval", 1) # if data_collect_parameters["shutterless"]: # subwedge_size = 1 # else: # subwedge_size = oscillation_parameters["number_of_images"] wedges_to_collect = self.prepare_wedges_to_collect( oscillation_parameters["start"], oscillation_parameters["number_of_images"], oscillation_parameters["range"], subwedge_size, oscillation_parameters["overlap"], ) nframes = sum([wedge_size for _, wedge_size in wedges_to_collect]) # Added exposure time for ProgressBarBrick. # Extra time for each collection needs to be added (in this case 0.04) self.emit( "collectNumberOfFrames", nframes, oscillation_parameters["exposure_time"] + 0.04, ) start_image_number = oscillation_parameters["start_image_number"] last_frame = start_image_number + nframes - 1 if data_collect_parameters["skip_images"]: for start, wedge_size in wedges_to_collect[:]: filename = image_file_template % start_image_number file_location = file_parameters["directory"] file_path = os.path.join(file_location, filename) if os.path.isfile(file_path): logging.info("Skipping existing image %s", file_path) del wedges_to_collect[0] start_image_number += wedge_size nframes -= wedge_size else: # images have to be consecutive break if nframes == 0: return # data collection self.first_image_timeout = 30 + oscillation_parameters["exposure_time"] self.data_collection_hook(data_collect_parameters) if "transmission" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting transmission to %f", data_collect_parameters["transmission"] ) self.set_transmission(data_collect_parameters["transmission"]) if "wavelength" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting wavelength to %f", data_collect_parameters["wavelength"] ) self.set_wavelength(data_collect_parameters["wavelength"]) elif "energy" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting energy to %f", data_collect_parameters["energy"] ) self.set_energy(data_collect_parameters["energy"]) if "resolution" in data_collect_parameters: resolution = data_collect_parameters["resolution"]["upper"] logging.getLogger("user_level_log").info( "Setting resolution to %f", resolution ) self.set_resolution(resolution) elif "detdistance" in oscillation_parameters: logging.getLogger("user_level_log").info( "Moving detector to %f", data_collect_parameters["detdistance"] ) self.move_detector(oscillation_parameters["detdistance"]) # 0: software binned, 1: unbinned, 2:hw binned self.set_detector_mode(data_collect_parameters["detector_mode"]) with cleanup(self.data_collection_cleanup): if not self.safety_shutter_opened(): logging.getLogger("user_level_log").info("Opening safety shutter") self.open_safety_shutter(timeout=10) logging.getLogger("user_level_log").info("Preparing intensity monitors") self.prepare_intensity_monitors() frame = start_image_number osc_range = oscillation_parameters["range"] exptime = oscillation_parameters["exposure_time"] npass = oscillation_parameters["number_of_passes"] # update LIMS if self.bl_control.lims: try: logging.getLogger("user_level_log").info( "Gathering data for LIMS update" ) data_collect_parameters["flux"] = self.get_flux() data_collect_parameters["flux_end"] = data_collect_parameters[ "flux" ] data_collect_parameters["wavelength"] = self.get_wavelength() data_collect_parameters[ "detectorDistance" ] = self.get_detector_distance() data_collect_parameters["resolution"] = self.get_resolution() data_collect_parameters["transmission"] = self.get_transmission() beam_centre_x, beam_centre_y = self.get_beam_centre() data_collect_parameters["xBeam"] = beam_centre_x data_collect_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: data_collect_parameters["undulatorGap%d" % (i)] = und[key] i += 1 data_collect_parameters[ "resolutionAtCorner" ] = self.get_resolution_at_corner() beam_size_x, beam_size_y = self.get_beam_size() data_collect_parameters["beamSizeAtSampleX"] = beam_size_x data_collect_parameters["beamSizeAtSampleY"] = beam_size_y data_collect_parameters["beamShape"] = self.get_beam_shape() hor_gap, vert_gap = self.get_slit_gaps() data_collect_parameters["slitGapHorizontal"] = hor_gap data_collect_parameters["slitGapVertical"] = vert_gap logging.getLogger("user_level_log").info( "Updating data collection in LIMS" ) self.bl_control.lims.update_data_collection( data_collect_parameters, wait=True ) logging.getLogger("user_level_log").info( "Done updating data collection in LIMS" ) except BaseException: logging.getLogger("HWR").exception( "Could not store data collection into LIMS" ) if self.bl_control.lims and self.bl_config.input_files_server: logging.getLogger("user_level_log").info( "Asking for input files writing" ) self.write_input_files(self.collection_id, wait=False) # at this point input files should have been written # TODO aggree what parameters will be sent to this function if data_collect_parameters.get("processing", False) == "True": self.trigger_auto_processing( "before", self.xds_directory, data_collect_parameters["EDNA_files_dir"], data_collect_parameters["anomalous"], data_collect_parameters["residues"], data_collect_parameters["do_inducedraddam"], data_collect_parameters.get("sample_reference", {}).get( "spacegroup", "" ), data_collect_parameters.get("sample_reference", {}).get("cell", ""), ) if self.run_without_loop: self.execute_collect_without_loop(data_collect_parameters) else: for start, wedge_size in wedges_to_collect: logging.getLogger("user_level_log").info( "Preparing acquisition, start=%f, wedge size=%d", start, wedge_size, ) self.prepare_acquisition( 1 if data_collect_parameters.get("dark", 0) else 0, start, osc_range, exptime, npass, wedge_size, data_collect_parameters["comment"], ) data_collect_parameters["dark"] = 0 i = 0 j = wedge_size while j > 0: frame_start = start + i * osc_range i += 1 filename = image_file_template % frame try: jpeg_full_path = jpeg_file_template % frame jpeg_thumbnail_full_path = ( jpeg_thumbnail_file_template % frame ) except BaseException: jpeg_full_path = None jpeg_thumbnail_full_path = None file_location = file_parameters["directory"] file_path = os.path.join(file_location, filename) self.set_detector_filenames( frame, frame_start, str(file_path), str(jpeg_full_path), str(jpeg_thumbnail_full_path), ) osc_start, osc_end = self.prepare_oscillation( frame_start, osc_range, exptime, npass ) with error_cleanup(self.reset_detector): self.start_acquisition(exptime, npass, j == wedge_size) self.do_oscillation(osc_start, osc_end, exptime, npass) self.stop_acquisition() self.write_image(j == 1) # Store image in lims if self.bl_control.lims: if self.store_image_in_lims(frame, j == wedge_size, j == 1): lims_image = { "dataCollectionId": self.collection_id, "fileName": filename, "fileLocation": file_location, "imageNumber": frame, "measuredIntensity": self.get_measured_intensity(), "synchrotronCurrent": self.get_machine_current(), "machineMessage": self.get_machine_message(), "temperature": self.get_cryo_temperature(), } if archive_directory: lims_image["jpegFileFullPath"] = jpeg_full_path lims_image[ "jpegThumbnailFileFullPath" ] = jpeg_thumbnail_full_path try: self.bl_control.lims.store_image(lims_image) except BaseException: logging.getLogger("HWR").exception( "Could not store store image in LIMS" ) self.generate_image_jpeg( str(file_path), str(jpeg_full_path), str(jpeg_thumbnail_full_path), wait=False, ) if data_collect_parameters.get("processing", False) == "True": self.trigger_auto_processing( "image", self.xds_directory, data_collect_parameters["EDNA_files_dir"], data_collect_parameters["anomalous"], data_collect_parameters["residues"], data_collect_parameters["do_inducedraddam"], data_collect_parameters.get("sample_reference", {}).get( "spacegroup", "" ), data_collect_parameters.get("sample_reference", {}).get( "cell", "" ), ) if data_collect_parameters.get("shutterless"): with gevent.Timeout( self.first_image_timeout, RuntimeError( "Timeout waiting for detector trigger, no image taken" ), ): while self.last_image_saved() == 0: time.sleep(exptime) last_image_saved = self.last_image_saved() if last_image_saved < wedge_size: time.sleep(exptime * wedge_size / 100.0) last_image_saved = self.last_image_saved() frame = max( start_image_number + 1, start_image_number + last_image_saved - 1, ) self.emit("collectImageTaken", frame) j = wedge_size - last_image_saved else: j -= 1 self.emit("collectImageTaken", frame) frame += 1 if j == 0: break # Bug fix for MD2/3(UP): diffractometer still has things to do even after the last frame is taken (decelerate motors and # possibly download diagnostics) so we cannot trigger the cleanup (that will send an abort on the diffractometer) as soon as # the last frame is counted self.diffractometer().wait_ready(10) # data collection done self.data_collection_end_hook(data_collect_parameters)
def do_collect(self, owner, data_collect_parameters): if self.__safety_shutter_close_task is not None: self.__safety_shutter_close_task.kill() logging.getLogger("user_level_log").info("Closing fast shutter") self.close_fast_shutter() # reset collection id on each data collect self.collection_id = None # Preparing directory path for images and processing files # creating image file template and jpegs files templates file_parameters = data_collect_parameters["fileinfo"] file_parameters["suffix"] = self.bl_config.detector_fileext image_file_template = ( "%(prefix)s_%(run_number)s_%%04d.%(suffix)s" % file_parameters ) file_parameters["template"] = image_file_template archive_directory = self.get_archive_directory(file_parameters["directory"]) data_collect_parameters["archive_dir"] = 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 ) else: jpeg_file_template = None jpeg_thumbnail_file_template = None # database filling if HWR.beamline.lims: data_collect_parameters["collection_start_time"] = time.strftime( "%Y-%m-%d %H:%M:%S" ) if HWR.beamline.machine_info is not None: logging.getLogger("user_level_log").info( "Getting synchrotron filling mode" ) data_collect_parameters[ "synchrotronMode" ] = self.get_machine_fill_mode() data_collect_parameters["status"] = "failed" logging.getLogger("user_level_log").info("Storing data collection in LIMS") (self.collection_id, detector_id) = HWR.beamline.lims.store_data_collection( data_collect_parameters, self.bl_config ) data_collect_parameters["collection_id"] = self.collection_id if detector_id: data_collect_parameters["detector_id"] = detector_id # Creating the directory for images and processing information logging.getLogger("user_level_log").info( "Creating directory for images and processing" ) self.create_directories( file_parameters["directory"], file_parameters["process_directory"] ) ( self.xds_directory, self.mosflm_directory, self.hkl2000_directory, ) = self.prepare_input_files( file_parameters["directory"], file_parameters["prefix"], file_parameters["run_number"], file_parameters["process_directory"], ) data_collect_parameters["xds_dir"] = self.xds_directory logging.getLogger("user_level_log").info("Getting sample info from parameters") sample_id, sample_location, sample_code = self.get_sample_info_from_parameters( data_collect_parameters ) data_collect_parameters["blSampleId"] = sample_id if HWR.beamline.sample_changer is not None: try: data_collect_parameters[ "actualSampleBarcode" ] = HWR.beamline.sample_changer.get_loaded_sample().get_id() data_collect_parameters["actualContainerBarcode"] = ( HWR.beamline.sample_changer.get_loaded_sample() .get_container() .get_id() ) logging.getLogger("user_level_log").info("Getting loaded sample coords") ( basket, vial, ) = HWR.beamline.sample_changer.get_loaded_sample().get_coords() data_collect_parameters["actualSampleSlotInContainer"] = vial data_collect_parameters["actualContainerSlotInSC"] = basket except Exception: data_collect_parameters["actualSampleBarcode"] = None data_collect_parameters["actualContainerBarcode"] = None else: data_collect_parameters["actualSampleBarcode"] = None data_collect_parameters["actualContainerBarcode"] = None centring_info = {} try: logging.getLogger("user_level_log").info("Getting centring status") centring_status = self.diffractometer().get_centring_status() except Exception: pass else: centring_info = dict(centring_status) # Save sample centring positions positions_str = "" motors = centring_info.get( "motors", {} ) # .update(centring_info.get("extraMotors", {})) motors_to_move_before_collect = data_collect_parameters.setdefault("motors", {}) for motor, pos in motors.items(): if motor in motors_to_move_before_collect: continue motors_to_move_before_collect[motor] = pos current_diffractometer_position = self.diffractometer().get_positions() for motor in motors_to_move_before_collect.keys(): if motors_to_move_before_collect[motor] is not None: try: if current_diffractometer_position[motor] is not None: positions_str += "%s=%f " % ( motor, current_diffractometer_position[motor], ) except Exception: logging.getLogger("HWR").exception("") positions_str = "" for motor, pos in motors_to_move_before_collect.items(): if pos is not None and motor is not None: positions_str += motor + ("=%f" % pos) + " " data_collect_parameters["actualCenteringPosition"] = positions_str.strip() self.move_motors(motors_to_move_before_collect) HWR.beamline.diffractometer.save_centring_positions() # take snapshots, then assign centring status (which contains images) to # centring_info variable take_snapshots = data_collect_parameters.get("take_snapshots", False) if take_snapshots: logging.getLogger("user_level_log").info("Taking sample snapshosts") self._take_crystal_snapshots(take_snapshots) centring_info = HWR.beamline.diffractometer.get_centring_status() # move *again* motors, since taking snapshots may change positions logging.getLogger("user_level_log").info( "Moving motors: %r", motors_to_move_before_collect ) self.move_motors(motors_to_move_before_collect) if HWR.beamline.lims: try: if self.current_lims_sample: self.current_lims_sample[ "lastKnownCentringPosition" ] = positions_str logging.getLogger("user_level_log").info( "Updating sample information in LIMS" ) HWR.beamline.lims.update_bl_sample(self.current_lims_sample) except Exception: logging.getLogger("HWR").exception( "Could not update sample information in LIMS" ) if centring_info.get("images"): # Save snapshots snapshot_directory = self.get_archive_directory( file_parameters["directory"] ) try: logging.getLogger("user_level_log").info( "Creating snapshosts directory: %r", snapshot_directory ) self.create_directories(snapshot_directory) except Exception: logging.getLogger("HWR").exception("Error creating snapshot directory") else: snapshot_i = 1 snapshots = [] for img in centring_info["images"]: img_phi_pos = img[0] img_data = img[1] snapshot_filename = "%s_%s_%s.snapshot.jpeg" % ( file_parameters["prefix"], file_parameters["run_number"], snapshot_i, ) full_snapshot = os.path.join(snapshot_directory, snapshot_filename) try: f = open(full_snapshot, "w") logging.getLogger("user_level_log").info( "Saving snapshot %d", snapshot_i ) f.write(img_data) except Exception: logging.getLogger("HWR").exception("Could not save snapshot!") try: f.close() except Exception: pass data_collect_parameters[ "xtalSnapshotFullPath%i" % snapshot_i ] = full_snapshot snapshots.append(full_snapshot) snapshot_i += 1 try: data_collect_parameters["centeringMethod"] = centring_info["method"] except Exception: data_collect_parameters["centeringMethod"] = None if HWR.beamline.lims: try: logging.getLogger("user_level_log").info( "Updating data collection in LIMS" ) if "kappa" in data_collect_parameters["actualCenteringPosition"]: data_collect_parameters["oscillation_sequence"][0][ "kappaStart" ] = current_diffractometer_position["kappa"] data_collect_parameters["oscillation_sequence"][0][ "phiStart" ] = current_diffractometer_position["kappa_phi"] HWR.beamline.lims.update_data_collection(data_collect_parameters) except Exception: logging.getLogger("HWR").exception( "Could not update data collection in LIMS" ) oscillation_parameters = data_collect_parameters["oscillation_sequence"][0] sample_id = data_collect_parameters["blSampleId"] subwedge_size = oscillation_parameters.get("reference_interval", 1) # if data_collect_parameters["shutterless"]: # subwedge_size = 1 # else: # subwedge_size = oscillation_parameters["number_of_images"] wedges_to_collect = self.prepare_wedges_to_collect( oscillation_parameters["start"], oscillation_parameters["number_of_images"], oscillation_parameters["range"], subwedge_size, oscillation_parameters["overlap"], ) nframes = sum([wedge_size for _, wedge_size in wedges_to_collect]) # Added exposure time for ProgressBarBrick. # Extra time for each collection needs to be added (in this case 0.04) self.emit( "collectNumberOfFrames", nframes, oscillation_parameters["exposure_time"] + 0.04, ) start_image_number = oscillation_parameters["start_image_number"] last_frame = start_image_number + nframes - 1 if data_collect_parameters["skip_images"]: for start, wedge_size in wedges_to_collect[:]: filename = image_file_template % start_image_number file_location = file_parameters["directory"] file_path = os.path.join(file_location, filename) if os.path.isfile(file_path): logging.info("Skipping existing image %s", file_path) del wedges_to_collect[0] start_image_number += wedge_size nframes -= wedge_size else: # images have to be consecutive break if nframes == 0: return # data collection self.first_image_timeout = 30 + oscillation_parameters["exposure_time"] self.data_collection_hook(data_collect_parameters) if "transmission" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting transmission to %f", data_collect_parameters["transmission"] ) HWR.beamline.transmission.set_value(data_collect_parameters["transmission"], timeout=30) if "wavelength" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting wavelength to %f", data_collect_parameters["wavelength"] ) HWR.beamline.energy.set_wavelength(data_collect_parameters["wavelength"], timeout=None) elif "energy" in data_collect_parameters: logging.getLogger("user_level_log").info( "Setting energy to %f", data_collect_parameters["energy"] ) HWR.beamline.energy.set_value(data_collect_parameters["energy"], timeout=None) if "resolution" in data_collect_parameters: resolution = data_collect_parameters["resolution"]["upper"] logging.getLogger("user_level_log").info( "Setting resolution to %f", resolution ) HWR.beamline.resolution.set_value(resolution, timeout=3500) elif "detector_distance" in oscillation_parameters: logging.getLogger("user_level_log").info( "Moving detector to %f", data_collect_parameters["detector_distance"] ) HWR.beamline.detector.distance.set_value( oscillation_parameters["detector_distance"], timeout=3500 ) # 0: software binned, 1: unbinned, 2:hw binned # self.set_detector_mode(data_collect_parameters["detector_mode"]) with cleanup(self.data_collection_cleanup): if not self.safety_shutter_opened(): logging.getLogger("user_level_log").info("Opening safety shutter") self.open_safety_shutter() flux_threshold = self.getProperty("flux_threshold", False) cryo_threshold = self.getProperty("cryo_threshold", False) # Wait for flux while flux_threshold and HWR.beamline.flux.get_value() < flux_threshold: logging.getLogger("user_level_log").info("Waiting for beam ...") gevent.sleep(0.5) # Wait for cryo while cryo_threshold and HWR.beamline.diffractometer.cryostream.get_value() > cryo_threshold: logging.getLogger("user_level_log").info("Cryo temperature too high ...") gevent.sleep(0.5) logging.getLogger("user_level_log").info("Preparing intensity monitors") self.prepare_intensity_monitors() frame = start_image_number osc_range = oscillation_parameters["range"] exptime = oscillation_parameters["exposure_time"] npass = oscillation_parameters["number_of_passes"] # update LIMS if HWR.beamline.lims: try: logging.getLogger("user_level_log").info( "Gathering data for LIMS update" ) data_collect_parameters["flux"] = HWR.beamline.flux.get_value() data_collect_parameters["flux_end"] = data_collect_parameters[ "flux" ] data_collect_parameters[ "wavelength" ] = HWR.beamline.energy.get_wavelength() data_collect_parameters[ "detectorDistance" ] = HWR.beamline.detector.distance.get_value() data_collect_parameters[ "resolution" ] = HWR.beamline.resolution.get_value() data_collect_parameters[ "transmission" ] = HWR.beamline.transmission.get_value() beam_centre_x, beam_centre_y = self.get_beam_centre() data_collect_parameters["xBeam"] = beam_centre_x data_collect_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: data_collect_parameters["undulatorGap%d" % (i)] = und[key] i += 1 data_collect_parameters[ "resolutionAtCorner" ] = self.get_resolution_at_corner() beam_size_x, beam_size_y = self.get_beam_size() data_collect_parameters["beamSizeAtSampleX"] = beam_size_x data_collect_parameters["beamSizeAtSampleY"] = beam_size_y data_collect_parameters["beamShape"] = self.get_beam_shape() hor_gap, vert_gap = self.get_slit_gaps() data_collect_parameters["slitGapHorizontal"] = hor_gap data_collect_parameters["slitGapVertical"] = vert_gap logging.getLogger("user_level_log").info( "Updating data collection in LIMS" ) HWR.beamline.lims.update_data_collection( data_collect_parameters, wait=True ) logging.getLogger("user_level_log").info( "Done updating data collection in LIMS" ) except Exception: logging.getLogger("HWR").exception( "Could not store data collection into LIMS" ) if HWR.beamline.lims and self.bl_config.input_files_server: logging.getLogger("user_level_log").info( "Asking for input files writing" ) self.write_input_files(self.collection_id, wait=False) # at this point input files should have been written # TODO aggree what parameters will be sent to this function if data_collect_parameters.get("processing", False) == "True": self.trigger_auto_processing( "before", self.xds_directory, data_collect_parameters["EDNA_files_dir"], data_collect_parameters["anomalous"], data_collect_parameters["residues"], data_collect_parameters["do_inducedraddam"], data_collect_parameters.get("sample_reference", {}).get( "spacegroup", "" ), data_collect_parameters.get("sample_reference", {}).get("cell", ""), ) if self.run_without_loop: self.execute_collect_without_loop(data_collect_parameters) else: for start, wedge_size in wedges_to_collect: logging.getLogger("user_level_log").info( "Preparing acquisition, start=%f, wedge size=%d", start, wedge_size, ) self.prepare_acquisition( 1 if data_collect_parameters.get("dark", 0) else 0, start, osc_range, exptime, npass, wedge_size, data_collect_parameters.get("comment", ""), ) data_collect_parameters["dark"] = 0 i = 0 j = wedge_size while j > 0: frame_start = start + i * osc_range i += 1 filename = image_file_template % frame try: jpeg_full_path = jpeg_file_template % frame jpeg_thumbnail_full_path = ( jpeg_thumbnail_file_template % frame ) except Exception: jpeg_full_path = None jpeg_thumbnail_full_path = None file_location = file_parameters["directory"] file_path = os.path.join(file_location, filename) self.set_detector_filenames( frame, frame_start, str(file_path), data_collect_parameters.get("shutterless", True), wait=False, ) osc_start, osc_end = self.prepare_oscillation( frame_start, osc_range, exptime, wedge_size, data_collect_parameters.get("shutterless", True), npass, j == wedge_size, ) with error_cleanup(self.reset_detector): self.start_acquisition( exptime, npass, j == wedge_size, data_collect_parameters.get("shutterless", True), ) self.do_oscillation( osc_start, osc_end, exptime, wedge_size, data_collect_parameters.get("shutterless", True), npass, j == wedge_size, ) self.write_image(j == 1) # Store image in lims if HWR.beamline.lims: if self.store_image_in_lims(frame, j == wedge_size, j == 1): lims_image = { "dataCollectionId": self.collection_id, "fileName": filename, "fileLocation": file_location, "imageNumber": frame, "measuredIntensity": HWR.beamline.flux.get_value(), "synchrotronCurrent": self.get_machine_current(), "machineMessage": self.get_machine_message(), "temperature": self.get_cryo_temperature(), } if archive_directory: lims_image["jpegFileFullPath"] = jpeg_full_path lims_image[ "jpegThumbnailFileFullPath" ] = jpeg_thumbnail_full_path try: HWR.beamline.lims.store_image(lims_image) except Exception: logging.getLogger("HWR").exception( "Could not store store image in LIMS" ) self.generate_image_jpeg( str(file_path), str(jpeg_full_path), str(jpeg_thumbnail_full_path), wait=False, ) if data_collect_parameters.get("processing", False) == "True": self.trigger_auto_processing( "image", self.xds_directory, data_collect_parameters["EDNA_files_dir"], data_collect_parameters["anomalous"], data_collect_parameters["residues"], data_collect_parameters["do_inducedraddam"], data_collect_parameters.get("sample_reference", {}).get( "spacegroup", "" ), data_collect_parameters.get("sample_reference", {}).get( "cell", "" ), ) if data_collect_parameters.get("shutterless"): with gevent.Timeout( self.first_image_timeout, RuntimeError( "Timeout waiting for detector trigger, no image taken" ), ): while self.last_image_saved() == 0: time.sleep(exptime) last_image_saved = self.last_image_saved() if last_image_saved < wedge_size: time.sleep(exptime) last_image_saved = self.last_image_saved() frame = max( start_image_number + 1, start_image_number + last_image_saved - 1, ) self.emit("collectImageTaken", frame) j = wedge_size - last_image_saved else: j -= 1 self.emit("collectImageTaken", frame) frame += 1 if j == 0: break # Bug fix for MD2/3(UP): diffractometer still has things to do even after the last frame is taken (decelerate motors and # possibly download diagnostics) so we cannot trigger the cleanup (that will send an abort on the diffractometer) as soon as # the last frame is counted self.diffractometer().wait_ready(10) # data collection done self.data_collection_end_hook(data_collect_parameters)
def startXrfSpectrum(self, ct, directory, arch_dir, prefix, session_id=None, blsample_id=None): s1 = self.gaussian(numpy.linspace(0, 3, SCAN_LENGTH), 1, 0.1) s2 = self.gaussian(numpy.linspace(0, 3, SCAN_LENGTH), 2, 0.3) * (0.25 * numpy.random.rand()) raw_data = s1 + s2 logging.getLogger("HWR").info("XRF Spectrum Started, task id: %d" % blsample_id) self.scanning = True self.emit("xrfSpectrumStarted", ()) with cleanup(self.ready_event.set): self.spectrumInfo["sessionId"] = session_id self.spectrumInfo["blSampleId"] = blsample_id if blsample_id is None: blsample_id = numpy.random.randint(1, 999999) scan_info = { "scan_nb": blsample_id, "title": "XRF Scan", "labels": ["energy", "diode value"], } scan_id = scan_info["scan_nb"] self.__scan_data[scan_id] = list() self.emit( "new_plot", { "id": scan_info["scan_nb"], "title": scan_info["title"], "labels": scan_info["labels"], }, ) for i in range(SCAN_LENGTH): try: data = {"energy": i, "diode value": raw_data[i]} new_data = numpy.column_stack( [data[name] for name in scan_info["labels"]]) self.__scan_data[scan_id].append(new_data) aux = numpy.concatenate(self.__scan_data[scan_id]).tolist() self.emit("plot_data", {"id": scan_id, "data": aux}) if divmod(i, SCAN_LENGTH / 10)[1] == 0: progress = i / float(SCAN_LENGTH) logging.getLogger("HWR").info( "XRF Spectrum Progress %f" % progress) self.emit("xrf_task_progress", (blsample_id, progress)) gevent.sleep(0.02) except Exception as ex: print(("Exception ", ex)) self.emit( "plot_end", { "id": scan_id, "data": numpy.concatenate( self.__scan_data[scan_id]).tolist(), "type": "XRFScan", }, ) mcaCalib = [10, 1, 21, 0] mcaConfig = {} mcaConfig["legend"] = "XRF test scan from XRF mockup" mcaConfig["htmldir"] = "html dir not defined" mcaConfig["min"] = raw_data[0] mcaConfig["max"] = raw_data[-1] mcaConfig["file"] = None res = [] for arr in self.__scan_data[scan_id]: res.append(arr[0].tolist()) self.emit("xrfSpectrumFinished", (res, mcaCalib, mcaConfig)) logging.getLogger("HWR").info("XRF Spectrum Finished") del self.__scan_data[scan_id]
def spectrum_command_finished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") self.spectrum_running = False xmin = 0 xmax = 0 mca_data = [] calibrated_data = [] try: spectrum_file_raw = open(self.spectrum_info["scanFilePath"], "w") except BaseException: logging.getLogger("HWR").exception( "XRFSpectrum: could not create spectrum result raw file %s" % self.spectrum_info["scanFilePath"]) try: archive_file_raw = open(self.spectrum_info["scanFileFullPath"], "w") except BaseException: logging.getLogger("HWR").exception( "XRFSpectrum: could not create spectrum result raw file %s" % self.spectrum_info["scanFileFullPath"]) for n, value in enumerate(self.spectrum_data): energy = (self.mca_calib[2] + self.mca_calib[1] * n + self.mca_calib[0] * n * n) / 1000 if energy < 20: if energy > xmax: xmax = value if energy < xmin: xmin = value calibrated_data.append([energy, value]) mca_data.append((n / 1000.0, value)) if spectrum_file_raw: spectrum_file_raw.write("%f,%f\r\n" % (energy, value)) if archive_file_raw: archive_file_raw.write("%f,%f\r\n" % (energy, value)) if spectrum_file_raw: spectrum_file_raw.close() if archive_file_raw: archive_file_raw.close() calibrated_array = numpy.array(calibrated_data) if self.transmission_hwobj is not None: self.spectrum_info[ "beamTransmission"] = self.transmission_hwobj.getAttFactor( ) self.spectrum_info["energy"] = self.get_current_energy() if self.beam_info_hwobj is not None: beam_size = self.beam_info_hwobj.get_beam_size() self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] * 1000) self.spectrum_info["beamSizeVertical"] = int(beam_size[1] * 1000) mca_config = {} mca_config["legend"] = self.spectrum_info["filename"] mca_config["file"] = self.config_filename mca_config["min"] = xmin mca_config["max"] = xmax mca_config["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") self.spectrum_info.pop("scanFilePath") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color": "black"}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info( "XRFSpectrum: Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"], ) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi=80) # logging.getLogger().debug("Copying .fit file to: %s", a_dir) # tmpname=filename.split(".") # logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit("xrfSpectrumFinished", (mca_data, self.mca_calib, mca_config))
def startXrfSpectrum( self, ct, directory, arch_dir, prefix, session_id=None, blsample_id=None ): s1 = self.gaussian(numpy.linspace(0, 3, SCAN_LENGTH), 1, 0.1) s2 = self.gaussian(numpy.linspace(0, 3, SCAN_LENGTH), 2, 0.3) * ( 0.25 * numpy.random.rand() ) raw_data = s1 + s2 logging.getLogger("HWR").info("XRF Spectrum Started, task id: %d" % blsample_id) self.scanning = True self.emit("xrfSpectrumStarted", ()) with cleanup(self.ready_event.set): self.spectrumInfo["sessionId"] = session_id self.spectrumInfo["blSampleId"] = blsample_id if blsample_id is None: blsample_id = numpy.random.randint(1, 999999) scan_info = { "scan_nb": blsample_id, "title": "XRF Scan", "labels": ["energy", "diode value"], } scan_id = scan_info["scan_nb"] self.__scan_data[scan_id] = list() self.emit( "new_plot", { "id": scan_info["scan_nb"], "title": scan_info["title"], "labels": scan_info["labels"], }, ) for i in range(SCAN_LENGTH): try: data = {"energy": i, "diode value": raw_data[i]} new_data = numpy.column_stack( [data[name] for name in scan_info["labels"]] ) self.__scan_data[scan_id].append(new_data) aux = numpy.concatenate(self.__scan_data[scan_id]).tolist() self.emit("plot_data", {"id": scan_id, "data": aux}) if divmod(i, SCAN_LENGTH / 10)[1] == 0: progress = i / float(SCAN_LENGTH) logging.getLogger("HWR").info( "XRF Spectrum Progress %f" % progress ) self.emit("xrf_task_progress", (blsample_id, progress)) gevent.sleep(0.02) except Exception as ex: print("Exception ", ex) self.emit( "plot_end", { "id": scan_id, "data": numpy.concatenate(self.__scan_data[scan_id]).tolist(), "type": "XRFScan", }, ) mcaCalib = [10, 1, 21, 0] mcaConfig = {} mcaConfig["legend"] = "XRF test scan from XRF mockup" mcaConfig["htmldir"] = "html dir not defined" mcaConfig["min"] = raw_data[0] mcaConfig["max"] = raw_data[-1] mcaConfig["file"] = None res = [] for arr in self.__scan_data[scan_id]: res.append(arr[0].tolist()) self.emit("xrfSpectrumFinished", (res, mcaCalib, mcaConfig)) logging.getLogger("HWR").info("XRF Spectrum Finished") del self.__scan_data[scan_id]