view(2);
yyaxis left
plot(cvsTable.LVDateTime, cvsTable.PressuredBar)
yyaxis right
plot(cvsTable.LVDateTime, cvsTable.ThermistorTemp)
figure
% index = find(cvsTable.VRSVOLTS > -0.003);
% cvsTable.VRSVOLTS(index) = NaN;
plot(cvsTable.LVDateTime, cvsTable.VRSVOLTS)

%

%Generate a 3D plot of Vrs vs time and pressure
close all

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

x = cvsTable.LVDateTime';
y = cvsTable.PressuredBar' ;
z = cvsTable.VRSVOLTS;

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

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)

%

%Separate out he individual ramps and holds

clear indexes stopIndex startIndex
lowPHoldThreshold = 10;
upPRampThreshold = 2060;
highPHoldThreshold = 1900;
downPRampThreshold = 15;

p_dBar = cvsTable.Pressure_dBar;

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

figure
plot(cvsTable.Pressure_dBar, cvsTable.VRS_VOLTS);

figure
hold on

indexOffset = 0;
numLoops = 26;
stopIndex = 0;
for i=1:numLoops
    indexes = find(p_dBar > lowPHoldThreshold);
    startIndex = stopIndex + 1;;
    stopIndex = startIndex + indexes(1) - 2;
 
    cycle(i).lowPHold.lvDateTime = cvsTable.lvDateTime(startIndex:stopIndex);
    cycle(i).lowPHold.Vrs = cvsTable.VRS_VOLTS(startIndex:stopIndex);
    cycle(i).lowPHold.Vrs_StdDev = cvsTable.VRS_STDDEV(startIndex:stopIndex);
    cycle(i).lowPHold.Vk = cvsTable.VK_VOLTS(startIndex:stopIndex);
    cycle(i).lowPHold.Vk_StdDev = cvsTable.VK_STDDEV(startIndex:stopIndex);
    cycle(i).lowPHold.Ik = cvsTable.IK_AMPS(startIndex:stopIndex);
    cycle(i).lowPHold.Ib = cvsTable.IB_AMPS(startIndex:stopIndex);
    cycle(i).lowPHold.VrsCorrected = appK2Data.Vrs_Vc_k2T(startIndex:stopIndex);
    
    cycle(i).lowPHold.startIndex = startIndex;
    cycle(i).lowPHold.stopIndex = stopIndex;
    indexOffset = indexOffset + stopIndex;

    fprintf(1, 'lowPHold.startIndex: %d   lowPHold.stopIndex: %d\n', ...
        cycle(i).lowPHold.startIndex, cycle(i).lowPHold.stopIndex)
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar > upPRampThreshold);
    startIndex = stopIndex + 1;
    stopIndex = startIndex + indexes(1) - 2;
 
    cycle(i).upPRamp.lvDateTime = cvsTable.lvDateTime(startIndex:stopIndex);
    cycle(i).upPRamp.Vrs = cvsTable.VRS_VOLTS(startIndex:stopIndex);
    cycle(i).upPRamp.Vrs_StdDev = cvsTable.VRS_STDDEV(startIndex:stopIndex);
    cycle(i).upPRamp.Vk = cvsTable.VK_VOLTS(startIndex:stopIndex);
    cycle(i).upPRamp.Vk_StdDev = cvsTable.VK_STDDEV(startIndex:stopIndex);
    cycle(i).upPRamp.Ik = cvsTable.IK_AMPS(startIndex:stopIndex);
    cycle(i).upPRamp.Ib = cvsTable.IB_AMPS(startIndex:stopIndex);
    cycle(i).upPRamp.VrsCorrected = appK2Data.Vrs_Vc_k2T(startIndex:stopIndex);
    
    cycle(i).upPRamp.startIndex = startIndex;
    cycle(i).upPRamp.stopIndex = stopIndex;
    indexOffset = indexOffset + stopIndex;

    fprintf(1, 'upPRamp.startIndex: %d   upPRamp.stopIndex: %d\n', ...
        cycle(i).upPRamp.startIndex, cycle(i).upPRamp.stopIndex)
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar < highPHoldThreshold);
    startIndex = stopIndex + 1;
    stopIndex = startIndex + indexes(1) - 2;
 
    cycle(i).highPHold.lvDateTime = cvsTable.lvDateTime(startIndex:stopIndex);
    cycle(i).highPHold.Vrs = cvsTable.VRS_VOLTS(startIndex:stopIndex);
    cycle(i).highPHold.Vrs_StdDev = cvsTable.VRS_STDDEV(startIndex:stopIndex);
    cycle(i).highPHold.Vk = cvsTable.VK_VOLTS(startIndex:stopIndex);
    cycle(i).highPHold.Vk_StdDev = cvsTable.VK_STDDEV(startIndex:stopIndex);
    cycle(i).highPHold.Ik = cvsTable.IK_AMPS(startIndex:stopIndex);
    cycle(i).highPHold.Ib = cvsTable.IB_AMPS(startIndex:stopIndex);
    cycle(i).highPHold.VrsCorrected = appK2Data.Vrs_Vc_k2T(startIndex:stopIndex);
  
    cycle(i).highPHold.startIndex = startIndex;
    cycle(i).highPHold.stopIndex = stopIndex;
    indexOffset = indexOffset + stopIndex;

    fprintf(1, 'highPHold.startIndex: %d   highPHold.stopIndex: %d\n', ...
        cycle(i).highPHold.startIndex, cycle(i).highPHold.stopIndex)
    p_dBar = p_dBar(stopIndex+1:end);

    indexes = find(p_dBar < downPRampThreshold);
    startIndex = stopIndex + 1;
    stopIndex = startIndex + indexes(1) - 2;
 
    cycle(i).downPRamp.lvDateTime = cvsTable.lvDateTime(startIndex:stopIndex);
    cycle(i).downPRamp.Vrs = cvsTable.VRS_VOLTS(startIndex:stopIndex);
    cycle(i).downPRamp.Vrs_StdDev = cvsTable.VRS_STDDEV(startIndex:stopIndex);
    cycle(i).downPRamp.Vk = cvsTable.VK_VOLTS(startIndex:stopIndex);
    cycle(i).downPRamp.Vk_StdDev = cvsTable.VK_STDDEV(startIndex:stopIndex);
    cycle(i).downPRamp.Ik = cvsTable.IK_AMPS(startIndex:stopIndex);
    cycle(i).downPRamp.Ib = cvsTable.IB_AMPS(startIndex:stopIndex);
    cycle(i).downPRamp.VrsCorrected = appK2Data.Vrs_Vc_k2T(startIndex:stopIndex);
    
    cycle(i).downPRamp.startIndex = startIndex;
    cycle(i).downPRamp.stopIndex = stopIndex;
    indexOffset = indexOffset + stopIndex;

    fprintf(1, 'downPRamp.startIndex: %d   downPRamp.stopIndex: %d\n', ...
        cycle(i).downPRamp.startIndex, cycle(i).downPRamp.stopIndex)
  
    plot(cycle(i).lowPHold.lvDateTime, cycle(i).lowPHold.Vrs);
    plot(cycle(i).upPRamp.lvDateTime, cycle(i).upPRamp.Vrs);
    plot(cycle(i).highPHold.lvDateTime, cycle(i).highPHold.Vrs);
    plot(cycle(i).downPRamp.lvDateTime, cycle(i).downPRamp.Vrs);
    datetick('x', 'mmmdd HH:MM');
    p_dBar = p_dBar(stopIndex+1:end);
