function uffout(pathname,filename,type)
%
%  Name: uffout.m
% 
%  Usage: uffout(pathname,filename,type)
% 
%  Description:
%    Exports the information in memory to a universal file
%
%    type = 'Measurement','Modal',or 'Geometry'
%

%  Version SWD970903
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This matlab source code was originally     %
% developed as part of "DIAMOND" at          %
% Los Alamos National Laboratory. It may     %
% be copied, modified, and distributed in    %
% any form, provided:                        %
%  a) This notice accompanies the files and  %
%     appears near the top of all source     %
%     code files.                            %
%  b) No payment or commercial services are  %
%     received in exchange for the code.     %
%                                            %
% Original copyright is reserved by the      %
% Regents of the University of California,   %
% in addition to Scott W. Doebling, Phillip  %
% J. Cornwell, Erik G. Straser, and Charles  %
% R. Farrar.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

global DATA MODES GEOM

warning off  % Supress matlab5 non-compliance warning messages

%    Descriptions and IDs

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

%    Date

dt = date;

switch type
case 'Measurement'
   %
   %  WRITE MEASUREMENT DATA
   %
   fprintf(1,'Saving Measurement Data...')
   
   %    Freq characteristics

   fmin = DATA{1}.cf-DATA{1}.sp/2;
   fdel = DATA{1}.sp / (DATA{1}.nlines-1);

   %
   %   Write FRFs
   %
   
   if isfield(DATA{1},'G'),
      if ~isempty(DATA{1}.G),
         for i = 1:size(respDOF,1),
            for j = 1:size(refDOF,1),
               [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,DATA{1}.G(:,i,j));
            end
         end
      end
   end
   
   %
   %   Write Coherence
   %
   
   if isfield(DATA{1},'Coh'),
      if ~isempty(DATA{1}.Coh),
         for i = 1:size(respDOF,1),
            for j = 1:size(refDOF,1),
               [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,DATA{1}.Coh(:,i,j)); 
            end
         end
      end
   end
   'ASPEC','XSPEC','FRF', or 'COH'
   
   %
   %  Write CPS
   %
   
   if isfield(DATA{1},'Gxy'),
      if ~isempty(DATA{1}.Gxy),
         for i = 1:size(respDOF,1),
            for j = 1:size(refDOF,1),
               [erc,ermessage] = write_uff([pathname,filename],58,'XSPEC',d1,d2,date,IDs,...
                  respDOF(i,1),respDOF(i,2),refDOF(j,1),refDOF(j,2),fmin,fdel,DATA{1}.Gxy(:,i,j)); 
            end
         end
      end
   end
   
   %
   %  Write Response PSD
   %
   
   if isfield(DATA{1},'Gyy'),
      if ~isempty(DATA{1}.Gyy),
         for i = 1:size(respDOF,1),
            for j = 1:size(refDOF,1),
               [erc,ermessage] = write_uff([pathname,filename],58,'ASPEC',d1,d2,date,IDs,...
                  respDOF(i,1),respDOF(i,2),refDOF(j,1),refDOF(j,2),fmin,fdel,DATA{1}.Gyy(:,i,j)); 
            end
         end
      end
   end
   
   
   %
   %  Write Reference PSD
   %
   
   if isfield(DATA{1},'Gxx'),
      if ~isempty(DATA{1}.Gxx),
         for j = 1:size(refDOF,1),
            [erc,ermessage] = write_uff([pathname,filename],58,'ASPEC',d1,d2,date,IDs,...
               1,1,refDOF(j,1),refDOF(j,2),fmin,fdel,DATA{1}.Gxx(:,j)); 
         end
      end
   end
   
   
   %
   %  DONE WRITING MEASUREMENT DATA
   %
   
   fprintf(1,'done\n')
   
   
   
case 'Modal'
   %
   %  WRITE MODAL ANALYSIS Results
   %
   fprintf(1,'Saving Modal Analysis Results...')
   
   %  Determine number of unique nodes
   
   node_num = [];
   
   for ir = 1:size(MODES.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 = MODES.respDOF(ir,1);
      
      nr = find(node_num == nn);
      
      if isempty(nr),
         node_num = [node_num;nn];
      end
      
   end
   
   nnode = length(node_num);
   
   %
   %  Write Real Modes
   %  
   
   %  Put modal vectors in correct format
   
   rr = zeros(nnode,MODES.nmodes,6);

   for ir = 1:size(MODES.respDOF,1),
      
      nn = MODES.respDOF(ir,1);
      
      nr = find(node_num == nn);
      
      ds = sign(MODES.respDOF(ir,2)); % sign of direction of this DOF
      dd = abs(MODES.respDOF(ir,2));  % magnitude of direction of this DOF
      
      for i = 1:MODES.nmodes,
         rr(nr,i,dd) = MODES.modalvector{i}(ir)*ds;
      end
      
   end
   
   % Define misc. variables
   
   analysis_type = 2; % Normal Mode Analysis
   data_character = 3; % 6 DOF global trans and rot
   response_type = 8; % Displacement mode shapes
   mode_num = [1:MODES.nmodes];
   for i = 1:length(MODES.nmodes),
      mode_freq(i) = MODES.Freq{i};
      mode_v_damping_ratio(i) = MODES.Damp{i};
   end
   mode_h_damping_ratio = zeros(1,MODES.nmodes);
   
   mode_mass = ones(1,nmod);

   [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)');
   
   %  Write Complex Modes
   
   
   nmod = length(Freq);
   
   
   
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


return