close all

maxTemp = 25;
colorBarMultiplier = 10;
colorMapSize = maxTemp * colorBarMultiplier;
colormap(jet(colorMapSize));
cmap = colormap;

x = cvsTable.lvDateTime';
y = cvsTable.Pressure_dBar' ;
z = cvsTable.VRS_VOLTS;

%colorIndex = cast((colorBarMultiplier * dTable.Thermistor_Temp), "uint64");
colorIndex = int32(colorBarMultiplier * cvsTable.Thermistor_Temp);

plot3(x, y, z, 'Color', [.75 .75 .71]);
hold on

scatter3(x, y, z, 5, cmap(colorIndex,:), 'filled');

zLimitIncr = 0.002;
set(gca, 'ydir', 'reverse');
zLimits = zlim;
set(gca, 'ztick', [zLimits(1):zLimitIncr:zLimits(2)]);
view(3);
datetick('x', 'mmmdd HH:MM');
grid on
tickLocations = [0, 0.2, 0.4, 0.6, 0.8, 1.0];
for i=1:length(tickLocations)
    tickLabels{i,:} = num2str((maxTemp * tickLocations(i)), '%3.0f');
end
%colorbar('Ticks', tickLocations, 'TickLabels', {'0 degC', num2str(maxTemp)});
colorbar('Ticks', tickLocations, 'TickLabels', tickLabels);
%title(" All Data From File " + i + ": " + csvFileName, 'Interpreter', 'none')
xlabel('Date Time')
ylabel('Pressure (dBar)')
zlabel('Vrs - Vc - k2T (volts)')
view(3)

%

clear indexes stopIndex startIndex
close all
pThresholdLow = 0.008;
pThresholdHigh = 0.03;

[cleanFlowRate, TFrm, TFoutlier] = rmoutliers(cvsTable.Flow_Rate, "movmean", 15);
plot(cvsTable.lvDateTime, cvsTable.Flow_Rate, cvsTable.lvDateTime(~TFrm), cleanFlowRate);
startIndex(1) = 1;
numLoops = 26;
for i=1:2:numLoops
    indexes = find(cleanFlowRate(startIndex(i):end) > pThresholdLow);
    stopIndex(i) = startIndex(1) + indexes(1) - 2;
    indexes = find(cleanFlowRate(stopIndex(i):end) > pThresholdHigh);
    startIndex(i+1) = stopIndex(i) + indexes(1);
    indexes = find(cleanFlowRate(startIndex(i+1):end) < pThresholdHigh);
    stopIndex(i+1) = startIndex(i+1) + indexes(1) - 3;
    indexes = find(cleanFlowRate(stopIndex(i+1):end) < pThresholdLow);
    startIndex(i+2) = stopIndex(i+1) + indexes(1);
end

figure
plot(dTable.lvDateTime, dTable.VRS_VOLTS)
datetick('x', 'mmmdd HH:MM');

hold on
for i=1:numLoops
    plot(dTable.lvDateTime(startIndex(i):stopIndex(i)), dTable.VRS_VOLTS(startIndex(i):stopIndex(i)), 'o')
end

%

clear indexes stopIndex startIndex
pThresholdLow = 10;
pThresholdHigh = 2060;

p_dBar = cvsTable.Pressure_dBar;

figure
plot(cvsTable.lvDateTime, cvsTable.VRS_VOLTS);
datetick('x', 'mmmdd HH:MM');
figure
hold on

