function [coord,nx,ny,lonc,latc,dx,dy,delx,dely,theta] = grdprm (grdname);
%
% [coord,nx,ny,lonc,latc,dx,dy,delx,dely,theta] = grdprm (grdname);
%
% This function reads the coordinate definition parameters from a
% GRIDS netCDF file.

%-------------------------------------------------------------------------------
% Initialize output.
%-------------------------------------------------------------------------------

coord = [];
nx    = [];
ny    = [];
lonc  = [];
latc  = [];
dx    = [];
dy    = [];
delx  = [];
dely  = [];
theta = [];

%-------------------------------------------------------------------------------
% Open GRIDS netCDF file.
%-------------------------------------------------------------------------------

ncgrd = netcdf (grdname);

if isempty(ncgrd)
   disp (' ');
   disp ('***Error:  GRDPRM - unable to open GRIDS netCDF file:');
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%-------------------------------------------------------------------------------
% Read horizontal coordinate definition parameters.
%-------------------------------------------------------------------------------

%------------------------
%--- Coordinate flag. ---
%------------------------

name  = 'coord';
coord = ncgrd{name}(:);

if isempty(coord)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%------------------------
%--- Grid dimensions. ---
%------------------------

name = 'tlon';
nx   = length(ncgrd(name));

if isempty(nx)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

name = 'tlat';
ny   = length(ncgrd(name));

if isempty(ny)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%------------------------------
%--- Transformation Center. ---
%------------------------------

name = 'rlngd';
lonc = ncgrd{name}(:);

if isempty(lonc)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

name = 'rlatd';
latc = ncgrd{name}(:);

if isempty(latc)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%---------------------
%--- Grid Spacing. ---
%---------------------

name = 'meandx';
dx   = ncgrd{name}(:);

if isempty(dx)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

name = 'meandy';
dy   = ncgrd{name}(:);

if isempty(dy)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%-----------------------------------
%--- Transformation-Grid Offset. ---
%-----------------------------------

name = 'delx';
delx = ncgrd{name}(:);

if isempty(delx)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

name = 'dely';
dely = ncgrd{name}(:);

if isempty(dely)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%--------------------------------
%--- Transformation Rotation. ---
%--------------------------------

name = 'thetad';
theta = ncgrd{name}(:);

if isempty(theta)
   disp (' ');
   disp (['***Error:  GRDPRM - unable to read ',setstr(34),name, ...
          setstr(34),' in GRIDS netCDF file:']);
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

%-------------------------------------------------------------------------------
% Close GRIDS netCDF file.
%-------------------------------------------------------------------------------

status = close (ncgrd);

if ~isempty(status)
   disp (' ');
   disp ('***Error:  GRDPRM - unable to close GRIDS netCDF file:');
   disp (['          ',setstr(34),grdname,setstr(34)]);
   disp (' ');
   return;
end;

clear ncgrd status;
