% load('24403401_QC.mat')
% load("23702901_QC.mat")

%

idx = 120;
[B] = loadBOG();
BOG2Plot = filterBOG(B, s, idx, 50, 1, 10, 1, 1, 3);
vrse = s.vrs;
idx500 = BOG2Plot.DEPTH == 500;
pHinmean = mean(BOG2Plot.pHin(idx500),'omitmissing');
[k0, Eo_ext_insitu] = calc_k0_ext(vrse(500,idx), s.tc(500,idx), s.psal(500,idx), pHinmean, -1.00E-03);
% k0 = -1.356;
% k0 = -1.355;
% k0 = -1.293764044180356;
% k0 = -1.3565;
k0 = -1.28522229719989;
[phfree,phtot]= phcalc_jp(vrse, s.pres, s.tc, s.psal, k0, -1.00E-03, [2.69E-05,-6.75E-08,8.05E-11,-5.09E-14,1.65E-17,-2.14E-21]');

%

idx = 120;
depthidx = 500;
vrse = s.vrs;

[k0, Eo_ext_insitu] = calc_k0_ext(vrse(depthidx,idx), s.tc(depthidx,idx), s.psal(depthidx,idx), s.pHin_canb(depthidx,idx), -1.00E-03);
% k0 = -1.356;
% k0 = -1.355;
% k0 = -1.293764044180356;
% k0 = -1.3565;
[phfree,phtot]= phcalc_jp(vrse, s.pres, s.tc, s.psal, k0, -1.00E-03, [2.69E-05,-6.75E-08,8.05E-11,-5.09E-14,1.65E-17,-2.14E-21]');

%

idx = 539;
[B] = loadBOG();
BOG2Plot = filterBOG(B, s, idx, 5, 0, 10, 1, 1, 1);

%

idx = 520;
figure(1)
clf
plot(s.pHin(:,idx),s.pres(:,idx),Color='red')
hold on
plot(s.pHin_canb(:,idx),s.pres(:,idx),Color='blue')

plot(nooffset.s.pHin(:,idx),nooffset.s.pres(:,idx),Color='red',LineStyle='--')
plot(nooffset.s.pHin_canb(:,idx),nooffset.s.pres(:,idx),Color='blue',LineStyle='--')

scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='green')
set(gca,'YDir','reverse')
title(['prof ',num2str(idx)])
ylim([0 600])
legend('spray','CanB','Spray (no offset)','CanB (no offset)','BOG',Location='southeast')

%

[B] = loadBOG();
BOG2Plot = filterBOG(B, s, idx, 10, 1, 3, 1, 1, 1);
idx500 = BOG2Plot.DEPTH == 500;
pHinmean = mean(BOG2Plot.pHin(idx500),'omitmissing');
[k0, Eo_ext_insitu] = calc_k0_ext(s.vrs(590,idx), s.tc(590,idx), s.psal(590,idx), pHinmean, 'UseDefaultk2');
Pcoefs = fliplr([-1.05E-21,7.28E-18,-1.98E-14,2.68E-11,-1.89E-08,8.73E-06]);
k0 = -1.4182;
[phfree,phtot]= phcalc_jp(s.vrs, s.pres, s.tc, s.psal, k0, -1.03E-03, Pcoefs');

%

figure(2)
clf
plot(s.pHin(:,idx),s.pres(:,idx),Color='red')
hold on
plot(s.pHin_canb(:,idx),s.pres(:,idx),Color='blue')
plot(s.pHin_canbtadic(:,idx),s.pres(:,idx),Color='k')
scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='green')
set(gca,'YDir','reverse')
title(['prof ',num2str(idx)])
ylim([0 600])
legend('spray','CanB','CanB w/ adj','BOG',Location='southeast')
%is the BOG data phtot or free??