function varargout = btnav2(year_base, data_file, ps_file, plot_title, note)
%  1.  NAME
%
%  btnav2 - Comparison of NAV and BT velocities
%
%  2.  SYNOPSIS
%
%  databtnav2(year_base, data_file, ps_file, plot_title, note )
%
%  3.  DESCRIPTION
%
%  This Matlab function makes a plot of NAV anf BT velocities for
%  comparions.  The input ASCII file can be generated with the xtract(1)
%  utility and must have the following columns:
%
%  1. Time in decimal days
%
%  2. Longitude in degrees
%
%  3. Latitude in degrees
%
%  4. U ship from ACCESS_VARIABLES
%
%  5. V ship from ACCESS_VARIABLES
%
%  6. Longitude from NAVIGATION
%
%  7. Latitude from NAVIGATION
%
%  8. Speed (in m/s) from NAVIGATION
%
%  9. Direction from NAVIGATION
%
%  10.
%     U from BOTTOM_TRACK
%
%  11.
%     V from BOTTOM_TRACK
%
%  12.
%     Depth from BOTTOM_TRACK
%
%  The return value data is an array with the data from the input file,
%  with the additionnal columns:
%
%  1. Ship speed
%
%  2. Ship direction
%
%  3. NAV eastward velocity
%
%  4. NAV northward velocity
%
%  5. BT speed
%
%  6. BT direction
%
%  4.  PARAMETERS
%
%     year_base
%        Base year corresponding to the times in the input file.
%
%     data_file
%        Name of input data file.
%
%     ps_file
%        Name for output postscript file.
%
%     plot_title
%        Title for plot.
%
%     note
%        Additionnal string to be printed on the plot.
%
%  5.  RETURNS
%
%  This function returns an array with the data from the input data file
%  an the addiionnal columns described above.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%---Read data:
d = read_asc(data_file);
%  1999/02/09 Jaccard Pierre
%             Sending data array to tonan() function to remove bad data from
%             input files.
d = tonan(d);
nd = size(d, 1);
nc = size(d, 2);
if nc ~= 12,
	error([ 'Wrong number of columns in file ' data_file ]);
end

%--- Initialize indexes:
t = 1; lon = 2; lat = 3; 
u = 4; v = 5;
nav_lon = 6; nav_lat = 7; nav_spd = 8; nav_dir = 9;
bt_u = 10; bt_v = 11; bt_depth = 12;

spd = 13; dir = 14;
nav_u = 15; nav_v = 16;
bt_spd = 17; bt_dir = 18;

%--- Fill columns:
d = d';
PI = pi;
ii = 1:nd;
uu = d(u,ii) + i*d(v,ii);
d(spd,ii) = abs(uu);
d(dir,ii) = 90.0 - (angle(uu) * 180.0/PI);
jj = find(d(dir,ii) < 0.0);
if length(jj) > 0,
	d(dir,jj) = d(dir,jj) + 360.0;
end

d(nav_u,ii) = d(nav_spd,ii) .* cos(PI/180.0 * (90.0 - d(nav_dir,ii)));
d(nav_v,ii) = d(nav_spd,ii) .* sin(PI/180.0 * (90.0 - d(nav_dir,ii)));

d(bt_u,ii) = -1.0 * d(bt_u,ii);
d(bt_v,ii) = -1.0 * d(bt_v,ii);
uu = d(bt_u,ii) + i*d(bt_v,ii);
d(bt_spd,ii) = abs(uu);
d(bt_dir,ii) = 90.0 - (angle(uu) * 180.0/PI);
jj = find(d(bt_dir,ii) < 0.0);
if length(jj) > 0,
	d(bt_dir,jj) = d(bt_dir,jj) + 360.0;
end

%--- Plot these data:
figure;
orient tall;

%--- U plot:
subplot(5,1,1);
plot(d(t,ii), d(u,ii), '-', ...
		d(t,ii), d(nav_u,ii), ':', ...
		d(t,ii), d(bt_u,ii), '--')
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('U (m/s)');
grid on;

%--- V plot:
subplot(5,1,2);
plot(d(t,ii), d(v,ii), '-', ...
		d(t,ii), d(nav_v,ii), ':', ...
		d(t,ii), d(bt_v,ii), '--')
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('V (m/s)');
grid on;

%--- Speed plot:
subplot(5,1,3);
plot(d(t,ii), d(spd,ii), '-', ...
		d(t,ii), d(nav_spd,ii), ':', ...
		d(t,ii), d(bt_spd,ii), '--')
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('Speed (m/s)');
grid on;

%--- Direction plot:
subplot(5,1,4);
plot(d(t,ii), d(dir,ii), '-', ...
		d(t,ii), d(nav_dir,ii), ':', ...
		d(t,ii), d(bt_dir,ii), '--')
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('Direction');
grid on;

%--- Range plot:
p5 = subplot(5,1,5);
plot(d(t,ii), d(bt_depth,ii), '+');
xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
ylabel('Depth');
grid on;
set(p5, 'YDir', 'reverse');

%--- Additionnal Stuff:
date_it([ '- ASC File: ' data_file ' - PS File: ' ps_file ]);
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,ii));
t2 = max(d(t,ii));
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.95, 0.02, str, 'units', 'normalized', ...
	'FontAngle', 'italic', 'HorizontalAlignment', 'right', ...
	'VerticalAlignment', 'bottom', 'FontSize', 8, 'FontWeight', 'normal');

str = sprintf('%s\n%s\n%s', ...
		'continuous: final position', ...
		'dotted: navigation', ...
		'dashed: bottom tracking');
text(0.05, 0.02, str, 'units', 'normalized', ...
	'FontAngle', 'normal', 'HorizontalAlignment', 'left', ...
	'VerticalAlignment', 'bottom', 'FontSize', 8, 'FontWeight', 'normal');

%--- Print the file:
eval([ 'print -dps ' ps_file ]);


%% 1999/03/03 Jaccard Pierre 
%%            Added possibility to get data array in calling process.

if nargout > 0,
  varargout{1} = d;
end





