%**************************************************************************
%This function plots the corrections to the Bx and By fields.
%
%Input:
%plt_file, plot file for results
%title_str, title for plot
%subtract_earth_field_end_scan_len, length over which the Earth field is determined (m)
%zm, z positions at which the field measurements are made (m)
%b, measured field (T)
%b_add, field added to B (T)
%
%Zachary Wolf
%9/2/17

function [] = field_corr_plot_earth_field_corr(plt_file, title_str, subtract_earth_field_end_scan_len, zm, b, b_add)

%Determine the Earth field at the entrance
index_array = find(abs(zm - zm(1)) < subtract_earth_field_end_scan_len);
b_enter = b(index_array);
zm_enter = zm(index_array);

%Determine the Earth field at the exit
index_array = find(abs(zm - zm(end)) < subtract_earth_field_end_scan_len);
b_exit =b(index_array);
zm_exit = zm(index_array);

%Initialize
figure;
    
%Plot the field
subplot(3, 1, 1);
hold on;
plot(zm, b, '-b', 'linewidth', 1.5);
plot(zm_enter, b_enter, '-r', 'linewidth', 1.5);
plot(zm_exit, b_exit, '-r', 'linewidth', 1.5);
title(title_str);
ylabel('B (T)');
hold off;
grid on;
zoom on;
    
%Plot the entrance field
subplot(3, 1, 2);
hold on;
plot(zm_enter, b_enter, '-r', 'linewidth', 1.5);
line(xlim, [-b_add, -b_add], 'color', 'b', 'linestyle', '--');
legend('Field', 'Enter+Exit Corr', 'location', 'south');
title('Entrance Field Used For Correction');
ylabel('B (T)');
hold off;
grid on;
zoom on;
    
%Plot the exit field
subplot(3, 1, 3);
hold on;
plot(zm_exit, b_exit, '-r', 'linewidth', 1.5);
line(xlim, [-b_add, -b_add], 'color', 'b', 'linestyle', '--');
legend('Field', 'Enter+Exit Corr');
title('Exit Field Used For Correction');
ylabel('B (T)');
xlabel('Zm (m)');
hold off;
grid on;
zoom on;

%Print
util_print_fig(plt_file);

%Done
