% This script analyzes the data taken on the SCU Cube.  The measurement
% were taken ~8 inches up from the bottom of the cube, along the center in x.   

SCU_Field = Load_tunnelfluxplt(1); % This is the measurement of the BX, By and Bz field in the cube.
Background_Field = Load_tunnelfluxplt(2); % This is the measurement of the field with the cube removed. 

Tesla_to_Gauss = 10000.0;
m2mm = 1000.0;

%% Make plots of the Cube and Background data all in one page

% Plot Bx vs Z

figure('units','normalized','outerposition',[0 0 0.9 0.9])
subplot(3,1,1)

plot(SCU_Field.Z*m2mm, SCU_Field.Bx*Tesla_to_Gauss, mm(1))
hold
plot(SCU_Field.Z*m2mm, Background_Field.Bx*Tesla_to_Gauss, mm(2))
xlabel({'Z','(mm)'});
ylabel({'Bx', '(Gauss)'});
title({'Cube and Background Data','Bx vs Z'});
legend('Cube Field', 'Background Field');


% Plot By vs Z
subplot(3,1,2)

plot(SCU_Field.Z*m2mm, SCU_Field.By*Tesla_to_Gauss, mm(1))
hold
plot(SCU_Field.Z*m2mm, Background_Field.By*Tesla_to_Gauss, mm(2))

xlabel({'Z','(mm)'});
ylabel({'By', '(Gauss)'});
title('By vs Z')
legend('Cube Field', 'Background Field');

% Plot Bz vs Z
subplot(3,1,3)

plot(SCU_Field.Z*m2mm, SCU_Field.Bz*Tesla_to_Gauss, mm(1))
hold
plot(SCU_Field.Z*m2mm, Background_Field.Bz*Tesla_to_Gauss, mm(2))

xlabel({'Z','(mm)'});
ylabel({'Bz', '(Gauss)'});
title('Bz vs Z')
legend('Cube Field', 'Background Field');

set(gcf,'Name', 'Field in SCU Cube and Backgound along XY = 0 beam axis vs Z');
saveas(gcf,'Field in SCU Cube and Backgound along XY = 0 beam axis vs Z.png')

%% Plot Cube minus Background data 
figure('units','normalized','outerposition',[0 0 0.9 0.9])
subplot(3,1,1)

plot(SCU_Field.Z*m2mm, (SCU_Field.Bx-Background_Field.Bx)*Tesla_to_Gauss, mm(1))

xlabel({'Z','(mm)'});
ylabel({'Bx', '(Gauss)'});
title({'Cube with Background Field Subtracted', 'Bx vs Z'})


% Plot By vs Z
subplot(3,1,2)

plot(SCU_Field.Z*m2mm, (SCU_Field.By-Background_Field.By)*Tesla_to_Gauss, mm(2))

xlabel({'Z','(mm)'});
ylabel({'By', '(Gauss)'});
title('By vs Z')


% Plot Bz vs Z
subplot(3,1,3)

plot(SCU_Field.Z*m2mm, (SCU_Field.Bz-Background_Field.Bz)*Tesla_to_Gauss, mm(3))
xlabel({'Z','(mm)'});
ylabel({'Bz', '(Gauss)'});
title('Bz vs Z')

set(gcf,'Name', 'SCU Field in Cube with Background Field Subtracted along XY beam axis vs Z');
saveas(gcf,'SCU Field in Cube with Background Field Subtracted along Beam Axis vs Z.png')