function ctdprf(year_base, prf_file, pos_file, plot_title, plot_prf, note)
%  1.  NAME
%
%  ctdprf - Plots CTD profiles
%
%  2.  SYNOPSIS
%
%  ctdprf(year_base, prf_file, pos_file, plot_title, plot_prf, note )
%
%  3.  DESCRIPTION
%
%  This Matlab function makes a plot of CTD profiles.  Input file can be
%  generated with xtract(1). CTD input file must have the following
%  columns:
%
%  1. Pressure (db)
%
%  2. Potential Temperature (deg C)
%
%  3. Salinity (ppm)
%
%     Position input file must have the following columns:
%
%  1. Time in decimal days
%
%  2. Longitude in degrees
%
%  3. Latitude in degrees
%
%  4.  PARAMETERS
%
%     year_base
%        Base year used in input position file.
%
%     prf_file
%        Input CTD data file.
%
%     pos_file
%        Input position file.
%
%     plot_title
%        Title for plot.
%
%     plot_prf
%        A string with profile decriptors to plot. These may be the
%        following characters: T (temperature),S (salinity) and s
%        (sigma).
%
%     note
%        Additionnal string to plot at lower left corner of plot
%
%  5.  FILES
%
%  o  ctd_prf.ps
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%--- Read data:
ctd = read_asc(prf_file);
ctd = ctd';

% Pos_file should normally contain one line only with time and position of
% current CTD profile. Function `read_asc' cannot handle files with one line
% only, so that reading this file has to be done manually.
fid = fopen(pos_file);
if fid > 0,
	[pos, c] = fscanf(fid, '%f');
	if c ~= 3,
		error([ 'Wrong number of data in file ' pos_file ]);
	end
else
	error([ 'Cannot locate file ' pos_file ]);
end
fclose(fid);

%--- Initialize indexes:
p = 1; tem = 2; sal = 3; sig = 4;
t = 1; lon = 2; lat = 3;

%--- Calculate sigma-theta: reference
ctd(sig,:) = rho(ctd(p,:), ctd(tem,:), ctd(sal,:));

%--- Select data to plot:
ii = 1:length(plot_prf);
iprf = [];
for i=ii,
	switch plot_prf(i)
		case 'T',
			iprf(i) = tem;
			xprf{i} = 'Potential Temperature (deg C)';
		case 'S',
			iprf(i) = sal;
			xprf{i} = 'Salinity (ppt)';
		case 's',
			iprf(i) = sig;
			xprf{i} = '\sigma_\theta (kg/m^3)';
		otherwise,
			error([ 'Unknown profile descriptor ' plot_prf(i) ]);
	end
end

%--- Select number of plots on page
n = 1;
m = length(ii);

%--- Prepare figure:
figure;
if m > 1,
	orient landscape
	axes('position', [0.1, 0.1, 0.8, 0.7]);
else
	orient tall;
end
	
%--- Plot data:
for i=ii,
	subplot(n,m,i);
	plot(ctd(iprf(i),:), ctd(p,:), '-');
	set(gca, 'YDir', 'reverse');
	xlabel(xprf{i});
	ylabel('Pressure (db)');
	grid on;
end

%--- Additionnal stuff:
date_it([ '- PRF File: ' prf_file ' - POS File: ' pos_file ...
			' - PS File: ctd_prf.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');

str = [ to_date(year_base, pos(t)) ', ' ...
			to_pos(pos(lat), 1, 'dm') ', ' to_pos(pos(lon), 0, 'dm') ];
text(0.5, 0.97, 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 the plot:
print -dps ctd_prf.ps



