function [o1,o2,o3,o4,o5,o6,o7,o8,o9,o10,o11,o12,o13,o14,o15,o16,o17] ...
                                    = read_uff(filename,get_data_set_type)
%usage:  
%
%for measurement data:
% [freq,data,errcode,errmessage,descript1,descript2,date,IDs,resp_pt_num, ...
%  r_direction,excit_ptnum,excit_direction,function_type]=read_uff(filename,58);
%
%for modal data:  (for normal modes)
% [analysis_type,data_character,response_type,mode_num, ...
%  mode_freq,mode_mass,mode_v_damping_ratio,mode_h_damping_ratio, ...
%  node_num,r1,r2,r3,r4,r5,r6,errcode,errmessage] = read_uff(filename,55);
%or       (for complex modes)
% [analysis_type,data_character,response_type,mode_num, ...
%  eig_val,model_A,model_B,dummy_var, ...
%  node_num,r1,r2,r3,r4,r5,r6,errcode,errmessage] = read_uff(filename,55);
%
%for Coordinate data:  (Grid pts)
% [pt_num,x_coord,y_coord,z_coord,errcode,errmessage] = read_uff(filename,15);
%
%for Display Sequence data:
% [disp_sequence,erc,ermessage] = read_uff(filename,82);
%
%to check on what data sets are available in file, filename:
% data_sets = read_uff(filename,0);
%
%  modified by Scott W Doebling for Matlab 5 compliance and for speed
%  version SWD970528

erc = 0;
im = sqrt(-1);
sp80=['          ','          ','          ','          ','          ', ...
      '          ','          ','          ','          ','          '];

if exist('get_data_set_type')~=1,
  get_data_set_type=58; 
end
[fid,ermesage] = fopen(filename,'rt');
if  fid==-1,
  ermessage = 'could not open file';
  disp(ermessage);
  erc = -1;
  return;
end

function_type=[];
lp15 = 0;
lp55 = 0;
lp58 = 0;
lp82 = 0;
o1=[];
eof=0;
looping=0;
%======================= data set loop 
while ~eof
looping=looping+1;
started=0;
maxlines = 1000;
DATA=reshape(blanks(maxlines*80),maxlines,80);

not_type = 0;  % Added

while 1,
  linest = fgetl(fid);
  if ~isstr(linest), eof=1; break, end
     if  sscanf(linest,'%f',1)== -1,
        if ~started,
           started=1;
           drow = 0;
           linest = fgetl(fid);
           if ~isstr(linest), eof=1; data_set_type=0;started=0; break, end
           data_set_type = sscanf(linest,'%i',1);
           %
           %  Added check to got to next data block if this block is
           %  not the correct type
           %
           if data_set_type ~= get_data_set_type,
              not_type = 1;
           end
           if get_data_set_type == 0,
              o1 = [o1;data_set_type];
           end
        else,
           break;
        end
   
     else,
        %if ~isempty(DATA),
           %DATA=str2mat(DATA,linest);
         %  DATA = [DATA;linest,blanks(80-length(linest))];
       %else,
           %DATA=linest;
        %   DATA=[linest,blanks(80-length(linest))];
        %end
        drow = drow+1;
        DATA(drow,:) = [linest,blanks(80-length(linest))];
     end
 
  end
  DATA = DATA(1:drow,:);
num_linests = size(DATA,1);
if ~started, break, end
if ~not_type  % Skip this block if it is not the type we are looking for

if data_set_type == 15 & get_data_set_type == 15,  % Coordinate Data
lp15 = lp15+1;
loop = lp15;

%-----------------------------Start of Coordinate Data-------------------------

  for i=1:num_linests,
    pt_num(loop,i) = sscanf(DATA(i,1:10),'%i',1);
    def_coord_sys = sscanf(DATA(i,11:20),'%i',1);
    disp_coord_sys = sscanf(DATA(i,21:30),'%i',1);
    color_var = sscanf(DATA(i,31:40),'%i',1);
    x_coord(loop,i) = sscanf(DATA(i,41:53),'%g',1);
    y_coord(loop,i) = sscanf(DATA(i,54:66),'%g',1);
    z_coord(loop,i) = sscanf(DATA(i,67:79),'%g',1);
  end

