function [status]=c_grid(Im,Jm,Bname);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2007 Rutgers University.                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Hernan G. Arango %%%
%                                                                           %
% function [status]=c_bath(Im,Jm,Bname);                                    %
%                                                                           %
% This function creates a bathymetry NetCDF file.                           %
%                                                                           %
% On Input:                                                                 %
%                                                                           %
%    Im          Number of RHO-points in the XI-direction.                  %
%    Jm          Number of RHO-points in the ETA-direction.                 %
%    Gname       GRID NetCDF file name.                                     %
%                                                                           %
% On Output:                                                                %
%                                                                           %
%    status      Error flag.                                                %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------
%  Get some NetCDF parameters.
%----------------------------------------------------------------------------

[ncglobal]=mexcdf('parameter','nc_global');
[ncdouble]=mexcdf('parameter','nc_double');
[ncunlim]=mexcdf('parameter','nc_unlimited');
[ncfloat]=mexcdf('parameter','nc_float');
[ncchar]=mexcdf('parameter','nc_char');

%----------------------------------------------------------------------------
%  Inquire dimensions from a existing NeTCDF file.
%----------------------------------------------------------------------------

Dname.lon ='lon';   Dsize.lon =Im;       Vname.lon ='lon';
Dname.lat ='lat';   Dsize.lat =Jm;       Vname.lat ='lat';
Dname.bath='bath';  Dsize.bath=ncunlim;  Vname.bath='hraw'

%----------------------------------------------------------------------------
%  Create bathymetry NetCDF file.
%----------------------------------------------------------------------------

[ncid,status]=mexcdf('nccreate',Bname,'nc_write');
if (ncid == -1),
  error(['C_BATH: nccreate - unable to create file: ', Bname]);
  return
end,

%-----------------------------------------------------------------------
%  Create global attribute(s).
%-----------------------------------------------------------------------

type='GRID file';
lstr=max(size(type));
[status]=mexcdf('ncattput',ncid,ncglobal,'type',ncchar,lstr,type);
if (status == -1),
  error(['C_BATH: ncattput - unable to global attribure: type.']);
  return
end,
history=['GRID file using Matlab script: c_bath, ', date_stamp];
lstr=max(size(history));
[status]=mexcdf('ncattput',ncid,ncglobal,'history',ncchar,lstr,history);
if (status == -1),
  error(['C_BATH: ncattput - unable to global attribure: history.']);
  return
end,

%----------------------------------------------------------------------------
%  Define dimensions.
%----------------------------------------------------------------------------

[did.lon]=mexcdf('ncdimdef',ncid,Dname.lon,Dsize.lon);
if (did.lon == -1),
  error(['C_BATH: ncdimdef - unable to define dimension: ',Dname.lon]);
end,

[did.lat]=mexcdf('ncdimdef',ncid,Dname.lat,Dsize.lat);
if (did.lat == -1),
  error(['C_BATH: ncdimdef - unable to define dimension: ',Dname.lat]);
end,

[did.bath]=mexcdf('ncdimdef',ncid,Dname.bath,Dsize.bath);
if (did.bath == -1),
  error(['C_BATH: ncdimdef - unable to define dimension: ',Dname.bath]);
end,

%----------------------------------------------------------------------------
%  Define variables.
%----------------------------------------------------------------------------

% Define spherical switch.

Var.name ='spherical';
Var.type =ncchar;
Var.dimid=[];
Var.long ='grid type logical switch';
Var.opt_T='spherical';
Var.opt_F='Cartesian';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Longitude.

Var.name =Vname.lon;
Var.type =ncdouble;
Var.dimid=[did.lat did.lon];
Var.long ='longitude';
Var.units='degree_east';
Var.field=[Vname.lon,', scalar'];
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Latitude.

Var.name =Vname.lat;
Var.type =ncdouble;
Var.dimid=[did.lat did.lon];
Var.long ='latitute';
Var.units='degree_north';
Var.field=[Vname.lat,', scalar'];
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Topography.

Var.name =Vname.bath;
Var.type =ncdouble;
Var.dimid=[did.bath did.lat did.lon];
Var.long ='topography';
Var.units='meter';
Var.field=[Vname.bath,', scalar'];
[varid,status]=nc_vdef(ncid,Var);
clear Var

%----------------------------------------------------------------------------
%  Leave definition mode and close NetCDF file.
%----------------------------------------------------------------------------

[status]=mexcdf('ncendef',ncid);
if (status == -1),
  error(['C_BATH: ncendef - unable to leave definition mode.']);
  return
end,

[status]=mexcdf('ncclose',ncid);
if (status == -1),
  error(['C_BATH: ncclose - unable to close GRID NetCDF file: ', Bname]);
  return
end,

return

