%**************************************************************************
%This function reads CMM tooling ball position values.  It compares the
%tooling ball positions and plots the comparisons.
%
%Zachary Wolf, 3/30/09

function [] = cmm_tbval_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
cmm_tbval_comp_param

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

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Compare Tooling Ball Positions:'));
util_dat_msg(dat_file, cellstr(COMPARE_NAME));

%Initialize the plots
cmm_tbval_comp_plot_init(plt_file, COMPARE_NAME, INPUT_FILE);

%Loop over input files, get data
[r, c] = size(INPUT_FILE);
for ii = 1 : c
    
    %Get the input file
    file_name = INPUT_FILE{ii};
    
    %Get the measurement date from the file name
    index = findstr(file_name, '.');
    date_str{ii} = file_name(index - 11 : index - 1);
    date_num(ii) = datenum(date_str{ii}, 'dd-mmm-yyyy');
    
    %Get the data
	[tbvals] = cmm_tbval_comp_read_data(file_name);
    tbval_data(ii) = tbvals;
    
    %Calculations
    x_ave(ii) = (tbvals.x(5) + tbvals.x(6) + tbvals.x(7) + tbvals.x(8)) / 4;
    y_ave(ii) = (tbvals.y(1) + tbvals.y(2) + tbvals.y(3) + tbvals.y(4)) / 4;
    
%End loop over analysis results
end

%Calculate changes in average positions
%This is the same as the average of the tooling ball position changes
dx_ave = x_ave - x_ave(1);
dy_ave = y_ave - y_ave(1);

%Write the tooling ball x positions to a file
cmm_tbval_comp_dat_xvals(dat_file, date_num, INPUT_NAME, tbval_data, x_ave, dx_ave);

%Write the tooling ball y positions to a file
cmm_tbval_comp_dat_yvals(dat_file, date_num, INPUT_NAME, tbval_data, y_ave, dy_ave);

%Plot the tooling ball x positions
cmm_tbval_comp_plot_xave(plt_file, INPUT_NAME, date_num, x_ave);
cmm_tbval_comp_plot_dxave(plt_file, INPUT_NAME, date_num, dx_ave);

%Calculations for the temperature test
temps_nom = [20.00, 19.00, 19.50, 20.50, 20.50, 21.00, 19.00, 20.00];
temps =     [20.03, 18.90, 19.45, 20.43, 20.49, 20.96, 19.06, 19.95];
dkdx = [2.6302, 2.6822, 2.6590, 2.6252, 2.6488, 2.5962, 2.6822, 2.6302];  %(1/m)
dk = dkdx .* dx_ave;

%Plot the tooling ball x positions for the temperature test
cmm_tbval_comp_plot_dxave_temp_test_bydate(plt_file, INPUT_NAME, date_num, dx_ave);
cmm_tbval_comp_plot_dxave_temp_test_bytemp(plt_file, INPUT_NAME, date_num, dx_ave);
cmm_tbval_comp_plot_dk_temp_test_bytemp(plt_file, INPUT_NAME, date_num, dk);

%Plot the tooling ball x positions for the reference undulator
% cmm_tbval_comp_plot_dxave_ref_und(plt_file, INPUT_NAME, date_num, dx_ave);
% cmm_tbval_comp_plot_dxave_ref_und_byrun(plt_file, INPUT_NAME, date_num, dx_ave);
% cmm_tbval_comp_plot_dxave_tempexcursion(plt_file, INPUT_NAME, date_num, dx_ave, TEMP_EXCURSION);
% cmm_tbval_comp_plot_dxave_hallcalib(plt_file, INPUT_NAME, date_num, dx_ave, HALL_CALIB);

%Plot the tooling ball y positions
cmm_tbval_comp_plot_yave(plt_file, INPUT_NAME, date_num, y_ave);
cmm_tbval_comp_plot_dyave(plt_file, INPUT_NAME, date_num, dy_ave);

%Plot the tooling ball y positions for the reference undulator
% cmm_tbval_comp_plot_dyave_ref_und(plt_file, INPUT_NAME, date_num, dy_ave);
% cmm_tbval_comp_plot_dyave_ref_und_byrun(plt_file, INPUT_NAME, date_num, dy_ave);

%Save the results to the mat file
save(mat_file, 'COMPARE_NAME', 'OUTPUT_DIR', 'INPUT_FILE', 'INPUT_NAME', 'date_str', 'date_num', 'tbval_data', 'x_ave', 'dx_ave', 'y_ave', 'dy_ave');

%Done
