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\Data\Performances\GDF2001\AE_GDF2001_122022_Ch F\AE_GDF2001_122022_Ch F.csv
%
% Auto-generated by MATLAB on 03-Jan-2023 13:19:14

%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 30);

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

% Specify column names and types
%opts.VariableNames = ["LabviewDateTime", "ThermistorTemp", "BathTemp", "BathSetPoint", "PressuredBar", "MaxFlow", "FlowRate", "PumpVolume", "CRC", "ID", "DATETIME", "UTCSECS", "CTDDEPTH", "CTDTEMP", "CTDSALINITY", "SAMPLECOUNTER", "RESETCOUNTER", "ERRORCOUNTER", "HOUSINGTEMPERATURE", "HOUSINGHUMIDITY", "INPUTVOLTAGE", "INPUTCURRENT", "FOOBARPHVALUE", "BACKUPBATTERY", "VRSVOLTS", "VRSSTDDEV", "VKVOLTS", "VKSTDDEV", "IKAMPS", "IBAMPS"];
opts.VariableNames = ["dtLV",       "thermTemp", "bathTemp", "bathSP", "pressureDBar", "maxFlow", "flowRate", "cycleNum", "pumpVolume", "CRC", "ID", "dateTime", "utcSecs", "ctdDepth", "ctdTemp", "ctdSalinity", "sampleCounter", "resetCounter", "errorCounter", "housingTemp", "housingHumidity", "inputVoltage", "inputCurrent", "foobarPHValue", "backupBattery", "vrsVolts", "vrsStdDev", "vkVolts", "vkStdDev", "ikAmps", "ibAmps"];
opts.VariableTypes = ["datetime",   "double",   "double",   "double",   "double",       "double", "double",     "double", "double", "string", "categorical", "datetime", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];

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

% Specify variable properties
opts = setvaropts(opts, "CRC", "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["CRC", "ID"], "EmptyFieldRule", "auto");
%opts = setvaropts(opts, "dtLV", "InputFormat", "MM/dd/uuuu hh:mm:ss aa");
%opts = setvaropts(opts, "dateTime", "InputFormat", "MM/dd/uuuu hh:mm:ss aa");

%% Process files
%path = '\\atlas\ProjectLibrary\ChemSensors\GeneM\GDF pH Sensor\Data\Performances\CSVFiles\';
path = '.\';
fullPath = strcat(path, '*.csv');

dataFiles = dir(strcat(path, '*.csv'));
fileDatenums = dataFiles.date;
[sortedTime, i] = sort([dataFiles.datenum]);
sortedDataFiles = dataFiles(i);
figIncr = 0.05;

data = readtable(stract(path, sortedDataFiles(i).name), opts)

%

for i=1:15
    cycle{i}.dt = data{1}.dtLV(find(data{1}.cycleNum == i));
    cycle{i}.pressureDBar = data{1}.pressureDBar(find(data{1}.cycleNum == i));
    cycle{i}.vrsVolts = data{1}.vrsVolts(find(data{1}.cycleNum == i));
    cycle{i}.vrsStdDev = data{1}.vrsStdDev(find(data{1}.cycleNum == i));
    cycle{i}.thermTemp = data{1}.thermTemp(find(data{1}.cycleNum == i));
    cycle{i}.pressureDBar = data{1}.pressureDBar(find(data{1}.cycleNum == i));
    cycle{i}.vkVolts = data{1}.vkVolts(find(data{1}.cycleNum == i));
    cycle{i}.vkStdDev = data{1}.vkStdDev(find(data{1}.cycleNum == i));
    cycle{i}.ikAmps = data{1}.ikAmps(find(data{1}.cycleNum == i));
    cycle{i}.ibAmps = data{1}.ibAmps(find(data{1}.cycleNum == i));
end

figure
hold on;
colorOrder = get(gca, 'ColorOrder');

for i=1:2:14
    x = [datenum(cycle{i}.dt)' datenum(cycle{i+1}.dt)'];
    y = [cycle{i}.pressureDBar' cycle{i+1}.pressureDBar'];
    z = [cycle{i}.vrsVolts' cycle{i+1}.vrsVolts'];
    plot3(x,y,z,'Color', colorOrder((1+rem(i, length(colorOrder))),:));
    %patch([x' nan],[y' nan],[z' nan],[z' nan],'EdgeColor','interp','FaceColor','none')
end
set(gca, 'ydir', 'reverse');
view(3);
datetick('x', 'mmmdd HH:MM')
grid on
xStart = datenum(cycle{1}.dt(1));
xEnd = datenum(cycle{length(cycle)}.dt(end));
xlim([xStart xEnd]);
hold off

%

figure;
hold on
for i=1:15
    hFig = waterfall(cycle{i}.pressureDBar', datenum(cycle{i}.dt(1)), cycle{i}.vrsVolts');
end
set(gca, 'xdir', 'reverse');
set(gca, 'ydir', 'reverse');
view(3);
datetick('y', 'mmmdd HH:MM')
grid on

%

flowRate = data{1}.flowRate;
figure
yyaxis left
plot(flowRate)
flowDir = sign(flowRate);
hold on;
yyaxis right
plot(flowDir)

%

xTemperature = datenum(data{1}.dtLV)';
yTemperature = zeros(size(xTemperature))';
zTemperature = data{1}.thermTemp';

zLimits = zlim;
zSpan = zLimits(2) - zLimits(1);
tSpan = max(zTemperature) - min(zTemperature);
zT = zTemperature / max(zTemperature);
zT = zT * zSpan;
zt = zT + min(zTemperature);
plot3(xTemperature, yTemperature, zT)