%**************************************************************************
%This function checks that the peaks and zeros of the field components are
%in the expected locations.
%
%Input:
%dat_file, file to write results to
%param, structure containing program parameters
%z_zero_bx1, zero positions of Bx1 (m)
%z_zero_by1, zero positions of By1 (m)
%z_zero_bz1, zero positions of Bz1 (m)
%z_zero_bx2, zero positions of Bx2 (m)
%z_zero_by2, zero positions of Bx2 (m)
%z_zero_bz2, zero positions of Bx2 (m)
%
%Zachary Wolf
%11/8/13

function [] = hall2_field_check_loc(dat_file, param, z_zero_bx1, z_zero_by1, z_zero_bz1, z_zero_bx2, z_zero_by2, z_zero_bz2)

%Check that the two probes measure Bx at the same location
%Using peak fields for calculations, so this doesn't need to be exact
util_dat_str(dat_file, ' ');
util_dat_str(dat_file, 'Check Hall Element Offsets:');
z_offset_tol = 1.e-3;
for ii = 1 : min([length(z_zero_bx1), length(z_zero_bx2)])
    [dz_min, i_min] = min(abs(z_zero_bx1(ii) - z_zero_bx2));
    dz_min_bx12(ii) = z_zero_bx1(ii) - z_zero_bx2(i_min);
end
bx_z_offset_err = mean(dz_min_bx12(4 : end - 4));
util_dat_str(dat_file, strcat('bx_z_offset_err probe12 = ', num2str(bx_z_offset_err), ' m'));
if abs(bx_z_offset_err) > z_offset_tol
    %warndlg('bx_z_offset_err is too large');
end

%Check that the two probes measure By at the same location
for ii = 1 : min([length(z_zero_by1), length(z_zero_by2)])
    [dz_min, i_min] = min(abs(z_zero_by1(ii) - z_zero_by2));
    dz_min_by12(ii) = z_zero_by1(ii) - z_zero_by2(i_min);
end
by_z_offset_err = mean(dz_min_by12(4 : end - 4));
util_dat_str(dat_file, strcat('by_z_offset_err probe12 = ', num2str(by_z_offset_err), ' m'));
if abs(by_z_offset_err) > z_offset_tol
    %warndlg('by_z_offset_err is too large');
end

%Check that the two probes measure Bz at the same location
for ii = 1 : min([length(z_zero_bz1), length(z_zero_bz2)])
    [dz_min, i_min] = min(abs(z_zero_bz1(ii) - z_zero_bz2));
    dz_min_bz12(ii) = z_zero_bz1(ii) - z_zero_bz2(i_min);
end
bz_z_offset_err = mean(dz_min_bz12(4 : end - 4));
util_dat_str(dat_file, strcat('bz_z_offset_err probe12 = ', num2str(bz_z_offset_err), ' m'));
if abs(bz_z_offset_err) > z_offset_tol
    %warndlg('bz_z_offset_err is too large');
end

%Check that probe 1 measures Bx and By at the same location
if strcmp(param.und_pol_mode, 'LIN_POL_VERT_FIELD') || strcmp(param.und_pol_mode, 'LIN_POL_HORIZ_FIELD')
for ii = 1 : length(z_zero_bx1)
    [dz_min, i_min] = min(abs(z_zero_bx1(ii) - z_zero_by1));
    dz_min_bxy1(ii) = z_zero_bx1(ii) - z_zero_by1(i_min);
end
bxy_z_offset_err = mean(dz_min_bxy1(4 : end - 4));
util_dat_str(dat_file, strcat('z_offset_err probe 1 bxy = ', num2str(bxy_z_offset_err), ' m'));
if abs(bxy_z_offset_err) > z_offset_tol
    %warndlg('bxy_z_offset_err is too large');
end
end

%Check that probe 2 measures Bx and By at the same location
if strcmp(param.und_pol_mode, 'LIN_POL_VERT_FIELD') || strcmp(param.und_pol_mode, 'LIN_POL_HORIZ_FIELD')
for ii = 1 : length(z_zero_bx2)
    [dz_min, i_min] = min(abs(z_zero_bx2(ii) - z_zero_by2));
    dz_min_bxy2(ii) = z_zero_bx2(ii) - z_zero_by2(i_min);
end
bxy_z_offset_err = mean(dz_min_bxy2(4 : end - 4));
util_dat_str(dat_file, strcat('z_offset_err probe 2 bxy = ', num2str(bxy_z_offset_err), ' m'));
if abs(bxy_z_offset_err) > z_offset_tol
    %warndlg('bxy_z_offset_err is too large');
end
end

%Check that probe 1 measures By and Bz 1/4 period apart
if strcmp(param.und_pol_mode, 'LIN_POL_VERT_FIELD') || strcmp(param.und_pol_mode, 'LIN_POL_HORIZ_FIELD')
for ii = 1 : length(z_zero_bz1)
    [dz_min, i_min] = min(abs(z_zero_bz1(ii) - z_zero_by1));
    dz_min_bzy1(ii) = z_zero_bz1(ii) - z_zero_by1(i_min);
end
bzy_z_offset_err = mean(dz_min_bzy1(4 : end - 4));
util_dat_str(dat_file, strcat('z_offset_err probe 1 bzy = ', num2str(bzy_z_offset_err), ' m'));
end

%Check that probe 2 measures By and Bz 1/4 period apart
if strcmp(param.und_pol_mode, 'LIN_POL_VERT_FIELD') || strcmp(param.und_pol_mode, 'LIN_POL_HORIZ_FIELD')
for ii = 1 : length(z_zero_bz2)
    [dz_min, i_min] = min(abs(z_zero_bz2(ii) - z_zero_by2));
    dz_min_bzy2(ii) = z_zero_bz2(ii) - z_zero_by2(i_min);
end
bzy_z_offset_err = mean(dz_min_bzy2(4 : end - 4));
util_dat_str(dat_file, strcat('z_offset_err probe 2 bzy = ', num2str(bzy_z_offset_err), ' m'));
end

%Done