function makemref
%
%  Name: makemref.m
% 
%  Usage: makemref
% 
%  Description:
%    Creates a DIAMOND data file with multiple reference FRF data matrices
%     from multiple DIAMOND data files with single reference FRF data
%

%  Version SWD970823
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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

%
%  Determine how many files are available
%

nref = input('How many single-reference files to combine?');

disp(['Preparing to load ',num2str(nref),' files'])

%  Prompt for each one of the single reference files

pathname = 'C:\';

%  Prompt for each one of the single ensemble files

pathname = 'C:\';

for ib = 1:nref,
   
   fprintf(1,'Preparing to Load File #%d of %d\n',ib,nref)
   
   [filename,pathname,error] = loadfile(0,pathname);

   if error~=0, return, end
      
   fprintf(1,'Loading File #%d of %d\n',ib,nref)
   
   loaddata2(pathname,filename,1)
   
   if isfield(DATA{1},'G'),
         
      if ib == 1,
         Gnew = zeros(size(DATA{1}.G,1),size(DATA{1}.G,2),nref,size(DATA{1}.G,4));
      end
      
      Gnew(:,:,ib,:) = DATA{1}.G;
      
   end

   if isfield(DATA{1},'Coh'),
         
      if ib == 1,
         Cohnew = zeros(size(DATA{1}.Coh,1),size(DATA{1}.Coh,2),nref,size(DATA{1}.Coh,4));
      end
   
      Cohnew(:,:,ib,:) = DATA{1}.Coh;
         
   end
      
   if isfield(DATA{1},'Gxy'),
         
      if ib == 1,
         Gxynew = zeros(size(DATA{1}.Gxy,1),size(DATA{1}.Gxy,2),nref,size(DATA{1}.Gxy,3));
      end

      Gxynew(:,:,ib,:) = DATA{1}.Gxy;
         
   end

   if isfield(DATA{1},'Gxx'),
      
      if ib == 1,
         Gxxnew = zeros(size(DATA{1}.Gxx,1),nref,size(DATA{1}.Gxx,3));
      end
         
      Gxxnew(:,ib,:) = DATA{1}.Gxx;
         
   end
   
   if isfield(DATA{1},'refDOF'),
      refDOFnew(ib,:) = DATA{1}.refDOF;
   end
   

   %if isfield(DATA{1},'Gyy'),
   %      
   %   if ib == 1,
   %      Gyynew = zeros(size(DATA{1}.Gyy,1),size(DATA{1}.Gyy,2),nref);
   %   end
   %   
   %   Gyynew(:,:,ib) = DATA{1}.Gyy;
   %   
   %end
   
end


%  Create averaged data sets

if exist('Gnew')==1,
   DATA{1}.G = Gnew;
end

if exist('Gxynew')==1,
  DATA{1}.Gxy = Gxynew;
end

if exist('Gxxnew')==1,
  DATA{1}.Gxx = Gxxnew;
end

if exist('Gyynew')==1,
   DATA{1}.Gyy = Gyynew;
end

DATA{1}.refDOF = refDOFnew;

%
%   Save the new DIAMOND data file with the multi-ref data
%

fprintf(1,'Saving Multiple reference Datafile...')

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

if error == 0,
   
   savedata(pathname,filename,1)
   
else
   
   disp('ERROR Saving Multi-reference Datafile!')
   
end

disp('Done Creating multi-reference Data file')

return