%**************************************************************************
%This function reads Hall probe array analysis results.  It compares the
%results and plots the comparisons.
%
%Zachary Wolf
%9/13/13

function [] = hall2_anal_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
hall2_anal_comp_param

%Output files
dat_file = [OUTPUT_DIR, 'hall2_anal_comp.dat'];
plt_file = [OUTPUT_DIR, 'hall2_anal_comp.ps'];
mat_file = [OUTPUT_DIR, 'hall2_anal_comp.mat'];

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Compare Analysis Results:'));
util_dat_msg(dat_file, cellstr(COMPARE_NAME));

%Initialize the plots
hall2_anal_comp_plot_init(plt_file, COMPARE_NAME, INPUT_FILE);

%Loop over analysis results, get data
[r, c] = size(INPUT_FILE);
for ii = 1 : c
    
    %Get the input file
    in_mat_file = INPUT_FILE{ii};

    %Input the linear polarization analysis results
    load(in_mat_file, 'param', 'b_meas', 'field', 'mag_ctr', 'beam_axis');
    %
    z{ii, :} = b_meas.z;
    bx1{ii, :} = b_meas.bx1;
    by1{ii, :} = b_meas.by1;
    bz1{ii, :} = b_meas.bz1;
    bx2{ii, :} = b_meas.bx2;
    by2{ii, :} = b_meas.by2;
    bz2{ii, :} = b_meas.bz2;
    %
	bx1_peak_ne{ii, :} = field.field_peaks_zeros.bx1_peak_ne;
	by1_peak_ne{ii, :} = field.field_peaks_zeros.by1_peak_ne;
	bz1_peak_ne{ii, :} = field.field_peaks_zeros.bz1_peak_ne;
    bx2_peak_ne{ii, :} = field.field_peaks_zeros.bx2_peak_ne;
	by2_peak_ne{ii, :} = field.field_peaks_zeros.by2_peak_ne;
	bz2_peak_ne{ii, :} = field.field_peaks_zeros.bz2_peak_ne;
	z_peak_bx1_ne{ii, :} = field.field_peaks_zeros.z_peak_bx1_ne;
	z_peak_by1_ne{ii, :} = field.field_peaks_zeros.z_peak_by1_ne;
	z_peak_bz1_ne{ii, :} = field.field_peaks_zeros.z_peak_bz1_ne;
	z_peak_bx2_ne{ii, :} = field.field_peaks_zeros.z_peak_bx2_ne;
	z_peak_by2_ne{ii, :} = field.field_peaks_zeros.z_peak_by2_ne;
	z_peak_bz2_ne{ii, :} = field.field_peaks_zeros.z_peak_bz2_ne;
    %
    z_peak_ctr{ii, :} = mag_ctr.z_peak_ctr;
	x1_minus_xc{ii, :} = mag_ctr.x1_minus_xc;
	y1_minus_yc{ii, :} = mag_ctr.y1_minus_yc;
	kx{ii, :} = mag_ctr.kx;
	ky{ii, :} = mag_ctr.ky;
	delta_x_calc{ii, :} = mag_ctr.delta_x_calc;
	delta_y_calc{ii, :} = mag_ctr.delta_y_calc;
    %
	xc{ii, :} = beam_axis.xc;
	yc{ii, :} = beam_axis.yc;
	xb{ii, :} = beam_axis.xb;
	yb{ii, :} = beam_axis.yb;
	bx_b{ii, :} = beam_axis.bx_b;
	by_b{ii, :} = beam_axis.by_b;
	bz_b{ii, :} = beam_axis.bz_b;

%End loop over analysis results
end

%Plot the fields
hall2_anal_comp_plot_bvsz(plt_file, z, bx1, 'Bx1', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, by1, 'By1', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, bz1, 'Bz1', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, bx2, 'Bx2', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, by2, 'By2', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, bz2, 'Bz2', INPUT_NAME);

%Plot the peak fields
hall2_anal_comp_plot_bpeak(plt_file, z_peak_bx1_ne, bx1_peak_ne, 'Bx1', INPUT_NAME);
hall2_anal_comp_plot_bpeak(plt_file, z_peak_by1_ne, by1_peak_ne, 'By1', INPUT_NAME);
hall2_anal_comp_plot_bpeak(plt_file, z_peak_bz1_ne, bz1_peak_ne, 'Bz1', INPUT_NAME);
hall2_anal_comp_plot_bpeak(plt_file, z_peak_bx2_ne, bx2_peak_ne, 'Bx2', INPUT_NAME);
hall2_anal_comp_plot_bpeak(plt_file, z_peak_by2_ne, by2_peak_ne, 'By2', INPUT_NAME);
hall2_anal_comp_plot_bpeak(plt_file, z_peak_bz2_ne, bz2_peak_ne, 'Bz2', INPUT_NAME);

%Plot x1_minus_xc, y1_minus_yc
hall2_anal_comp_plot_x1minusxc(plt_file, z_peak_ctr, x1_minus_xc, INPUT_NAME);
hall2_anal_comp_plot_y1minusyc(plt_file, z_peak_ctr, y1_minus_yc, INPUT_NAME);

%Plot kx, ky
hall2_anal_comp_plot_kx(plt_file, z_peak_ctr, kx, INPUT_NAME);
hall2_anal_comp_plot_ky(plt_file, z_peak_ctr, ky, INPUT_NAME);
 
%Plot delta_x_calc, delta_y_calc
hall2_anal_comp_plot_deltaxcalc(plt_file, z_peak_ctr, delta_x_calc, INPUT_NAME);
hall2_anal_comp_plot_deltaycalc(plt_file, z_peak_ctr, delta_y_calc, INPUT_NAME);

%Plot Xc, Yc
hall2_anal_comp_plot_xc(plt_file, z_peak_ctr, xc, INPUT_NAME);
hall2_anal_comp_plot_yc(plt_file, z_peak_ctr, yc, INPUT_NAME);

%Plot Xb, Yb
hall2_anal_comp_plot_xb(plt_file, z_peak_ctr, xb, INPUT_NAME);
hall2_anal_comp_plot_yb(plt_file, z_peak_ctr, yb, INPUT_NAME);

%Plot the fields on the beam axis
hall2_anal_comp_plot_bvsz(plt_file, z, bx_b, 'Bx On The Beam Axis', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, by_b, 'By On The Beam Axis', INPUT_NAME);
hall2_anal_comp_plot_bvsz(plt_file, z, bz_b, 'Bz On The Beam Axis', INPUT_NAME);

%Done
