%**************************************************************************
%This function reads the RTD electronics channel offset voltage data file.
%It calculates the average of the offset measurements.
%
%Zachary Wolf, 2/25/09

function [] = RTD_offset_calc()

%Clear the workspace
clear

%Parameters
OFFSET_CALC_NAME = 'MMF RTD Calibration System Offsets';
OFFSET_CALC_DIR = 'C:\Magdata\RTD Calib Offsets\';
INPUT_FILE = 'C:\Magdata\RTD Calib Offsets\offset_dat.txt';
CALIB_RESISTOR_VAL = 107.95;

%Set the working directory
cd(OFFSET_CALC_DIR(1 : end - 1));

%Output files
dat_file = [OFFSET_CALC_DIR, OFFSET_CALC_NAME, '.dat'];
mat_file = [OFFSET_CALC_DIR, OFFSET_CALC_NAME, '.mat'];
plt_file = [OFFSET_CALC_DIR, OFFSET_CALC_NAME, '.ps'];
fig_file = [OFFSET_CALC_DIR, OFFSET_CALC_NAME, '.fig'];

%Initialize the plots
calib_hist_plot_init(plt_file, OFFSET_CALC_NAME);

%Get the offset data
offset_dat = load(INPUT_FILE);
chan_num = offset_dat(:, 1)';
for ii = 1 : length(chan_num)
    v_meas{ii} = offset_dat(ii, 2 : end);
end

%Calculate the average of the measured voltages
for ii = 1 : length(chan_num)
    v_meas_ave(ii) = mean(v_meas{ii});
    v_meas_rms(ii) = std(v_meas{ii});
end

%Calculate the ideal equivalent temperature for the calibration resistor
[t_ideal] = RTD_RcalcT(CALIB_RESISTOR_VAL);

%Calculate the ideal voltage for the calibration resistor
[v_ideal] = RTD_RcalcV(CALIB_RESISTOR_VAL);

%Calculate the correction to add to each channel
v_corr = v_ideal - v_meas_ave;

%Calculate equivalent temperature from the output voltages
[t_meas_ave] = RTD_VcalcT(v_meas_ave);
[t_meas_rms] = RTD_VcalcT(v_meas_rms);
[t_corr] = RTD_VcalcT(v_corr);

%Write the results to the data file
fid = fopen(dat_file, 'w');
c = fix(clock);
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));
fprintf(fid, '\r\n');
fprintf(fid, 'RTD Offset Calculations:\r\n');
fprintf(fid, 'Calculation Name = %s\r\n', OFFSET_CALC_NAME);
fprintf(fid, 'Data Directory = %s\r\n', OFFSET_CALC_DIR);
fprintf(fid, 'Input File Name = %s\r\n', INPUT_FILE);
fprintf(fid, '\r\n');
fprintf(fid, 'Calibration Resistor Value = %f ohm\r\n', CALIB_RESISTOR_VAL);
fprintf(fid, 'Ideal Equivalent Temperature = %f C\r\n', t_ideal);
fprintf(fid, 'Ideal System Output Voltage = %f V\r\n', v_ideal);
fprintf(fid, '\r\n');
fprintf(fid, 'Chan      Vave        Vrms       Videal      Vcorr   \r\n');
fprintf(fid, ' Num      (V)         (V)         (V)         (V)    \r\n');
fprintf(fid, '-----  ----------  ----------  ----------  ----------\r\n');
fprintf(fid, '%5i  %10.6f  %10.6f  %10.6f  %10.6f\r\n', ...
    [chan_num; v_meas_ave; v_meas_rms; v_ideal * ones(1, length(chan_num)); v_corr]);
fprintf(fid, '\r\n');
fprintf(fid, 'Equivalent Temperatures:\r\n');
fprintf(fid, 'Chan      Tave        Trms       Tideal      Tcorr   \r\n');
fprintf(fid, ' Num      (C)         (C)         (C)         (C)    \r\n');
fprintf(fid, '-----  ----------  ----------  ----------  ----------\r\n');
fprintf(fid, '%5i  %10.6f  %10.6f  %10.6f  %10.6f\r\n', ...
    [chan_num; t_meas_ave; t_meas_rms; t_ideal * ones(1, length(chan_num)); t_corr]);
fclose(fid);

%Plot the equivalent temperature corrections
figure;
plot(chan_num, t_corr);
title(OFFSET_CALC_NAME);
xlabel('Channel Number');
ylabel('Temperature Correction (C)');
grid on;
zoom on;
orient landscape;
print('-append', '-dpsc', plt_file);

%Save the results
save(mat_file, 'offset_dat', 'chan_num', 'v_meas_ave', 'v_meas_rms', 'v_ideal', 'v_corr', 't_meas_ave', 't_meas_rms', 't_ideal', 't_corr');

%Save the current figure, the temperature history figure
hgsave(fig_file);

%Done
