Ejemplo n.º 1
0
    def reconstruct_all_lasing(self):
        self.clear_all_lasing_plots()

        screen_x0 = self.screen_x0
        beamline, n_streaker = self.beamline, self.n_streaker
        charge = self.charge
        streaker_offset = self.streaker_means[n_streaker]
        delta_gap = self.delta_gaps[n_streaker]
        pulse_energy = float(self.LasingEnergyInput.text()) * 1e-6
        slice_factor = int(self.LasingReconstructionSliceFactor.text())

        file_on = self.LasingOnDataLoad.text()
        file_off = self.LasingOffDataLoad.text()
        lasing_off_dict = h5_storage.loadH5Recursive(file_off)
        lasing_on_dict = h5_storage.loadH5Recursive(file_on)

        tracker_kwargs = self.get_tracker_kwargs()
        recon_kwargs = self.get_gauss_kwargs()
        las_rec_images = {}

        for main_ctr, (data_dict,
                       title) in enumerate([(lasing_off_dict, 'Lasing Off'),
                                            (lasing_on_dict, 'Lasing On')]):
            rec_obj = lasing.LasingReconstructionImages(
                screen_x0,
                beamline,
                n_streaker,
                streaker_offset,
                delta_gap,
                tracker_kwargs,
                recon_kwargs=recon_kwargs,
                charge=charge,
                subtract_median=True,
                slice_factor=slice_factor)

            rec_obj.add_dict(data_dict)
            if main_ctr == 1:
                rec_obj.profile = las_rec_images['Lasing Off'].profile
                rec_obj.ref_slice_dict = las_rec_images[
                    'Lasing Off'].ref_slice_dict
            rec_obj.process_data()
            las_rec_images[title] = rec_obj
            #rec_obj.plot_images('raw', title)
            #rec_obj.plot_images('tE', title)

        las_rec = lasing.LasingReconstruction(las_rec_images['Lasing Off'],
                                              las_rec_images['Lasing On'],
                                              pulse_energy,
                                              current_cutoff=1.5e3)
        las_rec.plot(plot_handles=self.all_lasing_plot_handles)
        self.all_lasing_canvas.draw()
