function psmooth2(file1, fstr1, file2, fstr2, psf, year, cruise, note)
%--------------------------------------------------------------------------
% 
% psmooth2.m -- 
% 
% Author          : Jaccard Pierre
% Created On      : 2000/08/29 06:58:59
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/08/29 06:59:28
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
%--------------------------------------------------------------------------

main_title = 'Comparison of Smoothed Velocities';

%--- Read the data:

data1 = tonan(read_asc(file1));
data2 = tonan(read_asc(file2));

%--- Remove lines with bad data:
ig1 = ones(size(data1(:,1)));
for i=1:size(data1,2),
	ig1 = ig1 .* (~isnan(data1(:,i)));
end
ib1 = find(ig1 == 0);
ig1 = find(ig1 ~= 0);

ig2 = ones(size(data2(:,1)));
for i=1:size(data2,2),
	ig2 = ig2 .* (~isnan(data2(:,i)));
end
ib2 = find(ig2 == 0);
ig2 = find(ig2 ~= 0);

%--- Swap index 1 to the array with least good data:
if length(ig1) > length(ig2),
	file3 = file1; file1 = file2; file2 = file3; clear file3;
	fstr3 = fstr1; fstr1 = fstr2; fstr2 = fstr3; clear fstr3;
	data3 = data1; data1 = data2; data2 = data3; clear data3;
	ig3   = ig1  ; ig1   = ig2  ; ig2   = ig3  ; clear ig3  ;
	ib3   = ib1  ; ib1   = ib2  ; ib2   = ib3  ; clear ib3  ;
end

%--- Interpolate to same times:
idata(:,1) = data1(ig1,1);
for i=2:size(data1,2),
	idata(:,i) = interp1(data2(ig2,1), data2(ig2,i), data1(ig1,1));
end

% Calculate speed and direction:
data1(:,9)    = ones(size(data1(:,1))) * NaN;
data1(:,10)   = ones(size(data1(:,1))) * NaN;
data1(:,11)   = ones(size(data1(:,1))) * NaN;
data1(:,12)   = ones(size(data1(:,1))) * NaN;
data1(ig1,9)  = to_speed(data1(ig1,2), data1(ig1,3));
data1(ig1,10) = to_dir(data1(ig1,2), data1(ig1,3));
data1(ig1,11) = to_speed(data1(ig1,4), data1(ig1,5));
data1(ig1,12) = to_dir(data1(ig1,4), data1(ig1,5));
idata(:,9)    = ones(size(idata(:,1))) * NaN;
idata(:,10)   = ones(size(idata(:,1))) * NaN;
idata(:,11)   = ones(size(idata(:,1))) * NaN;
idata(:,12)   = ones(size(idata(:,1))) * NaN;
idata(:,9)    = to_speed(idata(:,2), idata(:,3));
idata(:,10)   = to_dir(idata(:,2), idata(:,3));
idata(:,11)   = to_speed(idata(:,4), idata(:,5));
idata(:,12)   = to_dir(idata(:,4), idata(:,5));

%------------------------------------------------------------------
% Plot #1:

figure
orient tall
for n=1:4,
	% Select subplot:
	subplot(4,1,n);
	% Plot good data:
	switch(n)
		case 1, 
			iy = 2;
			str = [ 'U_{ref} (' fstr2 '-' fstr1 ') (m/s)' ];
		case 2, 
			iy = 3;
			str = [ 'V_{ref} ' fstr2 '-' fstr1 ' (m/s)' ];
		case 3, 
			iy = 9;
			str = [ 'Ref. Speed ' fstr2 '-' fstr1 ' (m/s)' ];
		case 4, 
			iy = 10;
			str = [ 'Ref. Direction (' fstr2 '-' fstr1 ') (degree)' ];
	end			
	plot(data1(ig1,1), idata(:,iy)-data1(ig1,iy), '.', 'MarkerSize', 5);
	ylabel(str);
	hold on
	% Plot bad data:
	if ~isempty(ib1),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymin + dy) * ones(size(ib1));
		plot(data1(ib1,1), ybad, 'x', 'MarkerSize', 5);
		text(1, 0.05, fstr1, 'FontSize', 8, 'units', 'normalized',  ...
				'HorizontalAlignment', 'left', ...
				'VerticalAlignment', 'bottom');
	end
	if ~isempty(ib2),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymax - dy) * ones(size(ib2));
		plot(data2(ib2,1), ybad, 'x', 'MarkerSize', 5);
		text(1, 0.95, fstr2, 'FontSize', 8, 'units', 'normalized', ...
				'HorizontalAlignment', 'left', ...
				'VerticalAlignment', 'top');
	end
	grid on;
end
xlabel([ 'Decimal Days (' int2str(year) ')' ]);

% Plot title:
old_axes = gca;
axes('position', [0 0 1 1]);
axis('off');
text(0.5, 0.95, [ cruise ' - ' main_title ' (1/2)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');

% Stamp:
date_it([ '-- ' fstr1 ':' file1 ' ' fstr2 ':' file2 ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

%------------------------------------------------------------------
% Plot #2:

figure
orient tall
for n=1:4,
	% Select subplot:
	subplot(4,1,n);
	% Plot good data:
	switch(n)
		case 1, 
			iy = 4;
			str = [ 'U_{ship} (' fstr2 '-' fstr1 ') (m/s)' ];
		case 2, 
			iy = 5;
			str = [ 'V_{ship} ' fstr2 '-' fstr1 ' (m/s)' ];
		case 3, 
			iy = 11;
			str = [ 'Ship Speed ' fstr2 '-' fstr1 ' (m/s)' ];
		case 4, 
			iy = 12;
			str = [ 'Ship Direction (' fstr2 '-' fstr1 ') (degree)' ];
	end			
	plot(data1(ig1,1), idata(:,iy)-data1(ig1,iy), '.', 'MarkerSize', 5);
	ylabel(str);
	hold on
	% Plot bad data:
	if ~isempty(ib1),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymin + dy) * ones(size(ib1));
		plot(data1(ib1,1), ybad, 'x', 'MarkerSize', 5);
		text(1, 0.05, fstr1, 'FontSize', 8, 'units', 'normalized',  ...
				'HorizontalAlignment', 'left', ...
				'VerticalAlignment', 'bottom');
	end
	if ~isempty(ib2),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymax - dy) * ones(size(ib2));
		plot(data2(ib2,1), ybad, 'x', 'MarkerSize', 5);
		text(1, 0.95, fstr2, 'FontSize', 8, 'units', 'normalized', ...
				'HorizontalAlignment', 'left', ...
				'VerticalAlignment', 'top');
	end
	grid on;
end
xlabel([ 'Decimal Days (' int2str(year) ')' ]);

% Plot title:
old_axes = gca;
axes('position', [0 0 1 1]);
axis('off');
text(0.5, 0.95, [ cruise ' - ' main_title ' (2/2)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');

% Stamp:
date_it([ '-- ' fstr1 ':' file1 ' ' fstr2 ':' file2 ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

return;

%------------------------------------------------------------------

function s = to_speed(u, v)

s = sqrt(u .* u + v .* v);

return;

%------------------------------------------------------------------

function d = to_dir(u, v)

d  = atan2(v, u) * 180.0/pi;
d = 90.0 - d;
d = mod(d, 360);

return;

