%Input data
run = [35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48];
gap_nom = [11, 12, 13, 15, 17, 20, 25, 30, 50, 100, 30, 20, 15, 10];
enc = [412519, 432530, 452540, 492555, 532556, 592571, 692575, 792573, 1192569, 2192563, 792551, 592546, 492526, 392476];
phase_int = [4.2255e-06, 3.8589e-06, 3.5270e-06, 2.9541e-06, 2.4818e-06, 1.9205e-06, 1.2679e-06, 8.4891e-07, 1.9206e-07, 9.3930e-09, 8.4883e-07, 1.9200e-06, 2.9537e-06, 4.6323e-06];

%Calculate the gap, run 48 is 10 mm
%gap = 10 + (enc - enc(end)) / 20000;
gap = 10 + (enc - 392573) / 20000;

%Write the SLAC measurements to the data file
dat_file = 'c:\magdata\danfysik phase shifter\slac_meas.dat;';
fid = fopen(dat_file, 'w');
fprintf(fid, '\r\n');
fprintf(fid, 'Danfysik Phase Shifter\r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'SLAC Measurements:\r\n');
fprintf(fid, ' Run    Nom Gap   Encoder     Gap          PI      \r\n');
fprintf(fid, '         (mm)                 (mm)     (T**2 mm**3)\r\n');
fprintf(fid, '-----  ---------  --------  ---------  ------------\r\n');
fprintf(fid, '%5i  %9.3f  %8i  %9.3f  %12.1f\r\n', [run; gap_nom; enc; gap; phase_int * 1.e9]);
fclose(fid);

%Fit the phase integral vs gap
index_array = find(gap < 31);
gg = gap(index_array);
xx = log(phase_int(index_array));
p = polyfit(gg, xx, 1);
p1 = p(1);
p2 = p(2);
g_fit = 10 : 30;
x_fit = p1 * g_fit + p2;
pi_fit = phase_int(end) * exp(p1 * (g_fit - g_fit(1)));

%Display p1
p1

%Plot the phase integral vs gap
figure;
hold on;
plot(gap, phase_int * 1.e9, 'b*');
plot(g_fit, pi_fit * 1.e9, 'g-');
title('Danfysik SXR Prototype Phase Shifter, SLAC PI vs Gap');
xlabel('Gap (mm)');
ylabel('PI (T**2 mm**3)');
grid on;
zoom on;
hold off;
%util_print_fig(plt_file);

%Danfysik data, temperature corrected
d_gap = [10, 12, 15, 20, 30, 100];
d_enc = [392573, 0, 0, 0, 0, 2192621];
d_pi = [4610, 3841, 2942, 1913, 847, 9] * 1.e-9;

%Write the Danfysik measurements to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'Danfysik Measurements:\r\n');
fprintf(fid, ' Nom Gap   Encoder       PI      \r\n');
fprintf(fid, '  (mm)               (T**2 mm**3)\r\n');
fprintf(fid, '---------  --------  ------------\r\n');
fprintf(fid, '%9.3f  %8i  %12.1f\r\n', [d_gap; d_enc; d_pi * 1.e9]);
fclose(fid);

%Plot the Danfysik phase integral vs gap
figure;
hold on;
plot(d_gap, d_pi * 1.e9, 'r*');
title('Danfysik SXR Prototype Phase Shifter, Danfysik PI vs Gap, Temp Corr 20C');
xlabel('Gap (mm)');
ylabel('PI (T**2 mm**3)');
grid on;
zoom on;
hold off;

%Plot both data sets
figure;
hold on;
plot(gap, phase_int * 1.e9, 'b*');
plot(d_gap, d_pi * 1.e9, 'r*');
legend('SLAC', 'Danfysik, TC 20C');
title('Danfysik SXR Prototype Phase Shifter, PI vs Gap');
xlabel('Gap (mm)');
ylabel('PI (T**2 mm**3)');
grid on;
zoom on;
hold off;

%Compare at same gaps
s_gap = [10, 12, 15, 20, 30, 100];
%s_pi = [11x4.2255e-06, 12x3.8589e-06, 13x3.5270e-06, 15x2.9541e-06, 17x2.4818e-06, 20x1.9205e-06, 25x1.2679e-06, 30x8.4891e-07, 50x1.9206e-07, 100x9.3930e-09, 30x8.4883e-07, 20x1.9200e-06, 15x2.9537e-06, 10x4.6323e-06];
s_pi = [4.6323e-06, 3.8589e-06, 2.9541e-06, 1.9205e-06, 8.4891e-07, 9.3930e-09];
diff_pi = s_pi - d_pi;

%Plot both data sets
figure;
hold on;
plot(s_gap, diff_pi * 1.e9, 'b*');
title('(PI SLAC - PI Danfysik) vs Gap');
xlabel('Gap (mm)');
ylabel('Diff PI (T**2 mm**3)');
grid on;
zoom on;
hold off;

%Plot both data sets
figure;
hold on;
plot(s_gap, diff_pi ./ s_pi, 'b*');
title('(PI SLAC - PI Danfysik) / PI SLAC vs Gap');
xlabel('Gap (mm)');
ylabel('Rel Diff PI');
grid on;
zoom on;
hold off;

%Display the mean relative diffence
index_array = find(s_gap < 50);
rel_diff = diff_pi(index_array) ./ s_pi(index_array);
rel_diff_ave = mean(rel_diff);
rel_diff_ave



