clear all
close all

<<<<<<< HEAD
%     if(fileNum == 5)
         plotEachProfile = true;
%     else
%         plotEachProfile = false;
%     end

plotWaterfalls = false;

%startingDir = 'C:\Users\gene\Documents\CPF\Data\CPF003\TankTests\2021Mar16-17\';
startingDir = '\\atlas\ProjectLibrary\CPF\Data\CPF003\Paragon Tests\2021Apr02\2021-4-2T17-3-58\';
=======
startingDir = 'C:\Users\gene\Documents\CPF\Data\CPF003\TankTests\2021Mar24\';
%startingDir = '\\atlas\ProjectLibrary\CPF\Data\CPF003\TankTests\2021Mar16-17\';
>>>>>>> e3e9efe6b1f2e68c04cb92817d9ad2eb41976414
fileList = ls([startingDir,'*.eng']);
numFiles = size(fileList, 1);

%% Variable names
fileFormat = 3  %1 = pre 2021Feb26, 2 = 1 to 2021Mar15, 3 = post 2021Mar15

if(fileFormat == 3)
varNames = {'stx', 'date', 'time', 	'state', 	'comment', 	...
    'pressure', 'bellowsPos', ...
    'pumpVolts', 	'pumpMilliamps', 'pumpCoulombs', 'pumpJoules', 'pumpMotor'...
    'hotelVolts', 	'hotelMilliamps', 'hotelCoulombs', 'hotelJoules', ...
    'CTDTemp', 	'CTDSal', 	...
    'RawPress', 	'LPFPress', 'LPFVel', 	'diffVel', 	'diff2Vel', 'slopeOfLPFPress', 	'CTDSamplePeriod', ...
    'LPS331Press', 	'LPS311Temp', 	'SHT21Humid', 	'SHT21Temp', ...
    'pidSP', 	'pidPV', 	'pidY1',	'pidY2', 'pidI', 	'pid_v', 'pid_u', 'pid_uCPS', 'pid_h',  ...
    'FLBB', 'Topt', 'Tphase', 'Rphase', 'NO3', 'pH_Vrs', 'OCR', 'GPS', 'IridModem', 'MSC'};
varTypes = {'string', 'string', 'string', 'string', 'string', ...
    'double', 'double', ...
    'double', 'double','double', 'double', 'string'...
    'double', 'double','double',  'double', ...
    'string', 'string', ...
    'double', 'double','double', 'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', 'double', 'double', 'double','double', 'double', ...
    'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'};   
elseif(fileFormat == 2)
varNames = {'stx', 'date', 'time', 	'state', 	'comment', 	...
    'pressure', 'bellowsPos', ...
    'pumpVolts', 	'pumpMilliAmps', 	'pumpJoules', ...
    'hotelVolts', 	'hotelMilliAmps', 	'hotelJoules', ...
    'CTDTemp', 	'CTDSal', 	...
    'RawPress', 	'LPFPress', 'LPFVel', 	'diffVel', 	'diff2Vel', 'slopeOfLPFPress', 	'CTDSamplePeriod', ...
    'LPS331Press', 	'LPS311Temp', 	'SHT21Humid', 	'SHT21Temp', ...
    'pidSP', 	'pidPV', 	'pidY1',	'pidY2', 'pidI', 	'pid_v', 'pid_u', 'pid_uCPS', 'pid_h',  ...
    'FLBB', 'Topt', 'Tphase', 'Rphase', 'NO3', 'pH_Vrs', 'OCR', 'GPS', 'IridModem', 'MSC'};
varTypes = {'string', 'string', 'string', 'string', 'string', ...
    'double', 'double', ...
    'double', 'double','double',  ...
    'double', 'double','double',  ...
    'string', 'string', ...
    'double', 'double','double', 'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', 'double', 'double', 'double','double', 'double', ...
    'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'};
else
varNames = {'stx', 'date', 'time', 	'state', 	'comment', 	'pressure', 'bellowsPos', ...
    'pumpVolts', 	'pumpMAmps', 	'pumpJoules', ...
    'hotelVolts', 	'hotelMAmps', 	'hotelJoules', ...
    'CTDTemp', 	'CTDSal', 	'RawPress', 	'LPFPress', 'LPFVel', 	'diffVel', 	'diff2Vel', 'slopeOfLPFPress', 	'CTDSamplePeriod', ...
    'LPS331Press', 	'LPS311Temp', 	'SHT21Humid', 	'SHT21Temp', ...
    'PID_SP', 	'PID_PV', 	'PID_Y1',	'PID_Y2', 'PID_I', 	'PID_v', 'PID_u', 'PID_uCPS', 'PID_h',  ...
    'FLBB', 'Topt', 'Tphase', 'Rphase', 'NO3', 'pH_Vrs', 'OCR', 'GPS', 'IridModem', 'MSC'};