%-----------------------------End of Coordinate Data---------------------------

elseif data_set_type == 55 & get_data_set_type == 55, % Modal Data
lp55 = lp55+1;
loop = lp55;

%-----------------------------Start of Modal Data------------------------------
  %========
  linest = 1;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  d1(loop,1:80) = st_tmp(1:80);

  %========
  linest = 2;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  d2(loop,1:80) = st_tmp(1:80);

  %========
  linest = 3;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  date(loop,1:80) = st_tmp(1:80);
 
  %========
  linest = 4;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  IDs(loop,1:80) = st_tmp(1:80);

  %========
  linest = 5;
  %========
  temp = sscanf(DATA(linest,:),'%s',1);

  %========
  linest = 6;
  %========
  model_type = sscanf(DATA(linest,1:10),'%i',1);
  if model_type ~=1,
    ermessage = 'not structural model type';
    disp(ermessage);
    erc = -1;
    return;
  end
  analysis_type(loop) = sscanf(DATA(linest,11:20),'%i',1);
  data_character(loop) = sscanf(DATA(linest,21:30),'%i',1);
  response_type(loop) = sscanf(DATA(linest,31:40),'%i',1);
  data_type = sscanf(DATA(linest,41:50),'%i',1);
  num_data_per_pt = sscanf(DATA(linest,51:60),'%i',1);  % Corrected to convert to decimal

  if analysis_type == 2  % Normal Mode

    %========
    linest = 7;
    %========
    two = sscanf(DATA(linest,1:10),'%i',1);
    if two ~=2,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    four = sscanf(DATA(linest,11:20),'%i',1);
    if four ~=4,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    tmp = sscanf(DATA(linest,21:30),'%i',1);
    mode_num(loop) = sscanf(DATA(linest,31:40),'%i',1);

    %========
    linest = 8;
    %========
    mode_freq(loop) = sscanf(DATA(linest,1:13),'%g',1);
    mode_mass(loop) = sscanf(DATA(linest,14:26),'%g',1);
    mode_v_damping_ratio(loop) = sscanf(DATA(linest,27:39),'%g',1);
    mode_h_damping_ratio(loop) = sscanf(DATA(linest,40:52),'%g',1);
  
  elseif analysis_type == 3, % Complex Eigenvalue, First Order (Displacement)

    %========
    linest = 7;
    %========
    two = sscanf(DATA(linest,1:10),'%i',1);
    if two ~=2,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    six = sscanf(DATA(linest,11:20),'%i',1);
    if six ~=6,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    tmp = sscanf(DATA(linest,21:30),'%i',1);
    mode_num(loop) = sscanf(DATA(linest,31:40),'%i',1);

    %========
    linest = 8;
    %========
    real_part = sscanf(DATA(linest,1:13),'%g',1);
    imaginary_part = sscanf(DATA(linest,14:26),'%g',1);
    eig_val(loop) = real_part + im * imaginary_part;
    real_part = sscanf(DATA(linest,27:39),'%g',1);
    imaginary_part = sscanf(DATA(linest,40:52),'%g',1);
    model_A(loop) = real_part + im * imaginary_part;
    real_part = sscanf(DATA(linest,53:65),'%g',1);
    imaginary_part = sscanf(DATA(linest,66:78),'%g',1);
    model_B(loop) = real_part + im * imaginary_part;

  elseif analysis_type == 7  % Complex Eigenvalue, Second Order (Velocity)

    %========
    linest = 7;
    %========
    two = sscanf(DATA(linest,1:10),'%i',1);
    if two ~=2,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    six = sscanf(DATA(linest,11:20),'%i',1);
    if six ~=6,
      ermessage = 'unexpected value';
      disp(ermessage);
      erc = -3;
    end
    tmp = sscanf(DATA(linest,21:30),'%i',1);
    mode_num(loop) = sscanf(DATA(linest,31:40),'%i',1);

    %========
    linest = 8;
    %========
    real_part = sscanf(DATA(linest,1:13),'%g',1);
    imaginary_part = sscanf(DATA(linest,14:26),'%g',1);
    eig_val(loop) = real_part + im * imaginary_part;
    real_part = sscanf(DATA(linest,27:39),'%g',1);
    imaginary_part = sscanf(DATA(linest,40:52),'%g',1);
    model_A(loop) = real_part + im * imaginary_part;
    real_part = sscanf(DATA(linest,53:65),'%g',1);
    imaginary_part = sscanf(DATA(linest,66:78),'%g',1);
    model_B(loop) = real_part + im * imaginary_part;

  else
    ermessage = 'analysis type is not supported';
    disp(ermessage);
    erc = -1;
    return
  end

  i=0;
  for lne=9:2:num_linests-1,
  i=i+1;

    %========
