%
%  
%  Name: uff2diamond
%
%  Usage: uff2diamond
%
%  Description:
%    Converts results of 'uff2mat' to
%    proper variable names for DIAMOND,
%    and saves to DIAMOND data file.

%  Version SWD960702
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


cdir = '/raid1/doebling/uc_data';

%
%  Prompt for conversion type (data, geometry, modes)
%


disp(' ')
disp(' Please select a data type to covert to DIAMOND format:')
disp(' ')
disp('  0)   Measurement Data')
disp('  1)   Modal Analysis Results (real or complex modes)')
disp('  2)   Sensor and Excitation Geometry')
disp(' ')
typestr = input('  ****  Please Select: ');

if ~isstr(typestr), typestr = num2str(typestr); end

%
%  Prompt for the file name to save as
%

[filename,pathname,error] = savefile(str2num(typestr),cdir);

%
%  Do the conversion and save the data
%

if error == 0,


   if strcmp(typestr,'0'),

%%
%%  Conversion is measurement data
%%

   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

%  Save the data

   disp('Saving the data...')

   eval(['save ',pathname,filename,' nlines cf sp domain respDOF refDOF t0 t1 test_type G'])

   disp('ALL DONE')

%
%  GEOMETRY FILE
%

   elseif strcmp(typestr,'2'),

      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		

      eval(['save ',pathname,filename,' Nodes Bars']);

   else
 
    disp(' ERROR: DATA TYPE NOT RECOGNIZED')

   end

else
   
   disp('File ERROR')

end