varTypes = {'string', 'string', 'string', 'string', 'string', ...
    'double', 'double', ...
    'double', 'double','double',  ...
    'double', 'double','double',  ...
    'string', 'string', ...
    'double', 'double','double', 'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', 'double', 'double', 'double','double', 'double', ...
    'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'};

    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;
end

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

for fileNum = 1:numFiles
    %% Open and read file
%     if(fileNum == 5)
         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))]);
     
    opts = delimitedTextImportOptions('VariableNames', varNames, 'VariableTypes', varTypes, 'Delimiter', '\t');
    tableData = readtable(fullFileName, opts);
 
    profile(fileNum).fileName = fullFileName;
    
    %% Process date/time
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing date/time ...']);
   
    dt = tableData.date + tableData.time;
    DT = datetime(dt, 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');
    DT.Format='yyyyMMMdd HH:mm:ss';

    %% Process pressure
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing pressure ...']);
    notNaN = ~isnan(tableData.pressure);
    profile(fileNum).pressure = tableData.pressure(notNaN);
    profile(fileNum).pressureDT = DT(notNaN);

    %% Process bellows position
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing bellows position ...']);
    notNaN = ~isnan(tableData.bellowsPos);
    profile(fileNum).bellowsPosition = tableData.bellowsPos(notNaN);
    profile(fileNum).bellowsPositionDT = DT(notNaN);
 
    %% Find glide on
    glideOnFind = strfind(tableData.comment, 'glide on');
    glideOnMsgIndexes = find(isempty(glideOnFind) == 0);
   
    
    %% Find glide off
 
    
    %% Plot pressure and bellows position
    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(3,1,1);
        yyaxis left
        plot(profile(fileNum).bellowsPositionDT, profile(fileNum).bellowsPosition);
        ylabel('Bellows Position (%IB Full)');
        yyaxis right
        plot(profile(fileNum).pressureDT, profile(fileNum).pressure);
        ylabel('Pressure (dBar)');
        hold on;
        
        plotstateTransitions(tableData, DT);
        
%         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 ...']);
    
    if(~ismissing(tableData.pid_uCPS(1)))
        notNaN = ~isnan(tableData.pid_uCPS);
        profile(fileNum).pumpSpeed = tableData.pid_uCPS(notNaN);
        profile(fileNum).pumpSpeedDT = DT(notNaN);
        
        if plotEachProfile
            hAx = subplot(3,1,2);
            yyaxis left
            plot(profile(fileNum).pumpSpeedDT, profile(fileNum).pumpSpeed);
            ylabel('Pump Speed (cps)');
            yyaxis right
            plot(profile(fileNum).pressureDT, profile(fileNum).pressure);
            ylabel('Pressure (dBar)');
            hold on;
            
            plotstateTransitions(tableData, DT);
        end
    end
    
    
    %% Process pressure and humidity
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing internal pressure and humidity ...']);
    notNaN = ~isnan(tableData.LPS331Press);
    profile(fileNum).intPressure = tableData.LPS331Press(notNaN);
    profile(fileNum).intPressureDT = DT(notNaN);
 
    notNaN = ~isnan(tableData.SHT21Humid);
    profile(fileNum).humidity = tableData.SHT21Humid(notNaN);
    profile(fileNum).humidityDT = DT(notNaN);
    
    if plotEachProfile
        hAx = subplot(3,1,3);
        yyaxis left
        plot(profile(fileNum).intPressureDT, profile(fileNum).intPressure);
        ylabel('Internal Pressure (mBar)');
        yyaxis right
        plot(profile(fileNum).humidityDT, profile(fileNum).humidity);
        ylabel('Humidity (%RH)');
        hold on;
        
        plotstateTransitions(tableData, DT);
    end
    
    %% Process pump power
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Calculating Pump Power ...']);

    notNaN = ~isnan(tableData.pumpVolts);
    profile(fileNum).pumpVolts = tableData.pumpVolts(notNaN);
    profile(fileNum).pumpVoltsDT = DT(notNaN);
    
    notNaN = ~isnan(tableData.pumpMilliamps);
    profile(fileNum).pumpAmps = tableData.pumpMilliamps(notNaN) / 1000.0;
    profile(fileNum).pumpAmpsDT = DT(notNaN);

    notNaN = ~isnan(tableData.pumpCoulombs);
    profile(fileNum).pumpCoulombs = tableData.pumpCoulombs(notNaN);
    profile(fileNum).pumpCoulombsDT = DT(notNaN);
   
    notNaN = ~isnan(tableData.pumpJoules);
    profile(fileNum).pumpJoules = tableData.pumpJoules(notNaN);
    profile(fileNum).pumpJoulesDT = DT(notNaN);

    vecLength = min(length(profile(fileNum).pumpAmpsDT), length(profile(fileNum).pumpVoltsDT));
    profile(fileNum).pumpPowerCalc = profile(fileNum).pumpVolts(1:vecLength) .* profile(fileNum).pumpAmps(1:vecLength);
    profile(fileNum).pumpPowerCalcDT = profile(fileNum).pumpVoltsDT(1:vecLength);
 
        
    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);  % Auto-fitted to the figure.
        yyaxis left
        plot(profile(fileNum).pumpVoltsDT, profile(fileNum).pumpVolts);
        ylabel('Pump Volts');
        yyaxis right
        plot(profile(fileNum).pumpAmpsDT, profile(fileNum).pumpAmps);
        ylabel('Pump Amps');
        hold on;
        
        plotstateTransitions(tableData, DT)
    end
    
    fprintf('%s\t', [datestr(now, 'HH:MM:SS ') 'Calculating Energy ...']);
    profile(fileNum).pumpEnergyCalc = profile(fileNum).pumpPowerCalc(1:end-1) .* seconds( profile(fileNum).pumpPowerCalcDT(2:end) - profile(fileNum).pumpPowerCalcDT(1:end-1));
    profile(fileNum).pumpEnergyCalcDT = profile(fileNum).pumpAmpsDT(1:end-1);
    
    profile(fileNum).pumpEnergyCalcSum = cumsum(profile(fileNum).pumpEnergyCalc);
    profile(fileNum).pumpEnergyCalcSumDT = profile(fileNum).pumpEnergyCalcDT;
    
    profile(fileNum).totalPumpEnergy = sum(profile(fileNum).pumpEnergyCalc);
    fprintf('Total Energy this profile = %.0f Joules\n\n', profile(fileNum).totalPumpEnergy);
    
    if plotEachProfile
        hAx = subplot(4,1,2);  % Auto-fitted to the figure.
        yyaxis left
        plot(profile(fileNum).pumpPowerCalcDT, profile(fileNum).pumpPowerCalc);
        ylabel('Pump Power (Watts)');
        yyaxis right
        plot(profile(fileNum).pressureDT, profile(fileNum).pressure);
        ylabel('Pressure (dBar)');
        hold on;
        
        xlimits = xlim;
        text(xlimits(1), 0.95*max(profile(fileNum).pumpPowerCalc), sprintf('Energy = %.0f Joules', profile(fileNum).totalPumpEnergy));
        
        plotstateTransitions(tableData, DT)
    end
    
     %% Process hotel power
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Calculating Hotel Power ...']);

    notNaN = ~isnan(tableData.hotelVolts);
    profile(fileNum).hotelVolts = tableData.hotelVolts(notNaN);
    profile(fileNum).hotelVoltsDT = DT(notNaN);
    
    notNaN = ~isnan(tableData.hotelMilliamps);
    profile(fileNum).hotelAmps = tableData.hotelMilliamps(notNaN) / 1000.0;
    profile(fileNum).hotelAmpsDT = DT(notNaN);
    
    notNaN = ~isnan(tableData.hotelCoulombs);
    profile(fileNum).hotelCoulombs = tableData.hotelCoulombs(notNaN);
    profile(fileNum).hotelCoulombsDT = DT(notNaN);
   
    notNaN = ~isnan(tableData.hotelJoules);
    profile(fileNum).hotelJoules = tableData.hotelJoules(notNaN);
    profile(fileNum).hotelJoulesDT = DT(notNaN);

    vecLength = min(length(profile(fileNum).hotelAmpsDT), length(profile(fileNum).hotelVoltsDT));
    profile(fileNum).hotelPowerCalc = profile(fileNum).hotelVolts(1:vecLength) .* profile(fileNum).hotelAmps(1:vecLength);
    profile(fileNum).hotelPowerCalcDT = profile(fileNum).hotelVoltsDT(1:vecLength);
        
    if plotEachProfile
        hAx = subplot(4,1,3);  % Auto-fitted to the figure.
        yyaxis left
        plot(profile(fileNum).hotelVoltsDT, profile(fileNum).hotelVolts);
        ylabel('Hotel Volts');
        yyaxis right
        plot(profile(fileNum).hotelAmpsDT, profile(fileNum).hotelAmps);
        ylabel('Hotel milliAmps');
        hold on;
        
        plotstateTransitions(tableData, DT)
    end
    
    fprintf('%s\t', [datestr(now, 'HH:MM:SS ') 'Calculating Energy ...']);
    profile(fileNum).hotelEnergyCalc = profile(fileNum).hotelPowerCalc(1:end-1) .* seconds( profile(fileNum).hotelPowerCalcDT(2:end) - profile(fileNum).hotelPowerCalcDT(1:end-1));
    profile(fileNum).hotelEnergyCalcDT = profile(fileNum).hotelPowerCalcDT(2:end);
    
    profile(fileNum).hotelEnergyCalcSum = cumsum(profile(fileNum).hotelEnergyCalc);
    profile(fileNum).hotelEnergyCalcSumDT = profile(fileNum).hotelEnergyCalcDT;
    
    profile(fileNum).totalHotelEnergy = sum(profile(fileNum).hotelEnergyCalc);
    fprintf('Total Energy this profile = %.0f Joules\n\n', profile(fileNum).totalHotelEnergy);
    
    if plotEachProfile
        hAx = subplot(4,1,4);  % Auto-fitted to the figure.
        yyaxis left
        plot(profile(fileNum).hotelPowerCalcDT, profile(fileNum).hotelPowerCalc);
        ylabel('Hotel Power (Watts)');
        yyaxis right
        plot(profile(fileNum).pressureDT, profile(fileNum).pressure);
        ylabel('Pressure (dBar)');
        hold on;
        
        xlimits = xlim;
        text(xlimits(1), 0.95*max(profile(fileNum).hotelPowerCalc), sprintf('Energy = %.0f Joules', profile(fileNum).totalHotelEnergy));
        
        plotstateTransitions(tableData, DT)
    end
    
