%
%  Name: uffout.m
% 
%  Usage: uffout
% 
%  Description:
%    Exports the information in memory to 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
%

if pathname == 0, pathname = 'c:\'; end

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

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


if exist('G') == 1,
   
disp('Saving Measurement Data...')

warning off  % Supress matlab5 non-compliance warning messages

%
%  WRITE MEASUREMENT DATA
%

%
%   Setup misc variables
%

%    Descriptions and IDs

d1 = 'Text Description 1';
d2 = 'Text Description 2';
IDs = 'Written by DIAMOND - Copyright Regents of Univ. of California';

%    Date

dt = date;

%    Freq characteristics

fmin = cf-sp/2;
fdel = sp / (nlines-1);


for i = 1:size(respDOF,1),
   for j = 1:size(refDOF,1),
%
%   Write FRFs
%

[erc,ermessage] = write_uff([pathname,filename],58,'FRF',d1,d2,dt,IDs,...
   respDOF(i,1),respDOF(i,2),refDOF(j,1),refDOF(j,2),fmin,fdel,G((i-1)*nlines+1:i*nlines,j)); 

%
%   Write Coherence
%

[erc,ermessage] = write_uff([pathname,filename],58,'COH',d1,d2,date,IDs,...
   respDOF(i,1),respDOF(i,2),refDOF(j,1),refDOF(j,2),fmin,fdel,Coh((i-1)*nlines+1:i*nlines,j)); 

   end
end


end

if exist('phir_resp')==1,
   
%     Load Modal Data

disp('Saving Modal Analysis Results... (Not yet active)')

%
%   Create UFF modal variables from DIAMOND modal variables
%

analysis_type = 2; % Normal Mode Analysis
data_character = 3; % 6 DOF global trans and rot
response_type = 8; % Displacement mode shapes

nmod = length(Freq);
mode_num = [1:nmod];
mode_freq = Freq;
mode_mass = ones(1,nmod);
mode_v_damping_ratio = Damp;
mode_h_damping_ratio = zeros(1,nmod);

node_num = [];

for ir = 1:size(respDOF,1),
   
   % See if node number already exists in node_num
   %  if it does, use existing entry as row number for r*
   %  if it doesn't, append and use new row number as row number for r*
   
   nn = respDOF(ir,1);
   
   nr = find(node_num == nn);
   
   if isempty(nr),
      node_num = [node_num;nn];
   end
   
end


nnode = length(node_num);

%  Allocate displacement variables, r*

rr = zeros(nnode,nmod,6);

%  Assign mode shape components to displacement variables

for ir = 1:size(respDOF,1),
   
   nn = respDOF(ir,1);
   
   nr = find(node_num == nn);

   ds = sign(respDOF(ir,2)); % sign of direction of this DOF
   dd = abs(respDOF(ir,2));  % magnitude of direction of this DOF
   
   rr(nr,:,dd) = phir_resp(ir,:)*ds;
   
end

[erc,ermessage] = write_uff([pathname,filename],55,analysis_type,data_character,...
   response_type,mode_num,...
   mode_freq,mode_mass,mode_v_damping_ratio,mode_h_damping_ratio, ...
   node_num,rr(:,:,1)',rr(:,:,2)',rr(:,:,3)',rr(:,:,4)',rr(:,:,5)',rr(:,:,6)');


end

if exist('Nodes')==1,
   
%
%     Save Geometry 
%

disp('Saving Geometry information...')

%     Save Coordinate Data

[erc,ermessage] = ...
   write_uff([pathname,filename],15,Nodes(:,1),Nodes(:,2),Nodes(:,3),Nodes(:,4));

%     Determine Display sequence

disp_sequence = [];

if exist('Bars') == 1,
   
   if ~isempty(Bars),
      
      for i = 1:size(Bars,1),
      
         disp_sequence = [disp_sequence,Bars(i,2:3),0];
      
      end
      
   end
   
end

if exist('Shells') == 1,
   
   if ~isempty(Shells),
      
      for i = 1:size(Shells,1),
      
         disp_sequence = [disp_sequence,Shells(i,2:5),0];
      
      end
      
   end
   
end
      
%     Save Display Sequence Data

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

end

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

warning on  % re-activate warning messages