%**************************************************************************
%hall_probe_calib.m
%
%This function reads Hall probe calibration measurements.  It fits the
%field as measured by an NMR vs the Hall probe voltage.  Plots of the fit
%and the residuals are made.  Results of the fit are output to a file.
%
%Zachary Wolf, 11/30/06

function [] = hall_probe_calib()

%Set up program parameters using the user interface
%Create the updated hall_probe_calib_param.m file which is run to set the parameter values
%hall_probe_calib_ui;

%Clear the workspace
clear

%Set parameter values by executing the parameter file
hall_probe_calib_param;

%Make the parameter structure
param.input_file_dir = INPUT_FILE_DIR;
param.input_file_name = INPUT_FILE_NAME;
param.output_file_dir = OUTPUT_FILE_DIR;
param.poly_fit_order = POLY_FIT_ORDER;
param.num_sig_outlier = NUM_SIG_OUTLIER;
param.nom_calib_const = NOM_CALIB_CONST;
param.max_b_nmr_fit = MAX_B_NMR_FIT;

%Output files
dat_file = [param.output_file_dir, 'calib.dat'];
plt_file = [param.output_file_dir, 'calib.ps'];
mat_file = [param.output_file_dir, 'calib.mat'];

%Read the calibration measurements
[header, i_mag, b_ref, t_hall, v_hall, b_nmr] = hall_calib_read_by_calib_meas(param);
%[header, i_mag, b_ref, t_hall, v_hall, b_nmr] = hall_calib_read_bxy_calib_meas(param);
%[header, b_nmr, v_hall] = hall_calib_read_desy_calib_meas(param);

%Initialize the plot file, plot the temperature, plot the reference
hall_calib_plot_init(plt_file, param, header);
hall_calib_plot_temp(plt_file, b_nmr, t_hall);
hall_calib_plot_ref(plt_file, b_nmr, b_ref);

%Make arrays for plotting
max_b_nmr_fit = param.max_b_nmr_fit;
b_nmr_fit_array = [-max_b_nmr_fit : 2 * max_b_nmr_fit / 200 : max_b_nmr_fit];
max_v_hall_fit = param.max_b_nmr_fit / param.nom_calib_const;
v_hall_fit_array = [-max_v_hall_fit : 2 * max_v_hall_fit / 200 : max_v_hall_fit];

%First pass, use all data points -----

%Polynomial fit of the measured data
[fit_coef_vvsb, v_hall_offset, v_hall_offset_corr, fit_coef_bvsvoc, b_nmr_resid] = hall_calib_calc_poly_fit(b_nmr, v_hall, param.poly_fit_order);

%Plot the results
plot_vvsb_b_vals = b_nmr_fit_array;
plot_vvsb_v_vals = polyval(fit_coef_vvsb, b_nmr_fit_array);
hall_calib_plot_vvsb(plt_file, ' ', b_nmr, v_hall, plot_vvsb_b_vals, plot_vvsb_v_vals, v_hall_offset);
plot_bvsvoc_v_vals = v_hall_fit_array;
plot_bvsvoc_b_vals = polyval(fit_coef_bvsvoc, v_hall_fit_array);
hall_calib_plot_bvsvoc(plt_file, ' ', v_hall_offset_corr, b_nmr, plot_bvsvoc_v_vals, plot_bvsvoc_b_vals);
hall_calib_plot_resid(plt_file, ' ', v_hall_offset_corr, b_nmr_resid);

%Second pass, remove outliers -----

%Find and remove outliers
sigma = std(b_nmr_resid);
index_out = find(abs(b_nmr_resid) >= param.num_sig_outlier * sigma);
index_no_out = find(abs(b_nmr_resid) < param.num_sig_outlier * sigma);
v_hall_out = v_hall(index_out);
b_nmr_out = b_nmr(index_out);
v_hall_no_out = v_hall(index_no_out);
b_nmr_no_out = b_nmr(index_no_out);

%Polynomial fit of the measured data with the outliers removed
[fit_coef_vvsb_no_out, v_hall_offset_no_out, v_hall_offset_corr_no_out, fit_coef_bvsvoc_no_out, b_nmr_resid_no_out] = hall_calib_calc_poly_fit(b_nmr_no_out, v_hall_no_out, param.poly_fit_order);

%Plot the results
plot_vvsb_b_vals_no_out = b_nmr_fit_array;
plot_vvsb_v_vals_no_out = polyval(fit_coef_vvsb_no_out, b_nmr_fit_array);
hall_calib_plot_vvsb(plt_file, ', No Outliers', b_nmr_no_out, v_hall_no_out, plot_vvsb_b_vals_no_out, plot_vvsb_v_vals_no_out, v_hall_offset_no_out);
plot_bvsvoc_v_vals_no_out = v_hall_fit_array;
plot_bvsvoc_b_vals_no_out = polyval(fit_coef_bvsvoc_no_out, v_hall_fit_array);
hall_calib_plot_bvsvoc(plt_file, ', No Outliers', v_hall_offset_corr_no_out, b_nmr_no_out, plot_bvsvoc_v_vals_no_out, plot_bvsvoc_b_vals_no_out);
hall_calib_plot_resid(plt_file, ', No Outliers', v_hall_offset_corr_no_out, b_nmr_resid_no_out);

%Compare to nominal calibration
plot_bvsvoc_bnom_vals_no_out = param.nom_calib_const * plot_bvsvoc_v_vals_no_out;
hall_calib_plot_nom_calib(plt_file, plot_bvsvoc_v_vals_no_out, plot_bvsvoc_bnom_vals_no_out, plot_bvsvoc_b_vals_no_out);

%Write results -----

%Write results to the data file
hall_calib_dat_results(dat_file, param, header, v_hall_out, b_nmr_out, v_hall_offset_no_out, fit_coef_bvsvoc_no_out);

%Fill the calibration structure
calib.i_mag = i_mag;
calib.b_ref = b_ref;
calib.t_hall = t_hall;
calib.v_hall = v_hall;
calib.b_nmr = b_nmr;
calib.v_hall_no_out = v_hall_no_out;
calib.b_nmr_no_out = b_nmr_no_out;
calib.v_hall_offset_no_out = v_hall_offset_no_out;
calib.v_hall_offset_corr_no_out = v_hall_offset_corr_no_out;
calib.fit_coef_bvsvoc_no_out = fit_coef_bvsvoc_no_out;
calib.b_nmr_resid_no_out = b_nmr_resid_no_out;
calib.plot_bvsvoc_v_vals_no_out = plot_bvsvoc_v_vals_no_out;
calib.plot_bvsvoc_b_vals_no_out = plot_bvsvoc_b_vals_no_out;
calib.plot_bvsvoc_bnom_vals_no_out = plot_bvsvoc_bnom_vals_no_out;

%Output the calibration structure
save(mat_file, 'param', 'header', 'calib');

%Done