figure
hold on
yyaxis left
plot(profile(fileNum).pumpJoulesDT, profile(fileNum).pumpJoules)
plot(profile(fileNum).hotelJoulesDT, profile(fileNum).hotelJoules)
plot(profile(fileNum).pumpEnergyCalcSumDT, profile(fileNum).pumpEnergyCalcSum)
plot(profile(fileNum).hotelEnergyCalcSumDT, profile(fileNum).hotelEnergyCalcSum)
yyaxis right
plot(profile(fileNum).pumpCoulombsDT, profile(fileNum).pumpCoulombs)
plot(profile(fileNum).hotelCoulombsDT, profile(fileNum).hotelCoulombs)
legend

hold off
    
    
    %     if(plotEachProfile)
    %         figure
%         plot(profile(fileNum).pumpEnergyCalcSumDT, profile(fileNum).pumpEnergyCalcSum, ...
%             profile(fileNum).hotelEnergyCalcSumDT, profile(fileNum).hotelEnergyCalcSum, ...
%             profile(fileNum).hotelEnergyCalcSumDT, (profile(fileNum).pumpEnergyCalcSum + profile(fileNum).hotelEnergyCalcSum));
%         plotstateTransitions(tableData, DT);
%     end
 
figure
hold on

    %% Process UBX messages
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Process GPS Messages ...']);

    ubxFind = strfind(tableData.GPS, 'UBX');
    ubxMsgIndexes = find(~cellfun('isempty', ubxFind));
    profile(fileNum).ubxMsgs = tableData.GPS(ubxMsgIndexes);
    profile(fileNum).ubxMsgsDT = DT(ubxMsgIndexes);
    for i=1:length(profile(fileNum).ubxMsgs)
        fprintf('%s %s\n', profile(fileNum).ubxMsgsDT(i),  profile(fileNum).ubxMsgs(i));
    end
    fprintf('\n');
    
    %% Process SBDIX messages
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Process Iridium Messages ...']);

