%**************************************************************************
%This function writes a summary of the mechanical measurements
%to the data file.
%
%Input:
%dat_file, data file for results
%cmm_data, structure containing the CMM measurements
%
%Zachary Wolf, 9/18/07

function [] = cmm_dat_summary(dat_file, cmm_data)

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

%Write the summary header
fprintf(fid, '\r\n');
fprintf(fid, '----- CMM Mechanical Measurements Summary -----\r\n');

%Cant angle
fprintf(fid, '\r\n');
fprintf(fid, 'Cant Angle:\r\n');
fprintf(fid, 'Average = %f mrad, RMS = %f mrad\r\n', mean(cmm_data.cant_angle) * 1000, std(cmm_data.cant_angle) * 1000);

%Midline angle
fprintf(fid, '\r\n');
fprintf(fid, 'Midline Angle:\r\n');
fprintf(fid, 'Average = %f deg, RMS = %f deg\r\n', mean(cmm_data.midline_angle) * 180 / pi, std(cmm_data.midline_angle) * 180 / pi);

%Gap center y position
fprintf(fid, '\r\n');
fprintf(fid, 'Y Position Of The Gap Center:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.y_ctr) * 1000, std(cmm_data.y_ctr) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.y_ctr) * 1000, max(cmm_data.y_ctr) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.y_ctr) - min(cmm_data.y_ctr)) * 1000);

%Gap height
fprintf(fid, '\r\n');
fprintf(fid, 'Gap Height:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.gap_height) * 1000, std(cmm_data.gap_height) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.gap_height) * 1000, max(cmm_data.gap_height) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.gap_height) - min(cmm_data.gap_height)) * 1000);

%Upper pole y position
fprintf(fid, '\r\n');
fprintf(fid, 'Y Position Of The Upper Pole:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.y_upper) * 1000, std(cmm_data.y_upper) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.y_upper) * 1000, max(cmm_data.y_upper) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.y_upper) - min(cmm_data.y_upper)) * 1000);

%Lower pole y position
fprintf(fid, '\r\n');
fprintf(fid, 'Y Position Of The Lower Pole:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.y_lower) * 1000, std(cmm_data.y_lower) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.y_lower) * 1000, max(cmm_data.y_lower) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.y_lower) - min(cmm_data.y_lower)) * 1000);

%Upper pole x position
fprintf(fid, '\r\n');
fprintf(fid, 'X Position Of The Upper Pole:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.x_upper) * 1000, std(cmm_data.x_upper) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.x_upper) * 1000, max(cmm_data.x_upper) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.x_upper) - min(cmm_data.x_upper)) * 1000);

%Lower pole x position
fprintf(fid, '\r\n');
fprintf(fid, 'X Position Of The Lower Pole:\r\n');
fprintf(fid, 'Average = %f mm, RMS = %f mm\r\n', mean(cmm_data.x_lower) * 1000, std(cmm_data.x_lower) * 1000);
fprintf(fid, 'Min = %f mm, Max = %f mm\r\n', min(cmm_data.x_lower) * 1000, max(cmm_data.x_lower) * 1000);
fprintf(fid, 'Range = %f mm\r\n', (max(cmm_data.x_lower) - min(cmm_data.x_lower)) * 1000);

%Close the data file
fclose(fid);

%Done
