%
%  Name: uffin.m
% 
%  Usage: uffin
% 
%  Description:
%    Imports the contents of a universal file
%

%  Version SWD970528
%
%  Author(s): Scott W. Doebling, doebling@lanl.gov
%
%  Institution: Los Alamos National Laboratory,
%		ESA-EA
%
%  Copyright (c) 1997, The Regents of the University
%     	of California. (See top level of code for
%        full copyright statement)
%

%
%   Get the filename
%


[filename,pathname,error]=loadfile(4,pathname);

if error ~= 0,
   errordlg(['Error #',num2str(error),' loading Universal file'])
   return
end


disp('Loading Measurement Data...')

warning off  % Supress matlab5 non-compliance warning messages


%  Load Measurement Data

 [freq,data,errcode,errmessage,descript1,descript2,date,IDs,resp_pt_num,...
       r_direction,excit_ptnum,excit_direction,function_type]=read_uff([pathname,filename],58);
 
%  Process Measurement Data

if ~isempty(freq),
   
   disp('Forming respDOF and refDOF...')

%  Determine the number of unique outputs and add them to 'respDOF'

   respDOF = [resp_pt_num(1) r_direction(1)];

   for i = 2:length(resp_pt_num),

       if isempty(find(respDOF(:,1) == resp_pt_num(i))),

          respDOF = [respDOF; resp_pt_num(i) r_direction(i)];

       end

   end

   nresp = size(respDOF,1);


%    Determine the number of unique inputs and add them to 'refDOF'

   refDOF = [excit_ptnum(1) excit_direction(1)];

   for i = 2:length(excit_ptnum),

       if isempty(find(refDOF(:,1) == excit_ptnum(i))),

          refDOF = [refDOF; excit_ptnum(i) excit_direction(i)];

       end

   end
   
   nref = size(refDOF,1);

%      Form frequency quantities and misc. variables

   disp('Forming Misc. Variables...')

   nlines = size(freq,2);

   sp = freq(1,nlines) - freq(1,1);

   cf = freq(1,1) + sp/2;

   domain = 'Frequency Domain';

   t0 = 0;

   t1 = 1/(freq(1,2) - freq(1,1));

   test_type = 'Response Reference';

%      Form data matrix

   disp('Forming FRF data matrix...')

   G = zeros(nresp*nlines,nref);

   for j = 1:nref,

       for i = 1:nresp,

%      find row in data matrix that contains this data

          drow = find(resp_pt_num==respDOF(i,1) & r_direction==respDOF(i,2) &...
             excit_ptnum==refDOF(j,1) & excit_direction==refDOF(j,2));

          if ~isempty(drow),

              G( (i-1)*nlines + 1 : i*nlines , j) = data(drow,:)'; 
 
          else

             disp(['ERROR: No entry found for resp#',num2str(i),' and ref#',num2str(j)])

          end

       end
   
   end
   
end



%     Load Modal Data

disp('Loading Modal Analysis Results...')

 [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([pathname,filename],55);

rr = [];

for i = 1:length(mode_num),
   
   rr(:,:,i) = [ r1(i,:)' r2(i,:)' r3(i,:)' r4(i,:)' r5(i,:)' r6(i,:)' ];
   
end

if ~isempty(analysis_type),
   
   %  Modal data exists
   
   if analysis_type == 2,
      if data_character == 2 | data_character == 3,
         
         %  Assign frequencies and damping
         
         Freq = mode_freq';
         Damp = mode_v_damping_ratio';
         
         %  Assign mode shapes
         
         phir_resp = [];
         respDOF = [];

         
         for j = 1:length(mode_num)
            nresp = 0;
            for i = 1:size(rr,1),
               rnz = find(rr(i,:,j) ~= 0); % Find nonzero entries in mode shape j at node i
               if ~isempty(rnz),
                  for zz = 1:length(rnz),
                     nresp = nresp + 1;
                     phir_resp(nresp,j) = rr(i,rnz(zz),j);
                     respDOF(nresp,:) = [node_num(j,i), rnz(zz)];
                  end
               end
            end
         end
         phir_ref = ones(1,size(phir_resp,2));     % DUMMY VALUES FOR REF MODE SHAPE
         refDOF = [max(max(node_num))+1, median(respDOF(:,2))]; % AND ASSOCIATED DOF
      else
         errordlg('ERROR: UFFIN only recognizes 3 or 6 DOF modal vectors')
      end
   else
      errordlg('ERROR: UFFIN only recognizes modal data of normal modes')
   end
   
end


%     Load Coordinate Data

disp('Loading Geometry information...')

[pt_num,x_coord,y_coord,z_coord,errcode,errmessage] = read_uff([pathname,filename],15);

%     Load Display Sequence Data

disp('Loading sequence')

[disp_sequence,erc,ermessage] = read_uff([pathname,filename],82);


if ~isempty('pt_num'),

   Nodes = [pt_num' x_coord' y_coord' z_coord'];

   ibar = 1;

   lastnode = 0;

   for i = 2:length(disp_sequence),
          
      if lastnode == 0,

         lastnode = disp_sequence(i);

      elseif disp_sequence(i) ~= 0 & disp_sequence(i) ~= lastnode,

         Bars(ibar,1:3) = [ibar lastnode disp_sequence(i)];

         lastnode = disp_sequence(i);

         ibar = ibar + 1;

      elseif disp_sequence(i) == 0,

         lastnode = 0;

      end

   end		
   
end

disp(['DONE importing Universal file:  ',pathname,filename])

warning on  % re-activate warning messages