function [grd, varargout] = ascii_grd(data, cols)
%  1.  NAME
%
%  ascii_grd - Get grid points from a columnar data array
%
%  2.  SYNOPSIS
%
%  [grd, data] = ascii_grd(data, cols)
%
%  3.  DESCRIPTION
%
%  This Matlab function removes first all lines containing bad data in
%  the specified column axes (this is carried out like an AND operator on
%  all specified columns). It finds then all different points along these
%  and returns them sorted in ascending order.
%
%  4.  PARAMETERS
%
%     data
%        Data array with columnar data.
%
%     cols
%        A vector specifying the column numbers to consider as grid axes.
%
%  5.  RETURNS
%
%     grd
%        Cell array with the column vectors of all different grid points
%        found along each specified grid axis. These are sorted in
%        ascending order.
%
%     data optional
%        The new data array with lines containing bad values along the
%        specified grid axes removed.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%--- Check that grid columns are defined:
nc = length(cols);
if nc < 1,
	error([ 'No specified columns' ]);
end
if ~isempty(find(cols < 1)),
  error([ 'Column numbers must be positive and non-zero' ]);
end
if ~isempty(find(cols > size(data,2))),
  error([ 'Column number exceeds number of available columns' ]);
end

%--- Remove all lines with bad data in the grid columns:
ibad = isnan(data(:, cols));
ibad = sum(ibad,2);
igood = find(~ibad);
if isempty(igood),
	error([ 'No valid grid data along specified columns' ]);
end
data = data(igood,:);

%--- Get all different grid values:
for c=1:nc,
	grd{c} = unique(data(:,cols(c)));
end


%--- Exit:
if nargout > 0,
	varargout{1} = data;
end

return;




