% Analyze SOL1

%  Load Data
Load_bhxyzvsxyplt_dev(2);  % B vs I -10 to 10 A at center
Load_bhxyzvsxyplt_dev(3);  % B vs z -10, -5, 5 10 amps along centerline
Load_bhxyzvsxyplt_dev(4); % Power supply off
Load_harplt_dev(5); %  Harmonics of solenoid coil 
Load_strplt_dev(6); % Integrated dipole at various x offsets
Load_strplt_dev(8); % Normal quad int grad
Load_strplt_dev(9); % Skew quad int grad
Load_strplt_dev(10); % Main quad int grad
Load_strplt_dev(14); % Main quad int grad canceled by trims
Load_bhvszplt_dev(15) % Bz at z = 0 at -10 to 10 amps in 0.5 amp steps
Load_bhvszplt_dev(16) % Bz vs z at -10, -5, 5, and 10 amps



% Plot B field vs current

figure
plot(Bhxyzvsxyplt2.Imag(:,1), Bhxyzvsxyplt2.Bz*10000, 'bo');
% hold
xlabel('Current (A)');
ylabel('Bz (G)');
title('Bz vs current at SOL1 center');
legend('Bz')

figure
plot(Bhxyzvsxyplt3.Y(1:151), Bhxyzvsxyplt3.Bx(1:151)*10000, 'bo');
 hold
 plot(Bhxyzvsxyplt3.Y(152:302), Bhxyzvsxyplt3.Bx(152:302)*10000, 'r*');
 plot(Bhxyzvsxyplt3.Y(303:453), Bhxyzvsxyplt3.Bx(303:453)*10000, 'g^');
 plot(Bhxyzvsxyplt3.Y(454:end), Bhxyzvsxyplt3.Bx(454:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('Bx (G)');
title('Bx vs z along centerline');
legend('-10 A', '-5 A', '5 A', '10 A')
saveas(gcf,'Bx vs z along mechanical centerline.png');

figure
plot(Bhxyzvsxyplt3.Y(1:151), Bhxyzvsxyplt3.By(1:151)*10000, 'bo');
 hold
 plot(Bhxyzvsxyplt3.Y(152:302), Bhxyzvsxyplt3.By(152:302)*10000, 'r*');
 plot(Bhxyzvsxyplt3.Y(303:453), Bhxyzvsxyplt3.By(303:453)*10000, 'g^');
 plot(Bhxyzvsxyplt3.Y(454:end), Bhxyzvsxyplt3.By(454:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('By (G)');
title('By vs z along centerline');
legend('-10 A', '-5 A', '5 A', '10 A')
saveas(gcf,'By vs z along mechanical centerline.png');

figure
plot(Bhxyzvsxyplt3.Y(1:151), Bhxyzvsxyplt3.Bz(1:151)*10000, 'bo');
 hold
 plot(Bhxyzvsxyplt3.Y(152:302), Bhxyzvsxyplt3.Bz(152:302)*10000, 'r*');
 plot(Bhxyzvsxyplt3.Y(303:453), Bhxyzvsxyplt3.Bz(303:453)*10000, 'g^');
 plot(Bhxyzvsxyplt3.Y(454:end), Bhxyzvsxyplt3.Bz(454:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('Bz (G)');
title('Bz vs z along centerline');
legend('-10 A', '-5 A', '5 A', '10 A')
saveas(gcf,'Bz vs z along mechanical centerline.png');

figure
plot(Bhxyzvsxyplt3.Y(1:151), -Bhxyzvsxyplt3.Bz(1:151)*10000, 'bo');
 hold
 plot(Bhxyzvsxyplt3.Y(152:302), -Bhxyzvsxyplt3.Bz(152:302)*10000, 'r*');
 plot(Bhxyzvsxyplt3.Y(303:453), Bhxyzvsxyplt3.Bz(303:453)*10000, 'g^');
 plot(Bhxyzvsxyplt3.Y(454:end), Bhxyzvsxyplt3.Bz(454:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('Bz (G)');
title('Bz vs z along centerline)');
legend('-Bz for -10 A', '-Bz for -5 A', '5 A', '10 A');

[max_10, ind_10] = max(Bhxyzvsxyplt3.Bz(454:end));
vline(Bhxyzvsxyplt3.Y(ind_10), 'b' , ['Max Bz at Z = ', num2str(Bhxyzvsxyplt3.Y(ind_10)), ' m']);

figure
 plot(Strplt8.Imag(:,3),Strplt8.SL, 'bo');
hold
 plot(Strplt9.Imag(:,2),Strplt9.SL, 'r*');
 xlabel('Current (A)');
ylabel('Int Grad (T)');
title('Integrated Gradient vs Current');
legend('Normal Quad', 'Skew Quad');
enhance_plot

% Plot Int Grad vs current for all currents, skip current near zero
 n = [1:9, 13:21];

 plot_polyfit_res(Strplt8.Imag(n,3), sign(Strplt8.THsp(n)).*Strplt8.SL(n)*10000, Strplt8.sigSL(n), 1, 'I', 'Integrated Gradient', 'A', 'G');

 enhance_plot
 

 plot_polyfit_res(Strplt9.Imag(n,2), sign(Strplt9.THsp(n)).*Strplt9.SL(n)*10000, Strplt9.sigSL(n), 1, 'I', 'Integrated Gradient', 'A', 'G');

 enhance_plot
 
 % Plot solenoid quadrupole term vs solenoid current w/ and w/o trim
 % correction
 figure
 plot(Strplt10.Imag(:,1), sign(Strplt10.THsp).*Strplt10.SL*10000, 'bo');
 hold
plot(Strplt14.Imag(:,1), sign(Strplt14.THsp).*Strplt14.SL*10000, 'r*');
 xlabel('SOL1 Current (A)');
ylabel('Int Grad (G)');
title('Integrated Gradient vs Current');
legend('SOL1 w/o quad trim correction', 'SOL1 w/ quad trim correction');
enhance_plot
 
% find the rotation angle from horizontal (to gravity) at -2 A (_m2) and +2 A (_2)
[SKQ_Mag_sum_m2, SKQ_Ang_sum_m2, SKQ_Mag_diff_m2, SKQ_Ang_diff_m2] = sum_vector_components( Strplt8.SL(1), Strplt8.THsp(1), Strplt8.SL(11), Strplt8.THsp(11));
[SKQ_Mag_sum_2, SKQ_Ang_sum_2, SKQ_Mag_diff_2, SKQ_Ang_diff_2] = sum_vector_components( Strplt8.SL(end), Strplt8.THsp(end), Strplt8.SL(11), Strplt8.THsp(11));
[NMQ_Mag_sum_m2, NMQ_Ang_sum_m2, NMQ_Mag_diff_m2, NMQ_Ang_diff_m2] = sum_vector_components( Strplt9.SL(1), Strplt9.THsp(1), Strplt9.SL(11), Strplt9.THsp(11));
[NMQ_Mag_sum_2, NMQ_Ang_sum_2, NMQ_Mag_diff_2, NMQ_Ang_diff_2] = sum_vector_components( Strplt9.SL(end), Strplt9.THsp(end), Strplt9.SL(11), Strplt9.THsp(11));


SKQ_minus_2_offset = SKQ_Ang_diff_m2 + 90;
SKQ_plus_2_offset = SKQ_Ang_diff_2;
NMQ_minus_2_offset = NMQ_Ang_diff_m2 + 45;
NMQ_plus_2_offset = NMQ_Ang_diff_2 - 45;


 Skew_angle_offset = mean([abs(SKQ_Ang_diff_m2 + 90), SKQ_Ang_diff_2])
 Normal_angle_offset = mean([abs(NMQ_Ang_diff_m2 + 45), NMQ_Ang_diff_2 - 45])


plot_polyfit_res(Bhvszplt15.numimag.imag, Bhvszplt15.hall*10000, 1, 1, 'Current', 'Bz', 'A', 'G');
title('LCLS-II SOL1 Current VS Bz');
 
figure
plot(Bhvszplt16.z(1:441)*1000, -Bhvszplt16.hall(1:441)*10000, 'bo');
 hold
 plot(Bhvszplt16.z(442:882)*1000, -Bhvszplt16.hall(442:882)*10000, 'r*');
 plot(Bhvszplt16.z(883:1323)*1000, Bhvszplt16.hall(883:1323)*10000, 'g^');
 plot(Bhvszplt16.z(1324:1764)*1000, Bhvszplt16.hall(1324:1764)*10000, 'ksq');
xlabel('Z (mm))');
ylabel('Bz (G)');
title('Bz vs z along centerline)');
legend('-Bz for -10 A', '-Bz for -5 A', '5 A', '10 A');

[max_10, ind_10] = max(Bhvszplt16.hall(1324:1764));
vline(Bhvszplt16.z(ind_10), 'k' , ['Max Bz at Z = ', num2str(Bhvszplt16.z(ind_10)*1000), ' mm at +10 A']);
 