%**************************************************************************
%This function writes the calibration results to the data file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%header, measurement file header
%v_hall_out, hall probe outliers (V)
%b_nmr_our, nmr outliers (T)
%v_hall_offset_no_out, hall probe offset voltage (V)
%fit_coef_bvsvoc_no_out, calibration fit parameters
%
%Zachary Wolf, 3/11/07

function [] = hall_calib_dat_results(dat_file, param, header, v_hall_out, b_nmr_out, v_hall_offset_no_out, fit_coef_bvsvoc_no_out)

%Open the data file
fid = fopen(dat_file, 'w');

%Get time and date information
%c = [y, m, d, h, mi, s]
c = fix(clock);

%Write a header to the data file
fprintf(fid, 'SLAC Magnetic Measurements\r\n');
fprintf(fid, 'Date: %02i-%02i-%4i\r\n', c(2), c(3), c(1));
fprintf(fid, 'Time: %02i:%02i:%02i\r\n', c(4), c(5), c(6));

%Write the measurement file header
fprintf(fid, '\r\n');
fprintf(fid, 'Measurement File Header:\r\n');
[r, c] = size(header);
for ii = 1:c
    if length(header{ii}) > 0
        fprintf(fid, '%s\r\n', header{ii});
    end
end

%Write the data header
fprintf(fid, '\r\n');
fprintf(fid, '----- Hall Probe Calibration -----\r\n');

%Document the outliers
fprintf(fid, '\r\n');
fprintf(fid, 'Outliers, removed from the analysis:\r\n');
fprintf(fid, '  V_hall        B_nmr   \r\n');
fprintf(fid, '    (V)          (T)    \r\n');
fprintf(fid, '-----------  -----------\r\n');
if length(v_hall_out) > 0
    fprintf(fid, '%11.8f  %11.8f\r\n', [v_hall_out; b_nmr_out]);
else
    fprintf(fid, '(none)');
end

%Write the Hall probe offset voltage
fprintf(fid, '\r\n');
fprintf(fid, 'Hall probe offset voltage = %11.8f V\r\n', v_hall_offset_no_out);

%Write the fit parameters
fprintf(fid, '\r\n');
fprintf(fid, 'Fit parameters:\r\n');
fprintf(fid, 'Voc = V - Voffset\r\n');
fprintf(fid, 'Bnmr = p0 + p1 Voc + p2 Voc^2 + p3 Voc^3 + ... + pn Voc^n\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '  n         pn      \r\n');
fprintf(fid, '         (T / V^n)  \r\n');
fprintf(fid, '-----  -------------\r\n');
n = 0 : param.poly_fit_order;
pn = fliplr(fit_coef_bvsvoc_no_out);
fprintf(fid, ' %2i    %13.9f\r\n', [n; pn]);

%Close the data file
fclose(fid);

%Done