Ejemplo n.º 2
0
def analyze_screen_calibration(filename_or_dict, do_plot=True, plot_handles=None):
    if type(filename_or_dict) is dict:
        data_dict = filename_or_dict
    elif type(filename_or_dict) is str:
        data_dict = h5_storage.loadH5Recursive(filename_or_dict)
    else:
        raise ValueError(type(filename_or_dict))

    screen_data = data_dict['pyscan_result']
    if 'x_axis_m' in screen_data:
        x_axis = screen_data['x_axis_m']
    else:
        #print(screen_data['x_axis'].shape)
        x_axis = screen_data['x_axis'].squeeze()*1e-6
        if len(x_axis.shape) == 2:
            x_axis = x_axis[0]

    assert len(x_axis.squeeze().shape) == 1

    if 'projx' in screen_data:
        projx = screen_data['projx']
    else:
        images = screen_data['image'].astype(float).squeeze()
        projx = images.sum(axis=-2)

    all_mean = []
    all_std = []
    for proj in projx:
        gf = gaussfit.GaussFit(x_axis, proj)
        all_mean.append(gf.mean)
        all_std.append(gf.sigma)

    index_median = np.argsort(all_mean)[len(all_mean)//2]
    projx_median = projx[index_median]
    beamsize = np.mean(all_std)

    projx_median -= projx_median.min()
    x0 = gaussfit.GaussFit(x_axis, projx_median).mean
    output = {
            'raw_data': data_dict,
            'x0': x0,
            'beamsize': beamsize,
            }

    if not do_plot:
        return output

    if plot_handles is None:
        fig, (sp_proj,) = screen_calibration_figure()
    else:
        (sp_proj, ) = plot_handles

    for proj in projx:
        sp_proj.plot(x_axis*1e3, proj-proj.min())
    sp_proj.plot(x_axis*1e3, projx_median-projx_median.min(), lw=3)
    sp_proj.axvline(x0*1e3)

    return output
Ejemplo n.º 3
0
    def load_calibration(self):
        self.clear_calib_plots()
        filename = self.LoadCalibrationFilename.text().strip()
        saved_dict = h5_storage.loadH5Recursive(filename)

        if 'raw_data' in saved_dict:
            saved_dict = saved_dict['raw_data']
        full_dict = self._analyze_streaker_calib(saved_dict)

        streaker_offset = full_dict['meta_data']['streaker_offset']
        self.updateStreakerCenter(streaker_offset)
        #self.tabWidget.setCurrentIndex(self.streaker_calib_plot_tab_index)
        if self.streaker_calib_canvas is not None:
            self.streaker_calib_canvas.draw()
Ejemplo n.º 4
0
    def streaker_set(self):
        widgets = (self.SetStreakerDirectCheck, self.SetStreakerFromLiveCheck,
                   self.SetStreakerSaveCheck)
        self._check_check(widgets, 'Check set streaker checkmarks')

        if self.SetStreakerDirectCheck.isChecked():
            meta_dict = None
        elif self.SetStreakerFromLiveCheck.isChecked():
            self.obtain_streaker_settings_from_live()
            if daq is None:
                raise RuntimeError('Cannot get settings from live!')
            meta_dict = daq.get_meta_data(self.screen)

        elif self.SetStreakerSaveCheck:
            filename = self.ReconstructionDataLoad.text().strip()
            dict_ = h5_storage.loadH5Recursive(filename)
            if 'meta_data' in dict_:
                meta_dict = dict_['meta_data']
            elif 'meta_data_end' in dict_:
                meta_dict = dict_['meta_data_end']

        if meta_dict is not None:
            streaker_dict = config.streaker_names[self.beamline]
            for n_streaker, gap_widget, offset_widget in [
                (0, self.StreakerGap0, self.StreakerOffset0),
                (1, self.StreakerGap1, self.StreakerOffset1),
            ]:
                streaker = streaker_dict[n_streaker]
                offset_mm = meta_dict[streaker + ':CENTER']
                gap_mm = meta_dict[streaker + ':GAP']
                if offset_mm < .01 * gap_mm / 2:
                    offset_mm = 0
                gap_widget.setText('%.4f' % gap_mm)
                offset_widget.setText('%.4f' % offset_mm)

        gaps = [
            float(self.StreakerGap0.text()) * 1e-3,
            float(self.StreakerGap1.text()) * 1e-3
        ]
        # beam offset is negative of streaker offset
        streaker_offsets = self.streaker_offsets

        print('Streaker is set: gaps: %s, offsets: %s' %
              (gaps, streaker_offsets))
        return gaps, streaker_offsets
Ejemplo n.º 5
0
    def gap_reconstruction(self):
        self.clear_gap_recon_plots()

        filename = self.LoadCalibrationFilename.text().strip()
        saved_dict = h5_storage.loadH5Recursive(filename)

        if 'raw_data' in saved_dict:
            saved_dict = saved_dict['raw_data']

        tracker = self.get_tracker(saved_dict['meta_data_begin'])
        gauss_kwargs = self.get_gauss_kwargs()
        gap_recon_dict = sc.reconstruct_gap(
            saved_dict,
            tracker,
            gauss_kwargs,
            plot_handles=self.gap_recon_plot_handles)
        n_streaker = gap_recon_dict['n_streaker']
        delta_gap = gap_recon_dict['delta_gap']
        gap = gap_recon_dict['gap']
        self.updateDeltaGap(delta_gap, n_streaker)
        print('Reconstructed gap: %.3f mm' % (gap * 1e3))
        self.gap_recon_canvas.draw()
Ejemplo n.º 6
0
import image_and_profile as iap
from h5_storage import loadH5Recursive
import elegant_matrix
import misc
import myplotstyle as ms

ms.plt.close('all')

archiver_dir = '/mnt/data/archiver_api_data/'
data_dir = '/mnt/data/data_2021-03-16/'
x0 = 0.0005552048387736093

lasing_on_file = data_dir + '20210316_202944_SARBD02-DSCR050_camera_snapshot.h5'
lasing_off_file = data_dir + '20210316_204139_SARBD02-DSCR050_camera_snapshot.h5'

lasing_on = loadH5Recursive(lasing_on_file)
lasing_off = loadH5Recursive(lasing_off_file)

x_axis = lasing_on['camera1']['x_axis'].astype(float) * 1e-6 - x0
y_axis = lasing_on['camera1']['y_axis'].astype(float) * 1e-6

image_on = lasing_on['camera1']['image'].astype(float)
image_off = lasing_off['camera1']['image'].astype(float)

if x_axis[1] < x_axis[0]:
    x_axis = x_axis[::-1]
    image_on = image_on[:, ::-1]
    image_off = image_off[:, ::-1]

if y_axis[1] < y_axis[0]:
    y_axis = y_axis[::-1]
Ejemplo n.º 7
0
 def loadH5Recursive2(path):
     return loadH5Recursive(
         os.path.join(storage_dir + 'data_2020-02-03/',
                      os.path.basename(path)))
Ejemplo n.º 8
0
elif hostname == 'pc11292.psi.ch':
    data_dir = '/sf/data/measurements/2021/05/19/'
elif hostname == 'pubuntu':
    data_dir = '/mnt/data/data_2021-05-19/'
data_dir1 = data_dir.replace('19', '18')

tracker_kwargs = config.get_default_tracker_settings()
tracker_kwargs['len_screen'] = 1000
tracker_kwargs['n_particles'] = int(50e3)
recon_kwargs = config.get_default_gauss_recon_settings()
tracker = tracking.Tracker(**tracker_kwargs)

#data_file = data_dir+'2021_05_19-14_49_38_Lasing_False_SARBD02-DSCR050.h5'
#data_file = data_dir + '2021_05_19-14_24_05_Calibration_SARUN18-UDCP020.h5'
data_file = data_dir1 + '2021_05_18-23_07_20_Calibration_SARUN18-UDCP020.h5'
data_dict = h5_storage.loadH5Recursive(data_file)

raw_data = data_dict['raw_data']
meta_data = data_dict['meta_data']

offset_index = -1

gaps = [10e-3, 9.94e-3]
streaker_offset = 372e-6
beam_offsets = [0, -(meta_data['offsets'][offset_index] - streaker_offset)]

tracker.set_simulator(raw_data['meta_data_begin'])

projx = raw_data['pyscan_result']['image'][offset_index].astype(
    np.float64).sum(axis=-2)
x_axis = raw_data['pyscan_result']['x_axis_m']
Ejemplo n.º 9
0
    dirname1 = '/storage/data_2021-05-18/'
    dirname2 = '/storage/data_2021-05-19/'
elif hostname == 'pc11292.psi.ch':
    dirname1 = '/sf/data/measurements/2021/05/18/'
    dirname2 = '/sf/data/measurements/2021/05/19/'
elif hostname == 'pubuntu':
    dirname1 = '/home/work/data_2021-05-18/'
    dirname2 = '/home/work/data_2021-05-19/'

with open('./bytes.pkl', 'rb') as f:
    bytes = pickle.load(f)
arr0 = np.frombuffer(bytes, dtype=np.uint16)
bg = arr0.reshape([2160, 2560])

empty_snapshot = dirname2 + '20210519_121718_SARBD02-DSCR050_camera_snapshot.h5'
image_dict = h5_storage.loadH5Recursive(empty_snapshot)
x_axis0 = image_dict['camera1']['x_axis']
y_axis0 = image_dict['camera1']['y_axis']

lasing_files = glob.glob(dirname1 + '2021_*Lasing_*SARBD02*.h5')
lasing_files += glob.glob(dirname1 + '*Calibration*.h5')

ny, nx = 4, 4
subplot = ms.subplot_factory(ny, nx, grid=False)

cmap = cm.get_cmap('viridis', 12)
lasing_snapshot = dirname1 + '20210518_233946_SARBD02-DSCR050_camera_snapshot.h5'

error_files = []

Ejemplo n.º 10
0
for p in blmeas_profile1, blmeas_profile2:
    p.cutoff2(5e-2)
    p.crop()
    p.reshape(int(1e3))

n_streaker = 1
screen_x0 = 4250e-6

tracker_kwargs = config.get_default_tracker_settings()
kwargs_recon = config.get_default_gauss_recon_settings()

kwargs_recon['delta_gap'] = [0, -62e-6]
streaker_offsets = [0., 374e-6]

lasing_off_dict = h5_storage.loadH5Recursive(lasing_off_file)
lasing_on_dict = h5_storage.loadH5Recursive(lasing_on_file)

ms.figure('Comparison Lasing On / Off')
subplot = ms.subplot_factory(2, 2)
sp_ctr = 1

sp_screen_comp = subplot(sp_ctr,
                         title='Measured projections',
                         xlabel='x (mm)',
                         ylabel='Intensity (arb. units)')
sp_ctr += 1
sp_profile_comp = subplot(sp_ctr,
                          title='Reconstructed profiles',
                          xlabel='x (mm)',
                          ylabel='I (kA)')
Ejemplo n.º 11
0
    (2, gap_file2, (sp_x4, sp_x3)),
]:

    for gap, files in gap_file:

        if not files:
            continue

        xx_list = []
        bpm1_list = []
        bpm2_list = []
        for file_ in files:
            if socket.gethostname() == 'desktop':
                file_ = os.path.join('/storage/data_2020-02-03',
                                     os.path.basename(file_))
            dict_ = loadH5Recursive(file_)

            bpm_data1 = dict_['scan 1']['data']['SARBD02-DBPM040']['X1']
            bpm_data2 = dict_['scan 1']['data']['SARBD02-DBPM010']['X1']

            bpm1_list.append(bpm_data1)
            bpm2_list.append(bpm_data2)

            xx = dict_['scan 1']['method']['actuators']['SARUN18-UDCP%i00' %
                                                        gapnum]['CENTER']
            xx_list.append(xx)

        len2 = sum(x.shape[-1] for x in bpm1_list)
        bpm1_data = np.zeros((len(xx_list[0]), len2))
        bpm2_data = bpm1_data.copy()
