clear all
close all

startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/CPF003/TankTests/2021Feb25/';
fileList = ls([startingDir,'*.eng']);
numFiles = size(fileList, 1);

%% Data column indexes
dateIndex = 2;
timeIndex = 3;
stateIndex = 4;
commentIndex = 5;
pressureIndex = 6;
bellowsPosIndex = 7;
pumpVoltsIndex = 8;
pumpAmpsIndex = 9;
pumpJoulesIndex = 10;
hotelVoltsIndex = 11;
hotelAmpsIndex = 12;
hotelJoulesIndex = 13;
ctdIndex = 15;
rawPressureIndex = 16;
InternalPressureIndex = 23;
IPTemperatureIndex = 20;
HumidityIndex = 21;
HumidityTemperatureIndex = 22;
pumpSpeedIndex = 30;
gpsIndex = 39;
iridiumIndex = 40;

numSatsIndex = 1;
numSBDIXIndex = 1;
profileStartTimeIndex = 1;
glideOnIndex = 1;
glideOffIndex = 1;
errorIndex = 1;
bpLimitErrorIndex = 1;


%% Process files
figX = 0.05;
figXIncr = 0.01;
figY = 0.3;
figYIncr = -0.02;

for fileNum = 1:numFiles
    %% Open and read file
    if fileNum == 3;
        plotEachProfile = true;
    else
        plotEachProfile = false;
    end
    
    fullFileName = [startingDir fileList(fileNum,:)];
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Importing Filename: ' fullFileName 'file number: ' int2str(fileNum) ' of ' int2str(length(fileList))]);
    
    fid = fopen(fullFileName);
    
    headerLine = fgetl(fid);
    header  = textscan(headerLine,'%s','Delimiter','\t');
    header  = header{1,1};
    
    formatString = '';
    for i = 1:length(header)
        formatString = [formatString,' %s'];
    end
    
    cellData = textscan(fid, formatString, 'Delimiter','\t');
    fclose(fid);
    
    %% Process date/time
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing date/time ...']);
    d = char(cellData{dateIndex});
    t = char(cellData{timeIndex});
    dt = [d t];
    dateTime = datetime(dt, 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');
    profileStartTime(profileStartTimeIndex) = dateTime(1);
    profileStartTimeIndex = profileStartTimeIndex + 1;
    
    %% Process pressure
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing pressure ...']);
    a = cellfun(@str2num, cellData{rawPressureIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    pressure = cell2mat(a);
    
    %% Find glide on
    glideOnFind = strfind(cellData{commentIndex}, 'glide on');
    glideOnMsgIndexes = find(cellfun('isempty', glideOnFind) == 0);
    for i = 1:length(glideOnMsgIndexes)
        for j = 0:5
            glideOnDepth(glideOnIndex) = str2double(char(cellData{pressureIndex}(glideOnMsgIndexes(i)-j)));
            if ~isnan(glideOnDepth(glideOnIndex))
                break;
            end
        end
        glideOnTime(glideOnIndex) = dateTime(glideOnMsgIndexes(i));
        glideOnIndex = glideOnIndex + 1;
    end
    
    %% Find glide off
    glideOffFind = strfind(cellData{commentIndex}, 'glide off');
    glideOffMsgIndexes = find(cellfun('isempty', glideOffFind) == 0);
    for i = 1:length(glideOffMsgIndexes)
        for j = 0:5
            glideOffDepth(glideOffIndex) = str2double(char(cellData{pressureIndex}(glideOffMsgIndexes(i)-j)));
            if ~isnan(glideOffDepth(glideOffIndex))
                break;
            end
        end
        glideOffTime(glideOffIndex) = dateTime(glideOffMsgIndexes(i));
        glideOffIndex = glideOffIndex + 1;
    end
    
    %% Process bellows position
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing bellows position ...']);
    a = cellfun(@str2num, cellData{bellowsPosIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    bellowsPosition = cell2mat(a);
    
    %% Plot pressure and bellows position
    pressureDT = dateTime(~isnan(pressure));
    pressure = pressure(~isnan(pressure));
    profile(fileNum).pressure = pressure;
    profile(fileNum).pressureDT = pressureDT;
    
    bellowsPositionDT = dateTime(~isnan(bellowsPosition));
    bellowsPosition = bellowsPosition(~isnan(bellowsPosition));
    profile(fileNum).bellowsPositionDT = bellowsPositionDT;
    profile(fileNum).bellowsPosition = bellowsPosition;
    
    if plotEachProfile
        fig = figure('Name',fullFileName,'NumberTitle','off');
        fig.Units = 'normalized';
        fig.Position = [figX figY 0.6 0.6];
        figX = figX + figXIncr;
        figY = figY + figYIncr;
        
        hAx = subplot(4,1,1);
        yyaxis left
        plot(pressureDT, pressure);
        ylabel('Pressure (dBar)');
        yyaxis right
        plot(bellowsPositionDT, bellowsPosition);
        ylabel('Bellows Position (%IB Full)');
        hold on;
        
        plotStateTransitions(cellData, dateTime);
        
        if length(glideOnMsgIndexes) > 0
            plot(glideOnTime, glideOnDepth, 'gO')
            text(glideOnTime, glideOnDepth, 'glide on');
        end
        glideOnMsgIndexes = [];
        if length(glideOffMsgIndexes) > 0
            plot(glideOffTime, glideOffDepth, 'rX')
            text(glideOffTime, glideOffDepth, 'glide off');
        end
        glideOffMsgIndexes = [];
        hold off;
    end
    
    %% Process pump speed
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing pump speed ...']);
    a = cellfun(@str2num, cellData{pumpSpeedIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    pumpSpeed = cell2mat(a);
    
    pumpSpeedDT = dateTime(~isnan(pumpSpeed));
    pumpSpeed = pumpSpeed(~isnan(pumpSpeed));
    profile(fileNum).pumpSpeedDT = pumpSpeedDT;
    profile(fileNum).pumpSpeed = pumpSpeed;
    
    if plotEachProfile
        hAx = subplot(4,1,2);
        yyaxis left
        plot(pressureDT, pressure);
        ylabel('Pressure (dBar)');
        yyaxis right
        plot(pumpSpeedDT, pumpSpeed);
        ylabel('Pump Speed (cps)');
        hold on;
        
        plotStateTransitions(cellData, dateTime);
    end
        %% Process pressure and humidity
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing internal pressure and humidity ...']);
    a = cellfun(@str2num, cellData{InternalPressureIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    internalPressure = cell2mat(a);
    
    internalPressureDT = dateTime(~isnan(internalPressure));
    internalPressure = internalPressure(~isnan(internalPressure));
    profile(fileNum).internalPressureDT = internalPressureDT;
    profile(fileNum).internalPressure = internalPressure;
    
    a = cellfun(@str2num, cellData{HumidityIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    humidity = cell2mat(a);
    
    humidityDT = dateTime(~isnan(humidity));
    humidity = humidity(~isnan(humidity));
    profile(fileNum).humidityDT = humidityDT;
    profile(fileNum).humidity = humidity;
    
    if plotEachProfile
        hAx = subplot(5,1,2);
        yyaxis left
        plot(pressureDT, pressure);
        ylabel('Pressure (dBar)');
        yyaxis right
        plot(internalPressureDT, internalPressure);
        ylabel('Internal Pressure (mBar)');
        hold on;
        
        plotStateTransitions(cellData, dateTime);
    end
    
    %% Process power
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Calculating Power ...']);
    clear a
    a = cellfun(@str2num, cellData{pumpVoltsIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    batteryVolts = cell2mat(a);
    
    clear a
    a = cellfun(@str2num, cellData{pumpAmpsIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    batteryAmps = cell2mat(a) / 1000.0;
    power = batteryVolts .* batteryAmps;
    
    batteryVoltsDT = dateTime(~isnan(batteryVolts));
    batteryVolts = batteryVolts(~isnan(batteryVolts));
    batteryAmpsDT = dateTime(~isnan(batteryAmps));
    batteryAmps = batteryAmps(~isnan(batteryAmps));

    vecLength = min(length(batteryAmpsDT), length(batteryVoltsDT));
    batteryPowerDT = batteryVoltsDT(1:vecLength);
    batteryPower = batteryAmps(1:vecLength) .* batteryVolts(1:vecLength);
    
    profile(fileNum).fileName = fullFileName;
    profile(fileNum).batteryVoltsDT = batteryVoltsDT;
    profile(fileNum).batteryVolts = batteryVolts;
    profile(fileNum).batteryAmpsDT = batteryAmpsDT;
    profile(fileNum).batteryAmps = batteryAmps;
    profile(fileNum).batteryPowerDT = batteryPowerDT;
    profile(fileNum).batteryPower = batteryPower;
    
    
    if plotEachProfile
        hAx = subplot(4,1,3);  % Auto-fitted to the figure.
        yyaxis left
        plot(batteryVoltsDT, batteryVolts);
        ylabel('Battery Volts');
        yyaxis right
        plot(batteryAmpsDT, batteryAmps);
        ylabel('Battery Amps');
        hold on;
        
        plotStateTransitions(cellData, dateTime)
    end
    
    fprintf('%s\t', [datestr(now, 'HH:MM:SS ') 'Calculating Energy ...']);
    powerValuesIndex = ~isnan(power);
    tempPower = power(powerValuesIndex);
    tempDateTime = dateTime(powerValuesIndex);
    energy = tempPower(1:end-1) .* seconds((tempDateTime(2:end) - tempDateTime(1:end-1)));
    
    energySum = cumsum(energy);
    energySum(length(energySum) + 1) = energySum(end);
    
    totalEnergy(fileNum) = sum(energy);
    fprintf('Total Energy this profile = %.0f Joules\n\n', totalEnergy(fileNum));
    
    
    if plotEachProfile
        hAx = subplot(4,1,4);  % Auto-fitted to the figure.
        yyaxis left
        plot(pressureDT, pressure);
        ylabel('Pressure (dBar)');
        yyaxis right
        plot(batteryPowerDT, batteryPower);
        ylabel('Battery Power (Watts)');
        hold on;
        
        text(batteryPowerDT(1), 0.95*max(batteryPower), sprintf('Energy = %.0f Joules', totalEnergy(fileNum)));
        
        plotStateTransitions(cellData, dateTime)
    end
    
    %% Process UBX messages
    ubxFind = strfind(cellData{gpsIndex}, 'UBX');
    ubxMsgIndexes = find(cellfun('isempty', ubxFind) == 0);
    try
        for i = 1:length(ubxMsgIndexes)
            ubxMsg = char(cellData{gpsIndex}(ubxMsgIndexes(i)));
            ubxSplit = strsplit(ubxMsg, ',');
            numSats(numSatsIndex) = str2double(char(ubxSplit(18)));
            numSatsTime(numSatsIndex) = dateTime(ubxMsgIndexes(i));
            numSatsIndex = numSatsIndex + 1;
        end
    catch me
        fprintf(1, 'Error parsing UBX message\n');
    end
    
    %% Process SBDIX messages
    sbdixFind = strfind(cellData{iridiumIndex}, '+SBDIX:');
    sbdixMsgIndexes = find(cellfun('isempty', sbdixFind) == 0);
    for i = 1:length(sbdixMsgIndexes)
        sbdixMsg = char(cellData{iridiumIndex}(sbdixMsgIndexes(i)));
        sbdixSplit = strsplit(sbdixMsg, ',');
        numSBDIX(numSBDIXIndex) = i;
        numSBDIXTime(numSBDIXIndex) = dateTime(sbdixMsgIndexes(i));
        numSBDIXIndex = numSBDIXIndex + 1;
    end
    
    %% Find error messages
    errorFind = strfind(cellData{commentIndex}, '*Error*');
    errorMsgIndexes = find(cellfun('isempty', errorFind) == 0);
    for i = 1:length(errorMsgIndexes)
        errorMsg(errorIndex) = cellData{commentIndex}(errorMsgIndexes(i));
        errorMsgTime(errorIndex) = dateTime(errorMsgIndexes(i));
        errorIndex = errorIndex + 1;
    end
    
    bpLimitErrorFind = strfind(cellData{commentIndex}, 'exceeds limits');
    bpLimitErrorMsgIndexes = find(cellfun('isempty', bpLimitErrorFind) == 0);
    for i = 1:length(bpLimitErrorMsgIndexes)
        bpLimitErrorMsg(bpLimitErrorIndex) = cellData{commentIndex}(bpLimitErrorMsgIndexes(i));
        bpLimitErrorMsgTime(bpLimitErrorIndex) = dateTime(bpLimitErrorMsgIndexes(i));
        bpLimitErrorIndex = bpLimitErrorIndex + 1;
    end
    
    fprintf(1, 'Summary of errors in %d files processed: \r\n', i );
    
    for i = 1:errorIndex-1
        fprintf(1, 'Error %d at time %s : %s \r\n', i, datestr(errorMsgTime(i)), errorMsg{i});
    end
    
    fprintf(1, '\r\nTotal number of Bellow Position Limit Errors = %d \r\n', bpLimitErrorIndex - 1);
    
    
    
    %% Other stuff
    %         figure(numSatsFig);
    %         clf;
    %         try
    %             plot(numSatsTime, numSats, '+-');
    %             title('Number of GPS Satellites vs Time')
    %         catch me
    %             title('Number of GPS Satellites not processed');
    %         end
    %
    %         figure(numIridiumTrysFig);
    %         clf;
    %         try
    %             plot(numSBDIXTime, numSBDIX, '+-');
    %             title('Number of SBDIX Attempts vs Time')
    %         catch me
    %             title('Number of SBDIX Attempts not processed');
    %         end
    
    %        fprintf(1, 'Enter 2 points on descend plot and 2 points on ascend plots to calculate platform velocity');
    %        [x y] = ginput(4)
    %
    %        dt = 24 * 3600 * datenum(pressureDT(round(x(1)):round(x(2))));
    %        dt = dt - dt(1);
    %        [p, s] = polyfit(dt, pressure(round(x(1)):round(x(2))), 1);
    %        fprintf(1, 'platform velocity (slope) between first pair of points = %f    ', p);
    %        dt = 24 * 3600 * datenum(pressureDT(round(x(3)):round(x(4))));
    %        dt = dt - dt(1);
    %        [p, s] = polyfit(dt, pressure(round(x(3)):round(x(4))), 1);
    %        fprintf(1,'\r\n');
    %        fprintf(1, 'platform velocity (slope) between second pair of points = %f  ', p);
    
    
end

%% Waterfall plots
fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Plotting waterfalls ...']);

figX = 0.05;
figXIncr = 0.1;
figY = 0.3;
figYIncr = -0.05;

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Pressure')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).pressureDT)
        x(i) = profile(profileNum).pressureDT(1);
    end
    y = profile(profileNum).pressureDT - profile(profileNum).pressureDT(1);
    plot3(x, y, profile(profileNum).pressure)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Depth (dBar)')
grid
view(3)

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Bellows Position')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).bellowsPositionDT)
        x(i) = profile(profileNum).bellowsPositionDT(1);
    end
    y = profile(profileNum).bellowsPositionDT - profile(profileNum).bellowsPositionDT(1);
    plot3(x, y, profile(profileNum).bellowsPosition)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Bellows Position (IB % Full')
grid
view(3)

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Battery Bus Volts')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).batteryVoltsDT)
        x(i) = profile(profileNum).batteryVoltsDT(1);
    end
    y = profile(profileNum).batteryVoltsDT - profile(profileNum).batteryVoltsDT(1);
    plot3(x, y, profile(profileNum).batteryVolts)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Battery Bus Volts')
grid
view(3)

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Battery Bus Power')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).batteryPowerDT)
        x(i) = profile(profileNum).batteryPowerDT(1);
    end
    y = profile(profileNum).batteryPowerDT - profile(profileNum).batteryPowerDT(1);
    plot3(x, y, profile(profileNum).batteryPower)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Battery Bus Power (Watts)')
grid
view(3)

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Internal Pressure (mBar)')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).internalPressureDT)
        x(i) = profile(profileNum).internalPressureDT(1);
    end
    y = profile(profileNum).internalPressureDT - profile(profileNum).internalPressureDT(1);
    plot3(x, y, profile(profileNum).internalPressure)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Internal Pressure (mBar)')
grid
view(3)

fig = figure;
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Internal Humidity (%RH)')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).humidityDT)
        x(i) = profile(profileNum).humidityDT(1);
    end
    y = profile(profileNum).humidityDT - profile(profileNum).humidityDT(1);
    plot3(x, y, profile(profileNum).humidity)
    clear x;
end
xlabel('Profile Start Time');
ylabel('Time from Profile Start')
zlabel('Internal Pressure (mBar)')
grid
view(3)


%% Function: plotStateTransitions

function plotStateTransitions(cellData, dateTime)
%figure(pressureFig);
ylimits = ylim

textY1 = ylimits(1) + 0.1 * (ylimits(2) - ylimits(1));
textY2 = ylimits(1) + 0.9 * (ylimits(2) - ylimits(1));

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Surface Ops')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( dateTime(index), textY1, 'Surface Ops')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'AB Dec Buoycy Fast')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY1, 'Dec Buoycy')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Descend')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( dateTime(index), textY2, 'Descend')
end
ylim(ylimits);

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Anchor')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY1, 'Anchor')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Park')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY1, 'Anchor')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Start CP Mode')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( dateTime(index), textY2, 'Start CP Mode')
end
indexes = find(cellfun('isempty', strfind(cellData{4}, 'DeAnchor')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY1, 'DeAnchor')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Ascend')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( dateTime(index), textY2, 'Ascend')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Init Profile')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY2, 'Init Profile')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Surface Ops Set Bellows')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY2, 'SO Set Bel.')
end

indexes = find(cellfun('isempty', strfind(cellData{4}, 'Dump CP Data')) == 0);
if( ~isempty(indexes))
    index = indexes(1);
    plot( [dateTime(index) dateTime(index)], [ylimits(1) ylimits(2)], 'g:');
    text( dateTime(index), textY2, 'Dump CP Data')
end

end

function createFigure(fileName, figNum)
figX = 0.05;
figXIncr = 0.05;
figY = 0.6;
figYIncr = -0.05;

pressureFig = figure;
pressureFig.Units = 'normalized';
pressureFig.Position = [figX figY 0.6 0.4];
title('Pressure & Bellows Position vs Time');
hold on;
grid on;

batteryVoltsFig = figure;
batteryVoltsFig.Units = 'normalized';
figX = figX + figXIncr;
figY = figY + figYIncr;
batteryVoltsFig.Position = [figX figY 0.6 0.4];
title('Battery Voltage & Current vs Time');
hold on;
grid on;

pumpSpeedFig = figure;
pumpSpeedFig.Units = 'normalized';
figX = figX + figXIncr;
figY = figY + figYIncr;
pumpSpeedFig.Position = [figX figY 0.6 0.4];
title('Pressure & Pump Speed vs Time');
hold on;
grid on;

pressureOnlyFig = figure;
pressureOnlyFig.Units = 'normalized';
figX = figX + figXIncr;
figY = figY + figYIncr;
pressureOnlyFig.Position = [figX figY 0.6 0.4];
title('Pressure & Pump Speed vs Time');
hold on;
grid on;
end









