%**************************************************************************
%This function reads measurements from the reference dipole.  It compares
%and plots the hall probe and NMR measurements.
%
%Zachary Wolf, 4/14/09

function [] = ref_dipole()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
ref_dipole_param

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

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Reference Dipole Analysis:'));
util_dat_msg(dat_file, cellstr(ANALYSIS_NAME));

%Initialize the plots
ref_dipole_plot_init(plt_file, ANALYSIS_NAME);

%Get the measurement data
[dat_vals] = ref_dipole_read_data(INPUT_FILE);

%Extract items of interest
for ii = 1 : length(dat_vals.dd)
    date_vec = [dat_vals.yy(ii), dat_vals.mm(ii), dat_vals.dd(ii)];
    date_num(ii) = datenum(date_vec);
end
b_hall = abs(dat_vals.b_hall);
b_nmr = abs(dat_vals.b_nmr);
b_delta = b_hall - b_nmr;
b_delta_frac = b_delta ./ b_nmr;
b_delta_frac_change = b_delta_frac - b_delta_frac(1);

%Write values to the data file
ref_dipole_dat_results(dat_file, ANALYSIS_NAME, date_num, b_hall, b_nmr, b_delta);

%Plot the Bhall measurements
ref_dipole_plot_bhall(plt_file, date_num, b_hall);

%Plot the Bnmr measurements
ref_dipole_plot_bnmr(plt_file, date_num, b_nmr);

%Plot the difference between the hall probe and nmr measurements
ref_dipole_plot_bdelta(plt_file, date_num, b_delta);

%Plot the fractional difference between the hall and nmr measurements
ref_dipole_plot_bdeltafrac(plt_file, date_num, b_delta_frac);

%Plot the change in the fractional difference between the hall and nmr measurements
ref_dipole_plot_bdeltafracchange(plt_file, date_num, b_delta_frac_change);

%Save the results to the mat file
save(mat_file, 'ANALYSIS_NAME', 'INPUT_FILE', 'OUTPUT_DIR', 'dat_vals', 'date_num', 'b_hall', 'b_nmr', 'b_delta', 'b_delta_frac', 'b_delta_frac_change');

%Done