Ejemplo n.º 12
0
blmeas_file = data_dir1 + '119325494_bunch_length_meas.h5'
blmeas_profile = iap.profile_from_blmeas(blmeas_file,
                                         gauss_kwargs['tt_halfrange'],
                                         gauss_kwargs['charge'], 0, True)
blmeas_profile.cutoff2(0.03)
blmeas_profile.crop()
blmeas_profile.reshape(1000)

ms.figure('Resolution', figsize=(10, 8))
ms.plt.subplots_adjust(hspace=0.4, wspace=0.8)
subplot = ms.subplot_factory(2, 3, grid=False)
sp_ctr = 1

image_file = data_dir1 + '2021_05_18-21_02_13_Lasing_False_SARBD02-DSCR050.h5'
image_dict = h5_storage.loadH5Recursive(image_file)
meta_data1 = image_dict['meta_data_begin']

screen_calib_file = data_dir1 + '2021_05_18-16_39_27_Screen_Calibration_SARBD02-DSCR050.h5'
screen_calib_dict = h5_storage.loadH5Recursive(screen_calib_file)

screen_calib_raw_image = screen_calib_dict['pyscan_result']['image'][0].astype(
    float)
x_axis_calib = screen_calib_dict['pyscan_result']['x_axis_m']
screen_x0 = gaussfit.GaussFit(x_axis_calib,
                              screen_calib_raw_image.sum(axis=0)).mean
x_axis_calib -= screen_x0
y_axis_calib = screen_calib_dict['pyscan_result']['y_axis_m']
screen_calib_raw_image -= np.median(screen_calib_raw_image)
screen_calib_image = iap.Image(screen_calib_raw_image, x_axis_calib,
                               y_axis_calib)
Ejemplo n.º 13
0
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#import analysis
import gaussfit
from h5_storage import loadH5Recursive

import myplotstyle as ms

plt.close('all')

filename = '/tmp/2021_03_16-20_22_26_Screen_data_SARBD02-DSCR050.h5'
dict_ = loadH5Recursive(filename)


ms.figure('Images')

subplot = ms.subplot_factory(3,3, grid=False)
sp_ctr = 1


sp_proj_x = subplot(sp_ctr, title='X')
sp_ctr += 1


sp_proj_y = subplot(sp_ctr, title='Y')
sp_ctr += 1

x_axis = dict_['pyscan_result']['x_axis'].astype(float)
y_axis = dict_['pyscan_result']['y_axis'].astype(float)
Ejemplo n.º 14
0
hostname = socket.gethostname()
if hostname == 'desktop':
    save_directory = '/storage/data_2020-07-26/'
    other = '/storage/data_2020-10-04/20201004_175405_undulator_wf.h5'
    magnet_file = '/storage/Philipp_data_folder/archiver_api_data/2020-07-26.h5'
