%**************************************************************************
%cmm_comp.m
%
%This function reads CMM analysis results.  It compares the
%results and plots the comparisons.
%
%Zachary Wolf, 12/24/07

function [] = cmm_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
cmm_comp_param

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

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

%Loop over analysis results, 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', 'cmm_data');
    
    %Get the data
    pole_num{ii, :} = cmm_data.pole_num;
    cant_angle{ii, :} = cmm_data.cant_angle;
    midline_angle{ii, :} = cmm_data.midline_angle;
    y_ctr{ii, :} = cmm_data.y_ctr;
    gap_height{ii, :} = cmm_data.gap_height;
    y_upper{ii, :} = cmm_data.y_upper;
    y_lower{ii, :} = cmm_data.y_lower;
    x_upper{ii, :} = cmm_data.x_upper;
    x_lower{ii, :} = cmm_data.x_lower;
    
    %Calculations
    x_ave{ii, :} = (x_upper{ii, :} + x_lower{ii, :}) / 2;
    
%End loop over analysis results
end

%Correct the gap heights to the same x value (the x value of the first data set)
for ii = 1 : c
    corr_gap_height{ii, :} = gap_height{ii, :} - (x_ave{ii, :} - x_ave{1, :}) .* cant_angle{ii, :};
end

%Plot the gap center heights
cmm_comp_plot_y_ctr(plt_file, pole_num, y_ctr, INPUT_NAME);

%Plot the undulator x positions
cmm_comp_plot_x_ave(plt_file, pole_num, x_ave, INPUT_NAME);

%Plot the undulator cant angle
cmm_comp_plot_cant_angle(plt_file, pole_num, cant_angle, INPUT_NAME);

%Plot the undulator gap height
cmm_comp_plot_gap_height(plt_file, pole_num, gap_height, INPUT_NAME);

%Plot the undulator gap height differernce
cmm_comp_plot_gap_height_diff(plt_file, pole_num, gap_height, INPUT_NAME);

%Plot the corrected undulator gap height differernce
cmm_comp_plot_corr_gap_height_diff(plt_file, pole_num, corr_gap_height, INPUT_NAME);

%Done
