%% Non-Isolated NanoFET Testing
%
%% Test 1:
% NanoFET_noniso board: A001001 is built into GliderFET001 and deployed in the seawater lab in its own bucket on wall power. Sensor is pulling data every 1 min.
% NanoFET_noniso board: A001002 is a benchtop unit in .01M HCL on wall power. Sensor is pulling data every 1 min.
%

% Seawater lab
% COM3, COM4, COM5
% GFET012, GFET004, GFET001_noniso
sw = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\Wetlab_Sensor_Calibration\NanoFet\K0\06_09_25_GFET001_GFET012_GFET_001_k0_plumbed_inline.csv');
sw.COM = categorical(sw.COM); % Make cat var and rename
sw.COM = renamecats(sw.COM, 'COM3', 'GFET001_noniso'); 
sw.COM = renamecats(sw.COM, 'COM4', 'GFET004'); 
sw.COM = renamecats(sw.COM, 'COM5', 'GFET0012'); 

%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_06102025.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');

%

idx = sw.COM == 'GFET001_noniso';
sw = sw(idx,:);
sw = sw(1:length(lab.COM),:);

%

% Filter data for GFET001_noniso
idx = sw.COM == 'GFET001_noniso';

% Center both datasets by subtracting the mean
sw_centered = sw.Vrse(idx) - mean(sw.Vrse(idx), 'omitnan');
sw_std = sw.Vrse_std(idx);

lab_centered = lab.Vrse - mean(lab.Vrse, 'omitnan');
lab_std = lab.Vrse_std;

% Create figure
figure
tl = tiledlayout(2,2);

nexttile
errorbar(sw_centered, sw_std, 'Color', 'r')
title('SW Lab GFET001\_noniso (Centered)')
ylabel('ΔVrse')
grid on

nexttile
errorbar(lab_centered, lab_std, 'Color', 'b')
title('Lab Reference (Centered)')
ylabel('ΔVrse')
xlabel('Index')
grid on

nexttile(tl,[1 2])
errorbar(sw_centered, sw_std, 'Color', 'r')
title('SW and lab')
hold on;
errorbar(lab_centered, lab_std, 'Color', 'b')
ylabel('ΔVrse')
grid on

%

% Create figure
figure
tl = tiledlayout(1,1);

nexttile
errorbar(sw_centered, sw_std, 'Color', 'r')
title('Sensor: GFET001\_noniso (Centered)')
hold on;
errorbar(lab_centered, lab_std, 'Color', 'b')
ylabel('ΔVrse')
grid on

%
%% Result:
% Benchtop unit has low standard deviation and is stable. Vrse shows a relationship with temperature.
% Seawater lab unit has high standard deviation and is not performing well. Standard deviation is an order of magnitude greater than the benchtop unit. This can be atributed to a bad sensor or electrical noise
%
%% Test 2:
% A001002 is deployed in .01M HCL with the same sensor head. Water bath has been turned on to control temperature. Stir bar has been turned on as well. Sensor is pulling data every 1 minute
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_06182025_stirandbath.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
Index = 1:length(lab.Vrse);
lab.Index = Index';
lab = lab(100:end,:); % warmup

%

tl = tiledlayout(2,2);
title(tl,'A001002 Stir Bar and Bath')
nexttile
errorbar(lab.Vrse - mean(lab.Vrse), lab.Vrse_std)
hold on
plot(runmean(lab.Vrse - mean(lab.Vrse),5),'k')
% title('Benchtop')
ylabel('Vrse - mean')
xlabel('Index')
grid on
nexttile
errorbar(lab.Vk - mean(lab.Vk), lab.Vk_std)
hold on
plot(runmean(lab.Vk - mean(lab.Vk),5),'k')
% title('Benchtop')
ylabel('Vk')
xlabel('Index')
grid on
nexttile
plot(lab.Ik)
hold on
plot(runmean(lab.Ik,5),'k')
% title('Benchtop')
ylabel('Ik')
xlabel('Index')
grid on
nexttile
plot(lab.Ib)
hold on
plot(runmean(lab.Ib,5),'k')
% title('Benchtop')
ylabel('Ib')
xlabel('Index')
grid on

%
%% Test 3:
% A001002 is deployed in .01M HCL with the same sensor head. Water bath has been turned on to control temperature with a stir rod.
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_06192025_stirandbath.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
startTime = datetime(2025,6,24,0,0,0); % start of June 24
endTime = datetime(2025,6,25,0,0,0);   % start of June 25 (exclusive)

idx = lab.Datetime >= startTime & lab.Datetime < endTime;

% Apply filter
lab = lab(idx,:);

%

