function hdgdiff(year_base, asc_file, plot_title, note);
%  1.  NAME
%
%  hdgdiff - - Compare mean and end ping heading data
%
%  2.  SYNOPSIS
%
%  hdgdiff(year_base, asc_file, plot_title, note )
%
%  3.  DESCRIPTION
%
%  Plot of heading differences vs. time, and calculate statistical data
%  about these. Differences are checked to be less than a maximal allowed
%  threshold value (this may happen sometimes if the system delivering
%  heading information has a malfunction). These points are plotted with
%  a cross.
%
%  Input data file must contain the following columns:
%
%  1. End ping GPS time
%
%  2. Mean ping heading
%
%  3. End ping heading
%
%  4.  PARAMETERS
%
%     year_base
%        Base year used for the decimal times in input file.
%
%     asc_file
%        Input data file.
%
%     plot_title
%        Title for the plot.
%
%     note
%        Additionnal string to print on the plot.
%
%  5.  FILES
%
%  o  hdg_diff.ps
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%---
max_err = 20.0;

%--- Read the data:
d = read_asc(asc_file);
nc = size(d,2);
nd = size(d,1);
if nc ~= 3,
	error([ 'Wrong number of columns in file ' asc_file ]);
end

%--- Initialize indexes:
t = 1; mn_hdg = 2; end_hdg = 3;
err_hdg = 4;

%--- Calculate differences:
d=d';
d(err_hdg,:) = d(mn_hdg,:)-d(end_hdg,:);

%--- Get good and bad points:
good = find(abs(d(err_hdg,:)) < max_err);
bad  = find(abs(d(err_hdg,:)) > max_err);

%--- Calculate statistics:
mn_err = mean(d(err_hdg,good));
sd_err = std(d(err_hdg,good));
md_err = median(d(err_hdg,good));

%--- Prepare plot:
figure;
orient tall;

%--- Plot differences:
plot(d(t,good), d(err_hdg,good), '.');
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('Mean ping hdg - End ping hdg (degree)');
grid on;
hold on;

%--- Plot bad points:
bad_err = ones(size(bad))*min(d(err_hdg,good));
plot(d(t,bad), bad_err, 'x');

%--- Additionnal stuff:
str = sprintf('Mean: %-8.4f - Sdev: %-8.4f - Median: %-8.4f', ...
		mn_err, sd_err, md_err);
date_it([ '- ASC File: ' asc_file ' - PS File: hdg_diff.ps - ' str ]);
old_axes = gca;
aa = axes('position', [0 0 1 1]);
axis('off');

str = plot_title;
text(0.5, 0.99, str, 'units', 'normalized', ...
	'FontAngle', 'italic', 'HorizontalAlignment', 'center', ...
	'VerticalAlignment', 'top', 'FontSize', 12, 'FontWeight', 'bold');

t1 = min(d(t,:));
t2 = max(d(t,:));
str = [ to_date(year_base, t1) ' to ' to_date(year_base, t2) ];
text(0.5, 0.96, str, 'units', 'normalized', ...
	'FontAngle', 'italic', 'HorizontalAlignment', 'center', ...
	'VerticalAlignment', 'top', 'FontSize', 10, 'FontWeight', 'bold');

str = note;
text(0.05, 0.02, str, 'units', 'normalized', ...
	'FontAngle', 'italic', 'HorizontalAlignment', 'left', ...
	'VerticalAlignment', 'bottom', 'FontSize', 8, 'FontWeight', 'normal');

%--- Print the plot:
print -dps hdg_diff.ps

