function psmooth1(file, psf, year, cruise, note)
%--------------------------------------------------------------------------
% 
% psmooth1.m -- 
% 
% Author          : Jaccard Pierre
% Created On      : 2000/08/28 16:30:59
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/08/29 07:31:19
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
%--------------------------------------------------------------------------

% CHANGES
%
%   2000/08/29 PJ
%              Corrected error in function to_dir(). Arguments to atan2()
%              were swaped

global ens_time

main_title = 'Smoothed Position Fixes';

%--- Read the data:
data = tonan(read_asc(file));

%--- Get index to good and bad data:
igood = ones(size(data(:,1)));
for i=1:size(data,2),
	igood = igood .* (~isnan(data(:,i)));
end
ibad  = find(igood == 0);
igood = find(igood ~= 0);
if isempty(igood),
	error([ 'Only bad data in file ' file ]);
end

%--- Calculate speed and direction for ref velocity:
data(:,9)      = ones(size(data(:,1))) * NaN;
data(:,10)     = ones(size(data(:,1))) * NaN;
data(igood,9)  = to_speed(data(igood,2), data(igood,3));
data(igood,10) = to_dir(data(igood,2), data(igood,3));

%--- Idem for ship:
data(:,11)     = ones(size(data(:,1))) * NaN;
data(:,12)     = ones(size(data(:,1))) * NaN;
data(igood,11) = to_speed(data(igood,4), data(igood,5));
data(igood,12) = to_dir(data(igood,4), data(igood,5));

%--- Calculate distance and time gaps:
data(:,13)     = ones(size(data(:,1))) * NaN;
data(:,14)     = ones(size(data(:,1))) * NaN;
data(igood,13) = gap_time(data(igood,1));
data(igood,14) = gap_distance(data(igood,13), data(igood,4), data(igood,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} (m/s)' ];
		case 2,
			iy = 3;
			str = [ 'V_{ref} (m/s)' ];
		case 3,
			iy = 9;
			str = [ 'Ref. Speed (m/s)' ];
		case 4,
			iy = 10;
			str = [ 'Ref. Direction (degree)' ];
	end
	plot(data(igood,1), data(igood,iy), '-');
	ylabel(str);
	hold on;
	% Plot bad data:
	if ~isempty(ibad),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymin + dy) * ones(size(ibad));
		plot(data(ibad,1), ybad, 'x', 'MarkerSize', 5);
	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/5)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');
	
% Stamp:
date_it([ '-- ' file ' -- ' 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} (m/s)' ];
		case 2,
			iy = 5;
			str = [ 'V_{ship} (m/s)' ];
		case 3,
			iy = 11;
			str = [ 'Ship Speed (m/s)' ];
		case 4,
			iy = 12;
			str = [ 'Ship Direction (degree)' ];
	end
	plot(data(igood,1), data(igood,iy), '-');
	ylabel(str);
	hold on;
	% Plot bad data:
	if ~isempty(ibad),
		ymin = min(get(gca, 'YLim'));
		ymax = max(get(gca, 'YLim'));
		dy = (ymax - ymin)/20;
		ybad = (ymin + dy) * ones(size(ibad));
		plot(data(ibad,1), ybad, 'x', 'MarkerSize', 5);
	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/5)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');
	
% Stamp:
date_it([ '-- ' file ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

%------------------------------------------------------------------
% Plot #3:

figure
orient tall
for n=1:3,
	% Select subplot:
	subplot(3,1,n);
	% Plot good data:
	switch(n)
		case 1,
			iy = 8;
			str = [ 'Filter Fraction' ]; 
			bar(data(igood,1), 1 - data(igood,iy));
		case 2,
			iy = 13;
			str = [ 'Time Gap (s)' ];
			bar(data(igood,1), data(igood,iy));
		case 3,
			iy = 14;
			str = [ 'Distance Gap (m)' ];
			bar(data(igood,1), data(igood,iy));
	end	
	ylabel(str);
	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 ' (3/5)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');

% Add legend for global values:
text(0.05, 0.05, ...
		sprintf('ens_time=%-12.4f', ens_time), ...
		'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', ...
		'FontSize', 8);

% Stamp:
date_it([ '-- ' file ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

%------------------------------------------------------------------
% Plot #4:

figure
orient landscape

% Plot data:
quiver(data(igood,6), data(igood,7), data(igood,2), data(igood,3));
xlabel('Longitude');
ylabel('Latitude');
grid on;

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

% Stamp:
date_it([ '-- ' file ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

%------------------------------------------------------------------
% Plot #5:

figure
orient landscape

% Plot data:
quiver(data(igood,6), data(igood,7), data(igood,4), data(igood,5));
xlabel('Longitude');
ylabel('Latitude');
grid on;

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

% Stamp:
date_it([ '-- ' file ' -- ' 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;

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

function dt = gap_time(t)

global ens_time

ii = 1:(length(t)-1);

dt = (t(ii+1) - t(ii)) * 86400 - ens_time;

dt(length(t)) = NaN;

return;

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

function ds = gap_distance(dt, u, v)

ii = 1:(length(u)-1);

du = u(ii+1) - u(ii);
dv = v(ii+1) - v(ii);

dvel = sqrt(du .* du + dv .* dv);

ds = dt(ii) .* dvel;

ds(length(u)) = NaN;

return;


