%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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)));

ctd_depth = fix_ctd_depth(data(:, m_depth), 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_pres = 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_lag = 0.05;
cond_lag = 0.4;
temp = interp1(ctd_time, temp, ctd_time+temp_lag); 

% Compute the temperature of the conductivity cell...

cell_tau = 5;
fcond = 0.5;
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,cell_temp,ctd_pres);
dens = sw_dens(salt, temp, ctd_pres);

%  If desired, output the time and CTD for gridding by Spyglass...

%if (exist( 'ctd_to_spy' ) == 1),
% if (ctd_to_spy == 1),
%	out(:,1)=data(ok, m_present_time);
%	out(:,2)=-ctd_depth;
%	out(:,3)=data(ok, water_temp);
%	out(:,4)=salt;
%	out(:,5)=dens;
%	fid = fopen(['col_ctd.' run_name], 'wt');
%	fprintf(fid, '%9.4f %9.4f %9.4f %9.4f %9.4f\n', out');
%	fclose(fid);
%	clear out;
%  end;
%end;

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

figure
cascade_t = 1;
set( gcf, 'name', 'Temperature Plots III' );
hold on;
for i = 1:max(yoyo),
   oku = find((yoyo == i) & (going_up));
   okd = find((yoyo == i) & (going_down));
   plot(data(oku, water_temp), data(oku, m_depth), 'b-');
   plot(data(okd, water_temp)+cascade_t*i, data(okd, m_depth), 'r-');
end
set(gca, 'ydir', 'reverse', 'ylim', [0 inf] )
hold on;
grid on;
