function [origin,delta,status] = pos_inq (ncid,varname,compact);
%
% function [origin,delta,status] = pos_inq (ncid,varname,compact);
%
%  This routine determines the coordinate data.
%
%  ------
%  Input:
%  ------
%
%     ncid......NetCDF file identifier.
%     varname...Variable name.
%     compact...Flag for compact or explicit storage.
%                  [0]  explicit storage.
%                  [1]  compact storage.
%
%  -------
%  Output:
%  -------
%
%     origin....Coordinate starting value.
%     delta.....Coordinate step size.
%     status....Exit status.  [0] no error  [<0] error

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

origin = [];
delta  = [];

%-------------------------------------------------------------------------------
%  Get variable identifier.
%-------------------------------------------------------------------------------

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

%-------------------------------------------------------------------------------
%  Read coordinate data depending upon storage type.
%-------------------------------------------------------------------------------

if (compact),

   %----------------------------------
   %--- Read compact storage data. ---
   %----------------------------------

   if ~isempty(findstr(varname,'lon')),
      axst = 0;
     else,
      axst = 1;
   end;
   [vals,status] = mexcdf ('ncvarget',ncid,varid,[0 axst],[2 1]);
   if (status<0),
      disp([setstr(7),'***Error:  POS_INQ - unable to read variable ', ...
                         setstr(34),varname,setstr(34),'.',setstr(7)]);
      return;
   end;

   origin = vals(1);
   delta  = vals(2);

 else,

   %-----------------------------------
   %--- Read explicit storage data. ---
   %-----------------------------------

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

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

   [vals,status] = mexcdf ('ncvarget',ncid,varid,0,ndim);
   if (status<0),
      disp([setstr(7),'***Error:  POS_INQ - unable to read variable ', ...
                        setstr(34),varname,setstr(34),'.',setstr(7)]);
      return;
   end;

   origin = vals(1);

   indp  = 2:ndim;
   indm  = indp-1;
   test  = abs(vals(indp))+abs(vals(indm));
   ind   = min(find(test==min(test)));
   delta = abs(vals(ind+1)-vals(ind));

end;