%     sbdixFind = strfind(tableData.IridModem, '+SBDIX:');
%     sbdixMsgIndexes = find(~cellfun('isempty', sbdixFind));
%     profile(fileNum).sbdixMsgs = tableData.IridModem(sbdixMsgIndexes);
%     profile(fileNum).sbdixMsgsDT = DT(sbdixMsgIndexes);
    
    %% Find error messages
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Find Error Messages ...']);

    errorFind = strfind(tableData.comment, '[ERROR]');
    errorMsgIndexes = find(~cellfun(@isempty, errorFind));
    profile(fileNum).errorMsg = tableData.comment(errorMsgIndexes);
    profile(fileNum).errorMsgDT = DT(errorMsgIndexes);
      
    fprintf(1, 'Errors in %d files processed: \r', i );
    
    for i = 1:length(errorMsgIndexes)
        fprintf(1, 'Error %d at time %s : %s \r', i, datestr(profile(fileNum).errorMsgDT(i)), profile(fileNum).errorMsg(i));
    end
    
    %% 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
if(plotWaterfalls)
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Plotting waterfalls ...']);
    
    figX = 0.05;
    figXIncr = 0.1;
    figY = 0.3;
    figYIncr = -0.05;
    
    %Depth
    fig = figure('Name','Depth','NumberTitle','off');
    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)
    
    % Bellows Position
    fig = figure('Name','Bellows Pos','NumberTitle','off');
    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)
    
    %Pump Volts
    fig = figure('Name','Pump Volts','NumberTitle','off');
    fig.Units = 'normalized';
    fig.Position = [figX figY 0.6 0.6];
    figX = figX + figXIncr;
    figY = figY + figYIncr;
    title('Pump Bus Volts')
    hold on;
    
    for profileNum = 1:length(profile)
        for i=1:length(profile(profileNum).pumpVoltsDT)
            x(i) = profile(profileNum).pumpVoltsDT(1);
        end
        y = profile(profileNum).pumpVoltsDT - profile(profileNum).pumpVoltsDT(1);
        plot3(x, y, profile(profileNum).pumpVolts)
        clear x;
    end
