ehTable = readtable('../NernstTest2026Jul29/NernstCal2026Jul29.csv');
specTable = readtable('../NernstTest2026Jul29/pHSpecData.txt');

%

dtString = ehTable.DateTime;
ehDT = datetime(dtString, 'InputFormat', 'yyyyMMMdd''T''HHmmss.SSS');

indexes = find(ehTable.pH == 0);
ehTable.pH(indexes) = NaN;

plot(ehDT,ehTable.pH)
xlabel('Date and Time');
ylabel('pH');
title('Endress&Hauser pH vs. Time');
grid on;

%

dtOffset = specTable.DateTime(1) - ehDT(1);
dtOffset = dtOffset - minutes(5);
specDT = specTable.DateTime - dtOffset;

hold on
plot(specDT,specTable.pH_tot,'Xg')
hold off

%

phOffset = -0.0935;
phScale = -0.0001 * (ehTable.pH - ehTable.pH(1));
phCorrection = phScale + phOffset;
correctedEHpH = ehTable.pH + phCorrection;

yyaxis left
hold on
plot(ehDT,ehTable.pH)
plot(ehDT, correctedEHpH)
xlabel('Date and Time');
ylabel('pH');
title('Endress&Hauser pH vs. Time');
grid on;

plot(specDT, specTable.pH_tot,'Xg')
legend('Raw E&H pH ', 'Offset and Scaled E&H pH', 'specpH')
hold off

%


for i=1:length(specDT)
    timeDiffs = specDT(i) - ehDT;
    [~,index] = min(abs(timeDiffs));
    pHError(i) = specTable.pH_tot(i) - correctedEHpH(index);
end

hold on
yyaxis right
plot(specDT, pHError, '+r')
ylim([-0.04 0.04]);
hold off

pHError


