%**************************************************************************
%This function determines the shims required to obtain the proper phase
%relation into and out of the undulator.
%
%Input:
%dat_file, data file
%plt_file, plot file
%param, parameter structure
%shim_param, shim parameter structure
%field, magnetic field structure
%traj, trajectory structure
%rad, structure containing radiation information
%
%Output:
%phasematch_shim, structure containing the phase matching shim information
%   expect_phase_poles  %expected phases on the poles based on the phase match phase
%   expect_phase_cell   %expected phase of the cell based on the pahse match phase
%   phase_err_enter     %phase errors on the poles entering the undulator (rad)
%   phase_err_exit      %phase errors on the poles leaving the undulator (rad)
%   des_phase_change_enter  %desired phase change from the shim at the entrance (rad)
%   des_phase_change_exit   %desired phase change from the shim at the exit (rad)
%   shim_dim_enter      %size of the shim required to give the phase change at the entrance (m)
%   shim_dim_exit       %size of the shim required to give the phase change at the exit (m)
%
%Zachary Wolf
%12/28/05

function [phasematch_shim] = pid_shim_phasematch(dat_file, plt_file, param, shim_param, field, traj, rad)

%Calculate the expected phase at the poles in the undulator and of the cell
[expect_phase_poles, expect_phase_cell] = phase_match_calc_phase_match_expect_phase(param.nom_phase_match, param.num_poles, param.num_end_poles);

%Calculate the phase errors entering and leaving the undulator
[phase_err_enter, phase_err_exit] = phase_match_calc_phase_match_err(expect_phase_poles, expect_phase_cell, param.num_end_poles, rad.phase_nom_peak, rad.phase_nom_cell);

%Calculate the desired phase changes at the entrance and exit
index_array = find(abs(phase_err_enter) > 0);
des_phase_change_enter = - mean(phase_err_enter(index_array));
index_array = find(abs(phase_err_exit) > 0);
des_phase_change_exit = - mean(phase_err_exit(index_array));

%Calculate entrance shim dimensions
shim_dim_enter = phaserr_shim_calc_phase_shim_dim(shim_param, 1, des_phase_change_enter);
if des_phase_change_enter > 0
    shim_dim_enter = - shim_dim_enter;
end

%Calculate exit shim dimensions
shim_dim_exit = phaserr_shim_calc_phase_shim_dim(shim_param, 1, des_phase_change_exit);
if des_phase_change_exit > 0
    shim_dim_exit = - shim_dim_exit;
end

%Save the results
phasematch_shim.expect_phase_poles = expect_phase_poles;
phasematch_shim.expect_phase_cell = expect_phase_cell;
phasematch_shim.phase_err_enter = phase_err_enter;
phasematch_shim.phase_err_exit = phase_err_exit;
phasematch_shim.des_phase_change_enter = des_phase_change_enter;
phasematch_shim.des_phase_change_exit = des_phase_change_exit;
phasematch_shim.shim_dim_enter = shim_dim_enter;
phasematch_shim.shim_dim_exit = shim_dim_exit;

%Model the phase at the poles using the impulse model, for comparison
[model_phase_poles, model_phase_cell] = phase_match_calc_phase_match_impulse_model(param);

%Write the results to the data file
phase_match_shim_dat_phase_match_shim(dat_file, param, shim_param, rad, phasematch_shim, model_phase_poles, model_phase_cell);

%Done