%    linest = 9; % linest 9 type of linest
    %========
    linest=lne;
    node_num(loop,i) = sscanf(DATA(linest,1:10),'%i',1);
    %========
%    linest = 10; % linest 9 type of linest
    %========
    linest=lne+1;
    if data_type == 2,   % real data
      r1(loop,i) = sscanf(DATA(linest,1:13),'%g',1);
      r2(loop,i) = sscanf(DATA(linest,14:26),'%g',1);
      r3(loop,i) = sscanf(DATA(linest,27:39),'%g',1);
      if num_data_per_pt==6,
        r4(loop,i) = sscanf(DATA(linest,40:52),'%g',1);
        r5(loop,i) = sscanf(DATA(linest,53:65),'%g',1);
        r6(loop,i) = sscanf(DATA(linest,66:78),'%g',1);
      end
    elseif data_type == 3,   % complex data
      p1 = sscanf(DATA(linest,1:13),'%g',1);
      p2 = sscanf(DATA(linest,14:26),'%g',1);
      r1(loop,i) = p1 + im * p2;
      p1 = sscanf(DATA(linest,27:39),'%g',1);
      p2 = sscanf(DATA(linest,40:52),'%g',1);
      r2(loop,i) = p3 + im * p4;
      p1 = sscanf(DATA(linest,53:65),'%g',1);
      p2 = sscanf(DATA(linest,66:78),'%g',1);
      r3(loop,i) = p5 + im * p6;
      if num_data_per_pt==6,
        ermessage = 'not setup to handle six coordinate of complex data';
        disp(ermessage);
        erc = -1;
        return
      end
    else,
      ermessage = 'what kind of data type was that?';
      disp(ermessage);
      erc = -1;
      return
    end
  end

%-----------------------------End of Modal Data--------------------------------

elseif data_set_type == 58 & get_data_set_type == 58,
   

                                                       % Measurement Data Type
lp58 = lp58+1;
loop = lp58;

