% NanoFets: COM3; COM4; COM5; COM6
% NanoFetIDs: DF403; DF418; DF417; DF413

%%
[spec_pH, spec_Temp_used, specpH] = readSpec('20250414_WWk0_Nanok0.txt');
specpH = specpH(2,:);
%%
data = readtable('04_09_25_k0.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
%% Calculate Spec pH measurement at insitu conditions
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
%%
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
%% COM4: DF418 GLIDERFET006
idx = ismember(data.COM,'COM4') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET006 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,0.000371965);
% k0 = 1.000517562355951
%% COM5: DF417 GLIDERFET005
idx = ismember(data.COM,'COM5') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET005 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,0.000311858);
% k0 = 1.000272910237151
%% COM6: DF413 GLIDERFET007
idx = ismember(data.COM,'COM6') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET007 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,0.000255359);

%% WireWalker pH: GDF006
[spec_pH, spec_Temp_used, specpH] = readSpec('20250414_WWk0_Nanok0.txt');
specpH = specpH(1,:);
data = readtable("WireWalker_04_29_25\WW_TT_04_25_25.csv");
% Convert milliseconds to seconds
timestamp_s = data.Time_ms_ / 1000;
% Convert to datetime
data.DateTime = datetime(timestamp_s, 'ConvertFrom', 'posixtime', 'TimeZone', 'UTC'); % RBR clock was not set
data = table2timetable(data,"RowTimes","DateTime");
data = retime(data,'regular','linear','TimeStep',minutes(1));


[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
imin = imin - 1; % Manually set to use previous sample. RBR was being removed from TT or shut off.
% Convert conductivity to salinity using sw_salt(cond, temp, pres)
% data.Salinity = sw_salt(data.Conductivity_mS_cm_/10,
% data.Temperature__C_, data.Pressure_dbar_); % need condr: ratio
data.Salinity = gsw_SP_from_C(data.Conductivity_mS_cm_/10, data.Temperature__C_, data.Pressure_dbar_);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature__C_(imin);

trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);

idx = data.DateTime == time_sample;
k0_WW_GDF006 = calc_k0_ext(data.pH_V_(idx),data.Temperature__C_(idx),data.Salinity(idx),specpH.pH_insitu,-0.001081);

%%
% CTD: COM1
% NanoFets: COM3; COM4; COM5; COM6
% NanoFetIDs: GliderFET008; GliderFET009; GliderFET010; GliderFET011
%%
data = readtable('04_24_25_k0.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
%%
[spec_pH, spec_Temp_used, specpH] = readSpec('20250506_mpHOx001_Nanok0.txt');
specpH = specpH(2,:);
%% Calculate Spec pH measurement at insitu conditions
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
%%
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);

%% COM3: GLIDERFET008
idx = ismember(data.COM,'COM3') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET008 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,3.734130E-04);

%% COM4: GLIDERFET009
idx = ismember(data.COM,'COM4') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET009 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,8.777280E-04);

%% COM5: GLIDERFET010
idx = ismember(data.COM,'COM5') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET010 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,7.406790E-04);

%% COM6: GLIDERFET011
idx = ismember(data.COM,'COM6') & data.DateTime == time_sample; % select com and datetime
k0_GLIDERFET011 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,3.260250E-04);
%% File created Wednesday, ‎June ‎18, ‎2025, ‏‎3:56:23 PM, sample frequency 1 min
data = readtable('06_18_25_GFET004_GFET012_k0_plumbed_inline.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('20250709_mfet_nano_k0_spec.txt');
specpH = specpH(2,:); % sample on June 19
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);

