clear all
close all

%
%% Import data from text file
% Script for importing data from the following text file:
%   filename: \\atlas\ProjectLibrary\ChemSensors\GeneM\GDF pH Sensor\Calibrations\FinalCalsFromChem\GDF15_Calibration_20211227_all data.TXT
% Auto-generated by MATLAB on 05-Jan-2026 12:17:14
%% Set up the Import Options and import the data

opts = delimitedTextImportOptions("NumVariables", 24);

% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = "\t";

% Specify column names and types
opts.VariableNames = ["PHCALIBRATIONFILEGENERATEDON122720211458", "VarName2", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7", "VarName8", "VarName9", "VarName10", "VarName11", "VarName12", "VarName13", "VarName14", "VarName15", "VarName16", "VarName17", "VarName18", "VarName19", "VarName20", "VarName21", "VarName22", "VarName23", "VarName24"];
opts.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];

% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Specify variable properties
opts = setvaropts(opts, ["PHCALIBRATIONFILEGENERATEDON122720211458", "VarName2", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7", "VarName8", "VarName9", "VarName10", "VarName11", "VarName12", "VarName13", "VarName14", "VarName15", "VarName16", "VarName17", "VarName18", "VarName19", "VarName20", "VarName21", "VarName22", "VarName23", "VarName24"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["PHCALIBRATIONFILEGENERATEDON122720211458", "VarName2", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7", "VarName8", "VarName9", "VarName10", "VarName11", "VarName12", "VarName13", "VarName14", "VarName15", "VarName16", "VarName17", "VarName18", "VarName19", "VarName20", "VarName21", "VarName22", "VarName23", "VarName24"], "EmptyFieldRule", "auto");

% Import the data
fileName = "\\atlas\ProjectLibrary\ChemSensors\GeneM\GDF pH Sensor\Calibrations\FinalCalsFromChem\GDF2001_Calibration_20230627.TXT"
allData = readmatrix(fileName, opts)

clear opts

%
%% Get Relevant Parameters

snRow = find(allData == "Durafet SN:");
serNum = str2double(allData(snRow, 2));

dateIndex = find(allData{1} == ':');
calDate = datetime(allData{1}(dateIndex(1)+1:end), 'Format', 'MM/dd/uuuu HH:mm');

startRow = find(contains(allData(:,1), '** PH POLYNOMIAL PRESSURE COEFFICIENTS: **') == 1);
nssr = str2double(allData(startRow + 1, 2));
Rsqrd = str2double(allData(startRow + 2, 2));
incr = 3;
while(~ismissing(allData(startRow + incr, 2)))
    polyCoef(incr-2) = str2double(allData(startRow + incr, 2));
    incr = incr + 1;
end
polyOrder = incr - 4;

fitPressures = linspace(0,2000,50);
fitVrs = polyval(flip(polyCoef), fitPressures);
vrsRange = max(fitVrs) - min(fitVrs);

figure
plot(fitPressures, fitVrs)
plot(fitVrs, fitPressures)
set(gca, 'YDir', 'reverse')

%
%
%% Build array of GDF Data

clear gdfData

gdfData(1).serNum = serNum;
gdfData(1).calDate = calDate;
gdfData(1).nssr = nssr;
gdfData(1).Rsqrd = Rsqrd;
gdfData(1).polyOrder = polyOrder;
gdfData(1).polyCoef = polyCoef;

gdfData(1).vrsRange = vrsRange;
gdfData(1).vrsData = vrsData;
