function nccopyschema(infile, outName)
% NCCOPYSCHEMA - Creates a empty ncfile using an existing ncfile's schema
%
% Use as: nccopyschema(infile, outfile)
%
% Inputs: infile = A string name of a netcdf file or a netcdf object. This file contains
%                  the schema to be copied.
%         outfile =  A string name of a file to be created. The resulting file will not
%                    contain any data but will have the same schema 
%                    (i.e. Attributes and variables) as the infile. Warning if a file
%                    of the same name exists it will be clobbered.

% Brian Schlining
% 02 Dec 1999

flag = 0;

if isstr(infile)
   infile = netcdf(infile);
   flag = 1;
end


% Open a netcdf file if a name is supplied, otherwise assume its a netcdf object
if isstr(outName)
   nc = netcdf(outName, 'clobber');
else 
   nc = outName;
end


if ~isempty(nc)
   fprintf(1, 'Reading %s\n', name(infile));
   fprintf(1, 'Writing %s\n', name(nc));
   % Copy global attributes
   a = att(infile);
   if ~isempty(a)
      for i = 1:length(a)
         fprintf(1,'Copying global attribute %s\n', name(a{i}));
         copy(a{i}, nc);   
      end
   end
   
   % Copy Dimensions
   d    = dim(infile);
   name = ncnames(d);
   
   for i = 1:length(d)
      if isrecdim(d{i})
         fprintf(1,'Copying unlimited-dimension, %s\n', name{i});
         nc(name{i}) = 0;
      else
         fprintf(1,'Copying dimension, %s\n', name{i});
         copy(d{i}, nc);
      end
   end
   
   % Copy Variables (without data)  
   v = var(infile);								% Get the variable objects
   if ~isempty(v)
      n = length(v);
      for i = 1:n
         if i ~=38&i~=52&i~=53&i~=39&i~=48&i~=58
         vDim = dim(v{i});			% Get the dimensions of each variable
         for j = 1:length(vDim)
            isRecordVDim(j) = isrecdim(vDim{j});   
         end
         
         try
            %fprintf(1,'Copying variables %4i\n', name(v{i}));
            RecordVDim = find(isRecordVDim);
            if isempty(RecordVDim)
               fprintf(1,'Copying non-record variable, %s (%i of %i)\n',char(ncnames(v{i})), i, n)
               copy(v{i}, nc, 1, 1);      % Copy non-record variables with data
            else
               fprintf(1,'Copying record variable, %s (%i of %i)\n',char(ncnames(v{i})), i, n)
               copy(v{i}, nc, 1, 1); 		% Copy record variables without data
            end
            
         catch
            fprintf(1,'Error\n')
         end
         end
      end
   end
   
   if isstr(outName)
      nc = close(nc);
   end
end

if flag
   close(infile)
end



