%**************************************************************************
%This function reads the mechanical measurement data from a file.
%
%Input:
%param, structure containing the program parameters
%
%Output:
%header, header of the measurement file
%cmm_data, structure containing the CMM measurements
%
%Zack Wolf, 9/11/07

function [header, cmm_data] = cmm_read_meas(param)

%Input data file
file_name = [param.input_file_dir, param.input_file_name];

%Open the CMM measurement data file
fid = fopen(file_name, 'r');
if fid == -1
    disp('idanal_read_meas_cmm: unable to open input file');
end

%Extract the header information
ii = 0;
while 1
    in_line = fgetl(fid);
    ii = ii +1;
    header{ii} = in_line;
    if strfind(in_line, '! TIP |   ANG   |   ANG   |   PNT    | GAP    |  X = 0  |  X = 0  |  FACE   |  FACE   |') > 0
        break;
    end
end
in_line = fgetl(fid);  %Last line of header
ii = ii +1;
header{ii} = in_line;

%Read the data
data_array = fscanf(fid, '%f %f %f %f %f %f %f %f %f', [9, inf]);
cmm_data.pole_num = data_array(1, :);
cmm_data.cant_angle = data_array(2, :);  %(mrad)
cmm_data.midline_angle = data_array(3, :);  %(mrad)
  %cmm_data.cant_angle = data_array(3, :);  %(mrad)
  %cmm_data.midline_angle = data_array(2, :);  %(mrad)
cmm_data.y_ctr = data_array(4, :);  %(mm)
cmm_data.gap_height = data_array(5, :);  %(mm)
cmm_data.y_upper = data_array(6, :);  %(mm)
cmm_data.y_lower = data_array(7, :);  %(mm)
  %cmm_data.y_upper = data_array(7, :);  %(mm)
  %cmm_data.y_lower = data_array(6, :);  %(mm)
cmm_data.x_lower = data_array(8, :);  %(mm)
cmm_data.x_upper = data_array(9, :);  %(mm)

%Convert to mks units and radians instead of degrees
cmm_data.cant_angle = cmm_data.cant_angle * 1e-3;
cmm_data.midline_angle = cmm_data.midline_angle * 1e-3;
cmm_data.y_ctr = cmm_data.y_ctr / 1000;
cmm_data.gap_height = cmm_data.gap_height / 1000;
cmm_data.y_upper = cmm_data.y_upper / 1000;
cmm_data.y_lower = cmm_data.y_lower / 1000;
cmm_data.x_upper = cmm_data.x_upper / 1000;
cmm_data.x_lower = cmm_data.x_lower / 1000;

%Close the file
fclose(fid);

%Done