else:
    save_directory = '/sf/data/measurements/2020/07/26/'
    other = '/sf/data/measurements/2020/10/04/20201004_175405_undulator_wf.h5'
    magnet_file = '/afs/psi.ch/intranet/SF/Beamdynamics/Philipp/data/archiver_api_data/2020-07-26.h5'

simulator = elegant_matrix.get_simulator(magnet_file)
timestamp = elegant_matrix.get_timestamp(2020, 7, 26, 17, 49, 0)
mat_dict, disp_dict = simulator.get_elegant_matrix(0, timestamp)
r12 = mat_dict['SARBD02.DSCR050'][0, 1]

other_dict = loadH5Recursive(other)
x_axis = other_dict['x_axis'].astype(np.float64) * 1e-6
y_axis = other_dict['y_axis'].astype(np.float64) * 1e-6
del other, other_dict

### Generate Gaussian beam to calculate initial wake potential

# approximate 40 fs beam
sig_t = 40e-15
time = np.linspace(0., 400e-15, 1000)

curr = np.exp(-(time - np.mean(time))**2 / (2 * sig_t**2))
curr[curr < 0.001 * curr.max()] = 0

curr = curr * 200e-12 / np.sum(curr)
wf_calc = wf_model.WakeFieldCalculator(time * c, curr, energy_eV, 1.)
import matplotlib.pyplot as plt
import socket

import h5_storage
import analysis

plt.close('all')

hostname = socket.gethostname()
if 'psi' in hostname or 'lc6a' in hostname or 'lc7a' in hostname:
    default_dir = '/sf/data/measurements/2021/04/25/'
    archiver_dir = '/afs/psi.ch/intranet/SF/Beamdynamics/Philipp/data/archiver_api_data/'
elif hostname == 'desktop':
    default_dir = '/storage/data_2021-04-25/'
    archiver_dir = '/storage/Philipp_data_folder/archiver_api_data/'
elif hostname == 'pubuntu':
    default_dir = '/home/work/data_2021-04-25/'
    archiver_dir = '/home/work/archiver_api_data/'

streaker_calib_file = default_dir + '2021_04_25-16_55_25_Calibration_SARUN18-UDCP020.h5'
data_dict = h5_storage.loadH5Recursive(streaker_calib_file)
analysis.analyze_streaker_calibration(data_dict['raw_data'])

plt.show()
    archiver_dir = '/storage/Philipp_data_folder/'
elif hostname == 'pc11292.psi.ch':
    dirname1 = '/sf/data/measurements/2020/10/03/'
    dirname2 = '/sf/data/measurements/2020/10/04/'
elif hostname == 'pubuntu':
    dirname1 = '/home/work/data_2020-10-03/'
    dirname2 = '/home/work/data_2020-10-04/'
    archiver_dir = '/home/work/'

magnet_file = archiver_dir + 'archiver_api_data/2020-10-03.h5'

blmeas38 = dirname1 + '129833611_bunch_length_meas.h5'

file0 = dirname1 + 'Passive_data_20201003T231958.mat'

dict0 = loadH5Recursive(file0 + '.h5')

subtract_min = True


def get_screen_from_proj(projX, x_axis, invert_x):
    if invert_x:
        xx, yy = (x_axis[::-1]).copy(), (projX[::-1]).copy()
    else:
        xx, yy = x_axis.copy(), projX.copy()
    if subtract_min:
        yy -= yy.min()
    screen = tracking.ScreenDistribution(xx, yy)
    screen.normalize()
    screen.cutoff(screen_cutoff)
    screen.reshape(len_profile)
Ejemplo n.º 17
0
lasing_on_files = [x[-1] for x in all_files if 'Lasing_True' in x[-1]]
lasing_off_files = []

for file1 in lasing_on_files:
    for _a1, _a2, _a3, file2 in all_files:
        if file2 == file1:
            a1, a2, a3 = _a1, _a2, _a3
            break
    for _a1, _a2, _a3, file2 in all_files:
        if (_a1, _a2, _a3) == (a1, a2, a3) and file1 != file2:
            lasing_off_files.append(file2)
            break

streaker_calib_file = data_dir1 + '/2021_05_18-22_11_36_Calibration_SARUN18-UDCP020.h5'
streaker_calib = h5_storage.loadH5Recursive(streaker_calib_file)['raw_data']
tracker_kwargs = config.get_default_tracker_settings()
gauss_kwargs = config.get_default_gauss_recon_settings()
charge = 200e-12

if False:
    tracker = tracking.Tracker(**tracker_kwargs)
    tracker.set_simulator(streaker_calib['meta_data_begin'])
    calib_dict = streaker_calibration.reconstruct_gap(streaker_calib, tracker,
                                                      gauss_kwargs, charge)
    streaker_offset = calib_dict['streaker_offset']
    screen_x0_arr = calib_dict['screen_x0']
    screen_x0 = screen_x0_arr[0]
    delta_gap = calib_dict['delta_gap']
else:
    delta_gap = 9.9929e-3 - 10e-3
