load("\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Spray_Data\25921201\full_data\2509021201.mat");
data.qual.ctd.c = data.qual.ctd.s; % Need this for function to work
%% method for upcast binning
[c, r] = size(data.ctd.p);
params = fieldnames(data.ctd);
for i = 1:c
    idxup = ismember(data.ctd.phase{i}, 0);
    % Remove elements from all CTD parameters
    for j = 1:length(params)
        data.ctd.(params{j}){i}(~idxup) = [];
    end
    
    % Remove elements from quality control fields (once per dive)
    data.qual.ctd.t{i}(~idxup) = [];
    data.qual.ctd.s{i}(~idxup) = [];
    data.qual.ctd.c{i}(~idxup) = [];
end

[c, r] = size(data.ph.p);
params = fieldnames(data.ph);
for i = 1:c
    idxup = ismember(data.ph.phase{i},0);
    for j = 1:length(params)
        data.ph.(params{j}){i}(~idxup) = [];
    end
    data.qual.ph.ph{i}(~idxup) = [];
end

[c, r] = size(data.dox.p);
params = fieldnames(data.dox);
for i = 1:c
    idxup = ismember(data.dox.phase{i},0);
    for j = 1:length(params)
        data.dox.(params{j}){i}(~idxup) = [];
    end
    data.qual.dox.ox{i}(~idxup) = [];
end

[c, r] = size(data.opt.p);
params = fieldnames(data.opt);
for i = 1:c
    idxup = ismember(data.opt.phase{i},0);
    for j = 1:length(params)
        data.opt.(params{j}){i}(~idxup) = [];
    end
    data.qual.opt.fl{i}(~idxup) = [];
end
%% Recalc pH upcasts only
data = ph_sp2(data,'0212act','BW'); % Recalc with proper k0
%% Bin
bindata_flash=binsat_sp2(data,0,10,1000,'d'); % bin data w/ sat function
%% Use class to plot binned upcasts
g = Spray2Data('25921201',savefigure=1);
g.getpHCal;
g.nprofs2plot=25;
% g.prof2plot = 17;
g.bindata = bindata_flash;
g.buildStruct;
g.addCarbonateParameters;
g.buildTable;
g.plotRecentProfiles;
%% Grab data at 500
T = g.T;
iplot = T.divenum == 17 & T.depth == 450; % data after 11 has lower drift
Tadj = T(iplot,:);
%% recalc k0 w/ binned
k0_canb = calc_k0_ext(Tadj.vrse,Tadj.tc,Tadj.psal,Tadj.phin_canb,g.pH_cal.k2_fp_c0)
%% recalc with raw data
prof = 15;
target = 450;
[~, idx_ph] = min(abs(data.ph.depth{prof} - target));
[~, idx_ctd] = min(abs(data.ctd.depth{prof} - target));
[~, idx_doxy] = min(abs(data.dox.depth{prof} - target));

sdn = datenum(datetime(data.time(prof,2)', 'ConvertFrom', 'posixtime'));
lat = data.lat(prof,2);
lon = data.lon(prof,2);
tc = data.ctd.t{prof}(idx_ctd);
pres = data.ctd.p{prof}(idx_ctd);
psal = data.ctd.s{prof}(idx_ctd);
doxy = data.dox.oxumolkg{prof}(idx_doxy);
vrse = data.ph.Vrse{prof}(idx_ph);
pHin = data.ph.ph{prof}(idx_ph);
k2 = data.qual.operator(7).params.k2;
pcoefs = data.qual.operator(7).params.Pcoefs;

canb = CANYONB(sdn, lat, lon, pres, tc, psal, doxy, {'AT', 'SiOH4', 'PO4', 'pH'});
pHin_canb = canb.pH + (canb.pH*0.0404 - 0.3168); % additional correction to make it consistent to 'spec pH'

Traw = table(sdn, lat, lon, pres, tc, psal, doxy, vrse, pHin, pHin_canb,...
    'VariableNames',{'sdn','lat','lon', 'pres','tc','psal','doxy', 'vrse', 'pHin' ,'pHin_canb'});

k0_canb_raw = k0frompH(vrse, pres, tc, psal, pHin_canb, k2, pcoefs)
% k0_canb_raw = calc_k0_ext(vrse,tc,psal,pHin_canb,k2)
% [~, pHin_raw] = phcalc_jp(vrse,pres,tc,psal,k0_canb_raw,k2,pcoefs)
%%
% disp(pHin_canb - pHin_raw)
