function [ww] = THOONK0Calc(ww,warmup,mindepth,maxdepth)
%THOONK0CALC add a K0_adj variable to the ww struct
% gtime = repmat(ww.Datenum,height(ww.Depth),1); % All dimensions must agree
% lat = repmat(ww.Latitude,height(ww.Depth),1);
% lon = repmat(ww.Longitude,height(ww.Depth),1);
% pres = ww.Pressure;
% temp = ww.Temperature;
% psal = ww.Practical_Salinity;
% doxy = ww.Dissolved_Oxygen;
% 
% changed because big file takes too long to run through canB. 1:21 is
% 70 to 74 m
depth = round(-ww.Depth,3);
imindepth = find(depth == mindepth);
imaxdepth = find(depth == maxdepth);

gtime = repmat(ww.Datenum,height(ww.Depth(imaxdepth:imindepth)),1); % All dimensions must agree
lat = repmat(ww.Latitude,height(ww.Depth(imaxdepth:imindepth)),1);
lon = repmat(ww.Longitude,height(ww.Depth(imaxdepth:imindepth)),1);
pres = ww.Pressure(imaxdepth:imindepth,:);
temp = ww.Temperature(imaxdepth:imindepth,:);
psal = ww.Practical_Salinity(imaxdepth:imindepth,:);
doxy = ww.Dissolved_Oxygen(imaxdepth:imindepth,:);

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

k2 = ww.k2;
try
    Vext = ww.pH_voltage(imaxdepth:imindepth,(warmup:end));
catch
    Vext = ww.pH_VRS(imaxdepth:imindepth,(warmup:end));
end
TC = ww.Temperature(imaxdepth:imindepth,(warmup:end));
sal = ww.Practical_Salinity(imaxdepth:imindepth,(warmup:end));
pH_insitu = CanyonB_pH.pH(:,warmup:end); % Estimated pH_insitu with CanyonB NN
[k0, ~] = calc_k0_ext(Vext, TC, sal, pH_insitu, k2); % Bresnahan et al. 2014
ww.k0_adj_full = k0;
ww.k0_adj = double(mean(k0,'all','omitmissing')); % avg deep water k0
ww.k0_std = double(std(k0,[],'all','omitmissing')); % avg deep water k0
ww.pH_canyonB = CanyonB_pH.pH; % save to struct
end

