%**************************************************************************
%This function uses different methods to calculate K_eff.  It uses all the
%poles in the calculation, all the even poles, all the odd poles, and all
%the points in the interior of the undulator.  The results are written to
%the data file.
%
%Input:
%dat_file, data file for results
%gamma, Lorentz factor of the beam
%z, z position along the undulator (m)
%slip, slippage at z (m)
%z_peak_no_ends, z positions of the field peaks, no ends (m)
%slip_peak_no_ends, slippage at the field peaks, no ends (m)
%
%Zachary Wolf, 2/23/07

function [] = k_dat_keff_check(dat_file, gamma, z, slip, z_peak_no_ends, slip_peak_no_ends)

%Use all the peaks
z_0 = z_peak_no_ends;
slip_0 = slip_peak_no_ends;
[s_0, i_0, resid_0] = slip_fit_slip_at_peaks(z_0, slip_0);
k_0 = k_calc_keff(gamma, s_0);

%Use every other peak
z_1 = z_peak_no_ends(1 : 2 : length(z_peak_no_ends));
slip_1 = slip_peak_no_ends(1 : 2 : length(z_peak_no_ends));
[s_1, i_1, resid_1] = slip_fit_slip_at_peaks(z_1, slip_1);
k_1 = k_calc_keff(gamma, s_1);

%Use the other peaks
z_2 = z_peak_no_ends(2 : 2 : length(z_peak_no_ends));
slip_2 = slip_peak_no_ends(2 : 2 : length(z_peak_no_ends));
[s_2, i_2, resid_2] = slip_fit_slip_at_peaks(z_2, slip_2);
k_2 = k_calc_keff(gamma, s_2);

%Use all the slippage samples within the undulator
index_array = find((z > z_peak_no_ends(1)) & (z < z_peak_no_ends(end)));
z_3 = z(index_array);
slip_3 = slip(index_array);
[s_3, i_3, resid_3] = slip_fit_slip_at_peaks(z_3, slip_3);
k_3 = k_calc_keff(gamma, s_3);

%Write the results to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'Calculate Keff Using Different Methods:\r\n');
fprintf(fid, 'All poles, no ends\r\n');
fprintf(fid, 'Keff_0 = %f\r\n', k_0);
fprintf(fid, 'First and odd poles, no ends\r\n');
fprintf(fid, 'Keff_1 = %f\r\n', k_1);
fprintf(fid, 'Second and even poles, no ends\r\n');
fprintf(fid, 'Keff_2 = %f\r\n', k_2);
fprintf(fid, 'All slippage samples, no ends\r\n');
fprintf(fid, 'Keff_3 = %f\r\n', k_3);
fclose(fid);

%Done
