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

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

path = '\\atlas\ProjectLibrary\ChemSensors\GeneM\GDF pH Sensor\Calibrations\FinalCalsFromChem\';
dataFiles = dir(strcat(path, '*.TXT'));
fileDatenums = dataFiles.date;
[sortedTime, i] = sort([dataFiles.datenum]);
sortedDataFileList = dataFiles(i);

%fileIndex = 1;
gdfIndex = 1;
for fileIndex=1:length(dataFiles)
   while(contains(sortedDataFileList(fileIndex).name, 'dec'))
       disp(sprintf('Skipping file number: %.0f %s', fileIndex, sortedDataFileList(fileIndex).name));
       fileIndex = fileIndex + 1;
   end
currentFile = strcat(path, sortedDataFileList(fileIndex).name);
disp(sprintf('Process GDF Number: %0.f  File number: %.0f %s', gdfIndex, fileIndex, currentFile));
allData = readmatrix(currentFile, opts);

%% Get Relevant Parameters

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

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

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

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

% if(abs(gdfData(gdfIndex).vrsRange) > 0.015)
%     figure
%     plot(fitPressures, gdfData(gdfIndex).fitVrs)
%     disp 'Large Vrs Range'
% end

gdfIndex = gdfIndex + 1;

%
%% Repeat for all files

end

%
%% Calc Hysteresis
%



%
%% Plot some data

vrsRangeMean = mean([gdfData(1:end).vrsRange]);
vrsRangeStdDev = std([gdfData(1:end).vrsRange]);

figure
plot([gdfData(1:end).calDate], [gdfData(1:end).vrsRange])
titleString = sprintf('Vrs Range (volts) vs Time All Final Cals\r\n Mean: %f   Std Dev %f', ...
    vrsRangeMean, vrsRangeStdDev);
title(titleString)
xlabel 'Date'
ylabel 'Vrs Range (volts)'
grid

figure
histogram([gdfData(1:end).vrsRange], 10)
title 'Vrs Range (volts) vs Time All Final Cals'
xlabel 'Vrs Range (volts)'
ylabel 'Number'
grid



%
%%