function [status]=nc_write(fname,vname,f);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997 Rutgers University.                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
% function [status]=nc_write(fname,vname,f)                                 %
%                                                                           %
% This routine writes in a generic multi-dimensional field into a NetCDF    %
% file.                                                                     %
%                                                                           %
% On Input:                                                                 %
%                                                                           %
%    fname       NetCDF file name (character string).                       %
%    vname       NetCDF variable name to read (character string).           %
%    f           Field (scalar, matrix or array).                           %
%                                                                           %
% On Output:                                                                %
%                                                                           %
%    status      Error flag.                                                %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%  Open NetCDF file.

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

%  Supress all error messages from NetCDF.

[ncopts]=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_WRITE: 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_WRITE: ncvarinq - unable to inquire about variable: ',vname])
end,

% Inquire about dimensions.

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

%----------------------------------------------------------------------------
%  Write out bathymetry into GRIDS NetCDF file.
%----------------------------------------------------------------------------

[status]=mexcdf('ncvarput',ncid,varid,start,count,f);
if (status == -1),
  error(['NC_WRITE: ncvarput - error while writting variable: ', vname])
end

%----------------------------------------------------------------------------
%  Close NetCDF file.
%----------------------------------------------------------------------------

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

return
