% Analyze LCLS II Solenoid 4051

%%  Load Data
   Load_bhallxyzvszplt(3);  % B vs I -10 to 10 A at center
   Load_bhallxyzvszplt(2);  % B vs z -10, -5, 5 10 amps along centerline
   Load_bhallxyzvszplt(4); % Power supply off
   Load_bnvszplt_dev(5) % Bz vs z at -10, -5, 5, and 10 amps along mechanical centerline
   Load_bnvszplt_dev(6) % Bn vs z at -10, -5, 5, and 10 amps along magnetic centerline
% 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(12); % Main quad int grad
 Load_strplt_dev(13); % Main quad int grad canceled by trims
 Load_bnvszplt_dev(6); % Bn vs Z at -10, -5, 5 and 10 A where n = 1,2,3,& 4, along magnetic axis. X offset = +17 mils, Y offset = +6 mils.
 Load_bnvszplt_dev(8); % Bn vs Z at 10 A where n = 1,2,3,& 4, along magnetic axis. X offset = +17 mils, Y offset = +6 mils. Longer z length.



%% Plot B field vs current at Z = 0


plot_polyfit_res(Bhxyzvszplt3.Imag(:,1), Bhxyzvszplt3.Bz*10000, 1, 1,'Current', 'Bz', 'A', 'G');
xlabel('Current (A)');
ylabel('Bz (G)');
title('Bz vs current for Solenoid 4051 at Z = 0');
legend('Bz')
saveas(gcf,'Bz vs current for Solenoid 4051 at Z = 0.png');  

%% Plot Bx,By and Bz vs z at -10, -5, 5, and 10 A
figure
plot(Bhxyzvszplt2.Z(1:441), Bhxyzvszplt2.Bx(1:441)*10000, 'bo');
 hold
 plot(Bhxyzvszplt2.Z(442:882), Bhxyzvszplt2.Bx(442:882)*10000, 'r*');
 plot(Bhxyzvszplt2.Z(883:1323), Bhxyzvszplt2.Bx(883:1323)*10000, 'g^');
 plot(Bhxyzvszplt2.Z(1324:end), Bhxyzvszplt2.Bx(1324: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(Bhxyzvszplt2.Z(1:441), Bhxyzvszplt2.By(1:441)*10000, 'bo');
 hold
 plot(Bhxyzvszplt2.Z(442:882), Bhxyzvszplt2.By(442:882)*10000, 'r*');
 plot(Bhxyzvszplt2.Z(883:1323), Bhxyzvszplt2.By(883:1323)*10000, 'g^');
 plot(Bhxyzvszplt2.Z(1324:end), Bhxyzvszplt2.By(1324:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('Bx (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(Bhxyzvszplt2.Z(1:441), Bhxyzvszplt2.Bz(1:441)*10000, 'bo');
 hold
 plot(Bhxyzvszplt2.Z(442:882), Bhxyzvszplt2.Bz(442:882)*10000, 'r*');
 plot(Bhxyzvszplt2.Z(883:1323), Bhxyzvszplt2.Bz(883:1323)*10000, 'g^');
 plot(Bhxyzvszplt2.Z(1324:end), Bhxyzvszplt2.Bz(1324:end)*10000, 'ksq');
xlabel('Z (m)');
ylabel('Bx (G)');
title('By vs z along centerline');
legend('-10 A', '-5 A', '5 A', '10 A')
[max_10, ind_10] = max(Bhxyzvszplt2.Bz(1324:end));
vline(Bhxyzvszplt2.Z(ind_10), 'b' , ['Max Bz at Z = ', num2str(Bhxyzvszplt2.Z(ind_10)), ' m']);
saveas(gcf,'Bz vs z along mechanical centerline.png');  
 
%% Power supply off
figure
plot(Bhxyzvszplt4.Z, Bhxyzvszplt4.Bx*10000, 'bo');
 hold
plot(Bhxyzvszplt4.Z, Bhxyzvszplt4.By*10000, 'r*');
plot(Bhxyzvszplt4.Z, Bhxyzvszplt4.Bz*10000, 'ksq');
xlabel('Z (m)');
ylabel('B (G)');
title('B vs z along centerline with power supply off');
legend('Bx', 'By', 'Bz')


 
 %% Plot solenoid quadrupole term vs solenoid current w/ and w/o trim
 % correction
 figure
 plot(Strplt12.Imag(:,1), sign(Strplt12.THsp).*Strplt12.SL*10000, 'bo');
 hold
plot(Strplt13.Imag(:,1), sign(Strplt13.THsp).*Strplt13.SL*10000, 'r*');
 xlabel('Current (A)');
ylabel('Int Grad (G)');
title('LCLS-II Solenoid 4051 Integrated Gradient vs Current');
legend('Soleniod w/o quad trim correction', 'Solenoid w/ quad trim correction');
enhance_plot
 
%% plot short coil harmonics repeatablity  
% 
% 
for i = 1:length(Bnvsz8.n)
    figure
    plot(Bnvsz8.z, Bnvsz8.n(i).bmag, 'bo')
    hold
    plot(Bnvsz6.z(76:100), Bnvsz6.n(i).bmag(76:100), 'r*')
    xlabel('Current (A)');
    ylabel(['Harmonic n = ', num2str(i), ' (G)']);
    title(['LCLS-II Solenoid 4051 Harmonic n = ', num2str(i), ' vs Current at 10 A for 2 seperate measurements']);
    legend('Magnetic axis w/o vacuum chamber longer z meas', 'Magnetic axis w/o vacuum chamber');
    enhance_plot
end
    
    