<<<<<<< HEAD
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Pump Bus Volts')
    grid
    view(3)
    
    %Pump power
    fig = figure('Name','Pump Power','NumberTitle','off');
    fig.Units = 'normalized';
    fig.Position = [figX figY 0.6 0.6];
    figX = figX + figXIncr;
    figY = figY + figYIncr;
    title('Pump Bus Power')
    hold on;
    
    for profileNum = 1:length(profile)
        for i=1:length(profile(profileNum).pumpPowerDT)
            x(i) = profile(profileNum).pumpPowerDT(1);
        end
        y = profile(profileNum).pumpPowerDT - profile(profileNum).pumpPowerDT(1);
        plot3(x, y, profile(profileNum).pumpPower)
        clear x;
    end
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Pump Bus Power (Watts)')
    grid
    view(3)
    
    %Internal Pressure
    fig = figure('Name','Internal Pressure','NumberTitle','off');
    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).intPressureDT)
            x(i) = profile(profileNum).intPressureDT(1);
        end
        y = profile(profileNum).intPressureDT - profile(profileNum).intPressureDT(1);
        plot3(x, y, profile(profileNum).intPressure)
        clear x;
=======
    y = profile(profileNum).pumpVoltsDT - profile(profileNum).pumpVoltsDT(1);
    plot3(x, y, profile(profileNum).pumpVolts)
    clear x;
end
xlabel('Profile Start time');
ylabel('Time from Profile Start')
zlabel('Pump Bus Volts')
grid
view(3)

%Pump power
fig = figure('Name','Pump Power (calculated)','NumberTitle','off');
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Pump Bus Power (calculated)')
hold on;

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

%Internal Pressure
fig = figure('Name','Internal Pressure','NumberTitle','off');
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).intPressureDT)
        x(i) = profile(profileNum).intPressureDT(1);
>>>>>>> e3e9efe6b1f2e68c04cb92817d9ad2eb41976414
    end
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Internal Pressure (mBar)')
    grid
    view(3)
    
    %Humidity
    fig = figure('Name','Humidity','NumberTitle','off');
    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
