

trex = CO2SYSv3(s.ta_canb(:), s.pHin(:), 1, 3, s.psal(:) + .08, s.tc(:), 25, s.pres(:), 0, s.sil_canb(:), s.po4_canb(:), 0, 0, 1, 10, 1, 2, 2); % convert from pH20atm to pH25atm
pH25atm_saloffset = reshape(trex(:,20), size(s.pres)); % changed from 18 to 20 following CO2SYSv3 update
pH25atm_saloffset(pH25atm_saloffset == -999) = NaN;
%%
BOG2Plot = filterBOG(B, s, 655, 10, 0, 10, 1, 1, 0);
%%
figure
clf
plot(pH25atm_saloffset(:,650),s.pres(:,650))
hold on
plot(BOG2Plot.pH25atm,BOG2Plot.pres,'.', 'Color', 'green')
set(gca,'YDir','reverse')
%%


%% compare BOG pH insitu with glider pH insitu and plot the residuals vs pres, temp, salinity

load('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Spray_Data\24403401\QC\24403401_QC.mat','s');

B = loadBOG();
% var names to match spray data
% B = renamevars(B, 'pH', 'pH20atm'); 
% B = renamevars(B, 'PRESSURE', 'pres'); 
% B = renamevars(B, 'TMP', 'tc'); 
% B = renamevars(B, 'SAL', 'psal');
% B = renamevars(B, 'O2', 'doxy');
% % ta = 2270; % estimate. Can also use canyonB.
% % trex = CO2SYSv3(ta, B.pH20atm, 1, 3, B.psal, 20, B.tc, B.pres, 0, B.SIO4, B.PO4, 0, 0, 1, 10, 1, 2, 2); % convert from pH20atm to pH25atm
% trex = CO2SYSv3(B.TCO2, B.pH20atm, 8, 3, B.psal, 20, B.tc, B.pres, 0, B.SIO4, B.PO4, 0, 0, 1, 10, 1, 2, 2); % convert from pH20atm to pH25atm
% B.pHin = reshape(trex(:,20), size(B.pres));
% B.pHin(B.pHin == -999) = NaN;
prof = 535;
BOG2Plot = filterBOG(B, s, prof, 5, 0, 10, 1, 1, 1);

%%
figure(1)
clf
plot(s.pHin(:,prof),s.pres(:,prof))
hold on
plot(BOG2Plot.pHin,BOG2Plot.pres,'.', 'Color', 'green')
plot(s.pHin_canb(:,prof),s.pres(:,prof))
set(gca,'YDir','reverse')
title('BOG pHin and Spray pHin near M1')
legend('Spray 034','BOG','CanB')
grid on
%%
figure(2)
clf
tl = tiledlayout(1,4);
title(tl,'Profile prof Spray and BOG. 10 yrs BOG. Same month. 5km radius')
nexttile
plot(s.doxy(:,prof),s.pres(:,prof))
hold on
plot(s.doxy_raw(:,prof),s.pres(:,prof))
plot(BOG2Plot.doxy,BOG2Plot.pres,'.', 'Color', 'green')
set(gca,'YDir','reverse')
title('doxy')
legend('Spray 034 doxy adj','doxy raw','BOG',Location='southeast')
grid on
nexttile
plot(s.tc(:,prof),s.pres(:,prof))
hold on
plot(BOG2Plot.tc,BOG2Plot.pres,'.', 'Color', 'green')
set(gca,'YDir','reverse')
title('temp')
legend('Spray 034','BOG',Location='southeast')
grid on
nexttile
plot(s.psal(:,prof),s.pres(:,prof))
hold on
plot(BOG2Plot.psal,BOG2Plot.pres,'.', 'Color', 'green')
set(gca,'YDir','reverse')
title('salinity')
legend('Spray 034','BOG',Location='southeast')
grid on
nexttile
plot(s.pHin(:,prof),s.pres(:,prof))
hold on
plot(BOG2Plot.pHin,BOG2Plot.pres,'.', 'Color', 'green')
plot(s.pHin_canb(:,prof),s.pres(:,prof))
set(gca,'YDir','reverse')
title('BOG pHin and Spray pHin near M1')
legend('Spray 034','BOG','CanB',Location='southeast')
grid on
%% calc residuals. Need to retime and interp.
BOG = table(BOG2Plot.DEPTH,BOG2Plot.pHin,'VariableNames',{'DEPTH','pHin'});
BOGmeans = groupsummary(BOG,"DEPTH",'mean');
figure(3)
clf
plot(s.pHin(:,prof),-s.depth(:,prof))
hold on
plot(BOGmeans.mean_pHin,BOGmeans.DEPTH,'.', 'Color', 'green')
set(gca,'YDir','reverse')
% title('BOG pHin and Spray pHin near M1')
% legend('Spray 034','BOG')
grid on
%%
% Assume B.pHin and B.pres, s.pHin and s.pres are already defined
% Interpolate s.pHin to match B.pres
BOG2Plot = sortrows(BOG2Plot,"pres","ascend");

pres = s.pres(:,prof);
pres = pres(~isnan(pres));
pHin = s.pHin(:,prof);
pHin = pHin(~isnan(pres));
tmp = s.tc(:,prof);
tmp = tmp(~isnan(pres));
psal = s.psal(:,prof);
psal = psal(~isnan(pres));
doxy = s.doxy(:,prof);
doxy = doxy(~isnan(pres));


pHin_interp = interp1(pres, pHin, BOG2Plot.pres, 'linear');
tmp_interp = interp1(pres, tmp, BOG2Plot.pres, 'linear');
psal_interp = interp1(pres, psal, BOG2Plot.pres, 'linear');
doxy_interp = interp1(pres, doxy, BOG2Plot.pres, 'linear');

% Compute residuals
residuals = pHin_interp - BOG2Plot.pHin;

% Display the results
figure(4)
clf
tl = tiledlayout(2,2);
ylabel(tl,'\Delta pH insitu (meas - BOG)')
nexttile
scatter(BOG2Plot.pres,residuals)
grid on
title('pres')
nexttile
scatter(tmp_interp,residuals)
grid on
title('tmp')
nexttile
scatter(psal_interp,residuals)
grid on
title('salinity')
nexttile
scatter(doxy_interp,residuals)
grid on
title('doxy')
%% 

% Compute residuals
residuals =  s.pHin(:,prof) - s.pHin_canb(:,prof);

% Display the results
figure(5)
clf
tl = tiledlayout(2,2);
ylabel(tl,'\Delta pH insitu (meas - canB)')
nexttile
scatter(s.pres(:,prof),residuals)
grid on
title('pres')
nexttile
scatter(s.tc(:,prof),residuals)
grid on
title('tmp')
nexttile
scatter(s.psal(:,prof),residuals)
grid on
title('salinity')
nexttile
scatter(s.doxy(:,prof),residuals)
grid on
title('doxy')
%%
figure(5)
clf
plot3(tmp_interp,psal_interp,residuals)
xlabel('tmp')
ylabel('psal')
zlabel('residuals')
grid on