function uvprf2(year_base, nav_file, prf_file, plot_title, note, ...
		plot_mean, plot_ship)
%------------------------------------------------------------------
% NAME
%
%   uvprf2 - Plot velocity profiles and navigation data
%
% SYNOPSIS
%
%   uvprf2(year_base, nav_file, prf_file, plot_title, note, ...
%          plot_mean, plot_ship)
%
% PARAMETERS
%
%   year_base
%
%   nav_file
%      Data file containing the following columns:
%
%         1. time
%         2. longitude
%         3. latitude
%         4. U ship
%         5. V ship
%
%
%   prf_file
%      Data file containing concatenated velocity profiles, with the following
%      columns: 
%
%         1. depth
%         2. u relative
%         3. v relative
% 
%      Depth must be monotonically increasing for each profile (this is
%      necessary for the function to split out each profile from the file. 
%
%   plot_title
%
%   note
%      Additionnal string to plot on the lower left corner of the plot.
%
%   plot_mean
%      If non-zero, plot a mean velocity profile. This assumes that profile data
%      are given for the same depths.
%
%   plot_ship
%      If non-zero, also plot information about navigation.
%
% DESCRIPTION
%
%   Absolute currents are calculated and the resulting profiles are plotted
%   together. 
%
% OUTPUT
%
%   uvprf.ps
%      Postscript file with the velocity profile plots.
%
%   shipprf.ps
%      Postscript file with the navigation plot (if selected).
%
%------------------------------------------------------------------
%--- Red navigation:
nav = read_asc(nav_file);

nav_d = size(nav, 1);
nav_c = size(nav, 2);
if nav_c ~= 5,
	error([ 'Wrong number of columns in file ' nav_file ]);
end

%--- Initialize nav indexes:
t = 1; lon = 2; lat = 3;
ship_u = 4; ship_v = 5;

ship_spd = 6; ship_dir = 7;

%--- Fill nav columns:
nav = nav';
PI = pi;
I = i;
ii = 1:nav_d;
uu = nav(ship_u,ii) + I*nav(ship_v,ii);
nav(ship_spd,ii) = abs(uu);
nav(ship_dir,ii) = 90.0 - (angle(uu) * 180.0/PI);
jj = find(nav(ship_dir,ii) < 0.0);
if length(jj) > 0,
	nav(ship_dir,jj) = nav(ship_dir,jj) + 360.0;
end

%--- Read profile data:
prf = read_asc(prf_file);
prf_d = size(prf, 1);
prf_c = size(prf, 2);
if prf_c ~= 3,
	error([ 'Wrong number of columns in file ' prf_file ]);
end

%--- Initialize prf indexes:
d = 1; prf_ru = 2; prf_rv = 3;

prf_au = 4; prf_av = 5; prf_as = 6; prf_ad = 7;

%--- Split the profile data:
prf = prf';
i1 = 1:(prf_d-1);
i2 = 2:prf_d;
jj = find( (prf(d,i2)-prf(d,i1)) < 0);
if length(jj) ~= (nav_d-1),
	error('Not same number of profiles and navigation data');
end
if length(jj) == 0,
	jj = prf_d;
end
iprf(1,1:2) = [ 1, jj(1) ];
for j=2:length(jj),
	iprf(j,1:2) = [ jj(j-1) + 1, jj(j) ];
end
j = j+1;
iprf(j, 1:2) = [ jj(j-1)+1, prf_d ];

%--- Fill profile columns
ii = 1:prf_d;
prf(prf_au:prf_av,ii) = ones(2,prf_d) * NaN;
for i=1:nav_d,
	jj = iprf(i,1):iprf(i,2);
	prf(prf_au,jj) = prf(prf_ru,jj) + nav(ship_u, i);
	prf(prf_av,jj) = prf(prf_rv,jj) + nav(ship_v, i);
end
i = sqrt(-1);

uu = prf(prf_au,ii) + I*prf(prf_av,ii);
prf(prf_as,ii) = abs(uu);
prf(prf_ad,ii) = 90.0 - (angle(uu) * 180.0/PI);
jj = find(prf(prf_ad,ii) < 0.0);
if length(jj) > 0,
	prf(prf_ad,jj) = prf(prf_ad,jj) + 360.0;
end

%--- Calculate men profile:
tmp = sort(prf(d,:));
ii = 1:length(tmp);
depths(1) = tmp(1);
j = 2;
for i=ii,
	if tmp(i) ~= depths(j-1),
		depths(j) = tmp(i);
		j = j+1;
	end
end

mn_au = 1; mn_av = 2; mn_as = 3; mn_ad = 4;
ii = 1:length(depths);
for i=ii, 
	jj = find(prf(d,:) == depths(i));
	mn(mn_au,i) = mean(prf(prf_au,jj));
	mn(mn_av,i) = mean(prf(prf_av,jj));
	mn(mn_as,i) = mean(prf(prf_as,jj));
	mn(mn_ad,i) = mean(prf(prf_ad,jj));
end

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

%--- U plot:
subplot(2,2,1);
set(gca, 'YDir', 'reverse');
hold on;
for i=1:nav_d,
	jj = iprf(i,1):iprf(i,2);
	plot(prf(prf_au,jj), prf(d,jj));
end
xlabel('U (m/s)');
ylabel('Depth (m)');
grid on;
if plot_mean,
	plot(mn(mn_au,:), depths(:), 'LineWidth', 2.0)
end

%--- V plot:
subplot(2,2,2);
set(gca, 'YDir', 'reverse');
hold on;
for i=1:nav_d,
	jj = iprf(i,1):iprf(i,2);
	plot(prf(prf_av,jj), prf(d,jj));
end
xlabel('V (m/s)');
grid on;
if plot_mean,
	plot(mn(mn_av,:), depths(:), 'LineWidth', 2.0)
end

%--- Speed plot:
subplot(2,2,3);
set(gca, 'YDir', 'reverse');
hold on;
for i=1:nav_d,
	jj = iprf(i,1):iprf(i,2);
	plot(prf(prf_as,jj), prf(d,jj));
end
xlabel('Speed (m/s)');
grid on;
if plot_mean,
	plot(mn(mn_as,:), depths(:), 'LineWidth', 2.0)
end

%--- Direction plot:
subplot(2,2,4);
set(gca, 'YDir', 'reverse');
hold on;
for i=1:nav_d,
	jj = iprf(i,1):iprf(i,2);
	plot(prf(prf_ad,jj), prf(d,jj));
end
xlabel('Direction');
grid on;
if plot_mean,
	plot(mn(mn_ad,:), depths(:), 'LineWidth', 2.0)
end

%--- Additionnal stuff:
date_it([ '- NAV File: ' nav_file ' - PRF File: ' prf_file ...
			' - PS File: uvprf.ps' ]);
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(nav(t,:));
t2 = max(nav(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 -dps uvprf.ps

if plot_ship,
	figure;
	orient tall;

	%--- U ship plot;
	subplot(2,2,1);
	hold on;
	plot(nav(t,:), nav(ship_u,:));
	xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
	ylabel('U Ship (m/s)');
	grid on;

	%--- V ship plot;
	subplot(2,2,2);
	hold on;
	plot(nav(t,:), nav(ship_v,:));
	xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
	ylabel('V Ship (m/s)');
	grid on;

	%--- Speed ship plot;
	subplot(2,2,3);
	hold on;
	plot(nav(t,:), nav(ship_spd,:));
	xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
	ylabel('Ship Speed (m/s)');
	grid on;

	%--- Direction ship plot;
	subplot(2,2,4);
	hold on;
	plot(nav(t,:), nav(ship_dir,:));
	xlabel([ 'Decimal Days (' int2str(year_base) ')' ]);
	ylabel('Ship Direction');
	grid on;

	date_it([ '- NAV File: ' nav_file ' - PRF File: ' prf_file ...
				' - PS File: prfship.ps' ]);
	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(nav(t,:));
	t2 = max(nav(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 -dps shipprf.ps
	
end




