function preflyr2(file1, fstr1, file2, fstr2, psf, unit, year, cruise, note)
%%-------------------------------------------------------------------------
%% 
%% preflyr2.m -- 
%% 
%% Author          : Jaccard Pierre
%% Created On      : 1999/09/17 11:37:32
%% Last Modified By: Jaccard Pierre
%% Last Modified On: 1999/09/17 17:47:43
%% Update Count    : 23
%% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
%% Version         : 0.0
%% Status          : Unknown
%% --------------------------------------------------------------------- ++
%% DESCRIPTION: 
%% 
%%    Undocumented.
%% 
%% --------------------------------------------------------------------- ++
%% REVISIONS: 
%% --------------------------------------------------------------------- ++
%% CHANGES: 
%%-------------------------------------------------------------------------

%--- 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(:, 4) = ones(size(data1(:,1))) * NaN;
data1(:, 5) = ones(size(data1(:,1))) * NaN;
data1(ig1,4) = to_speed(data1(ig1,2), data1(ig1,3));
data1(ig1,5) = to_dir(data1(ig1,2), data1(ig1,3));
idata(:, 4) = ones(size(idata(:,1))) * NaN;
idata(:, 5) = ones(size(idata(:,1))) * NaN;
idata(:,4) = to_speed(idata(:,2), idata(:,3));
idata(:,5) = to_dir(idata(:,2), idata(:,3));


%------------------------------------------------------------------
% Plot figure:

figure
orient tall
for n=1:4,
	% Select subplot:
	subplot(4,1,n);
	% Plot good data:
	iy = n+1;
	plot(data1(ig1,1), idata(:,iy)-data1(ig1,iy), '.', 'MarkerSize', 5);
	% Add ylabel:
	switch(n)
		case 1, 
			ylabel([ 'U (' fstr2 '-' fstr1 ') ' unit ]);
		case 2, 
			ylabel([ 'V (' fstr2 '-' fstr1 ') ' unit ]);
		case 3, 
			ylabel([ 'Speed (' fstr2 '-' fstr1 ') ' unit ]);
		case 4, 
			ylabel([ 'Direction (' fstr2 '-' fstr1 ') (degree)' ]);
	end			
	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 ' - Comparison of Reference Layer Velocities' ], ...
		'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;

















