
function theResult = rd_netcdf2(theNetCDFFile, varargin)

%
% function theResult = rd_netcdf2(theNetCDFFile, varargin)
% rd_netcdf2 -- Load NetCDF variables.
%  rd_netcdf2('theNetCDFFile', 'var1', 'var2', ...) loads the
%   given variables of 'theNetCDFFile' into the Matlab
%   workspace of the "caller" of this routine.  If no names
%   are given, all variables are loaded.  The loaded names
%   are returned or assigned to "ans".  No attributes are
%   loaded.
% 
% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%  All Rights Reserved.
%   Disclosure without explicit written consent from the
%    copyright owner does not constitute publication.
%
%
% version of DCT 9/29/97 to return all variables in a structure
% and to return fields w/ multiple dimensions.
%

if nargin < 1, help(mfilename), return, end

result = [];
if nargout > 0, theResult = result; end

% get # of dimensions, # of variables, # of attributes,
% and the record dimension
f = netcdf(theNetCDFFile, 'nowrite');

if isempty(f), return, end

% get variable names
if isempty(varargin), varargin = ncnames(var(f)); end

for i = 1:length(varargin)
     if ~isstr(varargin{i}), varargin{i} = inputname(i+1); end
     if isempty(find(varargin{i} == '-'))
       eval(['result.' varargin{i} ' = f{varargin{i}}(:);'])
     end
end

close(f)

if nargout > 0
  theResult = result;
else
  ncans(result)
end