<<<<<<< HEAD
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Humidity (%RH)')
    grid
    view(3)
    
    %Pump Energy sum
    fig = figure('Name','Pump Cumul. Energy','NumberTitle','off');
    fig.Units = 'normalized';
    fig.Position = [figX figY 0.6 0.6];
    figX = figX + figXIncr;
    figY = figY + figYIncr;
    title('Pump Cumulative Energy (Joules)')
    hold on;
    
    for profileNum = 1:length(profile)
        for i=1:length(profile(profileNum).pumpEnergySumDT)
            x(i) = profile(profileNum).pumpEnergySumDT(1);
        end
        y = profile(profileNum).pumpEnergySumDT - profile(profileNum).pumpEnergySumDT(1);
        plot3(x, y, profile(profileNum).pumpEnergySum)
        clear x;
    end
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Pump Cumulative Energy (Joules')
    grid
    view(3)
    
    %Hotel Energy sum
    fig = figure('Name','Hotel Cumul. Energy','NumberTitle','off');
    fig.Units = 'normalized';
    fig.Position = [figX figY 0.6 0.6];
    figX = figX + figXIncr;
    figY = figY + figYIncr;
    title('Hotel Cumulative Energy (Joules)')
    hold on;
    
    for profileNum = 1:length(profile)
        for i=1:length(profile(profileNum).hotelEnergySumDT)
            x(i) = profile(profileNum).hotelEnergySumDT(1);
        end
        y = profile(profileNum).hotelEnergySumDT - profile(profileNum).hotelEnergySumDT(1);
        plot3(x, y, profile(profileNum).hotelEnergySum)
        clear x;
    end
    xlabel('Profile Start time');
    ylabel('Time from Profile Start')
    zlabel('Hotel Cumulative Energy (Joules')
    grid
    view(3)
=======
    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('Humidity (%RH)')
grid
view(3)

%Pump Energy sum
fig = figure('Name','Pump Cumul. Energy','NumberTitle','off');
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Pump Cumulative Energy (Joules)')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).pumpEnergyCalcSumDT)
        x(i) = profile(profileNum).pumpEnergyCalcSumDT(1);
    end
    y = profile(profileNum).pumpEnergyCalcSumDT - profile(profileNum).pumpEnergyCalcSumDT(1);
    plot3(x, y, profile(profileNum).pumpEnergyCalcSum)
    clear x;
end
xlabel('Profile Start time');
ylabel('Time from Profile Start')
zlabel('Pump Cumulative Energy (Joules')
grid
view(3)

%Hotel Energy sum
fig = figure('Name','Hotel Cumul. Energy','NumberTitle','off');
fig.Units = 'normalized';
fig.Position = [figX figY 0.6 0.6];
figX = figX + figXIncr;
figY = figY + figYIncr;
title('Hotel Cumulative Energy (Joules)')
hold on;

for profileNum = 1:length(profile)
    for i=1:length(profile(profileNum).hotelEnergyCalcSumDT)
        x(i) = profile(profileNum).hotelEnergyCalcSumDT(1);
    end
    y = profile(profileNum).hotelEnergyCalcSumDT - profile(profileNum).hotelEnergyCalcSumDT(1);
    plot3(x, y, profile(profileNum).hotelEnergyCalcSum)
    clear x;
>>>>>>> e3e9efe6b1f2e68c04cb92817d9ad2eb41976414
end


hold off;


%% Function: plotstateTransitions

function plotstateTransitions(tableData, DT)
%figure(pressureFig);
ylimits = ylim;

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

indexes = find(strcmp(tableData.state, 'Surface Ops') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Surface Ops')
end

indexes = find(strcmp(tableData.state, 'AB Dec Buoycy Fast') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY2, 'Dec Buoycy')
end

indexes = find(strcmp(tableData.state, 'Descend')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Descend')
end

indexes = find(strcmp(tableData.state, 'Anchor')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY2, 'Anchor')
end

indexes = find(strcmp(tableData.state, 'Park')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Park')
end

indexes = find(strcmp(tableData.state, 'Start CP Mode')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY2, 'Start CP')
end

indexes = find(strcmp(tableData.state, 'Deanchor')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Deanchor')
end

indexes = find(strcmp(tableData.state, 'Ascend')== 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY2, 'Ascend')
end

indexes = find(strcmp(tableData.state, 'Init Profile') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Init Profile')
end
    
indexes = find(strcmp(tableData.state, 'Surface Ops Set Bellows') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY2, 'SO Set Bel')
end

indexes = find(strcmp(tableData.state, 'Dump CP Data') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Dump CP')
end

indexes = find(strcmp(tableData.state, 'RecoveryMode') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Recovery')
end

indexes = find(strcmp(tableData.state, 'Init Mission') == 1);
if(~isempty(indexes))
    index = indexes(1);
    plot( [DT(index) DT(index)], [ylimits(1) ylimits(2)], 'g-.');
    text( DT(index), textY1, 'Init Mission')
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;

pumpVoltsFig = figure;
pumpVoltsFig.Units = 'normalized';
figX = figX + figXIncr;
figY = figY + figYIncr;
pumpVoltsFig.Position = [figX figY 0.6 0.4];
title('Pump 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