Ejemplo n.º 18
0
def reconstruct_lasing(file_or_dict_on, file_or_dict_off, screen_center, structure_center, structure_length, file_current, r12, disp, energy_eV, charge, streaker, plot_handles, pulse_energy, n_slices, len_profile):

    if type(file_or_dict_on) is dict:
        dict_on = file_or_dict_on
    else:
        dict_on = h5_storage.loadH5Recursive(file_or_dict_on)
    dict_on_p = dict_on['pyscan_result']
    dict_on_m = dict_on['meta_data_end']

    if type(file_or_dict_off) is dict:
        dict_off = file_or_dict_off
    else:
        dict_off = h5_storage.loadH5Recursive(file_or_dict_off)
    dict_off_p = dict_off['pyscan_result']
    dict_off_m = dict_off['meta_data_end']

    if energy_eV == 'file':
        if 'SARBD01-MBND100:ENERGY-OP' in dict_on_m:
            energy_eV = dict_on_m['SARBD01-MBND100:ENERGY-OP']*1e6
        elif 'SARBD01-MBND100:P-SET' in dict_on_m:
            energy_eV = dict_on_m['SARBD01-MBND100:P-SET']*1e6
        else:
            raise ValueError('No energy saved!')


    input_dict = {
            'file_or_dict_on': file_or_dict_on,
            'file_or_dict_off': file_or_dict_off,
            'screen_center': screen_center,
            'structure_center': structure_center,
            'structure_length': structure_length,
            'file_current': file_current,
            'r12': r12,
            'disp': disp,
            'energy_eV': energy_eV,
            'charge': charge,
            'streaker': streaker,
            'pulse_energy': pulse_energy,
            }

    gaps, beam_offsets = [], []
    for dict_ in dict_off_m, dict_on_m:
        gaps.append(dict_[streaker+':GAP']*1e-3)
        beam_offsets.append(-(dict_[streaker+':CENTER']*1e-3-structure_center))

    if abs(gaps[0] - gaps[1]) > 1e-6:
        print('Gaps not the same!', gaps)
    if abs(beam_offsets[0] - beam_offsets[1]) > 1e-6:
        print('Beam offsets not the same!', beam_offsets)
    gap = np.mean(gaps)
    beam_offset = np.mean(beam_offsets)

    if pulse_energy is None:
        try:
            pulse_energy = dict_on_m[config.gas_monitor_pvs['Aramis']]*1e-6
        except KeyError:
            print('No pulse energy found! Use 100 uJ')
            pulse_energy = 100e-6

    images0 = dict_off_p['image'].astype(np.float64)
    images0_on = dict_on_p['image'].astype(np.float64)

    if 'x_axis_m' in dict_off_p:
        x_axis0 = dict_off_p['x_axis_m'].astype(np.float64)
        y_axis0 = dict_off_p['y_axis_m'].astype(np.float64)
    else:
        x_axis0 = dict_off_p['x_axis'].astype(np.float64)
        y_axis0 = dict_off_p['y_axis'].astype(np.float64)

    projx0 = images0.sum(axis=-2)
    median_index = misc.get_median(projx0, output='index')
    median_image_off = iap.Image(images0[median_index], x_axis0, y_axis0, x_offset=screen_center)

    projx0_on = images0_on.sum(axis=-2)
    median_index = misc.get_median(projx0_on, output='index')

    median_image_on = iap.Image(images0_on[median_index], x_axis0, y_axis0, x_offset=screen_center)

    current_dict = h5_storage.loadH5Recursive(file_current)
    if 'gaussian_reconstruction' in current_dict:
        wake_profile_dict = current_dict['gaussian_reconstruction']['reconstructed_profile']
        wake_profile = iap.BeamProfile.from_dict(wake_profile_dict)
    else:
        wake_profile = iap.profile_from_blmeas(current_dict, 200e-15, charge, energy_eV, True)

    wake_profile.cutoff2(0.1)
    wake_profile.crop()
    wake_profile.reshape(len_profile)

    wake_t, wake_x = wake_profile.get_x_t(gap, beam_offset, structure_length, r12)

    lasing_dict = lasing.obtain_lasing(median_image_off, median_image_on, n_slices, wake_x, wake_t, len_profile, disp, energy_eV, charge, pulse_energy=pulse_energy, debug=False)

    if plot_handles is None:
        plot_handles = lasing_figures()

    (fig, (sp_profile, sp_wake, sp_off, sp_on, sp_off_cut, sp_on_cut, sp_off_tE, sp_on_tE)) = plot_handles[0]
    (fig, (sp_power, sp_current, sp_centroid, sp_slice_size)) = plot_handles[1]

    slice_time = lasing_dict['slice_time']
    all_slice_dict = lasing_dict['all_slice_dict']
    power_from_Eloss = lasing_dict['power_Eloss']
    power_from_Espread = lasing_dict['power_Espread']

    sp_current.plot(slice_time*1e15, all_slice_dict['Lasing_off']['slice_current'], label='Off')
    sp_current.plot(slice_time*1e15, all_slice_dict['Lasing_on']['slice_current'], label='On')

    lasing_dict['all_images']['Lasing_off']['image_tE'].plot_img_and_proj(sp_off_tE)
    lasing_dict['all_images']['Lasing_on']['image_tE'].plot_img_and_proj(sp_on_tE)

    lasing_dict['all_images']['Lasing_off']['image_cut'].plot_img_and_proj(sp_off_cut)
    lasing_dict['all_images']['Lasing_on']['image_cut'].plot_img_and_proj(sp_on_cut)

    for key, sp_tE in [('Lasing_off',sp_off_tE), ('Lasing_on',sp_on_tE)]:
        slice_sigma = all_slice_dict[key]['slice_sigma']
        slice_centroid = all_slice_dict[key]['slice_mean']
        sp_slice_size.plot(slice_time*1e15, slice_sigma/1e6, label=key)
        sp_centroid.plot(slice_time*1e15, slice_centroid/1e6, label=key)

        lims = sp_tE.get_ylim()
        sp_tE.errorbar(slice_time*1e15, slice_centroid/1e6, yerr=slice_sigma/1e6, ls='None', marker='+', color='red')
        sp_tE.set_ylim(*lims)

    sp_power.plot(slice_time*1e15, power_from_Eloss/1e9, label='$\Delta$E')
    sp_power.plot(slice_time*1e15, power_from_Espread/1e9, label='$\Delta\sigma_E$')

    sp_current.legend()
    sp_power.legend()
    sp_slice_size.legend()
    sp_centroid.legend()

    median_image_off.plot_img_and_proj(sp_off)
    median_image_on.plot_img_and_proj(sp_on)

    sp_wake.plot(wake_t*1e15, wake_x*1e3)
    wake_profile.plot_standard(sp_profile)

    if type(input_dict['file_or_dict_on']) is dict:
        del input_dict['file_or_dict_on']
    if type(input_dict['file_or_dict_off']) is dict:
        del input_dict['file_or_dict_off']

    output = {
            'input': input_dict,
            'lasing_dict': lasing_dict,
            }
    #import pdb; pdb.set_trace()
    return output
