psaloffset = load("24403401_QC.mat");
nopsaloffset = load("24403401_QC_nooffset.mat");

%

idx =  539;
[B] = loadBOG();
BOG2Plot = filterBOG(B, psaloffset.s, idx, 5, 0, 5, 1, 1, 1); % typical M1 cast around April

%

% idx = 150;
sdn = ones(size(psaloffset.s.pres,1),1) * psaloffset.s.sdn(idx);
lon = ones(size(psaloffset.s.pres,1),1) * psaloffset.s.lon(idx);
lat = ones(size(psaloffset.s.pres,1),1) * psaloffset.s.lat(idx);
canb = CANYONB(sdn, lat, lon, psaloffset.s.pres(:,idx), psaloffset.s.tc(:,idx), psaloffset.s.psal(:,idx), nopsaloffset.s.doxy(:,idx), {'pH'});
pHin_canb_psaloffsetdoxyadj = canb.pH;

%

figure(1)
clf
plot(nopsaloffset.s.pHin_canb(:,idx),nopsaloffset.s.pres(:,idx),Color='blue')
hold on
plot(psaloffset.s.pHin_canb(:,idx),psaloffset.s.pres(:,idx),Color='red')
plot(pHin_canb_psaloffsetdoxyadj,nopsaloffset.s.pres(:,idx),Color='black')
scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='green')
legend('canB w/o psal offset','CanyonB psal offset','CanyonB psal offset & doxy adj','BOG',Location='southeast')
set(gca,'YDir','reverse')
title(['prof ',num2str(idx),' pHin, typical M1 cast'])
ylim([0 600])
grid on
xticks(7.45:.025:8)
xtickangle(45)

%

idx = 50;
figure(2)
clf
% plot(nopsaloffset.s.pHin(:,idx),nopsaloffset.s.pres(:,idx),Color='red')
% hold on
% plot(nopsaloffset.s.pHcorr(:,idx),nopsaloffset.s.pres(:,idx),Color='blue')
plot(psaloffset.s.pHin(:,idx),psaloffset.s.pres(:,idx),Color='red')
hold on
plot(psaloffset.s.pHcorr_flipped(:,idx),psaloffset.s.pres(:,idx),Color='blue')
plot(psaloffset.s.pHin_canb(:,idx),psaloffset.s.pres(:,idx),Color='black')
scatter(BOG2Plot.pHin,BOG2Plot.pres,'green')
grid on

% plot(s.pHin_canb_psaloffset,s.pres(:,idx),Color='blue')
% plot(s.pHin_canb(:,idx),s.pres(:,idx),Color='black')
% scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='green')
% plot(pHin_canb_psaloffsetdoxyadj,nopsaloffset.s.pres(:,idx),Color='black')

legend('Spray pHin (psaloffset)','Spray pHin_corr (psaloffset)','CanyonB','BOG',Location='southeast')
title(['prof ',num2str(idx),' pHin'])
% legend('Spray pHin','BOG (5 km, 5 years, May)',Location='southeast')
ylim([0 600])
set(gca,'YDir','reverse')

%

%no pH adj
[phfree,phtot]= phcalc_jp(s.vrs, s.pres, s.tc, s.psal, -1.355, -1.00E-03, [2.69E-05,-6.75E-08,8.05E-11,-5.09E-14,1.65E-17,-2.14E-21]'); % calc pH
figure(2)
clf
plot(phtot(:,idx),s.pres(:,idx),Color='red')
hold on
% plot(s.pHin_canb_psaloffset,s.pres(:,idx),Color='blue')
plot(s.pHin_canb(:,idx),s.pres(:,idx),Color='black')
scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='grey')
legend('Spray pHin','BOG (5 km, 5 years, may)',Location='southeast')
set(gca,'YDir','reverse')
title(['prof ',num2str(idx),' pHin'])
ylim([0 600])

%

idx = 10;
[B] = loadBOG();
BOG2Plot = filterBOG(B, s, 539, 5, 2, 10, 1, 1, 1);
figure(3)
clf
plot(s.pHcorr(:,idx),s.pres(:,idx),Color='red')
hold on
plot(s.pHin_canb(:,idx),s.pres(:,idx),Color='blue')
scatter(BOG2Plot.pHin,BOG2Plot.pres,Color='green')
legend('Spray','canB','BOG',Location='southeast')
set(gca,'YDir','reverse')
title(['prof ',num2str(idx),' pHin'])
ylim([0 600])

%

%check offset
ctd = readtable('\\atlas.shore.mbari.org\ChemWebData\termserv\testtank\microcat\sbe37.log'); % CTD
vars = {'temp','cond','pres','sal','density','dow','ddmmmyyyy','HHMMSS'};
ctd.Properties.VariableNames = vars;
ctd.datetime = datetime(ctd.ddmmmyyyy, 'InputFormat', 'dd MMM yyyy') + ctd.HHMMSS;
ctd.datetime.TimeZone = 'America/Los_Angeles'; % PST/PDT

%% match indices
ctd.datetime = dateshift(ctd.datetime,"start","minute");

%

SprayCTD_psal = 33.7050;
% 07/25/2024 19:30:01
SprayCTD_dt = datetime(2024,7,25,19,30,00,TimeZone='UTC');
SprayCTD_dt.TimeZone = 'America/Los_Angeles';

%

idx = ctd.datetime == SprayCTD_dt;
CTD_sal = table2array(ctd(idx,'sal'))

%

SprayCTD_psal - CTD_sal

%



%

% 04/11/2024 16:22:37
SprayCTD_psal = 33.6729;
SprayCTD_dt = datetime(2024,4,11,16,30,00,TimeZone='UTC');
SprayCTD_dt.TimeZone = 'America/Los_Angeles';
idx = ctd.datetime == SprayCTD_dt;
CTD_sal = table2array(ctd(idx,'sal'))
psaloffset = CTD_sal - SprayCTD_psal

%

figure(4)
clf
plot(s.pHcorr(:,idx),s.pres(:,idx),Color='red')
hold on
plot(nooffset.s.pHcorr(:,idx),s.pres(:,idx),Color='blue')