function hdr = get_hdr(log_file)
%  1.  NAME
%
%  get_hdr - Get headeer information from log files
%
%  2.  SYNOPSIS
%
%  hdr = get_hdr(log_file )
%
%  3.  DESCRIPTION
%
%  Scans a log file for a header description and returns information
%  about the columns of data in the log file. This function has been
%  especially wrritten to parse log files generated by scan_nbr(1),
%  scan_nav(1) and process(1).
%
%  4.  PARAMETERS
%
%     log_file
%        Name of log file.
%
%  5.  RETURNS
%
%     hdr
%        A cell array of the variables contained in the log file. The
%        index to the cell corresponds to the column of the variable.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% ---------------------------------
% Initializations:
hdr = [];
hdrs= [];
hdrn= [];
fplog = fopen(log_file, 'r');

% ---------------------------------
% Read until beginning of header:
finished = 0;
while ~finished & ~feof(fplog),
  line = fgetl(fplog);
  if length(line) > 1,
	 % Check for correct comments inside the line		
	 com = findstr(line, 'Data Description');
	 line= strcat(line, '  ');
	 if (length(com) > 0) & strcmp(line(1:1), '%'),
		finished = 1;
	 end
  end
end

% ---------------------------------
% Warning message:
if ~finished,
  disp(['Warning: did not find any data description header']);
  hdr{1} = 'NO DATA DESCRIPTION';
  return;
end

% ---------------------------------
% Read until end of header:
finished = 0;
hdr = '';
n = 0;
while ~finished & ~feof(fplog),
	line = fgetl(fplog);
	%if length(line) > 1,
	% Check for end of header
	com = findstr(line, '% *');
	if length(com) > 0,
		finished = 1;
	else
		com = findstr(line, '%');
		line= strcat(line, ' ');
		if (length(com) > 0) & strcmp(line(1:1), '%'),
			n = n+1;
			line = strrep(line, '\n', '');
			line = strrep(line, '\r', '');
			hdr = strvcat(hdr, line(2:length(line)));
		end
	end
end
fclose(fplog);

% ---------------------------------
% Warning message:		
if ~finished,
  disp(['Warning: did not find end of data description header']);
  hdr{1} = 'NO DATA DESCRIPTION';
  return;
end

% ---------------------------------
% Process header description
for i=1:n,
	line = hdr(i,:);
	com = findstr(line, ':');
	if(length(com) > 0),
		hdrn(i) = str2num(line(1:com(1)-1));
		hdrs    = strvcat(hdrs, line(com(1)+1:length(line)));
	else 
		disp(['Warning: did not find data description separator']);
		hdr{1} = 'NO DATA DESCRIPTION';
		hdrs=[];
		hdrn=[];
		return;
	end
end	

% ------------------------
% Build return cell array:
hdr = [];
for i=1:n,
  %--- Remove leading blanks:
  len = length(hdrs(i,:));
  j = 1;
  while isspace(hdrs(i,j)) & (j < len),
	 j = j+1;
  end
  %--- and trailing blanks:
  hdr{i} = deblank(hdrs(i,j:len));
end

%--- Print header content:
line = sprintf('\nVARIABLES:\n\n');
for i=1:n,
	line = sprintf('%s %3d %s\n', line, i, hdr{i});
end;
fprintf('%s\n', line);

return;

