def set_exposure(self, new_exposure, from_experiment=False): """ Tries to set new detector exposure :arg: 'exposure' - value of new exposure, in '?', type is int :return: """ self.logger.info('Going to set exposure...') self.basic_tomo_check(from_experiment) if type(new_exposure) not in (int, float): raise ModExpError( error='Incorrect type! Exposure type must be int, but it is ' + str(type(new_exposure))) # TO DELETE THIS LATER if new_exposure < 0.1 or 16000 < new_exposure: raise ModExpError(error=( 'Exposure must have value from 0.1 to 16000 (given is %.1f )' % new_exposure)) new_exposure = round(new_exposure) # TO DELETE THIS LATER self.logger.info('Setting value %.1f...' % new_exposure) set_exposure = self.try_thrice_change_attr( "exposure", new_exposure, error_str='Could not set new exposure because of tomograph') self.logger.info('Exposure was set!')
def set_y(self, new_y, from_experiment=False): """ Tries to set new vertical position of object :arg: 'new_y' - value of new vertical position, in 'popugaychiki', type is int :return: """ self.logger.info('Going to set new vertical position...') self.basic_tomo_check(from_experiment) if type(new_y) not in (int, float): raise ModExpError( error='Incorrect type! Position type must be int, but it is ' + str(type(new_y))) # TO DELETE THIS LATER self.logger.info('Setting value %.1f...' % new_y) if new_y < -5000 or 2000 < new_y: raise ModExpError(error='Position must have value from -30 to 30') set_y = self.try_thrice_change_attr( "vertical_position", new_y, error_str='Could not set new position because of tomograph') self.logger.info('Position was set!')
def set_x(self, new_x, from_experiment=False): """ Tries to set new horizontal position of object :arg: :return: """ self.logger.info('Going to set new horizontal position...') self.basic_tomo_check(from_experiment) if type(new_x) not in (int, float): raise ModExpError( error='Incorrect type! Position type must be int, but it is ' + str(type(new_x))) # TO DELETE THIS LATER self.logger.info('Setting value %.1f...' % new_x) if new_x < -5000 or 2000 < new_x: raise ModExpError( error='Position must have value from -5000 to 2000') set_x = self.try_thrice_change_attr( "horizontal_position", new_x, error_str='Could not set new position because of tomograph') self.logger.info('Position was set!')
def set_angle(self, new_angle, from_experiment=False): """ Tries to set new angle position of object :arg: 'new_angle' - value of new angle position, in 'grades', type is float :return: """ self.logger.info('Going to set new angle position...') self.basic_tomo_check(from_experiment) if type(new_angle) not in (int, float): raise ModExpError( error= 'Incorrect type! Position type must be int or float, but it is ' + str(type(new_angle))) # TO DELETE THIS LATER self.logger.info('Setting value %.1f...' % new_angle) new_angle %= 360 set_angle = self.try_thrice_change_attr( "angle_position", new_angle, error_str='Could not set new position because of tomograph') self.logger.info('Position was set!')
def try_thrice_function(func, args=(), error_str='Error during trying thrice function'): """ Tries to call some TANGO function three times :arg: 'func' - called function 'args' - function 'func' is being called with arguments 'args' :return: """ logger = get_logger() if type(args) not in (tuple, list): args = (args, ) exception_message = '' for i in range(0, 3): try: answer = func(*args) except PyTango.DevFailed as e: # (24.11.15) Not very mature place: logging exception description before logging common # error info (error_str), which will be logged after catching ModExpError exception for stage in e: logger.info(stage.desc) exception_message = e[-1].desc except Exception as e: logger.info(e.message) # Can be problems with converting to JSON e.message exception_message = e.message else: return answer raise ModExpError(error=error_str, exception_message=exception_message)
def basic_tomo_check(self, from_experiment): if not from_experiment: if self.current_experiment is not None: raise ModExpError( error='On this tomograph experiment is running') else: if self.current_experiment.to_be_stopped: # someone called experiment_stop() function raise self.current_experiment.stop_exception
def get_frame(self, exposure, with_open_shutter, send_to_webpage=False, from_experiment=False): """ Tries get frame with some exposure :arg: 'exposure' - exposure, which frame should get with :return: """ self.logger.info('Going to get image...') self.logger.info('With open shutter: ' + str(with_open_shutter)) self.basic_tomo_check(from_experiment) # if with_open_shutter == True: # self.open_shutter(0, from_experiment=from_experiment, exp_is_advanced=exp_is_advanced) time.sleep(0.2) # Tomograph takes exposure multiplied by 10 and rounded if exposure: self.logger.info( 'Getting an image with exposure %.1f milliseconds...' % exposure) self.set_exposure(exposure, from_experiment=from_experiment) if with_open_shutter: self.open_shutter(from_experiment=from_experiment) else: self.close_shutter(from_experiment=from_experiment) try: frame_metadata_json = try_thrice_function( func=self.tomograph_proxy.GetFrame, error_str='Could not get image because of tomograph') except Exception as e: raise e finally: self.close_shutter(from_experiment=from_experiment) try: frame_metadata = json.loads(frame_metadata_json) except TypeError: raise ModExpError( error='Could not convert frame\'s JSON into dict') self.logger.info('Image was get, reading the image from detector...') raw_image = self.try_thrice_read_attr_detector( "image", extract_as=PyTango.ExtractAs.Nothing, error_str='Could not read image because of tomograph') frame_metadata['image_data']['raw_image'] = raw_image raw_image_with_metadata = frame_metadata return raw_image_with_metadata
def source_set_current(self, new_current, from_experiment=False): self.logger.info('Going to set current on source...') self.basic_tomo_check(from_experiment=from_experiment) self.logger.info('Checking format...') if type(new_current) is not float: self.logger.info( 'Incorrect format! Current type must be float, but it is ' + str(type(new_current))) raise ModExpError(error='Incorrect format: type must be float') # TO DELETE THIS LATER self.logger.info('Format is correct, new current value is %.1f...' % new_current) if new_current < 2 or 80 < new_current: raise ModExpError(error='Current must have value from 2 to 80!') self.logger.info('Parameters are normal, setting new current...') set_current = self.try_thrice_change_attr( "xraysource_current", new_current, error_str='Could not set current') self.logger.info('New value of current was set!')
def source_set_voltage(self, new_voltage, from_experiment=False): self.logger.info('Going to set voltage on source...') self.basic_tomo_check(from_experiment=from_experiment) self.logger.info('Checking format...') if type(new_voltage) is not float: self.logger.info( 'Incorrect format! Voltage type must be float, but it is ' + str(type(new_voltage))) raise ModExpError(error='Incorrect format: type must be float') # TO DELETE THIS LATER self.logger.info('Format is correct, new voltage value is %.1f...' % new_voltage) if new_voltage < 2 or 60 < new_voltage: raise ModExpError(error='Voltage must have value from 2 to 60!') self.logger.info('Parameters are normal, setting new voltage...') set_voltage = self.try_thrice_change_attr( "xraysource_voltage", new_voltage, error_str='Could not set voltage') self.logger.info('New value of voltage was set!')
def try_thrice_change_attr(self, attr_name, new_value, error_str): """ Try to change some attribute of Tango device three times :arg: 'attr_name' - type is string 'new_value' - type is type of attribute :return: """ exception_message = '' for i in range(0, 3): try: self.tomograph_proxy.write_attribute(attr_name, new_value) set_value = self.tomograph_proxy[attr_name].value except PyTango.DevFailed as e: exception_message = e[-1].desc else: return set_value raise ModExpError(error=error_str, exception_message=exception_message)
def experiment_stop(tomo_num): logger.info('\n\nREQUEST: EXPERIMENT/STOP') tomograph = TOMOGRAPHS[tomo_num - 1] # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0 # success, exp_stop_reason_txt, response_if_fail = check_request(request.data) # if not success: # return response_if_fail # if not exp_stop_reason_txt: # exp_stop_reason_txt = "unknown" exp_stop_reason_txt = "unknown" if tomograph.current_experiment is not None: tomograph.current_experiment.to_be_stopped = True tomograph.current_experiment.stop_exception = ModExpError( error=exp_stop_reason_txt, stop_msg=SOMEONE_STOP_MSG) return create_response(True)
def try_thrice_read_attr(self, attr_name, extract_as=PyTango.ExtractAs.Numpy, error_str='Error trying thrice read attribute'): """ Try to read some attribute of Tango device three times :arg: 'attr_name' - type is string 'extract-as' - method of extraction :return: """ exception_message = '' for i in range(0, 3): try: attr = self.tomograph_proxy.read_attribute( attr_name, extract_as) except PyTango.DevFailed as e: exception_message = e[-1].desc else: return attr raise ModExpError(error=error_str, exception_message=exception_message)