idx = ismember(data.COM,'COM4') & data.DateTime == time_sample; % select com and datetime 4 is gliderfet004
k0_GLIDERFET004 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,3.734130E-04);
idx = ismember(data.COM,'COM3') & data.DateTime == time_sample; % select com and datetime 3 is gliderfet012
k0_GLIDERFET012 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,3.734130E-04);
%% GliderFET014
data = readtable('GliderFET014_CAL.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('20250902_FTk0.txt');
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 5.234860E-04;
idx = data.DateTime == time_sample;
k0_GLIDERFET014 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,k2);
%% GliderFET015 
data = readtable('GliderFET015_CAL.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('20250902_FTk0.txt');
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 5.216180E-04;
idx = data.DateTime == time_sample;
k0_GLIDERFET015 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,k2);
%% GliderFET016 
data = readtable('GliderFET016_CAL.csv');
data.DateTime.TimeZone = "America/Los_Angeles";
data.DateTime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('20250902_FTk0.txt');
[timediff, imin] = min(abs(data.DateTime - specpH.DateTime)); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.DateTime(imin);
CTD_Sal = data.Salinity(imin);
CTD_T = data.Temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 4.816290E-04;
idx = find(data.DateTime == time_sample);
k0_GLIDERFET016 = calc_k0_ext(data.Vrse(idx),data.Temperature(idx),data.Salinity(idx),specpH.pH_insitu,k2);
%% Make CAL file
data1 = readtable("NanoFETS\10_07_2025_k0_wetlab_GFET007_GFET013_GFET002_GFET016.csv");
data2 = readtable("NanoFETS\10_09_2025_k0_wetlab_GFET007_GFET013_GFET016_plumbedinline.csv");
data = [data1 ; data2];
idx013 = ismember(data.SensorID,'GliderFET013');
idx016 = ismember(data.SensorID,'GliderFET016');
idx002 = ismember(data.SensorID,'GliderFET002');

gfet013 = data(idx013,:);
gfet016 = data(idx016,:);
gfet002 = data(idx002,:);


writetable(gfet013,'C:\Users\bwerb\Documents\MATLAB\WetLabk0\CAL\GliderFET013_DF431_CAL.csv');
writetable(gfet016,'C:\Users\bwerb\Documents\MATLAB\WetLabk0\CAL\GliderFET016_DF429_CAL.csv');
writetable(gfet002,'C:\Users\bwerb\Documents\MATLAB\WetLabk0\CAL\GliderFET002_DF409_CAL.csv');
%% GliderFET016_DF429
data = readtable('GliderFET016_DF429_CAL.csv');
data.Datetime.TimeZone = "America/Los_Angeles";
data.Datetime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('Cal_underway_ph.txt');
  % Preallocate arrays for the two k0 values
k0_values = zeros(2, 1);

% Loop through the first two DateTime values
for i = 1:4
    [timediff, imin] = min(abs(data.Datetime - specpH.DateTime(i)));
    time_sample = data.Datetime(imin);
    CTD_Sal = data.Salinity(imin);
    CTD_T = data.Temperature(imin);
    
    trex = CO2SYS(2270, specpH.pH_tot(i), 1, 3, CTD_Sal, specpH.Temp_Samp(i), CTD_T, 0, 0, 0, 0, 1, 10, 1);
    
    % Spec pH input for next step
    pH_insitu_current = trex(:,18);
    
    k2 = 4.816290E-04;
    idx = find(data.Datetime == time_sample);
    k0_values(i) = calc_k0_ext(data.Vrse(idx), data.Temperature(idx), data.Salinity(idx), pH_insitu_current, k2);
end

% Return both k0 values
k0_GLIDERFET016_DF429_1 = k0_values(1);
k0_GLIDERFET016_DF429_2 = k0_values(2);
k0_GLIDERFET016_DF429_3 = k0_values(3);
k0_GLIDERFET016_DF429_4 = k0_values(4);
k0_GLIDERFET016_DF429 = mean(k0_values(3:4)) 
%% GliderFET013_DF431
data = readtable('GliderFET013_DF431_CAL.csv');
data.Datetime.TimeZone = "America/Los_Angeles";
data.Datetime.TimeZone = "UTC";
[spec_pH, spec_Temp_used, specpH] = readSpec('Cal_underway_ph.txt');
% Preallocate arrays for the two k0 values
k0_values = zeros(2, 1);

