function [londat,latdat,status] = depth_init (ncid);
%
% function [londat,latdat,status] = depth_init (ncid);
%
% This function determines the storage characteristics of the
% topography in the given netCDF file.
%
% ------
% Input:
% ------
%
%    ncid.....NetCDF identifier for open topography file.
%
% -------
% Output:
% -------
%
%    londat...Longitude data.
%                londat(1):  number of longitudinal points.
%                londat(2):  starting longitude.
%                londat(3):  longitudinal grid spacing.
%                londat(4):  cyclic flag.
%                            [0]  longitudinal extent does NOT span globe.
%                            [1]  longitudinal extent spans globe.
%    latdat...Latitude data.
%                latdat(1):  number of latitudinal points.
%                latdat(2):  starting latitude.
%                latdat(3):  latitudinal grid spacing.
%    status...Exit status.  [0] no errors.  [<0] error.

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

londat = zeros(4,1);
latdat = zeros(3,1);

%-------------------------------------------------------------------------------
%  Get topography identifier.
%-------------------------------------------------------------------------------

varname        = 'topo';
[topid,status] = mexcdf ('ncvarid',ncid,varname);
if (status < 0),
   disp([setstr(7),'***Error:  DEPTH_INIT - unable to find variable ', ...
                      setstr(34),varname,setstr(34),'.',setstr(7)]);
   return
end;

%-------------------------------------------------------------------------------
%  Get size information on the topographic data.
%-------------------------------------------------------------------------------

[name,type,ndim,dims,natt,status] = mexcdf ('ncvarinq',ncid,topid);
if (status<0),
   disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
        'about variable ',setstr(34),varname,setstr(34),'.',setstr(7)]);
   return;
end;

for n = 1:ndim,


   [name,dimsize,status] = mexcdf ('ncdiminq',ncid,dims(n));
   if (status<0),
      disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
            'about dimension ',num2str(dims(n)),'.',setstr(7)]);
      return;
   end;

   if ~isempty(findstr(name,'lon')),
      londat(1) = dimsize;
     elseif ~isempty(findstr(name,'lat')),
      latdat(1) = dimsize;
   end;

end;

%-------------------------------------------------------------------------------
%  Get the position information.
%-------------------------------------------------------------------------------

%---------------------------------------
%---  Try to find positions of data  ---
%--- from the "positions" attribute. ---
%---------------------------------------

posatt = 0;

for n = 0:(natt-1),
   [name,status] = mexcdf ('ncattname',ncid,topid,n);
   if (status>=0),
      posatt = posatt | ~isempty(findstr(name,'positions'));
   end;
end;

if (posatt),
   [name,status] = mexcdf ('ncattget',ncid,topid,'positions');
  else
   status=-1;
end;

%------------------------------------------------
%--- Determine compactness of positions data. ---
%------------------------------------------------

if (status==0),
   ctest = ~isempty(findstr(name,'compact'));
  else,
   ctest  = 0;
end;

%----------------------------------------
%--- Get positions information either ---
%--- from attribute or default names. ---
%----------------------------------------

if (status==0),

   %---------------------------------
   %--- Parse positions variables ---
   %---   names and read data.    ---
   %---------------------------------

   nnd = findstr(name,',') - 1;
   if ~isempty(findstr(name(1:nnd),'lon')),
      [lonst,dlon,status] = pos_inq (ncid,name(1:nnd),ctest);
      if (status<0),
         disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
               'about position data ',setstr(34),name(1:nnd), ...
               setstr(34),'.',setstr(7)]);
         return;
      end;
    else,
      [latst,dlat,status] = pos_inq (ncid,name(1:nnd),ctest);
      if (status<0),
         disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
               'about position data ',setstr(34),name(1:nnd), ...
               setstr(34),'.',setstr(7)]);
         return;
      end;
   end;

   nnd         = findstr(name,';');
   name(1:nnd) = [];
   while strcmp(name(1:1),' '),
      name(1:1) = [];
   end;

   nnd = findstr(name,',') - 1;
   if ~isempty(findstr(name(1:nnd),'lon')),
      [lonst,dlon,status] = pos_inq (ncid,name(1:nnd),ctest);
      if (status<0),
         disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
               'about position data ',setstr(34),name(1:nnd), ...
               setstr(34),'.',setstr(7)]);
         return;
      end;
    else,
      [latst,dlat,status] = pos_inq (ncid,name(1:nnd),ctest);
      if (status<0),
         disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
               'about position data ',setstr(34),name(1:nnd), ...
               setstr(34),'.',setstr(7)]);
         return;
      end;
   end;

 else,

   %----------------------------------------
   %--- Use default names for positions. ---
   %----------------------------------------

   [lonst,dlon,status] = pos_inq (ncid,'topo_lon',ctest);
   if (status<0),
      disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
            'about position data ',setstr(34),'topo_lon', ...
            setstr(34),'.',setstr(7)]);
      return;
   end;

   [latst,dlat,status] = pos_inq (ncid,'topo_lat',ctest);
   if (status<0),
      disp([setstr(7),'***Error:  DEPTH_INIT - unable to inquire ', ...
            'about position data ',setstr(34),'topo_lat', ...
            setstr(34),'.',setstr(7)]);
      return;
   end;

end;

londat(2) = lonst;
londat(3) = dlon;

latdat(2) = latst;
latdat(3) = dlat;

%-------------------------------------------------------------------------------
%  Determine if longitudinal extent covers the globe.
%-------------------------------------------------------------------------------

londat(4) = (360.0 - londat(1)*londat(3)) < (0.5*londat(3));