Ejemplo n.º 19
0
len_profile = int(2e3)

data_files = [data_dir + os.path.basename(x) for x in data_files0]

#Lasing off
lasing_off_file0 = '/sf/data/measurements/2021/03/16/2021_03_16-20_42_57_Screen_data_SARBD02-DSCR050.h5'
lasing_off_file = data_dir + os.path.basename(lasing_off_file0)

median_indices = OrderedDict()
median_images = OrderedDict()

full_slice_dict = OrderedDict()

for ctr, (data_file, label) in enumerate([(lasing_off_file, 'Lasing Off'),
                                          (data_files[-1], 'Lasing On')]):
    data_dict = loadH5Recursive(data_file)
    images = data_dict['pyscan_result']['image'].astype(np.float64)
    x_axis = data_dict['pyscan_result']['x_axis'].astype(np.float64)
    y_axis = data_dict['pyscan_result']['y_axis'].astype(np.float64)

    projx = images.sum(axis=-2)

    median_index = misc.get_median(projx, output='index')
    median_indices[label] = median_index
    print(label, median_index)

    sp_ctr = np.inf
    ny, nx = 3, 3
    subplot = ms.subplot_factory(ny, nx)

    full_slice_dict[label] = np.zeros([len(images), 5, n_slices])
Ejemplo n.º 20
0
def reconstruct_current(data_file_or_dict, n_streaker, beamline, tracker_kwargs_or_tracker, rec_mode, kwargs_recon, screen_x0, streaker_centers, blmeas_file=None, plot_handles=None, do_plot=True):

    if type(tracker_kwargs_or_tracker) is dict:
        tracker = tracking.Tracker(**tracker_kwargs_or_tracker)
    elif type(tracker_kwargs_or_tracker) is tracking.Tracker:
        tracker = tracker_kwargs_or_tracker
    else:
        raise ValueError(type(tracker_kwargs_or_tracker))

    if type(data_file_or_dict) is dict:
        screen_data = data_file_or_dict
    else:
        screen_data = h5_storage.loadH5Recursive(data_file_or_dict)

    if 'meta_data' in screen_data:
        meta_data = screen_data['meta_data']
    elif 'meta_data_begin' in screen_data:
        meta_data = screen_data['meta_data_begin']
    else:
        print(screen_data.keys())
        raise ValueError

    if 'pyscan_result' in screen_data:
        pyscan_data = screen_data['pyscan_result']
    else:
        pyscan_data = screen_data

    x_axis = pyscan_data['x_axis_m']
    projx = pyscan_data['image'].sum(axis=-2)
    if rec_mode == 'Median':
        median_projx = misc.get_median(projx)
        proj_list = [median_projx]
    elif rec_mode == 'All':
        proj_list = projx

    tracker.set_simulator(meta_data)

    if x_axis[1] < x_axis[0]:
        revert = True
        x_axis = x_axis[::-1]
    else:
        revert = False

    output_dicts = []
    for proj in proj_list:
        if revert:
            proj = proj[::-1]

        meas_screen = tracking.ScreenDistribution(x_axis, proj)
        kwargs_recon['meas_screen'] = meas_screen

        #print('Analysing reconstruction')
        kwargs = copy.deepcopy(kwargs_recon)

        gaps, streaker_offsets = get_gap_and_offset(meta_data, beamline)

        kwargs['meas_screen']._xx = kwargs['meas_screen']._xx - screen_x0
        kwargs['beam_offsets'] = -(streaker_offsets - streaker_centers)
        kwargs['gaps'] = gaps
        kwargs['meas_screen'].cutoff2(tracker.screen_cutoff)
        kwargs['meas_screen'].crop()
        kwargs['meas_screen'].reshape(tracker.len_screen)
        kwargs['n_streaker'] = n_streaker

        # Only allow one streaker at the moment
        for n in (0,1):
            if n != kwargs['n_streaker']:
                kwargs['beam_offsets'][n] = 0

        gauss_dict = current_profile_rec_gauss(tracker, kwargs, plot_handles, blmeas_file, do_plot=do_plot)
        output_dict = {
                'input': {
                    'data_file_or_dict': data_file_or_dict,
                    'n_streaker': n_streaker,
                    'beamline': beamline,
                    'tracker_kwargs': tracker_kwargs_or_tracker,
                    'rec_mode': rec_mode,
                    'kwargs_recon': kwargs_recon,
                    'screen_x0': screen_x0,
                    'streaker_centers': streaker_centers,
                    'blmeas_file': blmeas_file,
                    },
                'gauss_dict': gauss_dict,
                }
        output_dicts.append(output_dict)

    if rec_mode == 'Median':
        return output_dict
    elif rec_mode == 'All':
        return output_dicts
    else:
        print(rec_mode)
