% plot_sw43
%
% This script plots time-versus depth plots of different quantities
% at mooring point 43.  The quantities are read and then filtered.

% These are needed to access certain functions
addpath('/home/phaley/Matlab/Tools')
addpath('/home/phaley/Matlab/Annotate')

saveplot = 1;
%saveplot = 0;

%------------------------------------------------------------------------------
% Open mooring data file
%------------------------------------------------------------------------------

dir = '/projects/awacs/Data/Synoptic/Mooring/';
file = 'SW43SBE.mat';
datafile = [dir file];

tlim_s = [2006 8 14 0 0 0];
tlim_e = [2006 9 26 0 0 0];

load(datafile);

%------------------------------------------------------------------------------
% Since the times and sample rates differ by level, we trim and interpolate
%------------------------------------------------------------------------------

for lvl = 1:size(ctd.T,2)
	t0(lvl) = ctd.time{lvl}(1);
	tf(lvl) = ctd.time{lvl}(end);
	%len(lvl) = size(ctd.T{lvl});
end

ts = max(t0);                    % Start time (MJD)
te = min(tf);                    % End time (MJD)
dt = 600/86400;                  % Interval b/t filtered, subsampled data (days)

t = repmat(ts:dt:te, [size(ctd.T,2) 1]);

%------------------------------------------------------------------------------
% Filter the field data level by level
%------------------------------------------------------------------------------

for lvl = 1:size(ctd.T,2)
	sf = 1 / ctd.sample_int(lvl);  % Sample freq (1/s)
	Tc = 50*60;                    % Cutoff period (s) 
	cf = 1 / Tc;                   % Cutoff freq (1/s)
	wn = cf / (sf/2);              % Normalized freq

	[b, a] = butter(4, wn);
	Tf = filtfilt(b, a, ctd.T{lvl});
	
	% Now linearly interpolate the data to subsampled times
	T(lvl,:) = interp1(ctd.time{lvl}, Tf, t(1,:));
end

z = repmat(-ctd.z_nominal, [size(t,2) 1]);

%------------------------------------------------------------------------------
% Plot the data
%------------------------------------------------------------------------------

zlim = [-375 0]; % 30,33 <=> -80, 35 <=> -60, 43 <=> -375
 
figure(1)
clim = [6.0 27.0];
clvl = nice(clim, 4);
%[C, H] = contourf(t, z', T, clvl);
%set(gca, 'FontSize', 16, 'FontWeight', 'Bold', 'LineWidth', 2);
[C,H] = contourf (t,z',T,clvl); tplotlim = get(gca,'XLim'); set (gca,'FontSize',16,'FontWeight','Bold','LineWidth',2, ...
                                'XLim',[datenum(tlim_s) datenum(tlim_e)]);
set(H, 'EdgeColor', 'None')
caxis(clim);
datetick('x', 'dd', 'keeplimits');
h = cbarf(T, clvl);
set(h, 'FontSize', 16, 'FontWeight', 'Bold');
caxis(clim);
ylabel('Depth  (m)');
title(['T (' setstr(176) 'C)']);
if ~isempty(zlim)
	set(gca, 'YLim', zlim);
end
 
if (saveplot)
	print(gcf, '-dpng', 'T_at_mooring43.png');
end

%clear all;
