function varargout = next_log(fplog, varargin)
%  1.  NAME
%
%  next_log - Read next data from a log file
%
%  2.  SYNOPSIS
%
%  [data, pos, n] = next_log(fplog, what, info )
%
%  3.  DESCRIPTION
%
%  Get the next line of data from a log file. Log files are ASCII files
%  with log data ordered in columns along lines. These files may contain
%  empty lines or matlab comment lines. This function has been written
%  for parsing log files generated by utilities _ref(scan_nbr(1)) and
%  ref_(scan_nav(1)), and visualize them graphically within Matlab.
%
%  4.  PARAMETERS
%
%     fplog
%        File pointer to log file.
%
%     what optional
%        A string specifying what to look for in the log file.  The
%        currently only possible value is data,which looks for lines of
%        numerical data. Further implementations would support reading of
%        warning or error messages.
%
%     info optional
%        Number of lines to read before giving information about current
%        reading state. If it is an array like [n,nstart],the current
%        line number is set to nstart instead of zero,and information
%        will be output every n lines.
%
%  5.  RETURNS
%
%     data
%        Array of data from next log line. The returned value depends on
%        the parameter what. For data,it is an array from the next non-
%        empty and non-comment line.
%
%     pos optional
%        Position in log file.
%
%     n optional
%        If info is an array specifying the start line number,n is the
%        current line number in the log file. Else it is the number of
%        lines skipped until current line.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% ------------------------------------------------------------------
% Process input arguments:
% ------------------------------------------------------------------

what = 'data';
imax = -1;
nstart = 0;

l = nargin - 1;
switch l
  case 1,
	 what = varargin{1};
  case 2,
	 info = varargin{2};
	 if length(info) > 1,
		imax = info(1);
		nstart = info(2);
	 else
		imax = info;
		nstart = 0;
	 end
end

% ------------------------------------------------------------------
% Initialize output arguments:
% ------------------------------------------------------------------
for i=1:nargout,
  varargout{i} = NaN;
end

% ------------------------------------------------------------------
% Loop until next line:
% ------------------------------------------------------------------
finished = 0;
i = 0;
n = nstart;
while ~finished & ~feof(fplog),
	% Read next line:
	pos = ftell(fplog);
	line = fgetl(fplog);
	i = i+1;
	n = n+1;
	% Check for comments
	com = findstr(line, '%');
	% Check for empty lines:
	notempty = isspace(line);
	if ~isempty(notempty),
	  notempty = (length(find(notempty)) > 0);
	else
	  notempty = 0;
	end
	% Process log lines:
	switch what
	  case 'data',
		 if (length(com) == 0) & notempty,
			finished = 1;
			data = str2num(line);
		 end
	end
	% Check for information:
	if (imax > 0) & (i >= imax),
		i = 0;
		fprintf(2, 'next_log - line number: %d\n', n);
	end
end

% Check end of file:
if ~finished,
	data = [];
	n = -1;
end

% ------------------------------------------------------------------
% Return values:
% ------------------------------------------------------------------

varargout{1} = data;
if nargout > 1,
  varargout{2} = pos;
end
if nargout > 2,
  varargout{3} = n;
end

return;