Ejemplo n.º 21
0
import matplotlib.pyplot as plt
import h5_storage
import analysis

plt.close('all')

file_on = '/sf/data/measurements/2021/05/18/2021_05_18-18_13_33_Lasing_True_SARBD02-DSCR050.h5'
file_off = '/sf/data/measurements/2021/05/18/2021_05_18-18_27_23_Lasing_False_SARBD02-DSCR050.h5'

dict_on = h5_storage.loadH5Recursive(file_on)

energy_eV = dict_on['meta_data_end']['SARBD01-MBND100:ENERGY-OP'] * 1e6

screen_center = 900e-6
structure_center = 370e-6

structure_length = 1

file_current = '/sf/data/measurements/2021/05/18/2021_05_18-19_26_41_PassiveReconstruction.h5'

r12 = 7.130170460315602
disp = 0.439423807827296
streaker = 'SARUN18-UDCP020'
pulse_energy = 600e-6
charge = 200e-12

obj = analysis.Reconstruction(screen_center, [0, structure_center])

analysis.reconstruct_lasing(file_on, file_off, screen_center, structure_center,
                            structure_length, file_current, r12, disp,
                            energy_eV, charge, streaker, None, pulse_energy)
Ejemplo n.º 22
0
        5.06: '/sf/data/measurements/2021/04/25/2021_04_25-17_16_30_Lasing_False_SARBD02-DSCR050.h5',
        5.04: '/sf/data/measurements/2021/04/25/2021_04_25-17_33_31_Lasing_False_SARBD02-DSCR050.h5',
        5.02: '/sf/data/measurements/2021/04/25/2021_04_25-17_36_28_Lasing_False_SARBD02-DSCR050.h5',
        5.00: '/sf/data/measurements/2021/04/25/2021_04_25-17_37_20_Lasing_False_SARBD02-DSCR050.h5',
        -4.4: '/sf/data/measurements/2021/04/25/2021_04_25-17_40_15_Lasing_False_SARBD02-DSCR050.h5',
        -4.42: '/sf/data/measurements/2021/04/25/2021_04_25-17_41_26_Lasing_False_SARBD02-DSCR050.h5',
        -4.38: '/sf/data/measurements/2021/04/25/2021_04_25-17_42_51_Lasing_False_SARBD02-DSCR050.h5',
        -4.36: '/sf/data/measurements/2021/04/25/2021_04_25-17_44_59_Lasing_False_SARBD02-DSCR050.h5',
        }


screen_analysis = analysis.analyze_screen_calibration(screen_file)
screen_center = screen_analysis['x0']
print('Screen center module', screen_center*1e6, 'um')

calib_dict = h5_storage.loadH5Recursive(calib_file)
raw_data = calib_dict['raw_data']

result0 = analysis.analyze_streaker_calibration(raw_data, True)
print('Module streaker center', result0['meta_data']['streaker_offset']*1e6, 'um')

mask = np.logical_and(raw_data['streaker_offsets'] < 5.04e-3, raw_data['streaker_offsets'] > -0.0044)
#mask = np.ones_like(raw_data['streaker_offsets'], dtype=bool)
raw_data['streaker_offsets'] = raw_data['streaker_offsets'][mask]
raw_data['pyscan_result']['image'] = raw_data['pyscan_result']['image'][mask]
print(raw_data['streaker_offsets'])


ms.figure('Screen projections')
subplot = ms.subplot_factory(1,1)
sp_proj = subplot(1, title='Screen projections', xlabel='x (mm)', ylabel='Intensity (arb. units)')
Ejemplo n.º 23
0
 def add_file(self, filename):
     data_dict = h5_storage.loadH5Recursive(filename)
     self.add_dict(data_dict)
files2 = [
    data_dir2 + '2021_05_19-14_14_22_Calibration_SARUN18-UDCP020.h5',
    data_dir2 + '2021_05_19-14_24_05_Calibration_SARUN18-UDCP020.h5',
]

files3 = [
    data_dir +
    '2021_05_19-00_13_25_Calibration_SARUN18-UDCP020.h5',  # Bad data
    data_dir +
    '2021_05_19-00_24_47_Calibration_SARUN18-UDCP020.h5',  # Bad data maybe
]

for files in files1, files2:
    tracker_kwargs = config.get_default_tracker_settings()
    dict1 = h5_storage.loadH5Recursive(files[0])
    dict2 = h5_storage.loadH5Recursive(files[1])
    dict1 = sc.analyze_streaker_calibration(dict1['raw_data'], False)
    dict2 = sc.analyze_streaker_calibration(dict2['raw_data'], False)
    magnet_data = dict1['raw_data']['meta_data_begin']
    tracker_kwargs['magnet_file'] = magnet_data
    tracker_kwargs['quad_wake'] = False

    tracker = tracking.Tracker(**tracker_kwargs)
    #tracker.wake2d = True
    #tracker.split_streaker = 5
    #tracker.set_simulator(magnet_data)

    meta_data = sc.analyze_streaker_calibration_stitch_together(
        dict1,
        dict2,
Ejemplo n.º 25
0
load_compact = True
storage_dir = '/storage/'

if home_office:

    def loadH5Recursive2(path):
        return loadH5Recursive(
            os.path.join(storage_dir + 'data_2020-02-03/',
                         os.path.basename(path)))
else:
    loadH5Recursive2 = loadH5Recursive

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
    })
import h5_storage
import image_and_profile as iap

file_ = '/home/work/tmp_reconstruction/2021_04_26-12_23_40_PassiveReconstruction.h5'

