%**************************************************************************
%This program takes calibrated field measurements from a 2-probe array.
%It calculates the position of probe 1 (on the probe center line) relative to
%the magnetic center.  It also calculates parameters describing the
%undulaltor field.
%
%Input:
%param, structure containing program parameters
%dat_file, file to write data to
%plt_file, file to send plots to
%b_meas, structure containing measured fields
%
%Output:
%field, structure containing field analysis information
%
%Zachary Wolf
%7/3/13

function [field] = hall2_anal_probe_field(param, dat_file, plt_file, b_meas)

%Extract the fields from the b_meas structure
z = b_meas.z;
bx1 = b_meas.bx1;
by1 = b_meas.by1;
bz1 = b_meas.bz1;
bx2 = b_meas.bx2;
by2 = b_meas.by2;
bz2 = b_meas.bz2;

%Plot the fields
halln_field_plot_bxyz_vs_z(plt_file, 'Probe 1', z, bx1, by1, bz1);
halln_field_plot_bxyz_vs_z(plt_file, 'Probe 2', z, bx2, by2, bz2);
halln_field_plot_bxy_vs_z(plt_file, 'Probe 1', z, bx1, by1);
halln_field_plot_bxy_vs_z(plt_file, 'Probe 2', z, bx2, by2);
halln_field_plot_byz_vs_z(plt_file, 'Probe 1', z, by1, bz1);
halln_field_plot_byz_vs_z(plt_file, 'Probe 2', z, by2, bz2);
halln_field_plot_bxz_vs_z(plt_file, 'Probe 1', z, bx1, bz1);
halln_field_plot_bxz_vs_z(plt_file, 'Probe 2', z, bx2, bz2);
hall2_field_plot_b1b2_vs_z(plt_file, 'Bx', z, bx1, bx2);
hall2_field_plot_b1b2_vs_z(plt_file, 'By', z, by1, by2);
hall2_field_plot_b1b2_vs_z(plt_file, 'Bz', z, bz1, bz2);

%Find the peaks and zeros of the fields
[field_peaks_zeros] = hall2_field_find_peaks_zeros(dat_file, plt_file, param, z, bx1, by1, bz1, bx2, by2, bz2);

%Extract field peaks and zeros
bx1_peak_ne = field_peaks_zeros.bx1_peak_ne;
by1_peak_ne = field_peaks_zeros.by1_peak_ne;
bz1_peak_ne = field_peaks_zeros.bz1_peak_ne;
bx2_peak_ne = field_peaks_zeros.bx2_peak_ne;
by2_peak_ne = field_peaks_zeros.by2_peak_ne;
bz2_peak_ne = field_peaks_zeros.bz2_peak_ne;
z_peak_bx1_ne = field_peaks_zeros.z_peak_bx1_ne;
z_peak_by1_ne = field_peaks_zeros.z_peak_by1_ne;
z_peak_bz1_ne = field_peaks_zeros.z_peak_bz1_ne;
z_peak_bx2_ne = field_peaks_zeros.z_peak_bx2_ne;
z_peak_by2_ne = field_peaks_zeros.z_peak_by2_ne;
z_peak_bz2_ne = field_peaks_zeros.z_peak_bz2_ne;
z_zero_bx1_ne = field_peaks_zeros.z_zero_bx1_ne;
z_zero_by1_ne = field_peaks_zeros.z_zero_by1_ne;
z_zero_bx2_ne = field_peaks_zeros.z_zero_bx2_ne;
z_zero_by2_ne = field_peaks_zeros.z_zero_by2_ne;

%Plot the field peaks
halln_field_plot_b12_peaks_no_ends(plt_file, 'Bx', z_peak_bx1_ne, bx1_peak_ne, z_peak_bx2_ne, bx2_peak_ne);
halln_field_plot_b12_peaks_no_ends(plt_file, 'By', z_peak_by1_ne, by1_peak_ne, z_peak_by2_ne, by2_peak_ne);
halln_field_plot_b12_peaks_no_ends(plt_file, 'Bz', z_peak_bz1_ne, bz1_peak_ne, z_peak_bz2_ne, bz2_peak_ne);

%Return the results in the field structure
field.z = z;
field.bx1 = bx1;
field.by1 = by1;
field.bz1 = bz1;
field.bx2 = bx2;
field.by2 = by2;
field.bz2 = bz2;
field.field_peaks_zeros = field_peaks_zeros;
field.bx1_peak_ne = bx1_peak_ne;
field.by1_peak_ne = by1_peak_ne;
field.bz1_peak_ne = bz1_peak_ne;
field.bx2_peak_ne = bx2_peak_ne;
field.by2_peak_ne = by2_peak_ne;
field.bz2_peak_ne = bz2_peak_ne;
field.z_peak_bx1_ne = z_peak_bx1_ne;
field.z_peak_by1_ne = z_peak_by1_ne;
field.z_peak_bz1_ne = z_peak_bz1_ne;
field.z_peak_bx2_ne = z_peak_bx2_ne;
field.z_peak_by2_ne = z_peak_by2_ne;
field.z_peak_bz2_ne = z_peak_bz2_ne;
field.z_zero_bx1_ne = z_zero_bx1_ne;
field.z_zero_by1_ne = z_zero_by1_ne;
field.z_zero_bx2_ne = z_zero_bx2_ne;
field.z_zero_by2_ne = z_zero_by2_ne;

%Done