%-----------------------------Start of Measurement Data------------------------
  %========
  linest = 1;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  d1(loop,1:80) = st_tmp(1:80);

  %========
  linest = 2;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  d2(loop,1:80) = st_tmp(1:80);

  %========
  linest = 3;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  date(loop,1:80) = st_tmp(1:80);
  
  %========
  linest = 4;
  %========
  st_tmp = [sscanf(DATA(linest,:),'%c',80),sp80];
  IDs(loop,1:80) = st_tmp(1:80);

  %========
  linest = 5;
  %========
  temp = sscanf(DATA(linest,:),'%s',1);

  %========
  linest = 6;
  %========
  function_type = [function_type;sscanf(DATA(linest,1:5),'%i',1)];
  if function_type(loop) ~=4,
    ermessage = 'not frf data';
    disp(ermessage);
    erc = -3;
  end
  tmp = sscanf(DATA(linest,6:15),'%i',1);
  tmp = sscanf(DATA(linest,16:20),'%i',1);
  load_case_id(loop) = sscanf(DATA(linest,21:30),'%i',1);
  if load_case_id(loop) ~= 0,
    ermessage = 'need single point excitation';
    disp(ermessage);
    erc = -1;
    return;
  end
  temp = sscanf(DATA(linest,32:41),'%s',1);
  rptnum(loop) = sscanf(DATA(linest,42:51),'%i',1);
  rdir(loop) = sscanf(DATA(linest,52:55),'%i',1);
  temp = sscanf(DATA(linest,57:66),'%s',1);
  eptnum(loop) = sscanf(DATA(linest,67:76),'%i',1);
  edir(loop) = sscanf(DATA(linest,77:80),'%i',1);

  %========
  linest = 7;  % data form
  %========
  data_type(loop) = sscanf(DATA(linest,1:10),'%i',1);
  numpt(loop) = sscanf(DATA(linest,11:20),'%i',1);
  freq_spacing(loop) = sscanf(DATA(linest,21:30),'%i',1);
  if freq_spacing(loop) ~= 1,
    ermessage = 'need evenly spaced frequency';
    disp(ermessage);
    erc = -1;
    return;
  end
  fmin(loop) = sscanf(DATA(linest,31:43),'%g',1);
  fdel(loop) = sscanf(DATA(linest,44:56),'%g',1);
  tmp = sscanf(DATA(linest,57:69),'%g',1);

  %========
  linest = 8;  % abscissa data characteristics
  %========
  ab_data_type(loop) = sscanf(DATA(linest,1:10),'%i',1);
  if ab_data_type(loop) ~= 18,  %Frequency
    ermessage = 'need frequency on abcissa';
    disp(ermessage);
    erc = -1;
    return;
  end
  tmp = sscanf(DATA(linest,11:15),'%i',1);
  tmp = sscanf(DATA(linest,16:20),'%i',1);
  tmp = sscanf(DATA(linest,21:25),'%i',1);
  temp = sscanf(DATA(linest,27:46),'%s',1);
  temp = sscanf(DATA(linest,48:67),'%s',1);

  %========
  linest = 9;  % Ordinate (or ordinate numerator) Data Characteristics
  %========
  or_num_data_type(loop) = sscanf(DATA(linest,1:10),'%i',1);
%  if or_num_data_type(loop) ~= 12,  %Acceleration
%    ermessage = 'expected acceleration data in the ordinate numerator';
%    disp(ermessage);
%  end
  tmp = sscanf(DATA(linest,11:15),'%i',1);
  tmp = sscanf(DATA(linest,16:20),'%i',1);
  tmp = sscanf(DATA(linest,21:25),'%i',1);
  temp = sscanf(DATA(linest,27:46),'%s',1);
  temp = sscanf(DATA(linest,48:67),'%s',1);

  %========
  linest = 10;  % Ordinate Denominator Data Characteristics
  %========
  or_den_data_type(loop) = sscanf(DATA(linest,1:10),'%i',1);
%  if or_den_data_type(loop) ~= 13,  % Excitation force
%    ermessage = 'expected force data in the ordinate denominator';
%    disp(ermessage);
%  end
  tmp = sscanf(DATA(linest,11:15),'%i',1);
  tmp = sscanf(DATA(linest,16:20),'%i',1);
  tmp = sscanf(DATA(linest,21:25),'%i',1);
  temp = sscanf(DATA(linest,27:46),'%s',1);
  temp = sscanf(DATA(linest,48:67),'%s',1);

  %========
  linest = 11;  % Z-axis Data Characteristics
  %========
  tmp = sscanf(DATA(linest,1:10),'%i',1);
  tmp = sscanf(DATA(linest,11:15),'%i',1);
  tmp = sscanf(DATA(linest,16:20),'%i',1);
  tmp = sscanf(DATA(linest,21:25),'%i',1);
  temp = sscanf(DATA(linest,27:46),'%s',1);
  temp = sscanf(DATA(linest,48:67),'%s',1);
  
  %========
  linest = 12;  % Data Values
  %========
  %first put all data values in a single row vector (add spaces between linests)
  file_data = DATA(linest:num_linests,:);
  file_data = [file_data,setstr(32*ones(num_linests-linest+1,1))];
  file_data = file_data';
  file_data = file_data(:)';
  zindex=find(abs(file_data)~=0);
  file_data=file_data(zindex);
  if data_type(loop) == 2,
    data = sscanf(file_data,'%g',numpt(loop));
  elseif data_type(loop) == 4,
    data = sscanf(file_data,'%g',numpt(loop));
  elseif data_type(loop) == 5,
    newdata = sscanf(file_data,'%g',2*numpt(loop));
    indx = 2*[1:numpt(loop)];
    data = newdata(indx-1) + im* newdata(indx);
  elseif data_type(loop) == 6,
    newdata = sscanf(file_data,'%g',2*numpt(loop));
    indx = 2*[1:numpt(loop)];
    data = newdata(indx-1) + im* newdata(indx);
  else,
    ermessage = ['data type ',int2str(data_type(loop)),' is not supported'];
    disp(ermessage);
    erc = -1;
    return;
  end
  meas_data(loop,:) = data';
  freq(loop,:)=[fmin(loop):fdel(loop):fmin(loop)+(numpt(loop)-1)*fdel(loop)];