dd = h5_storage.loadH5Recursive(file_)
rec_profile = iap.BeamProfile.from_dict(
    dd['gaussian_reconstruction']['reconstructed_profile'])
Ejemplo n.º 27
0
    archiver_dir = '/home/work/'

magnet_file = archiver_dir + 'archiver_api_data/2020-10-03.h5'

blmeas38 = dirname1 + '129833611_bunch_length_meas.h5'
#blmeas25 = dirname2 + 'Bunch_length_meas_2020-10-04_14-43-30.h5'
#blmeas25 = dirname2 + '129918532_bunch_length_meas.h5'
blmeas25 = dirname2 + 'Bunch_length_meas_2020-10-04_14-53-46.h5'

file0 = dirname1 + 'Passive_data_20201003T231958.mat'

file38 = dirname1 + 'Passive_data_20201003T233852.mat'
file25 = dirname2 + 'Passive_data_20201004T172425.mat'
file25b = dirname2 + 'Passive_data_20201004T163828.mat'

dict38 = loadH5Recursive(file38 + '.h5')
dict25 = loadH5Recursive(file25 + '.h5')
dict0 = loadH5Recursive(file0 + '.h5')
dict25b = loadH5Recursive(file25b + '.h5')

subtract_min = True


def get_screen_from_proj(projX, x_axis, invert_x):
    if invert_x:
        xx, yy = (-x_axis[::-1]).copy(), (projX[::-1]).copy()
    else:
        xx, yy = x_axis.copy(), projX.copy()
    if subtract_min:
        yy -= yy.min()
Ejemplo n.º 28
0
def load_screen_data(filename_or_dict, key, index):
    if type(filename_or_dict) is dict:
        dict_ = filename_or_dict
    elif filename_or_dict.endswith('.h5'):
        dict_ = loadH5Recursive(filename_or_dict)
    elif filename_or_dict.endswith('.mat'):
        dict_ = loadmat(filename_or_dict)
    else:
        raise ValueError('Must be h5 or mat file. Is: %s' % filename_or_dict)

    dict0 = dict_
    if 'pyscan_result' in dict0:
        dict_ = dict_['pyscan_result']
        key = 'image'
    else:
        dict_ = dict0

    if 'x_axis_m' not in dict_:
        print(dict_.keys())
    x_axis = dict_['x_axis_m']
    data = dict_[key].astype(float)
    if index not in ('None', None):
        index = int(index)
        data = data[index]

    if len(data.shape) == 2:
        # Assume saved data are already projections
        projx = data
    elif len(data.shape) == 3:
        # Assume saved data are images
        projx = np.zeros((data.shape[0], len(x_axis)))
        for n_img, img in enumerate(data):
            try:
                projx[n_img, :] = img.sum(axis=0)
            except:
                import pdb
                pdb.set_trace()
    else:
        raise ValueError('Expect shape of 2 or 3. Is: %i' % len(data.shape))

    y_axis = dict_['y_axis_m'] if 'y_axis_m' in dict_ else None

    if np.abs(x_axis.max()) > 1:
        x_axis *= 1e-6
        print('Converting x_axis from um to m')
    if np.abs(y_axis.max()) > 1:
        y_axis *= 1e-6
        print('Converting y_axis from um to m')

    # TBD
    # - Add y information. Needed for lasing reconstruction.

    if x_axis[1] < x_axis[0]:
        x_axis = x_axis[::-1]
        projx = projx[:, ::-1]
    if y_axis[1] < y_axis[0]:
        y_axis = y_axis[::-1]

    output = {
        'x_axis': x_axis,
        'projx': projx,
        'y_axis': y_axis,
    }
    if 'meta_data' in dict0:
        output['meta_data'] = dict0['meta_data']
    if 'meta_data_end' in dict0:
        output['meta_data'] = dict0['meta_data_end']
    return output
Ejemplo n.º 29
0
    archiver_dir = '/home/work/'

magnet_file = archiver_dir + 'archiver_api_data/2020-10-03.h5'

blmeas38 = dirname1 + '129833611_bunch_length_meas.h5'
blmeas25 = dirname2 + '129918532_bunch_length_meas.h5'
blmeas25 = dirname2 + 'Bunch_length_meas_2020-10-04_14-43-30.h5'
blmeas13 = dirname2 + '129920069_bunch_length_meas.h5'

file0 = dirname1 + 'Passive_data_20201003T231958.mat'

file38 = dirname1 + 'Passive_data_20201003T233852.mat'
file25 = dirname2 + 'Passive_data_20201004T172425.mat'
file13 = dirname2 + 'Passive_data_20201004T221502.mat'

dict38 = loadH5Recursive(file38 + '.h5')
dict25 = loadH5Recursive(file25 + '.h5')
dict0 = loadH5Recursive(file0 + '.h5')
dict13_h5 = loadH5Recursive(file13 + '.h5')
dict13_h5['value'] = np.array(-4.25)  # from archiver
#dict13_h5['x_axis'] = dict25['x_axis']

proj13 = dict13_h5['projx']
proj13_new = np.reshape(proj13,
                        (proj13.shape[0] * proj13.shape[1], proj13.shape[2]))
dict13_h5['projx'] = proj13_new


def get_screen_from_proj(projX, x_axis, invert_x):
    if invert_x:
        xx, yy = (-x_axis[::-1]).copy(), (projX[::-1]).copy()
Ejemplo n.º 30
0
 def add_other_h5(self, file_):
     new_dict = loadH5Recursive(file_)
     assert len(set(new_dict.keys()).intersection(set(self.keys()))) == 0
     self.update(new_dict)