% Loop through the first two DateTime values
for i = 1:4
    [timediff, imin] = min(abs(data.Datetime - specpH.DateTime(i)));
    time_sample = data.Datetime(imin);
    CTD_Sal = data.Salinity(imin);
    CTD_T = data.Temperature(imin);
    
    trex = CO2SYS(2270, specpH.pH_tot(i), 1, 3, CTD_Sal, specpH.Temp_Samp(i), CTD_T, 0, 0, 0, 0, 1, 10, 1);
    
    % Spec pH input for next step
    pH_insitu_current = trex(:,18);
    
    k2 = 4.816290E-04;
    idx = find(data.Datetime == time_sample);
    k0_values(i) = calc_k0_ext(data.Vrse(idx), data.Temperature(idx), data.Salinity(idx), pH_insitu_current, k2);
end

% Return both k0 values
k0_GLIDERFET013_DF431_1 = k0_values(1);
k0_GLIDERFET013_DF431_2 = k0_values(2);
k0_GLIDERFET013_DF431_3 = k0_values(3);
k0_GLIDERFET013_DF431_4 = k0_values(4);
k0_GLIDERFET013_DF431 = mean(k0_values(3:4)) 
%% GliderFET005_DF432
% data = readtable('GliderFET013_DF431_CAL.csv');
% data.Datetime.TimeZone = "America/Los_Angeles";
% data.Datetime.TimeZone = "UTC";
% [spec_pH, spec_Temp_used, specpH] = readSpec('Cal_underway_ph.txt');
% % Preallocate arrays for the two k0 values
% k0_values = zeros(2, 1);
% 
% % Loop through the first two DateTime values
% for i = 1:4
%     [timediff, imin] = min(abs(data.Datetime - specpH.DateTime(i)));
%     time_sample = data.Datetime(imin);
%     CTD_Sal = data.Salinity(imin);
%     CTD_T = data.Temperature(imin);
% 
%     trex = CO2SYS(2270, specpH.pH_tot(i), 1, 3, CTD_Sal, specpH.Temp_Samp(i), CTD_T, 0, 0, 0, 0, 1, 10, 1);
% 
%     % Spec pH input for next step
%     pH_insitu_current = trex(:,18);
% 
%     k2 = 4.816290E-04;
%     idx = find(data.Datetime == time_sample);
%     k0_values(i) = calc_k0_ext(data.Vrse(idx), data.Temperature(idx), data.Salinity(idx), pH_insitu_current, k2);
% end
% 
% % Return both k0 values
% k0_GLIDERFET013_DF431_1 = k0_values(1);
% k0_GLIDERFET013_DF431_2 = k0_values(2);
% k0_GLIDERFET013_DF431_3 = k0_values(3);
% k0_GLIDERFET013_DF431_4 = k0_values(4);
% k0_GLIDERFET013_DF431 = mean(k0_values(3:4)) 

