function [f]=nc_read(fname,vname,iprint);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997 Rutgers University.                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
% function [f]=nc_read(fname,vname)                                         %
%                                                                           %
% This routine reads in a generic multi-dimensional field from a NetCDF     %
% file.                                                                     %
%                                                                           %
% On Input:                                                                 %
%                                                                           %
%    fname       NetCDF file name (character string).                       %
%    vname       NetCDF variable name to read (character string).           %
%    iprint      Optional, flag for printing out information (interger):    %
%                  iprint = 0, do not print information.                    %
%                  iprint = 1, print information.                           %
%                                                                           %
% On Output:                                                                %
%                                                                           %
%    f           Field (scalar, matrix or array).                           %
%    status      Error flag.                                                %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%  If not provided, set-up printing information switch.

if (nargin < 3),
  iprint=1;
end,

% Open NetCDF file.

[ncid]=mexcdf('ncopen',fname,'nc_nowrite');
if (ncid == -1),
  error(['NC_READ: ncopen - unable to open file: ' fname])
  return
end,

% Supress all error messages from NetCDF.

[status]=mexcdf('setopts',0);

%----------------------------------------------------------------------------
% Inquire about requested variable.
%----------------------------------------------------------------------------

% Get variable ID.

[varid]=mexcdf('ncvarid',ncid,vname);
if (varid < 0),
  [status]=mexcdf('ncclose',ncid);
  nc_inq(fname);
  disp('  ');
  error(['NC_READ: ncvarid - cannot find variable: ',vname])
  return
end,

% Get information about requested variable.

[vname,nctype,ndims,dimids,natts,status]=mexcdf('ncvarinq',ncid,varid);
if (status == -1),
  error(['NC_READ: ncvarinq - unable to inquire about variable: ',vname])
end,

% Inquire about dimensions.

for n=1:ndims
  [name,dsize,status]=mexcdf('ncdiminq',ncid,dimids(n));
  if (status == -1),
    error(['NC_READ: ncdiminq - unable to inquire about dimension ID: ',...
          num2str(dimids(n))])
  else
    lstr=length(name);
    dimnam(n,1:lstr)=name(1:lstr);
    dimsiz(n)=dsize;
    start(n)=0;
    count(n)=dsize;
  end,
end,

%----------------------------------------------------------------------------
% Read in requested variable.
%----------------------------------------------------------------------------

%  Read in scalar.

if (ndims == 0),
  [f,status]=mexcdf('ncvarget1',ncid,varid,[0]);
  if (status == -1),
    error(['NC_READ: ncvarget1 - error while reading: ',vname])
  end,

%  Read in a vector or a matrix.

elseif (ndims == 1 | ndims == 2),
  [f,status]=mexcdf('ncvarget',ncid,varid,start,count);
  if (status == -1),
    error(['NC_READ: ncvarget - error while reading: ',vname])
  end

%  Read in a 3D-array.

elseif (ndims == 3),
  for n=1:dimsiz(1),
    start(1)=n-1;
    count(1)=1;
    [v,status]=mexcdf('ncvarget',ncid,varid,start,count);
    if (status == -1),
      error(['NC_READ: ncvarget - error while reading: ',vname,...
             ' at index ',num2str(n)]);
    end,
    f(:,:,n)=v;
  end,

%  Read in a 4D-array.

elseif (ndims == 4),
  for n=1:dimsiz(1),
    start(1)=n-1;
    count(1)=1;
    for m=1:dimsiz(2),
      start(2)=m-1;
      count(2)=1;
      [v,status]=mexcdf('ncvarget',ncid,varid,start,count);
      if (status == -1),
        error(['NC_READ: ncvarget - error while reading: ',vname,...
             ' at indices ',num2str(n),num2str(m)]);
      end,
      f(:,:,n,m)=v;
    end,
  end,
end,

% Print information about variable.

if (iprint),
  if (ndims > 0),
    disp('  ')
    disp([vname ' has the following dimensions (input order):']);
    disp('  ')
    for n=1:ndims,
      s=['           '  int2str(n) ') ' dimnam(n,:) ' = ' int2str(dimsiz(n))];
      disp(s);
    end,
    disp('  ')
    disp([vname ' loaded into an array of size:  [' int2str(size(f)) ']']);
    disp('  ')
  else
    disp('  ')
    disp([vname ' is a scalar and has a value of ',num2str(f)]);
    disp('  ')
  end,
end,
    
%----------------------------------------------------------------------------
% Close NetCDF file.
%----------------------------------------------------------------------------

[status]=mexcdf('ncclose',ncid);
if (status == -1),
  error(['NC_READ: ncclose - unable to close NetCDF file.'])
end

return
