%**************************************************************************
%cmm_anal.m
%
%This function reads CMM measurements made on an insertion device.
%The measurements are analyzed and the results are output to a file.  The
%mechanical measurements are placed in a structure with the following
%elements:
%cmm_data, structure containing the mechanical measurements
%   pole_num, array of pole numbers
%   cant_angle, angle between the upper and lower poles (rad)
%   midline_angle, angle of the midline between the upper and lower pole (rad)
%   y_ctr, y position of the midline at nominal x center position (m)
%   gap_height, gap height at nominal x center position (m)
%   y_upper, y position of the upper pole at nominal x center position (m)
%   y_lower, y position of the lower pole at nominal x center position (m)
%   x_upper, x position of the upper pole (m)
%   x_lower, x position of the lower pole (m)
%
%Zachary Wolf, 9/11/07

function [] = cmm_anal()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
cmm_anal_param;

%Make the parameter structure
param.input_file_dir = INPUT_FILE_DIR;
param.input_file_name = INPUT_FILE_NAME;
param.coord_sys_fig_file = COORD_SYS_FIG_FILE;
param.shim_y_range = SHIM_Y_RANGE;
param.shim_angle = SHIM_ANGLE;

%Output files
dat_file = [param.input_file_dir, 'cmm_anal.dat'];
plt_file = [param.input_file_dir, 'cmm_anal.ps'];
mat_file = [param.input_file_dir, 'cmm_anal.mat'];

%Read the measurements
[header, cmm_data] = cmm_read_meas(param);

%Initialize the plot file
cmm_plot_init(plt_file, param, header);

%Initialize the data file
cmm_dat_init(dat_file, param, header);

%Display the coordinate system
cmm_plot_coord_syst(plt_file, param);

%Plot the mechanical measurements
cmm_plot_cant_angle(plt_file, cmm_data.pole_num, cmm_data.cant_angle);
cmm_plot_midline_angle(plt_file, cmm_data.pole_num, cmm_data.midline_angle);
%cmm_plot_midline_angle_diff(plt_file, cmm_data.pole_num, cmm_data.midline_angle);
cmm_plot_y_ctr(plt_file, cmm_data.pole_num, cmm_data.y_ctr);
cmm_plot_gap_height(plt_file, cmm_data.pole_num, cmm_data.gap_height);
cmm_plot_y_upper(plt_file, cmm_data.pole_num, cmm_data.y_upper);
cmm_plot_y_lower(plt_file, cmm_data.pole_num, cmm_data.y_lower);
cmm_plot_x_upper(plt_file, cmm_data.pole_num, cmm_data.x_upper);
cmm_plot_x_lower(plt_file, cmm_data.pole_num, cmm_data.x_lower);

%Write a summary of the measurements to the data file
cmm_dat_summary(dat_file, cmm_data);

%Calculate shim motion required to straighten the y position of the gap center
[y_ctr_shim] = cmm_calc_y_ctr_shim(cmm_data.y_ctr);
[y_ctr_shim_fit] = cmm_calc_y_ctr_shim_fit(y_ctr_shim);

%Plot the fit to the shim size
cmm_plot_y_ctr_shim(plt_file, cmm_data.pole_num, y_ctr_shim, y_ctr_shim_fit);

%Calculate required shim replacements
[y_ctr_shim_upper_replace, y_ctr_shim_lower_replace, y_ctr_shim_fit_after_replace] = cmm_calc_y_ctr_shim_replace(param, y_ctr_shim_fit);

%Write shim replacements to the data file
cmm_dat_y_ctr_shim_replace(dat_file, y_ctr_shim_fit, y_ctr_shim_upper_replace, y_ctr_shim_lower_replace);

%Calculate the x motions of the shims after the replacements have been made
[y_ctr_shim_upper_dx, y_ctr_shim_lower_dx] = cmm_calc_y_ctr_shim_dx(param.shim_angle, y_ctr_shim_fit_after_replace);

%Write the shim dimensions to the data file
cmm_dat_y_ctr_shim(dat_file, y_ctr_shim_fit_after_replace, y_ctr_shim_upper_dx, y_ctr_shim_lower_dx);

%Output the measurement data
save(mat_file, 'param', 'header', 'cmm_data');

%Done
