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

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

navgf = input('How many files form the Averaged set?');

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

%  Prompt for each one of the single ensemble files

pathname = 'C:\';

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

   if error~=0, return, end
      
   fprintf(1,'Loading File #%d of %d\n',ib,navgf)
   
   loaddata2(pathname,filename,1)
   
   if isfield(DATA{1},'G'),
         
      if ib == 1,
         Gnew = zeros(size(DATA{1}.G,1),size(DATA{1}.G,2),size(DATA{1}.G,3),navgf);
      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),size(DATA{1}.Coh,3),navgf);
      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),size(DATA{1}.Gxy,3),navgf);
      end

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

   if isfield(DATA{1},'Gxx'),
      
      if ib == 1,
         Gxxnew = zeros(size(DATA{1}.Gxx,1),size(DATA{1}.Gxx,2),navgf);
      end
         
      Gxxnew(:,:,ib) = DATA{1}.Gxx;
         
   end

   if isfield(DATA{1},'Gyy'),
         
      if ib == 1,
         Gyynew = zeros(size(DATA{1}.Gyy,1),size(DATA{1}.Gyy,2),navgf);
      end
      
      Gyynew(:,:,ib) = DATA{1}.Gyy;
      
   end
end
      
%  Create averaged data sets

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

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

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

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

DATA{1}.navg = navgf;

%  Compute Coherence Function

fprintf(1,'Forming Coherence Function...')

nresp = size(DATA{1}.G,2);

Gxxbig = zeros(size(DATA{1}.Gxy));

for i = 1:nresp,
   Gxxbig(:,i) = DATA{1}.Gxx;
end

DATA{1}.Coh = abs(DATA{1}.Gxy).^2 ./ Gxxbig ./ DATA{1}.Gyy;

fprintf(1,'done\n')

%
%   Save the new DIAMOND data file with the averaged variables
%

fprintf(1,'Saving Averaged Datafile...\n')

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

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

disp('Done Creating Averaged Data file')