%-------------------------------End of Measurement Data------------------------

elseif data_set_type == 82 & get_data_set_type == 82, 
						% Display Sequence Data
lp82 = lp82+1;
loop = lp82;
%-------------------------------Start of Display Sequence Data-----------------

  %========
  linest = 1;
  %========
  tmp = sscanf(DATA(linest,1:10),'%i',1);
  num_display_entries = sscanf(DATA(linest,11:20),'%i',1);
  color_var = sscanf(DATA(linest,21:30),'%i',1);

  %========
  linest = 2;
  %========
  temp = sscanf(DATA(linest,:),'%c',1);

  %========
  linest = 3;
  %========
  file_data = DATA(linest:num_linests,:);
  file_data = [file_data,setstr(32*ones(num_linests-linest+1,1))];
  file_data = file_data';
  file_data = file_data(:)';
  data = sscanf(file_data,'%g',num_display_entries);
  disp_sequence(loop,:) = data';

%-------------------------------End of Display Sequence Data-------------------

end

if data_set_type~=15&data_set_type~=55&  ...
   data_set_type~=58&data_set_type~=82,
  ermessage = 'unrecognized type of data set';
  disp(ermessage);
  erc = -2;
end

end
%======================= data set loop 
  
if get_data_set_type == 15, % Coordinate Data
   if exist('pt_num')==1,
      o1 = pt_num;
      o2 = x_coord;
      o3 = y_coord;
      o4 = z_coord;
      o5 = erc;
      o6 = ermessage;
   else
      o1 = []; o2=[];o3=[];o4=[];o5=[];o6=[];
   end
elseif get_data_set_type == 55, % Modal Data (analysis data at nodes)
   if exist('analysis_type')==1,
      o1 = analysis_type;
      o2 = data_character;
      o3 = response_type;
      o4 = mode_num;
      if analysis_type == 2,
         o5 = mode_freq;
         o6 = mode_mass;
         o7 = mode_v_damping_ratio;
         o8 = mode_h_damping_ratio;
      else,
         o5 = eig_val;
         o6 = model_A;
         o7 = model_B;
         o8 = 0;
      end   
      o9 = node_num;
      o10 = r1;
      o11 = r2;
      o12 = r3;
      o13 = r4;
      o14 = r5;
      o15 = r6;
      o16 = erc;
      o17 = ermessage;
   else
      o1 = []; o2=[];o3=[];o4=[];o5=[];o6=[];o7=[];o8=[];o9=[];o10=[];
      o11 = []; o12=[];o13=[];o14=[];o15=[];o16=[];o17=[];
   end
   
elseif get_data_set_type == 58, % Measurement Data Type
   if exist('freq')==1,
      o1 = freq;
      o2 = meas_data;
      o3 = erc;
      o4 = ermessage;
      o5 = d1;
      o6 = d2;
      o7 = date;
      o8 = IDs;
      o9 = rptnum;
      o10 = rdir;
      o11 = eptnum;
      o12 = edir;
      o13 = function_type;
   else
      o1 = []; o2=[];o3=[];o4=[];o5=[];o6=[];o7=[];o8=[];o9=[];o10=[];
      o11 = []; o12=[];o13=[];
   end
   
elseif get_data_set_type == 82, % Display Sequence Data
   if exist('disp_sequence')==1,
      o1 = disp_sequence;
      o2 = erc;
      o3 = ermessage;
   else
      o1 = []; o2=[];o3=[];
   end
   
end   % End of not_type condition

end

erclose = fclose(fid);
if  erclose==-1,
  ermessage = 'error while closing file';
  disp(ermessage);
  erc = -1;
  return;
end

