function theResult = del(self, theObject, theDestination)

% del -- Delete a NetCDF dimension, variable, or attribute.
%  del(self, theObject, theDestination) copies all the entities
%   of self, a netcdf object, except theObject, an ncdim, ncvar,
%   or ncatt object, to theDestination, either a netcdf object
%   for an open NetCDF file, or the name to be used for a new
%   NetCDF file.  The netcdf object of theDestination is returned,
%   with the actual file in writeable 'data' mode.  The destination
%   defaults to the name of the NetCDF file associated with self,
%   extended with '.new'.  To copy all entities, use theObject = [].
 
% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%  All Rights Reserved.
%   Disclosure without explicit written consent from the
%    copyright owner does not constitute publication.

if nargin < 1, help netcdf/del, return, end

if nargin < 2, theObject = []; end
if nargin < 3, theDestination = [name(self) '.new']; end

result = [];

switch class(theDestination)
case 'char'
   result = netcdf(theDestination, 'clobber');
case 'netcdf'
   result = redef(theDestination);
otherwise
   warning(' ## Illegal syntax.')
   return
end

if isempty(result)
   if nargout > 0, theResult = result; end
   return
end

if ~isempty(theObject)
   theObjectName = name(theObject);
end

% Get the global entities.

d = dim(self);
v = var(self);
a = att(self);   % Global attributes.

% Cull out theObject.

if ~isempty(theObject) & ncid(theObject) == ncid(self)
switch class(theObject)
case 'ncdim'
   for i = length(d):-1:1
      if strcmp(theObjectName, name(d{i}))
         d(i) = [];
      end
   end
case 'ncvar'
   for i = length(v):-1:1
      if strcmp(theObjectName, name(v{i}))
         v(i) = [];
      end
   end
case 'ncatt'
   for i = length(a):-1:1
      if strcmp(theObjectName, name(a{i}))
         a(i) = [];
      end
   end
otherwise
   warning(' ## Illegal syntax.')
   return
end
end

% Copy to theDestination.

hello 1
for i = 1:length(a)
a_name = name(a{i})
%  result < a{i};   % a{i} does not know itsValue here.
end

hello 2
for i = 1:length(d)
d_name = name(d{i})
   result < d{i};   % Works.
end

hello 3
for i = 1:length(v)
v_name = name(v{i})
   vnew{i} = (result < v{i});   % Does not work.
   if ~isempty(vnew{i})
      a = att(v{i});
      if isa(theObject, 'ncatt') & ...
         ncid(theObject) == ncid(v{i}) & ...
         varid(theObject) == varid(v{i})
         for k = 1:length(a):-1:1
            if strcmp(theObjectName, name(a{i}))
               a(k) = [];
            end
         end
      end
      for k = 1:length(a);
a_name = [name(v{i}) ':' name(a{k})]
%        vnew{i} < a{k};
      end
   end
end

hello 4

endef(result)

for i = 1:length(v)
   if ~isempty(vnew{i})
      vnew{i}(:) = v{i}(:);
   end
end

if nargout > 0
   theResult = result;
end
