
%Enter parameters
output_directory = 'c:\magdata\break lengths\';
output_filename = 'break_lengths.dat';
slot = 1 : 33;
gamma = [26692.70, 26690.65, 26688.61, 26686.34, 26684.30, ...
         26682.25, 26679.99, 26677.94, 26675.90, 26673.63, ...
         26671.59, 26669.54, 26667.27, 26665.23, 26663.19, ...
         26660.92, 26658.88, 26656.83, 26654.56, 26652.52, ...
         26650.48, 26648.21, 26646.17, 26644.12, 26641.85, ...
         26639.81, 26637.77, 26635.50, 26633.46, 26631.41, ...
         26629.14, 26627.10, 26625.06];
% k = [3.500000, 3.499557, 3.499114, 3.498623, 3.498180, ...
%      3.497737, 3.497246, 3.496803, 3.496360, 3.495869, ...
%      3.495426, 3.494984, 3.494492, 3.494049, 3.493607, ...
%      3.493115, 3.492672, 3.492230, 3.491738, 3.491296, ...
%      3.490853, 3.490361, 3.489919, 3.489476, 3.488984, ...
%      3.488542, 3.488099, 3.487608, 3.487165, 3.486722, ...
%      3.486231, 3.485788, 3.485345];
k = [3.500000, 3.499557, 3.499114, 3.498623, 3.498180, ...
     3.497737, 3.497246, 3.496803, 3.496360, 3.495869, ...
     3.495426, 3.494984, 3.494492, 3.494049, 3.493607, ...
     3.493115, 3.492672, 3.492230, 3.491738, 3.491296, ...
     3.490853, 3.490361, 3.489919, 2.255709, 2.255328, ...
     2.254986, 2.254644, 2.254263, 2.253921, 2.253578, ...
     2.253198, 2.252856, 2.252513];
lambda_u = 0.03;  %(m)
l_break = [0.470, 0.470, 0.898, 0.470, 0.470, ...
           0.898, 0.470, 0.470, 0.898, 0.470, ...
           0.470, 0.898, 0.470, 0.470, 0.898, ...
           0.470, 0.470, 0.898, 0.470, 0.470, ...
           0.898, 0.470, 0.470, 0.898, 0.470, ...
           0.470, 0.898, 0.470, 0.470, 0.898, ...
           0.470, 0.470, 0.898];  %(m)
l_cell = 3.656;  %(m)
l_und = 3.400;  %(m)
lambda_r_nom = 1.5e-10 * ones(1, length(slot));  %(m)
lambda_r_nom(24 : 33) = lambda_r_nom(24 : 33) / 2;

%Calculate the radiation wavelength
lambda_r = lambda_u * (1 + (1 / 2) * k.^2) ./ (2 * gamma.^2);

%Calculate the drift lengths
l_drift = l_break - (l_cell - l_und) / 2 - (l_cell - l_und) / 2;

%Calculate the slippage in the drifts
s_drift = l_drift ./ (2 * gamma.^2);

%Calculate the phase change in the drifts
p_drift = s_drift * 360. ./ lambda_r_nom;

%Calculate the phase error in the drifts
pe_drift = mod(p_drift, 360.);

%Calculate the phase correction to the undulator exit end
p_corr_und_exit = - pe_drift;

%Write the results
dat_file = [output_directory, output_filename];
fid = fopen(dat_file, 'w');
c = fix(clock);
fprintf(fid, 'SLAC Magnetic Measurements\r\n');
fprintf(fid, 'Date: %02i-%02i-%4i\r\n', c(2), c(3), c(1));
fprintf(fid, 'Time: %02i:%02i:%02i\r\n', c(4), c(5), c(6));
fprintf(fid, '\r\n\r\n');
fprintf(fid, 'Undulator Period = %f m\r\n', lambda_u);
fprintf(fid, 'Undulator Length = %f m\r\n', l_und);
fprintf(fid, 'Cell Length = %f m\r\n', l_cell);
fprintf(fid, '\r\n\r\n');
fprintf(fid, '                         Phase In Drift Regions Between Cells           \r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'Slot    Gamma        K       Lambda_r  Lam_r_nom  L_break  L_drift   S_drift    P_drift \r\n');
fprintf(fid, ' #                             (A)        (A)       (m)      (m)       (A)       (deg)  \r\n');
fprintf(fid, '----  ---------  ---------  ---------  ---------  -------  -------  ---------  ---------\r\n');
fprintf(fid, '%4i  %9.2f  %9.6f  %9.6f  %9.6f  %7.3f  %7.3f  %9.6f  %9.3f\r\n', [slot; gamma; k; lambda_r * 1.e10; lambda_r_nom * 1.e10; l_break; l_drift; s_drift * 1.e10; p_drift]);
%fprintf(fid, '%c', 12);  %page break
fprintf(fid, '\f');  %page break
fprintf(fid, '\r\n\r\n');
fprintf(fid, '      Phase Correction To Undulator Exit End           \r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'Slot    Gamma        K      P_Err_dft  dP_Und_Exit\r\n');
fprintf(fid, ' #                            (deg)       (deg)   \r\n');
fprintf(fid, '----  ---------  ---------  ---------  -----------\r\n');
fprintf(fid, '%4i  %9.2f  %9.6f  %9.3f  %11.3f\r\n', [slot; gamma; k; pe_drift; p_corr_und_exit]);
fclose(fid);
