%% Load Nano data
basedir = '\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet';
file = 'boxtest_02282025.csv';
opts = detectImportOptions(fullfile(basedir,file));
opts.VariableNamesLine = 5;
data = readtable(fullfile(basedir,file), opts);
meta = readlines(fullfile(basedir, file));
meta = meta(1:3); % Extract relevant lines

% First split by ':'
CTD = strtrim(strsplit(meta(1), ':'));
NanoFets = strtrim(strsplit(meta(2), ':'));
NanoFetIDs = strtrim(strsplit(meta(3), ':'));

% Assign to struct
meta = struct;
meta.CTD = CTD(2);
meta.NanoFets = strtrim(strsplit(NanoFets(2), ';'));
meta.NanoFetIDs = strtrim(strsplit(NanoFetIDs(2), ';'));

%% Load Spec data
basedir = 'C:\Users\bwerb\Documents\SprayDashBoard\GlideAppPackage\TestProjectLibrary\25202901\calibration_data';
file = '20250204_TTk0_25202901.txt';

specfilename = fullfile(basedir,file);
[spec_pH, spec_Temp_used] = readSpec(specfilename);

%% Calculate Spec pH measurement at insitu conditions
[timediff, imin] = min(abs(data.DateTime - SpecpHData.Datetime)); % first occurance, there will be multiple with same because of the nanos in a string

CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
trex = CO2SYS(2270, SpecpHData.pH_tot, 1, 3, CTD_Sal, SpecpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
SpecpHData.pH_insitu = trex(:,18);


%% Calculate k0
icom = ismember(data.COM,'COM4'); % select com and datetime
calc_k0_ext(data.Vrse(icom),data.Temperature(icom),data.Salinity(icom),SpecpHData.pH_insitu)




%%