%**************************************************************************
%calib_hist.m
%
%This function reads Hall probe calibration results.  It plots the history
%of calibration results for a probe.
%
%Zachary Wolf, 3/11/07

function [] = calib_hist()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
calib_hist_param

%Output file
plt_file = [OUTPUT_DIR, 'calib_hist.ps'];

%Initialize the plots
calib_hist_plot_init(plt_file, PROBE_NAME);

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

    %Input the analysis results
    load(mat_file, 'param', 'header', 'calib');
    
    %Get the data
    i_mag{ii, :} = calib.i_mag;
    b_ref{ii, :} = calib.b_ref;
    t_hall{ii, :} = calib.t_hall;
    v_hall{ii, :} = calib.v_hall;
    b_nmr{ii, :} = calib.b_nmr;
    v_hall_offset_no_out{ii, :} = calib.v_hall_offset_no_out;
    v_hall_offset_corr_no_out{ii, :} = calib.v_hall_offset_corr_no_out;
    b_nmr_resid_no_out{ii, :} = calib.b_nmr_resid_no_out;
    plot_bvsvoc_v_vals_no_out{ii, :} = calib.plot_bvsvoc_v_vals_no_out;
    plot_bvsvoc_b_vals_no_out{ii, :} = calib.plot_bvsvoc_b_vals_no_out;
    plot_bvsvoc_bnom_vals_no_out{ii, :} = calib.plot_bvsvoc_bnom_vals_no_out;
    
    %Calculate the offset in terms of field
    b_hall_offset_no_out{ii, :} = v_hall_offset_no_out{ii, :} * param.nom_calib_const;
    
    %Get the fit coefficients, calculate the field at 5 V (1 T)
    fit_coef = calib.fit_coef_bvsvoc_no_out;
    b_at_5v(ii) = polyval(fit_coef, 5);
    
    %Get the calibration date, make the calibration name
	[date_str, date_vec] = calib_hist_get_date_header(header);
    CALIB_NAME{ii} = date_str;
    date_num(ii) = datenum(date_vec);
    
%End loop over calibrations
end

%Plot the history of the calibrated field relative to the nominal calibration
calib_hist_plot_nom_calib(plt_file, plot_bvsvoc_v_vals_no_out, plot_bvsvoc_bnom_vals_no_out, plot_bvsvoc_b_vals_no_out, CALIB_NAME);

%Plot the history of the field at 5 V output from the hall probe
calib_hist_plot_bat5v(plt_file, date_num, b_at_5v);
db_at_5v = b_at_5v - b_at_5v(1);
calib_hist_plot_dbat5v(plt_file, date_num, db_at_5v);

%Plot the history of the calibration temperatures
calib_hist_plot_temp(plt_file, b_nmr, t_hall, CALIB_NAME);

%Plot the history of the fit residuals
calib_hist_plot_resid(plt_file, v_hall_offset_corr_no_out, b_nmr_resid_no_out, CALIB_NAME);

%Plot the history of the voltage offsets
calib_hist_plot_voffset(plt_file, date_num, v_hall_offset_no_out);

%Plot the history of the equivalent field offsets
calib_hist_plot_boffset(plt_file, date_num, b_hall_offset_no_out);

%Done