tl = tiledlayout(2,2);
title(tl,'A001002 Stir Bar and Bath')
nexttile
errorbar(lab.Datetime, lab.Vrse - mean(lab.Vrse), lab.Vrse_std)
hold on
plot(lab.Datetime, runmean(lab.Vrse - mean(lab.Vrse),5),'k')
% title('Benchtop')
ylabel('Vrse - mean')
% xlabel('Index')
grid on
nexttile
errorbar(lab.Datetime, lab.Vk - mean(lab.Vk), lab.Vk_std)
hold on
plot(lab.Datetime, runmean(lab.Vk - mean(lab.Vk),5),'k')
% title('Benchtop')
ylabel('Vk')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ik)
hold on
plot(lab.Datetime, runmean(lab.Ik,5),'k')
% title('Benchtop')
ylabel('Ik')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ib)
hold on
plot(lab.Datetime, runmean(lab.Ib,5),'k')
% title('Benchtop')
ylabel('Ib')
% xlabel('Index')
grid on

%

tl = tiledlayout(2,2);
title(tl,'A001002 Stir Bar and Bath')
nexttile
errorbar(lab.Datetime, lab.Vrse, lab.Vrse_std)
hold on
plot(lab.Datetime, runmean(lab.Vrse,5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vrse [Volts]')
% xlabel('Index')
grid on
legend("Error Bars (STD)","runmean 5 min")
nexttile
errorbar(lab.Datetime, lab.Vk, lab.Vk_std)
hold on
plot(lab.Datetime, runmean(lab.Vk, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vk [Volts]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ik)
hold on
plot(lab.Datetime, runmean(lab.Ik, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ik [Nano Amps]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ib)
hold on
plot(lab.Datetime, runmean(lab.Ib, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ib [Nano Amps]')
% xlabel('Index')
grid on

%
% At 13:03 Luke starts pump priming cycle. This corresponds exactly with the enormous spike in Ik and Vk. This is similar to the response seen in glider deployment 25202901. Vk has not returned to its previous value range in over 24 hours. Ik immediately returned to its previous range. Vrse had to response to this test.
%
% Test 3 repeat!
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_06192025_stirandbath.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
startTime = datetime(2025,6,25,0,0,0); % start of June 24
endTime = datetime(2025,6,26,0,0,0);   % start of June 25 (exclusive)

idx = lab.Datetime >= startTime & lab.Datetime < endTime;

% Apply filter
lab = lab(idx,:);

%

tl = tiledlayout(2,2);
title(tl,'A001002 Stir Bar and Bath')
nexttile
errorbar(lab.Datetime, lab.Vrse, lab.Vrse_std)
hold on
plot(lab.Datetime, runmean(lab.Vrse,5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vrse [Volts]')
% xlabel('Index')
grid on
legend("Error Bars (STD)","runmean 5 min")
nexttile
errorbar(lab.Datetime, lab.Vk, lab.Vk_std)
hold on
plot(lab.Datetime, runmean(lab.Vk, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vk [Volts]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ik)
hold on
plot(lab.Datetime, runmean(lab.Ik, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ik [Nano Amps]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ib)
hold on
plot(lab.Datetime, runmean(lab.Ib, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ib [Nano Amps]')
% xlabel('Index')
grid on

%
% At 11:09, Luke inserts the tubing into the beaker. This corresponds with a negative spike in Ik and a positive spike in Vk. This is exactly the same effect as the previous trial.
% This trial had a 15 minute warmup period before starting the priming cycle.
% The priming cycle starts at 11:24 which has no effect on Vk or Ik.
% The priming cycle ends 500 seconds after 11:24 (~11:32)
% 11:09 is when the tubing is inserted into the beaker and 11:36 is when it is removed. Vk seems to only respond to the tubing, not the pump.
% At 11:32 the pump cycle ends. Ik responds to this but not the start of the cycle (11:24). Ik also responds to the tubing being inserted, but not removed.
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_06252025_stirandbath.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
tl = tiledlayout(2,2);
title(tl,'A001002 Stir Bar and Bath')
nexttile
errorbar(lab.Datetime, lab.Vrse, lab.Vrse_std)
hold on
plot(lab.Datetime, runmean(lab.Vrse,5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vrse [Volts]')
% xlabel('Index')
grid on
legend("Error Bars (STD)","runmean 5 min")
nexttile
errorbar(lab.Datetime, lab.Vk, lab.Vk_std)
hold on
plot(lab.Datetime, runmean(lab.Vk, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Vk [Volts]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ik)
hold on
plot(lab.Datetime, runmean(lab.Ik, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ik [Nano Amps]')
% xlabel('Index')
grid on
nexttile
plot(lab.Datetime, lab.Ib)
hold on
plot(lab.Datetime, runmean(lab.Ib, 5),'k-',MarkerSize=1)
% title('Benchtop')
ylabel('Ib [Nano Amps]')
% xlabel('Index')
grid on

%
%% Test 4
% GliderFET001_noniso has been added in a beaker of sea water
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_07012025_stirandbath_GFET.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
lab.COM = renamecats(lab.COM, 'COM4', 'A001001');
startTime = datetime(2025,7,7,0,0,0); % start of June 26
endTime = datetime(2025,7,7,8,0,0);   % start of June 27 (exclusive)

idx = lab.Datetime >= startTime & lab.Datetime < endTime;

% Apply filter
lab = lab(idx,:);
idx = lab.Vrse < -2;
lab(idx,:) = [];

%

figure
clf
gscatter(lab.Datetime,lab.Vrse,lab.COM)
grid on
legend(Location="northwest")

%

idx_A001001 = lab.COM=="A001001";
idx_A001002 = lab.COM=="A001002";
A001001 = lab.Vrse(idx_A001001);
A001002 = lab.Vrse(idx_A001002);

clf
figure
plot(lab.Datetime(idx_A001001),lab.Vrse(idx_A001001),'.')
% hold on
% plot(lab.Datetime(idx_A001002),lab.Vrse(idx_A001002) - A001002(1),'.')
% grid on
% legend('A001001: SW','A001002: HCL')
ylabel('Vrse (V)')
% ylim([-0.005 0.005])

%

clf
figure
scatter(lab.Datetime(idx_A001001),lab.Vk(idx_A001001),'.')
hold on
scatter(lab.Datetime(idx_A001002),lab.Vk(idx_A001002),'.')
grid on
legend('A001001: SW','A001002: HCL')
ylabel('Vrse (V)')

%

figure
clf
gscatter(lab.Datetime,lab.Vk,lab.COM)
grid on
legend(Location="northwest")

%

idx_A001001 = lab.COM == "A001001";
idx_A001002 = lab.COM == "A001002";
tl = tiledlayout(2,1);
% title(tl,'A001001 GFET001')
nexttile
errorbar(lab.Datetime(idx_A001001), lab.Vrse(idx_A001001), lab.Vrse_std(idx_A001001))
hold on
plot(lab.Datetime(idx_A001001), runmean(lab.Vrse(idx_A001001),5),'k-',MarkerSize=1)
grid on
std_mean = mean(lab.Vrse_std(idx_A001001));
title(['SW, std mean: ', num2str(std_mean)])
mn = mean(lab.Vrse(idx_A001001));
ylim([mn-.003 mn+.003])

nexttile
errorbar(lab.Datetime(idx_A001002), lab.Vrse(idx_A001002), lab.Vrse_std(idx_A001002))
hold on
plot(lab.Datetime(idx_A001002), runmean(lab.Vrse(idx_A001002),5),'k-',MarkerSize=1)
grid on
std_mean = mean(lab.Vrse_std(idx_A001002));
title(['HCL, std mean: ', num2str(std_mean)])
mn = mean(lab.Vrse(idx_A001002));
ylim([mn-.003 mn+.003])
% 
% ylabel('Vrse [Volts]')
% % xlabel('Index')
% grid on
% legend("Error Bars (STD)","runmean 5 min")
% nexttile
% errorbar(lab.Datetime, lab.Vk, lab.Vk_std)
% hold on
% plot(lab.Datetime, runmean(lab.Vk, 5),'k-',MarkerSize=1)
% % title('Benchtop')
% ylabel('Vk [Volts]')
% % xlabel('Index')
% grid on
% nexttile
% plot(lab.Datetime, lab.Ik)
% hold on
% plot(lab.Datetime, runmean(lab.Ik, 5),'k-',MarkerSize=1)
% % title('Benchtop')
% ylabel('Ik [Nano Amps]')
% % xlabel('Index')
% grid on
% nexttile
% plot(lab.Datetime, lab.Ib)
% hold on
% plot(lab.Datetime, runmean(lab.Ib, 5),'k-',MarkerSize=1)
% % title('Benchtop')
% ylabel('Ib [Nano Amps]')
% % xlabel('Index')
% grid on

%

clf
figure
gscatter(lab.Datetime(idx_A001002),lab.Vrse(idx_A001002))
legend('A001002')
grid on;

%
%% Test 5
% Board A001001 is now in HCL
% This data shows that both boards perform well in HCL under stable conditions.
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_07242025_A001001_SBE10343.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001002');
% lab.COM = renamecats(lab.COM, 'COM4', 'A001001');
startTime = datetime(2025,7,24,0,0,0); % start of June 26
endTime = datetime(2025,7,25,0,0,0);   % start of June 27 (exclusive)

idx = lab.Datetime >= startTime & lab.Datetime < endTime;

% Apply filter
lab = lab(idx,:);

%

clf
figure
gscatter(lab.Datetime,lab.Vrse)
legend('A001001')
grid on;

%
%% Test 6
% Board A001001 in GFET paired with DF40x back in sea water. The isfet has been cleaned significantly.
%

% Benchtop G119
lab = readtable('\\atlas.shore.mbari.org\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\nanoFET\noniso_testing\data\BenchTest_07242028_A001001_DF403_A001002_SBE10343.csv');
lab.COM = categorical(lab.COM);
lab.COM = renamecats(lab.COM, 'COM3', 'A001001');
lab.COM = renamecats(lab.COM, 'COM4', 'A001002');
startTime = datetime(2025,7,28,12,0,0); % start of June 26
endTime = datetime(2025,7,29,0,0,0);   % start of June 27 (exclusive)

idx = lab.Datetime >= startTime & lab.Datetime < endTime & lab.COM == 'A001001';

% Apply filter
lab = lab(idx,:);

%

clf
figure
gscatter(lab.Datetime,lab.Vrse)
legend('A001001')
grid on;