def calibrate_screen(self): self.clear_screen_plots() n_images = int(self.CalibrateScreenImages.text()) image_dict = daq.get_images(self.screen, n_images, dry_run=self.dry_run) try: result = analysis.analyze_screen_calibration( image_dict, True, self.screen_calib_plot_handles) except: date = datetime.now() basename = date.strftime( '%Y_%m_%d-%H_%M_%S_' ) + 'Screen_Calibration_data_%s.h5' % self.screen.replace( '.', '_') filename = os.path.join(self.save_dir, basename) h5_storage.saveH5Recursive(filename, image_dict) print('Saved screen calibration data %s' % filename) raise x0, beamsize = self.update_screen_calibration(result) date = datetime.now() basename = date.strftime( '%Y_%m_%d-%H_%M_%S_' ) + 'Screen_Calibration_%s.h5' % self.screen.replace('.', '_') elog_text = 'Screen calibration\nScreen center at %i um\nBeamsize %i um' % ( x0 * 1e6, beamsize * 1e6) self.elog_and_H5(elog_text, [self.screen_calib_fig], 'Screen center calibration', basename, image_dict)
def calibrate_streaker(self): self.clear_calib_plots() start, stop, step = float(self.Range1Begin.text()), float( self.Range1Stop.text()), int(float(self.Range1Step.text())) range1 = np.linspace(start, stop, step) start, stop, step = float(self.Range2Begin.text()), float( self.Range2Stop.text()), int(float(self.Range2Step.text())) range2 = np.linspace(start, stop, step) range_ = np.concatenate([range1, [0], range2 ]) * 1e-3 # Convert mm to m range_.sort() range_ = np.unique(range_) streaker = config.streaker_names[self.beamline][self.n_streaker] n_images = int(self.CalibrateStreakerImages.text()) if daq is None: raise ImportError('Daq not available') result_dict = daq.data_streaker_offset(streaker, range_, self.screen, n_images, self.dry_run) try: full_dict = self._analyze_streaker_calib(result_dict) except: date = datetime.now() basename = date.strftime( '%Y_%m_%d-%H_%M_%S_' ) + 'Calibration_data_%s.h5' % streaker.replace('.', '_') filename = os.path.join(self.save_dir, basename) h5_storage.saveH5Recursive(filename, result_dict) print('Saved streaker calibration data %s' % filename) raise date = datetime.now() basename = date.strftime( '%Y_%m_%d-%H_%M_%S_') + 'Calibration_%s.h5' % streaker.replace( '.', '_') streaker_offset = full_dict['meta_data']['streaker_offset'] self.updateStreakerCenter(streaker_offset) elog_text = 'Streaker calibration streaker %s\nCenter: %i um' % ( streaker, streaker_offset * 1e6) self.elog_and_H5(elog_text, [self.streaker_calib_fig], 'Streaker center calibration', basename, full_dict) self.tabWidget.setCurrentIndex(self.streaker_calib_plot_tab_index)
def elog_and_H5(self, text, figs, title, basename, data_dict): filename = os.path.join(self.save_dir, basename) h5_storage.saveH5Recursive(filename, data_dict) print('Saved %s' % filename) attachments = [] for num, fig in enumerate(figs): fig_title = filename.replace('.h5', '_%i.png' % num) fig_filename = os.path.join(self.save_dir, fig_title) fig.savefig(fig_filename, bbox_inches='tight', pad_inches=0) print('Saved %s' % fig_filename) attachments.append(fig_filename) text += '\nData saved in %s' % filename text += '\nBeamline: %s' % self.beamline text += '\nStreaker: %s' % self.streaker text += '\nScreen: %s' % self.screen if elog is None: print('Cannot save to ELOG') print('I would post:') print(text) elif self.ElogSaveCheck.isChecked(): dict_att = { 'Author': 'Application: PostUndulatorStreakerAnalysis', 'Application': 'PostUndulatorStreakerAnalysis', 'Category': 'Measurement', 'Title': title } self.logbook.post(text, attributes=dict_att, attachments=attachments) print('ELOG entry saved.') else: print('Save to ELOG is not checked in GUI') return filename
bl_meas_structure = '/sf/data/measurements/2020/02/03/Bunch_length_meas_2020-02-03_15-51-34.h5' if load_compact: bl_meas_compact = loadH5Recursive('./example_current_profile.h5') energy_eV = bl_meas_compact['energy_eV'] time_profile = bl_meas_compact['time_profile'] current = bl_meas_compact['current'] else: bl_meas = loadH5Recursive2(bl_meas_structure) time_profile, current = bl_meas['Meta_data'][ 'Time axes'][::-1] * 1e-15, bl_meas['Meta_data'][ 'Current profile'][::-1] energy_eV = bl_meas['Input']['Energy'].squeeze() * 1e6 saveH5Recursive('./example_current_profile.h5', { 'time_profile': time_profile, 'current': current, 'energy_eV': energy_eV }) rescale_factor = 200e-12 / np.sum(current) yy_charge = current * rescale_factor xx_space = time_profile * c xx_space -= xx_space.min() #cutoff #yy_charge[yy_charge < 0.1*yy_charge.max()] = 0 fig = ms.figure('Wake effect') subplot = ms.subplot_factory(2, 3) sp_ctr = 1
dirname2 = '/home/work/data_2020-10-04/' files = [ 'Passive_data_20201003T231958.mat', 'Passive_data_20201003T233852.mat', 'Passive_data_20201004T161118.mat', 'Passive_data_20201004T172425.mat', 'Passive_data_20201004T223859.mat', ] for file_ in files: try: file1 = dirname1 + file_ dict_ = mat73.loadmat(file1) except: file1 = dirname2 + file_ dict_ = mat73.loadmat(file1) dict2 = {} for x, y in dict_.items(): try: y2 = np.array(y) if y2.dtype != np.dtype('O'): dict2[x] = y2 except: print('Pass for %s' % x) file2 = file1 + '.h5' h5_storage.saveH5Recursive(file2, dict2) print('Saved %s' % file2)
gap_mm = 10 projections = np.zeros( [len(dict_['Image']), len(dict_['Image'][0]), len(x_axis)], dtype=np.float32) for a, b in itertools.product(range(len(dict_['Image'])), range(len(dict_['Image'][0]))): projections[a, b] = dict_['Image'][a][b].sum(axis=-1) new_dict = { 'streaker_offsets': new_offsets, 'screen': screen, 'n_images': projections.shape[1], 'streaker': streaker, 'pyscan_result': { 'x_axis_m': x_axis, 'projx': projections, }, 'meta_data': { streaker + ':GAP': gap_mm, }, } h5_storage.saveH5Recursive(matfile.replace('.mat', '_new.h5'), new_dict) streaker_analysis = analysis.analyze_streaker_calibration(new_dict) plt.show()
print(w.filename) if sp_ctr > 9: ms.figure('Continued') sp_ctr = 1 sp = subplot(sp_ctr, title='%i %s' % (sigma * 1e15, label), scix=True) sp_ctr += 1 if use_constant_bin_number: bin_edges = bin_number else: bin_edges = np.arange(w['x'].min(), w['x'].max() + hist_bin_size, hist_bin_size) hist, bin_edges, _ = sp.hist(w['x'], density=True, bins=bin_edges) output['proj_' + label][n_offset, n_sigma, :len(bin_edges) - 1] = hist output['axis_' + label][n_offset, n_sigma, :len(bin_edges) - 1] = bin_edges[:-1] #sim0.__del__() #sim.__del__() h5_storage.saveH5Recursive('./bins_200_300e3_370um_larger_beamsize.h5', output) plt.show()
for key in ['Image', 'knob', 'value', 'x_axis', 'y_axis']: try: value = inp_dict[key] except KeyError: print('Continue for key %s' % key) continue if key == 'Image': outp = [] if type(value[0]) is list: for n_offset in range(len(value)): outp.append([]) for n_image in range(len(value[n_offset])): img = value[n_offset][n_image].T outp[n_offset].append(img.sum(axis=0)) else: for n_image in range(len(value)): img = value[n_image].T outp.append(img.sum(axis=0)) outp_dict['projx'] = np.array(outp) else: outp_dict[key] = np.array(value) new_file = dir_+file_+'.h5' saveH5Recursive(new_file, outp_dict) print(new_file)