% Experiment pumped surface water from the test tank through the flow stream and recorded pH. Then a 2L bag with a known pH (different from TT) was attached into the flow stream. To characterize response time we are looking at the time to reach the a stable state.
%

% Spec data
spec = readtable("20240725_DF409_testtankflow.txt");
TestTankpH20 = spec.pH_tot(4);
AcidifiedSWpH20 = spec.pH_tot(5);
% convert to pHin

%

T1 = readtable("Test1.txt");

%

% [k0, Eo_ext_insitu] = calc_k0_ext(mean([T1.Vrse(20),T1.Vrse(end-20)]), 20.3058, 33.7050, TestTankpH20, -0.00100128);
T1.pH20 = calc_dfet_pHext(T1.Vrse, 20.3058, 33.7050, -1.355); % k0 from deployment

%

figure(1)
clf
tiledlayout(2,1)
nexttile
plot(T1.MM_DD_YYYYHH_MM_SS,T1.Vrse)
title('vrse')
xlim([datetime(2024,7,25,19,11,0), datetime(2024,7,25,19,12,0)])
% ylim([-0.995 -0.88])
nexttile
plot(T1.MM_DD_YYYYHH_MM_SS(1:end-1),diff(T1.Vrse))
title('diff(vrse)')
xlim([datetime(2024,7,25,19,11,0), datetime(2024,7,25,19,12,0)])

%

idx = 600:700;
time = T1.MM_DD_YYYYHH_MM_SS(idx);
vrse = T1.Vrse(idx);
std = T1.Vrse_std(idx);
pre = 25;
post = 75;

figure(2)
clf
tiledlayout(2,1)
nexttile
plot(time,std)
% hold on
% scatter(time(pre),std(pre))
% scatter(time(post),std(post))
title('vrse')
disp(time(post) - time(pre))

%

figure(3)
clf
tiledlayout(2,1)
nexttile
plot(T1.MM_DD_YYYYHH_MM_SS,T1.Vrse_std)
title('vrse std')
xlim([datetime(2024,7,25,19,11,0), datetime(2024,7,25,19,12,0)])
nexttile
plot(T1.MM_DD_YYYYHH_MM_SS(1:end-1),diff(T1.Vrse_std))
title('diff(vrse std)')
xlim([datetime(2024,7,25,19,11,0), datetime(2024,7,25,19,12,0)])

%

T2 = readtable("Test2.txt");

%

figure(2)
plot(T2.MM_DD_YYYYHH_MM_SS,T2.Vrse)