indexOffset = 0;
numLoops = 26;
for i=1:numLoops
    indexes = find(p_dBar > pThresholdLow);
    stopIndex = indexes(1) - 1;
    cycle(i).lowPHold.lvDateTime = cvsTable.lvDateTime(1:stopIndex);
    cycle(i).lowPHold.Vrs = cvsTable.VRS_VOLTS(1:stopIndex);
    cycle(i).lowPHold.Vrs_StdDev = cvsTable.VRS_STDDEV(1:stopIndex);
    cycle(i).lowPHold.Vk = cvsTable.VK_VOLTS(1:stopIndex);
    cycle(i).lowPHold.Vk_StdDev = cvsTable.VK_STDDEV(1:stopIndex);
    cycle(i).lowPHold.Ik = cvsTable.IK_AMPS(1:stopIndex);
    cycle(i).lowPHold.Ib = cvsTable.IB_AMPS(1:stopIndex);
    cycle(i).lowPHold.startIndex = indexOffset + 1;
    cycle(i).lowPHold.stopIndex = stopIndex + indexOffset;
    indexOffset = indexOffset + stopIndex;
    fprintf(1, 'lowPHold.startIndex: %d   lowPHold.stopIndex: %d\n', ...
        cycle(i).lowPHold.startIndex, cycle(i).lowPHold.stopIndex)
    plot(cvsTable.lvDateTime(cycle(i).lowPHold.startIndex:cycle(i).lowPHold.stopIndex), ...
        cvsTable.VRS_VOLTS(cycle(i).lowPHold.startIndex:cycle(i).lowPHold.stopIndex));
    datetick('x', 'mmmdd HH:MM');
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar > pThresholdHigh);
    stopIndex = indexes(1) + 1;
    cycle(i).upPRamp.lvDateTime = cvsTable.lvDateTime(1:stopIndex);
    cycle(i).upPRamp.Vrs = cvsTable.VRS_VOLTS(1:stopIndex);
    cycle(i).upPRamp.Vrs_StdDev = cvsTable.VRS_STDDEV(1:stopIndex);
    cycle(i).upPRamp.Vk = cvsTable.VK_VOLTS(1:stopIndex);
    cycle(i).upPRamp.Vk_StdDev = cvsTable.VK_STDDEV(1:stopIndex);
    cycle(i).upPRamp.Ik = cvsTable.IK_AMPS(1:stopIndex);
    cycle(i).upPRamp.Ib = cvsTable.IB_AMPS(1:stopIndex);
    cycle(i).upPRamp.startIndex = indexOffset + 1;
    cycle(i).upPRamp.stopIndex = stopIndex + indexOffset;
    indexOffset = indexOffset + stopIndex;
    fprintf(1, 'upPRamp.startIndex: %d   upPRamp.stopIndex: %d\n', ...
        cycle(i).upPRamp.startIndex, cycle(i).upPRamp.stopIndex)
    plot(cvsTable.lvDateTime(cycle(i).upPRamp.startIndex:cycle(i).upPRamp.stopIndex), ...
        cvsTable.VRS_VOLTS(cycle(i).upPRamp.startIndex:cycle(i).upPRamp.stopIndex));
    datetick('x', 'mmmdd HH:MM');
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar < pThresholdHigh);
    stopIndex = indexes(1) - 1;
    cycle(i).highPHold.lvDateTime = cvsTable.lvDateTime(1:stopIndex);
    cycle(i).highPHold.Vrs = cvsTable.VRS_VOLTS(1:stopIndex);
    cycle(i).highPHold.Vrs_StdDev = cvsTable.VRS_STDDEV(1:stopIndex);
    cycle(i).highPHold.Vk = cvsTable.VK_VOLTS(1:stopIndex);
    cycle(i).highPHold.Vk_StdDev = cvsTable.VK_STDDEV(1:stopIndex);
    cycle(i).highPHold.Ik = cvsTable.IK_AMPS(1:stopIndex);
    cycle(i).highPHold.Ib = cvsTable.IB_AMPS(1:stopIndex);
    cycle(i).highPHold.startIndex = indexOffset + 1;
    cycle(i).highPHold.stopIndex = stopIndex + indexOffset;
    indexOffset = indexOffset + stopIndex;
    fprintf(1, 'highPHold.startIndex: %d   highPHold.stopIndex: %d\n', ...
        cycle(i).highPHold.startIndex, cycle(i).highPHold.stopIndex)
    plot(cvsTable.lvDateTime(cycle(i).highPHold.startIndex:cycle(i).highPHold.stopIndex), ...
        cvsTable.VRS_VOLTS(cycle(i).highPHold.startIndex:cycle(i).highPHold.stopIndex));
    datetick('x', 'mmmdd HH:MM');
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar < pThresholdLow);
    stopIndex = indexes(1) - 1;
    cycle(i).downPRamp.lvDateTime = cvsTable.lvDateTime(1:stopIndex);
    cycle(i).downPRamp.Vrs = cvsTable.VRS_VOLTS(1:stopIndex);
    cycle(i).downPRamp.Vrs_StdDev = cvsTable.VRS_STDDEV(1:stopIndex);
    cycle(i).downPRamp.Vk = cvsTable.VK_VOLTS(1:stopIndex);
    cycle(i).downPRamp.Vk_StdDev = cvsTable.VK_STDDEV(1:stopIndex);
    cycle(i).downPRamp.Ik = cvsTable.IK_AMPS(1:stopIndex);
    cycle(i).downPRamp.Ib = cvsTable.IB_AMPS(1:stopIndex);
    cycle(i).downPRamp.startIndex = indexOffset + 1;
    cycle(i).downPRamp.stopIndex = stopIndex + indexOffset;
    indexOffset = indexOffset + stopIndex;
    fprintf(1, 'downPRamp.startIndex: %d   downPRamp.stopIndex: %d\n', ...
        cycle(i).downPRamp.startIndex, cycle(i).downPRamp.stopIndex)
    plot(cvsTable.lvDateTime(cycle(i).downPRamp.startIndex:cycle(i).downPRamp.stopIndex), ...
        cvsTable.VRS_VOLTS(cycle(i).downPRamp.startIndex:cycle(i).downPRamp.stopIndex));
    datetick('x', 'mmmdd HH:MM');
    p_dBar = p_dBar(stopIndex+1:end);
end

datetick('x', 'mmmdd HH:MM');

% figure
% plot(cvsTable.lvDateTime, cvsTable.VRS_VOLTS)
% hold on
% for i=1:numLoops
%     %plot(cvsTable.lvDateTime(startIndex(i):stopIndex(i)), cvsTable.VRS_VOLTS(startIndex(i):stopIndex(i)), 'o')
% end