function prefabs1(file, psf, year, cruise, note)
%%-------------------------------------------------------------------------
%% 
%% prefabs1.m -- 
%% 
%% Author          : Jaccard Pierre
%% Created On      : 1999/09/17 13:43:42
%% Last Modified By: Jaccard Pierre
%% Last Modified On: 2000/01/21 12:52:49
%% Update Count    : 39
%% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
%% Version         : 0.0
%% Status          : Unknown
%% --------------------------------------------------------------------- ++
%% DESCRIPTION: 
%% 
%%    Undocumented.
%% 
%% --------------------------------------------------------------------- ++
%% REVISIONS: 
%% --------------------------------------------------------------------- ++
%% CHANGES: 
%%-------------------------------------------------------------------------

global min_speed max_speed ens_time

main_title = 'Absolute Reference Layer Velocities';

%--- 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(:,10) = ones(size(data(:,1))) * NaN;
data(:,11) = ones(size(data(:,1))) * NaN;
data(igood,10) = to_speed(data(igood,4), data(igood,5));
data(igood,11) = to_dir(data(igood,4), data(igood,5));

%--- Calculate speed and ratio for gaps:
data(:,12) = ones(size(data(:,1))) * NaN;
data(:,13) = ones(size(data(:,1))) * NaN;
data(igood,12) = to_speed(data(igood,7), data(igood,8));
data(igood,13) = to_ratio(data(igood,12), data(igood,9), data(igood,6));

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

figure
orient tall
for n=1:4,
	% Select subplot:
	subplot(4,1,n);
	% Plot good data:
	switch(n)
		case 1,
			iy = 4;
			str = [ 'U_{ref} (m/s)' ];
		case 2,
			iy = 5;
			str = [ 'V_{ref} (m/s)' ];
		case 3,
			iy = 10;
			str = [ 'Ref. Speed (m/s)' ];
		case 4,
			iy = 11;
			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/3)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');
	
% Stamp:
date_it([ '-- ' file ' -- ' psf ' -- ' note ]);
eval([ 'print -dps -append ' psf ]); 

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

figure
orient tall
for n=1:3,
	% Select subplot:
	subplot(3,1,n);
	% Plot good data:
	switch(n)
		case 1,
			iy = 9;
			str = [ 'T_{span}' ]; 
		case 2,
			iy = 12;
			str = [ 'U_{gap} (m/s)' ];
		case 3,
			iy = 13;
			str = [ 'Gap Ratio' ];
	end
	bar(data(igood,1), data(igood,iy)); %, '.', 'MarkerSize', 5);
	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 ' (2/3)' ], ...
		'HorizontalAlignment', 'center', ...
		'VerticalAlignment', 'bottom');

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

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

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

figure
orient landscape

% Plot data:
quiver(data(igood,2), data(igood,3), 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 ' (3/3)' ], ...
		'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 r = to_ratio(u, t, f)

global min_speed max_speed ens_time

r = u;

ii = find(u < min_speed);
if ~isempty(ii),
	r(ii) = ones(size(ii)) * min_speed;
end

r = (r .* t)./(f .* max_speed);

return;

