def test_archiving(): evt_subscriber_device_fqdn = "archiving/hdbpp/eventsubscriber01" config_manager_device_fqdn = "archiving/hdbpp/confmanager01" conf_manager_proxy = DeviceProxy(config_manager_device_fqdn) evt_subscriber_device_proxy = DeviceProxy(evt_subscriber_device_fqdn) conf_manager_proxy.set_timeout_millis(5000) evt_subscriber_device_proxy.set_timeout_millis(5000) attribute = "sys/tg_test/1/double_scalar" # wait for the attribute to be online. max_retries = 10 sleep_time = 30 for x in range(0, max_retries): try: att = AttributeProxy(attribute) att.read() break except DevFailed as df: if (x == (max_retries - 1)): raise df logging.info("DevFailed exception: " + str(df.args[0].reason) + ". Sleeping for " + str(sleep_time) + "ss") sleep(sleep_time) conf_manager_proxy.write_attribute("SetAttributeName", attribute) conf_manager_proxy.write_attribute("SetArchiver", evt_subscriber_device_fqdn) conf_manager_proxy.write_attribute("SetStrategy", "ALWAYS") conf_manager_proxy.write_attribute("SetPollingPeriod", 1000) conf_manager_proxy.write_attribute("SetPeriodEvent", 3000) try: conf_manager_proxy.command_inout("AttributeAdd") except DevFailed as df: if not str(df.args[0].reason) == 'Already archived': logging.info("DevFailed exception: " + str(df.args[0].reason)) evt_subscriber_device_proxy.Start() max_retries = 10 sleep_time = 1 for x in range(0, max_retries): try: # Check status of Attribute Archiving in Configuration Manager result_config_manager = conf_manager_proxy.command_inout( "AttributeStatus", attribute) # Check status of Attribute Archiving in Event Subscriber result_evt_subscriber = evt_subscriber_device_proxy.command_inout( "AttributeStatus", attribute) assert "Archiving : Started" in result_config_manager assert "Archiving : Started" in result_evt_subscriber except DevFailed as df: if (x == (max_retries - 1)): raise df logging.info("DevFailed exception: " + str(df.args[0].reason) + ". Sleeping for " + str(sleep_time) + "ss") sleep(sleep_time)
def cm_configure_attributes(): configure_success_count = 0 configure_fail_count = 0 already_configured_count = 0 total_attrib_count = 0 with open(attr_list_file, 'r') as attrib_list_file: attribute_list = json.load(attrib_list_file) for attribute in attribute_list: total_attrib_count += 1 attribute_fqdn = "tango://" + os.environ[ 'TANGO_HOST'] + "/" + attribute is_already_archived = False attr_list = evt_subscriber_proxy.read_attribute( "AttributeList").value if attr_list is not None: for already_archived in attr_list: if attribute.lower() in str(already_archived).lower(): print("Attribute " + attribute + " already configured.") is_already_archived = True already_configured_count += 1 break if not is_already_archived: print("Attribute " + attribute + " not configured. Configuring it now. ") max_retries = 10 sleep_time = 30 for x in range(0, max_retries): try: att = AttributeProxy(attribute_fqdn) att.read() break except DevFailed as df: if (x == (max_retries - 1)): raise df print("DevFailed exception: " + str(df.args[0].reason) + ". Sleeping for " + str(sleep_time) + "ss") sleep(sleep_time) conf_manager_proxy.write_attribute("SetAttributeName", attribute_fqdn) conf_manager_proxy.write_attribute("SetArchiver", evt_subscriber_device_fqdn) conf_manager_proxy.write_attribute("SetStrategy", "ALWAYS") conf_manager_proxy.write_attribute("SetPollingPeriod", 1000) conf_manager_proxy.write_attribute("SetPeriodEvent", 3000) conf_manager_proxy.AttributeAdd() configure_success_count += 1 print("attribute_fqdn " + attribute_fqdn + " " + " added successfuly") return configure_success_count, configure_fail_count, already_configured_count, total_attrib_count
def init_device(self): self.debug_stream("Preparing device") Device.init_device(self) try: self.image_proxy = AttributeProxy(self.ImageProxy) self.debug_stream('Init was done') except: self.error_stream('Could not contact camera :( ') self.set_state(DevState.OFF) self.set_state(DevState.ON)
def configure_attribute(attribute): conf_manager_proxy = DeviceProxy("archiving/hdbpp/confmanager01") #logging.info(conf_manager_proxy.Status()) evt_subscriber_device_fqdn = "archiving/hdbpp/eventsubscriber01" evt_subscriber_device_proxy = DeviceProxy(evt_subscriber_device_fqdn) is_already_archived = False attr_list = evt_subscriber_device_proxy.read_attribute( "AttributeList").value if attr_list is not None: for already_archived in attr_list: #logging.info("Comparing: " + str(attribute) + " and " + str(already_archived).lower()) if attribute in str(already_archived).lower(): is_already_archived = True #logging.info("is_already_archived: True") break if not is_already_archived: # wait for the attribute to be up and running for configuring it. #logging.info("Adding attribute not archived....") max_retries = 10 sleep_time = 30 for x in range(0, max_retries): try: att = AttributeProxy(attribute) att.read() #logging.info("Attribute online value=" + str(att.read())) break except DevFailed as df: if (x == (max_retries - 1)): raise df logging.info("DevFailed exception: " + str(df.args[0].reason) + ". Sleeping for " + str(sleep_time) + "ss") sleep(sleep_time) conf_manager_proxy.write_attribute("SetAttributeName", attribute) conf_manager_proxy.write_attribute("SetArchiver", evt_subscriber_device_fqdn) conf_manager_proxy.write_attribute("SetStrategy", "ALWAYS") conf_manager_proxy.write_attribute("SetPollingPeriod", 1000) conf_manager_proxy.write_attribute("SetPeriodEvent", 3000) conf_manager_proxy.AttributeAdd() evt_subscriber_device_proxy.Start() sleep(3) # the polling result_config_manager = conf_manager_proxy.AttributeStatus(attribute) result_evt_subscriber = evt_subscriber_device_proxy.AttributeStatus( attribute) assert "Archiving : Started" in result_config_manager assert "Archiving : Started" in result_evt_subscriber conf_manager_proxy.AttributeRemove(attribute)
def SetAxisExtraPar(self, axis, name, value): try: self._log.debug("SetAxisExtraPar [%d] %s = %s" % (axis, name, value)) self.axisAttributes[axis][name] = value if name in [ TANGO_ATTR, TANGO_ATTR_ENC, TANGO_LIMIT_PLUS, TANGO_LIMIT_MINUS ]: key = TAU_ATTR if name == TANGO_ATTR_ENC: key = TAU_ATTR_ENC elif name == TANGO_LIMIT_PLUS: key = TAU_LIMIT_PLUS elif name == TANGO_LIMIT_MINUS: key = TAU_LIMIT_MINUS try: self.axisAttributes[axis][key] = AttributeProxy(value) except Exception as e: self.axisAttributes[axis][key] = None raise e except DevFailed as df: de = df[0] self._log.error("SetExtraAttribute DevFailed: (%s) %s" % (de.reason, de.desc)) self._log.error("SetExtraAttribute DevFailed: %s" % str(df)) # raise df except Exception as e: self._log.error("SetExtraAttribute Exception: %s" % str(e))
def _read_cbf_output_link(self): """Get the CBF output link map from the CSP subarray device. This provides the map of FSP to channels needed to construct the receive address map. :return: Channel link map string as read from CSP. """ LOG.debug('Reading cbfOutputLink attribute ...') attribute_fqdn = self._config.get('cspCbfOutlinkAddress', None) if attribute_fqdn is None: msg = "'cspCbfOutlinkAddress' not found in PB configuration" self._set_obs_state(ObsState.FAULT, verbose=False) self._raise_command_error(msg) LOG.debug('Reading cbfOutLink from: %s', attribute_fqdn) attribute_proxy = AttributeProxy(attribute_fqdn) attribute_proxy.ping() LOG.debug('Waiting for cbfOutputLink to provide config for scanId: %s', self._scanId) cbf_out_link_dict = {} start_time = time.time() # FIXME(BMo) Horrible hack to poll the CSP device until the scanID \ # matches - use events instead!! cbf_out_link = '' while cbf_out_link_dict.get('scanId') != self._scanId: cbf_out_link = attribute_proxy.read().value cbf_out_link_dict = json.loads(cbf_out_link) time.sleep(1.0) elapsed = time.time() - start_time LOG.debug('Waiting for cbfOutputLink attribute (elapsed: %2.4f s) ' ': %s', elapsed, cbf_out_link) if elapsed >= 20.0: self._set_obs_state(ObsState.FAULT, verbose=False) self._raise_command_error( 'Timeout reached while reading cbf output link!') break # event_id = attribute_proxy.subscribe_event( # tango.EventType.CHANGE_EVENT, # self._cbf_output_link_callback # ) # self._events_telstate[event_id] = attribute_proxy # print(attribute_proxy.is_event_queue_empty()) # events = attribute_proxy.get_events() LOG.debug('Channel link map (str): "%s"', cbf_out_link) return cbf_out_link
def _get_channel_link_map(self, scan_id, timeout=30.0): """Get channel link map from the CSP Tango device attribute. :param scan_id: Scan ID to match :param timeout: Timeout in seconds :returns: Validated channel link map as dict """ LOG.debug('Reading channel link map from %s', self._cbf_outlink_address) attribute_proxy = AttributeProxy(self._cbf_outlink_address) attribute_proxy.ping() LOG.debug( 'Waiting for CSP attribute to provide channel link map for ' 'scan ID %s', scan_id) # This is a horrendous hack to poll the CSP device until the scan # ID matches. It needs to refactored to use events. start_time = time.time() while True: channel_link_map_str = attribute_proxy.read().value channel_link_map = self._validate_json_config( channel_link_map_str, 'channel_link_map.json') if channel_link_map is None: self._set_obs_state(ObsState.FAULT) self._raise_command_error('Channel link map validation ' 'failed') break if channel_link_map.get('scanID') == scan_id: break elapsed = time.time() - start_time LOG.debug( 'Waiting for scan ID on CSP attribute ' '(elapsed: %2.4f s)', elapsed) if elapsed > timeout: self._set_obs_state(ObsState.FAULT) self._raise_command_error('Timeout reached while waiting for ' 'scan ID on CSP attribute') channel_link_map = None break time.sleep(1.0) return channel_link_map
def attribute_add(self, fqdn, polling_period=1000, period_event=3000): if not self.is_already_archived(fqdn): AttributeProxy(fqdn).read() self.conf_manager_proxy.write_attribute("SetAttributeName", fqdn) self.conf_manager_proxy.write_attribute("SetArchiver", self.eventsubscriber) self.conf_manager_proxy.write_attribute("SetStrategy", "ALWAYS") self.conf_manager_proxy.write_attribute("SetPollingPeriod", int(polling_period)) self.conf_manager_proxy.write_attribute("SetPeriodEvent", int(period_event)) self.conf_manager_proxy.AttributeAdd() return True return False
class Autocorrelator(Device): ''' Autocorrelator This device can calculate the pulsewidth in fs of the incoming laser detected by the selected camera. To select which is the camera connected to the autocorrelator, one must write in dev_properties from the Basler device in Jive, the serial number of the desired camera, which is usually written on the camera as S/N). Then this code will automatically receive a matrix with all the values (the image itself) and one will be able to calculate the FWHM in x and y under different situations. ''' ImageProxy = device_property( dtype=str, default_value='domain/family/member/attribute', ) data_x = attribute(name='data_x', label='data x', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) #data_y = attribute(name='data_y', label='data y',max_dim_x=4096, # dtype=(DevFloat,), access=AttrWriteType.READ) fitting_x = attribute(name='fitting_x', label='fitting x', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) #fitting_y = attribute(name='fitting_y', label='fitting y',max_dim_x=4096, # dtype=(DevFloat,), access=AttrWriteType.READ) mu_x = attribute(name='mu_x', label='Location of maximum in data x (pixels)', dtype="float", format="%4.3f", access=AttrWriteType.READ) center1 = attribute( label='peak 1, without the quartz (loc. max in pixels)', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=True) center2 = attribute(label='peak 2, with the quartz (loc. max in pixels)', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=True) xpos = attribute(label='start integrating x (pixel)', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=True) xwin = attribute(label='integration window x (pixel)', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=True) calibration = attribute(name='calibration', label='calibration (fs/pixel)', dtype="float", format="%4.3f", access=AttrWriteType.READ) gaussian_pulsewidth_x = attribute(name='gaussian_pulsewidth_x', label='gauss pulsewidth: x axis(fs)', dtype="float", format="%4.3f", access=AttrWriteType.READ) #gaussian_pulsewidth_y = attribute(name='gaussian_pulsewidth_y', label='y axis(fs)', # dtype="float",format="%4.3f", access=AttrWriteType.READ) sech2_pulsewidth_x = attribute(name='sech2_pulsewidth_x', label='sech2 pulsewidth: x axis (fs)', dtype="float", format="%4.3f", access=AttrWriteType.READ) #sech2_pulsewidth_y = attribute(name='sech2_pulsewidth_y', label='y axis (fs)', # dtype="float",format="%4.3f", access=AttrWriteType.READ) # twodgaussian = attribute(name='twodgaussian', label='two dimensional integration', # dtype="float", format="%4.3f", access=AttrWriteType.READ) sigma_try = device_property(dtype="int") __position_1 = 780 __position_2 = 1129 __xposi = 100 __xwind = 50 d = 95 * 1e-6 #conversion into m c_0 = co * 1e-15 #conversion into m/fs width = 300 sqrt2 = np.sqrt(2) N = 0 def init_device(self): self.debug_stream("Preparing device") Device.init_device(self) try: self.image_proxy = AttributeProxy(self.ImageProxy) self.debug_stream('Init was done') except: self.error_stream('Could not contact camera :( ') self.set_state(DevState.OFF) self.set_state(DevState.ON) def read_data_x(self): self.debug_stream("Graphing x axis") real_data = np.array(self.image_proxy.read().value) self.N = len(real_data[0, :]) self.x2 = np.linspace(0, self.N, self.N) self.x_axis = np.mean(real_data[int(self.__xposi):int(self.__xposi) + int(self.__xwind), :], axis=0) self.debug_stream('cameras x axis was graphed colected properly') return self.x_axis #def read_data_y(self): # self.debug_stream("Graphing y axis") # real_data = np.array(self.image_proxy.read().value) # self.N2 = len(real_data[:,0]) # self.y2 = np.linspace(0,self.N2,self.N2) # self.y_axis = np.mean(real_data, axis = 1) # self.debug_stream('cameras y axis was graphed properly') # return self.y_axis def read_mu_x(self): self.debug_stream("Calculating the center of the peak in the x axis") def gaussian_paula(x, mu, A, sigma, c): return A * np.exp(-(x - mu)**2 / (2 * sigma**2)) + c mod = lm.Model(gaussian_paula) self.parsx = lm.Parameters() real_data = np.array(self.image_proxy.read().value) self.x_axis = np.mean(real_data, axis=0) #self.x_axis = np.mean(real_data[self.__xposi:self.__xposi+self.__xwind,:], axis = 0) self.x_max = np.max(self.x_axis) self.x_min = np.min(self.x_axis) self.mu = self.x_axis.argmax() self.parsx.add('mu', value=self.mu) self.parsx.add('A', value=self.x_max - self.x_min) self.parsx.add('c', value=self.x_min) self.parsx.add('sigma', value=self.sigma_try) self.debug_stream('parameters fitting x axis data') self.out_gaussx = mod.fit(self.x_axis, self.parsx, x=self.x2) self.debug_stream('fitting x axis done succesfully') self.a = self.out_gaussx.best_values['sigma'] return self.out_gaussx.best_values['mu'] def read_fitting_x(self): return self.out_gaussx.best_fit def read_center1(self): return self.__position_1 def write_center1(self, value): self.__position_1 = value def read_center2(self): return self.__position_2 def write_center2(self, value): self.__position_2 = value def read_xpos(self): return self.__xposi def write_xpos(self, value): self.__xposi = value def read_xwin(self): return self.__xwind def write_xwin(self, value): self.__xwind = value def read_calibration(self): t = self.d / self.c_0 self.calibr_real = abs(t / (self.__position_1 - self.__position_2)) return self.calibr_real def read_gaussian_pulsewidth_x(self): return self.a * 2 * np.sqrt( 2 * np.log(2)) * self.calibr_real * self.sqrt2 def read_sech2_pulsewidth_x(self): return self.a * 2 * np.sqrt(2 * np.log(2)) * self.calibr_real * 1.55
class BeamProfiler(Device): ''' Beam profiler This device can measure the FWHM of any incoming signal. It grabs the data from an image and does a gaussian fitting both in x and y axis of the image. This device returns the FWHM both in x and y axis in micrometer. Take into account that in order to be able to read the value FWHM, first you should click on both data_x and data_y tabs so that the device knows which the length of the data in both axis. To select which is the camera one wants to use, you must write in dev_properties from the Basler device in Jive, the serial number of the desired camera, which is usually written on the camera as S/N). ****** In case the camera is changed, the propertie resolution is a parameter that needs to be checked because it may be different for each camera brand. Basler cameras acA1300 - 60gm have a resolution of 5.3 micrometer/pixel. ''' ImageProxy = device_property( dtype=str, default_value='domain/family/member/attribute', ) Resolution = device_property( dtype="float", default_value=1, ) data_x = attribute(name='data_x', label='data x', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) data_y = attribute(name='data_y', label='data y', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) from_x = attribute(label='from x (select pixel position)', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=False) to_x = attribute(label='to x', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=False) from_y = attribute(label='from y', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=False) to_y = attribute(label='to y', dtype="float", access=AttrWriteType.READ_WRITE, memorized=True, hw_memorized=False) fitting_x = attribute(name='fitting_x', label='fitting x', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) fitting_y = attribute(name='fitting_y', label='fitting y', max_dim_x=4096, dtype=(DevFloat, ), access=AttrWriteType.READ) width_x = attribute(label='FWHM x', unit='um', dtype="float", format="%4.3f", access=AttrWriteType.READ) width_y = attribute(label='FWHM y', dtype="float", unit='um', format="%4.3f", access=AttrWriteType.READ) __minimum_y = 0 __minimum_x = 0 __maximum_y = 1 __maximum_x = 1 sigma_try = 33.15 def init_device(self): self.debug_stream("Preparing device") Device.init_device(self) try: self.image_proxy = AttributeProxy(self.ImageProxy) self.debug_stream('Init was done') except: self.error_stream('Could not contact camera :( ') self.set_state(DevState.OFF) self.set_state(DevState.ON) def read_data_x(self): self.debug_stream("Graphing x axis") real_data = np.array(self.image_proxy.read().value) self.N = len(real_data[0, :]) self.x2 = np.linspace(0, self.N, self.N) self.x_axis = np.mean(real_data, axis=0) self.debug_stream('cameras x axis was graphed colected properly') return self.x_axis def read_data_y(self): self.debug_stream("Graphing y axis") real_data = np.array(self.image_proxy.read().value) self.N2 = len(real_data[:, 0]) self.y2 = np.linspace(0, self.N2, self.N2) self.y_axis = np.mean(real_data, axis=1) self.debug_stream('cameras y axis was graphed properly') return self.y_axis def read_from_y(self): return self.__minimum_y def write_from_y(self, value): self.__minimum_y = value def read_from_x(self): return self.__minimum_x def write_from_x(self, value): self.__minimum_x = value def read_to_x(self): return self.__maximum_x def write_to_x(self, value): self.__maximum_x = value def read_to_y(self): return self.__maximum_y def write_to_y(self, value): self.__maximum_y = value def read_width_x(self): self.debug_stream('trying to calculate the width x') real_data = np.array(self.image_proxy.read().value) self.x_axis = np.mean(real_data, axis=0) self.debug_stream('getting data x') self.__maximum_x = int(self.__maximum_x) self.__minimum_x = int(self.__minimum_x) aa = self.__minimum_x bb = self.__maximum_x self.x_axis = self.x_axis[aa:bb] self.debug_stream('redefining limits in x') self.x = np.linspace(aa, bb, len(self.x_axis)) self.debug_stream('ordinates image axis') def gaussian_paula(x, mu, A, sigma, c): return A * np.exp(-(x - mu)**2 / (2 * sigma**2)) + c self.mod = lm.Model(BeamProfiler.gaussian) self.pars = lm.Parameters() self.x_max = np.max(self.x_axis) self.x_min = np.min(self.x_axis) self.muu = self.x_axis.argmax() self.pars.add('mu', value=self.muu) self.pars.add('A', value=self.x_max - self.x_min) self.pars.add('c', value=self.x_min) self.pars.add('sigma', value=self.sigma_try) self.out_x = self.mod.fit(self.x_axis, self.pars, x=self.x) self.debug_stream('parameters & fitting x axis data successful') return 2 * np.sqrt( 2 * np.log(2)) * self.out_x.best_values['sigma'] * self.Resolution def read_width_y(self): self.debug_stream('trying to calculate the width y') real_data = np.array(self.image_proxy.read().value) y_axis = np.mean(real_data, axis=1) self.__maximum_y = int(self.__maximum_y) self.__minimum_y = int(self.__minimum_y) cc = self.__minimum_y dd = self.__maximum_y self.y_axis = y_axis[cc:dd] self.y = np.linspace(cc, dd, len(self.y_axis)) self.mod = lm.Model(BeamProfiler.gaussian) self.pars = lm.Parameters() self.y_max = np.max(self.y_axis) self.y_min = np.min(self.y_axis) self.muu = self.y_axis.argmax() self.pars.add('mu', value=self.muu) self.pars.add('A', value=self.y_max - self.y_min) self.pars.add('c', value=self.y_min) self.pars.add('sigma', value=self.sigma_try) self.out_y = self.mod.fit(self.y_axis, self.pars, x=self.y) self.debug_stream('parameters & fitting y axis data successful') return 2 * np.sqrt( 2 * np.log(2)) * self.out_y.best_values['sigma'] * self.Resolution def read_fitting_x(self): return self.out_x.best_fit def read_fitting_y(self): return self.out_y.best_fit @staticmethod def gaussian(x, mu, A, sigma, c): return A * np.exp(-(x - mu)**2 / (2 * sigma**2)) + c