data = readtable("WW_Upcast_1Hz.txt");

%

T = data(41:end,:); % deployment data

%

T.DateTime = T.mm_dd_yyyy + T.HH_MM_SS;
T.DateTime.TimeZone = "UTC";

%

figure;
tl = tiledlayout(5,1,"TileSpacing","tight","Padding","tight");
ylabel(tl,'Depth [m]')
nexttile
scatter(T,"DateTime","Depth",ColorVariable="Temperature",Marker=".")
colorbar
set(gca,'Ydir','Reverse')
title('Temperature')
xlabel('')
ylabel('')

nexttile
scatter(T,"DateTime","Depth",ColorVariable="Salinity",Marker=".")
colorbar
set(gca,'Ydir','Reverse')
title('Salinity')
xlabel('')
ylabel('')

nexttile
scatter(T,"DateTime","Depth",ColorVariable="DissolvedO2",Marker=".")
colorbar
set(gca,'Ydir','Reverse')
title('Doxy')
xlabel('')
ylabel('')

% clim([200 300])
% 
nexttile
scatter(T,"DateTime","Depth",ColorVariable="pH",Marker=".")
colorbar
set(gca,'Ydir','Reverse')
title('pH')
xlabel('')
ylabel('')

% 
% 
nexttile
scatter(T,"DateTime","Depth",ColorVariable="ChlorophyllA",Marker=".")
colorbar
set(gca,'Ydir','Reverse')
title('Chlorophyll')
xlabel('')
ylabel('')

%

temperature_extreme = mean(data.Temperature) - 1.5*std(data.Temperature);
psal_extreme = mean(data.Salinity) + 1.5*std(data.Salinity);
doxy_extreme = mean(data.DissolvedO2) - 1.5*std(data.DissolvedO2);
idx_extreme = data.Temperature <= temperature_extreme | data.Salinity >= psal_extreme | data.DissolvedO2 < doxy_extreme;
data_extremes = data(idx_extreme,:);

%

figure;
idx = data.Depth > 80;
scatter3(data(idx,:),"Temperature","Salinity","DissolvedO2",Marker=".")
hold on;
scatter3(data_extremes,"Temperature","Salinity","DissolvedO2",Marker=".",MarkerFaceColor='r')

%

addpath(genpath('C:\Users\spraydata\Documents\GitHub\Spray_Processing\Functions\CANYON_B\'))

%

depth = round(data_extremes.Depth,3);
gtime = datenum(data_extremes.DateTime); % All dimensions must agree
lat = data_extremes.Latitude;
lon = data_extremes.Longitude;
pres = data_extremes.Pressure;
temp = data_extremes.Temperature;
psal = data_extremes.Salinity;
doxy = data_extremes.DissolvedO2;

CanyonB_pH = CANYONB(gtime,lat,lon,pres,temp,psal,doxy,'pH'); % insitu total scale

k2 = -0.001081;
pH_insitu = CanyonB_pH.pH; % Estimated pH_insitu with CanyonB NN
[k0, ~] = calc_k0_ext(data_extremes.Vrse, temp, psal, pH_insitu, k2); % Bresnahan et al. 2014
k0_adj = mean(k0)
k0_std = std(k0)
pH_canyonB = CanyonB_pH.pH; % save to struct