end

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

%

lowPHoldThreshold = 20;
upRampThreshold = 2000;
highPHoldThreshold = 1950;
downRampThreshold = 20;

press = cvsTable.Pressure_dBar;

startIndex(1) = 1;
for i=1:4:10
    %find low pressure hold indexes
    indexes = find(press(startIndex(i):end) > lowPHoldThreshold);
    stopIndex(i) =  startIndex(i) + indexes(1) - 1;

    %find up pressure ramp indexes
    startIndex(i+1) = stopIndex(i) + 1;
    indexes = find(press(startIndex(i+1):end) > upRampThreshold);
    stopIndex(i+1) = startIndex(i+1) + indexes(1) -1;
    
    %find high pressure hold indexes
    startIndex(i+2) = stopIndex(i+1) + 1;
    indexes = find(press(startIndex(i+2):end) < highPHoldThreshold);
    stopIndex(i+2) = startIndex(i+2) + indexes(1) - 1;
    
    %find down pressure ramp indexes
    startIndex(i+3) = stopIndex(i+2) + 1;
    indexes = find(press(startIndex(i+3):end) > upRampThreshold);
    stopIndex(i+3) = startIndex(i+3) + indexes(1) - 1;

    startIndex(i+4) = stopIndex(i+3) + 1;
end
