%calculate k0 from original data and compare with prior value to find if
%there is any error. Need to find discrepancy between spec calculated value
%and the lab one.
%% Original Calculation:
% CTD_SAL= 33.449;
% CTD_TC = 20;
% Vrs_Ext_avg = -0.942494667;
% pH_Spec =  7.951624795;
% k2 = -9.66638e-04;
% % k0ext 20240513 = 1.421359965661642;

%% load data
data = readtable("20240509_WWpH02_GDF5_Test2.txt");
vars = {'Datetime','something','vrse', 'vrse_std', 'vk', 'vk_std', 'ik', 'ib'};
data.Properties.VariableNames = vars;

spec = readtable("20240509_T51_pH_k0_spec.txt");
pH_tot = mean(spec.pH_tot(1:3));
temp_used = mean(spec.Temp_used(1:3));
time = spec.DateTime(2);
%%

Vext = mean(data.vrse(end-3:end));
TC = 18.8889; % ambient room temp
sal = 33.449; % carboy
pH_insitu = CO2SYSv3(2250, pH_tot, 1, 3, sal, temp_used, TC, 0, 0, 0, 0, 0, 0, 1, 10, 1, 2, 2);
pH_insitu = pH_insitu(20);

[k0, Eo_ext_insitu] = calc_k0_ext(Vext, TC, sal, pH_insitu, 'UseDefaultk2');
%% 
datevec(data.Datetime)
%%
plot(data.vrse)


%% Both myself and Joe were unable to find the source of error in k0. The value found here is nearly identical to the original.
% BW 7/18/24