temp_lag = 0.00;   				% 0.05 best guess...
cond_lag = -0.1335+temp_lag;	% -0.1335 best guess...
cell_tau = 6.2940;				% 6.294 best guess...
fcond = 0.0644;					% 0.0644 best guess...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ctd plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Use UNESCO salinity and density calculations...  - JGB, 6/29/96
%
%  Correct for difference in position of pressure sensor & cond/temp cells
%  and attitude of vehicle.  Note that roll not acounted for....
%
%		J. G. Bellingham
%      Substantially rewritten 12/26/98 

ok = find(~isnan(data(:, water_temp)));

dx = 0.4; dy = 0.2;  % position of CTD intake valid for MABay98
ctd_depth = data(:, m_depth) - (dx .* sin(data(:, m_pitch)) - dy .* cos(data(:, m_pitch)));

%  Convert m of freshwater to decibars  (from JWB 05/02/95)
% (3.281 ft/m)*(12 in/ft)*(2.49082e-2 dBar/in of H2O)

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

ctd_depth = ctd_depth(ok)*3.281*12*2.49082e-2;
cond = data(ok, water_cond);
temp = data(ok, water_temp);
ctd_time = data(ok, m_present_time);

n = length(cond);

% ------------------------------------------------------------------------
% Correct for time lags
%   Temperature  = 0.05 s
%   Conductivity = 0.35 s + temperature lag

temp = interp1(ctd_time, temp, ctd_time+cond_lag); 

% ------------------------------------------------------------------------
% Compute the temperature of the conductivity cell...

cell_temp = temp;
g = exp(-diff(ctd_time)/cell_tau);

for indx = 2:length(ctd_time),
   cell_temp(indx) = g(indx-1)*cell_temp(indx-1) + (1-g(indx-1))*temp(indx);
end

cond_temp = temp - fcond * (temp - cell_temp);

% ------------------------------------------------------------------------
% Compute the salinity

salt = sw_salt(10*cond/sw_c3515,cond_temp,ctd_depth);
dens = sw_dens(salt, temp, ctd_depth);

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

figure
set( gcf, 'name', 'Temperature and Salinity Plots' );
subplot(211);
hold off;
plot(ctd_time, temp, 'r.');
hold on;
grid on
title(['Temperature vs time ',run_name]);
ylabel('degrees C');
xlabel('seconds');
subplot(212);
hold off;
plot(ctd_time, salt, 'r.');
hold on;
grid on
title(['Salinity vs time ',run_name]);
ylabel('psu');
xlabel('seconds');
zoom on

figure
set( gcf, 'name', 'Temperature, Salinity, Density Plots II' );
subplot(131);
hold off;
plot(temp, ctd_depth, 'b-');
set(gca, 'ydir', 'reverse', 'ylim', [0 inf] )
hold on;
grid on;
title(['Temperature vs depth ']);
xlabel('degrees C');
ylabel('depth (m)');
subplot(132);
hold off;
plot(salt, ctd_depth, 'b-');
set(gca, 'ydir', 'reverse', 'ylim', [0 inf] )
hold on;
grid on
title(['Salinity vs depth ',run_name]);
xlabel('psu');
ylabel('depth (m)');
subplot(133);
hold off;
plot(dens, ctd_depth, 'b-');
set(gca, 'ydir', 'reverse', 'ylim', [0 inf] )
hold on;
grid on
title(['Density vs depth ']);
xlabel('kg/m^3');
ylabel('depth (m)');
zoom on