%% k0 4 sensors from 3/30/26 21:25 UTC Spec sample
%% 3/30/36 GliderFET016_DF429
data = readtable("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\03_26_26_id_17_18_16_6.csv");
data.datetime.TimeZone = "UTC";
data = data(data.gliderfet_id == 16,:);
[spec_pH, spec_Temp_used, specpH] = readSpec("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\spec_ph\FT_k0_spec_ph.txt");
[timediff, imin] = min(abs(data.datetime - specpH.DateTime(24))); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.datetime(imin);
CTD_Sal = data.salinity(imin);
CTD_T = data.temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 0.000481629;
idx = find(data.datetime == time_sample);
k0_GLIDERFET016 = calc_k0_ext(data.vrse(idx),data.temperature(idx),data.salinity(idx),specpH.pH_insitu(24),k2);
%% 3/30/36 GliderFET017_DF434
data = readtable("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\03_26_26_id_17_18_16_6.csv");
data.datetime.TimeZone = "UTC";
data = data(data.gliderfet_id == 17,:);
[spec_pH, spec_Temp_used, specpH] = readSpec("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\spec_ph\FT_k0_spec_ph.txt");
[timediff, imin] = min(abs(data.datetime - specpH.DateTime(24))); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.datetime(imin);
CTD_Sal = data.salinity(imin);
CTD_T = data.temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 5.11835e-04;
idx = find(data.datetime == time_sample);
k0_GLIDERFET017 = calc_k0_ext(data.vrse(idx),data.temperature(idx),data.salinity(idx),specpH.pH_insitu(24),k2);
%% 3/30/36 GliderFET018_DF435
data = readtable("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\03_26_26_id_17_18_16_6.csv");
data.datetime.TimeZone = "UTC";
data = data(data.gliderfet_id == 18,:);
[spec_pH, spec_Temp_used, specpH] = readSpec("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\spec_ph\FT_k0_spec_ph.txt");
[timediff, imin] = min(abs(data.datetime - specpH.DateTime(24))); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.datetime(imin);
CTD_Sal = data.salinity(imin);
CTD_T = data.temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 4.94261e-04;
idx = find(data.datetime == time_sample);
k0_GLIDERFET018 = calc_k0_ext(data.vrse(idx),data.temperature(idx),data.salinity(idx),specpH.pH_insitu(24),k2);
%% 3/30/36 GliderFET006_DF423
data = readtable("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\03_26_26_id_17_18_16_6.csv");
data.datetime.TimeZone = "UTC";
data = data(data.gliderfet_id == 6,:);
[spec_pH, spec_Temp_used, specpH] = readSpec("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\spec_ph\FT_k0_spec_ph.txt");
[timediff, imin] = min(abs(data.datetime - specpH.DateTime(24))); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.datetime(imin);
CTD_Sal = data.salinity(imin);
CTD_T = data.temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);
k2 = 0.000877728;
idx = find(data.datetime == time_sample);
k0_GLIDERFET006 = calc_k0_ext(data.vrse(idx),data.temperature(idx),data.salinity(idx),specpH.pH_insitu(24),k2);
%% Very pH looks good from all sensors
data = readtable("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\03_26_26_id_17_18_16_6.csv");
data.datetime.TimeZone = "UTC";
FETs = [6, 16, 17, 18];
k2 = [0.000877728, 0.000481629, 5.11835e-04, 4.94261e-04];
k0 = [-0.937863853143663,-1.09303417869636,-1.04657736637256,-1.04789420944276];
data.pHin = NaN(height(data),1);
for i = 1:length(FETs)
    idx = find(data.gliderfet_id == FETs(i));
    data.pHin(idx) = calc_pHext(data.vrse(idx),data.temperature(idx),data.salinity(idx),k0(i),k2(i));
end
%% Plot good data
[spec_pH, spec_Temp_used, specpH] = readSpec("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\spec_ph\FT_k0_spec_ph.txt");
[timediff, imin] = min(abs(data.datetime - specpH.DateTime(24))); % first occurance, there will be multiple with same because of the nanos in a string
time_sample = data.datetime(imin);
CTD_Sal = data.salinity(imin);
CTD_T = data.temperature(imin);
trex = CO2SYS(2270, specpH.pH_tot, 1, 3, CTD_Sal, specpH.Temp_Samp, CTD_T, 0, 0, 0, 0, 1, 10, 1);
% Spec pH input for next step
specpH.pH_insitu = trex(:,18);

data2plot = data(37477:37848,:);

data2plot.gliderfet_id = categorical(data2plot.gliderfet_id);

figure;

% Row 1: pH
subplot(2,1,1); hold on;
for i = 1:length(FETs)
    idx = data2plot.gliderfet_id == string(FETs(i));
    plot(data2plot.datetime(idx), data2plot.pHin(idx), '.', 'DisplayName', sprintf('FET %d', FETs(i)));
end
plot(specpH.DateTime(24),specpH.pH_insitu(24),'ro')
xline(specpH.DateTime(24),'red')
legend({'6','16','17','18','Spec pHin'});
ylabel('pH');
title('pH by GliderFET');
grid on;

% Row 2: vrse (normalized to first value)
subplot(2,1,2); hold on;
for i = 1:length(FETs)
    idx = data2plot.gliderfet_id == string(FETs(i));
    vrse = data2plot.vrse(idx);
    plot(data2plot.datetime(idx), vrse - vrse(1), '.', 'DisplayName', sprintf('FET %d', FETs(i)));
end
legend;
xlabel('Datetime (UTC)');
ylabel('\DeltaVrse');
title('\DeltaVrse by GliderFET (normalized to first value)');
grid on;

linkaxes(findall(gcf,'Type','axes'), 'x'); % sync x-axis zoom/pan across both subplots