clear all
close all

plotWaterfalls = false;
plotProfiles = false;

global MilliVoltsScaleFactor NanoJoulesScaleFactor AmpsScaleFactor WattsScaleFactor SecondsScaleFactor CoulombsScaleFactor
MilliVoltsScaleFactor = 1.0;
AmpsScaleFactor = 0.001;
WattsScaleFactor = 1e-3;
SecondsScaleFactor = 0.001;
CoulombsScaleFactor =1e-6;
NanoJoulesScaleFactor = 1e-9;

%startingDir = '\\atlas\ProjectLibrary\CPF\Data\AnalysisScripts\2022-4-25T16-25-48\';
%startingDir = '\\atlas\ProjectLibrary\CPF\CPF003\Data\TankTests\2023-8-23T17-2-13\';
startingDir = '\\atlas\ProjectLibrary\CPF\CPF003\Data\TankTests\2023-11-2T23-24-9\';

totalHotelEnergyTransit = 0;
totalPumpEnergyTransit = 0;

fileList = ls([startingDir,'*.eng']);
numFiles = size(fileList,1);

%This is the theoretical energy available based on IE data sheet
energyAvailable = 1600000; % NanoJoules = 6 * 28.8 * MilliVolts * 2.6 * amp*hrs
%This is the calculated energy available based on 97 profile CPF003 tank test 2021-9-3T18-48-26
%energyAvailable = 1389736;

global tableData profile fileNum
global glideOnMsgIndexes glideOffMsgIndexes

skipFiles = [];

%% Import data from text file
% Script for importing data from the following text file:
%
%    filename: \\atlas\projectlibrary\CPF\CPF003\Data\TankTests\2023-11-2T23-24-9\CPF003.001v2.eng
%
% Auto-generated by MATLAB on 29-Nov-2023 13:23:14

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

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

% Specify column names and types
opts.VariableNames = ["STX", "Date", "Time", "State", "Comment", "Comment2", "Pressure", "BelPos", "PumpMilliamps", "PumpMilliVolts", "PumpMicroWatts", "PumpMilliSeconds", "PumpUCoulombs", "PumpNanoJoules", "HotelMilliamps", "HotelMilliVolts", "HotelMicroWatts", "HotelMilliSeconds", "HotelUCoulombs", "HotelNanoJoules", "pumpMotorMsg", "CTDTemp", "CTDSal", "RawPress", "LPFPress", "LPFVel", "DiffVel", "Diff2Vel", "SlopeLPFPress", "CTDSamplePeriod", "LPS331Press", "LPS331Temp", "SHT21Humid", "SHT21Temp", "pidSP", "pidPV", "pidY1", "pidY2", "pidI", "pid_v", "pid_u", "pid_uCPS", "pid_h", "FLBB", "Topt", "Tphase", "Rphase", "NO3", "pHVrs", "OCRtext", "OCRCh1", "OCRCh2", "OCRCh3", "OCRCH4", "GPS", "IridModem", "MSC"];
opts.VariableTypes = ["categorical", "datetime", "datetime", "categorical", "string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "string", "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", "double", "string", "string", "double", "double", "double", "double", "string", "string", "string"];

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

% Specify variable properties
opts = setvaropts(opts, ["Comment", "Comment2", "pumpMotorMsg", "CTDTemp", "CTDSal", "FLBB", "Topt", "Tphase", "Rphase", "pHVrs", "OCRtext", "GPS", "IridModem", "MSC"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["STX", "State", "Comment", "Comment2", "pumpMotorMsg", "CTDTemp", "CTDSal", "FLBB", "Topt", "Tphase", "Rphase", "pHVrs", "OCRtext", "GPS", "IridModem", "MSC"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "Date", "InputFormat", "dd-MMM-yy");
opts = setvaropts(opts, "Time", "InputFormat", "HH:mm:ss.SSS");
opts = setvaropts(opts, "NO3", "TrimNonNumeric", true);
opts = setvaropts(opts, "NO3", "ThousandsSeparator", ",");

% Import the data
%CPF003 = readtable("\\atlas\projectlibrary\CPF\CPF003\Data\TankTests\2023-11-2T23-24-9\CPF003.001v2.eng", opts);

%for fileNum = 1:numFiles
for fileNum = 1:1
    if (ismember(fileNum,skipFiles) == 0)
        fprintf('\nProcessing file number: %d\n', fileNum);
        fullFileName = [startingDir fileList(fileNum,:)];
        fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Importing Filename: ' fullFileName 'file number: ' int2str(fileNum) ' of ' int2str(length(fileList))]);

        tableData = readtable("\\atlas\projectlibrary\CPF\CPF003\Data\TankTests\2023-11-2T23-24-9\CPF003.001v2.eng", opts);

        profile(fileNum).fileName = fileList(fileNum,:);
        profile(fileNum).fullFileName = fullFileName;

        processDateTime();
        processPressure();
        processPVs();
        processBellowPosition();
        findGlide();
        processHotelEnergy();
        checkHotelEnergy();
        calculateHotelEnergy();
        processPumpEnergy();
        calculatePumpEnergy();

        endIndex = min(length(profile(fileNum).hotelNanoJoulesCalc), length(profile(fileNum).pumpNanoJoulesCalc));
        profile(fileNum).totalNanoJoulesCalcSum = profile(fileNum).hotelNanoJoulesCalcSum(1:endIndex) + profile(fileNum).pumpNanoJoulesCalcSum(1:endIndex);
        profile(fileNum).totalNanoJoulesCalcSumDT = profile(fileNum).hotelNanoJoulesCalcSumDT(1:endIndex);

        % EMTotalNanoJoules is the NanoJoules value read directly from the LTC2946 energy
        % monitor and gets reset to zero every time the EM gets powered down
        endIndex = min(length(profile(fileNum).hotelNanoJoules), length(profile(fileNum).pumpNanoJoules));
        profile(fileNum).EMTotalNanoJoules = profile(fileNum).hotelNanoJoules(1:endIndex) + profile(fileNum).pumpNanoJoules(1:endIndex);
        profile(fileNum).EMTotalNanoJoulesDT = profile(fileNum).hotelNanoJoulesDT(1:endIndex);
        
        % profileTotalNanoJoules is the NanoJoules value as if the EM did not get reset
        % to zero during power down
        profile(fileNum).profileTotalNanoJoules = profile(fileNum).EMTotalNanoJoules;
        profile(fileNum).profileTotalNanoJoulesDT = profile(fileNum).EMTotalNanoJoulesDT;        
        NanoJoulesDiff = diff(profile(fileNum).profileTotalNanoJoules);
        indexes = find(NanoJoulesDiff < 0);
        for i = 1:length(indexes)
            profile(fileNum).profileTotalNanoJoules(indexes(i)+1:end) = profile(fileNum).EMTotalNanoJoules(indexes(i)) + ...
                profile(fileNum).profileTotalNanoJoules(indexes(i)+1:end);
        end
        figure
        hold on
        yyaxis right
        plot(profile(fileNum).EMTotalNanoJoulesDT, profile(fileNum).EMTotalNanoJoules)
        ylabel('EM Sensor Total NanoJoules')
        yyaxis left
        plot(profile(fileNum).profileTotalNanoJoulesDT, profile(fileNum).profileTotalNanoJoules)
        ylabel('Profile Sensor Total NanoJoules')
    
%         figure
%         plot(profile(fileNum).totalNanoJoulesCalcSumDT, profile(fileNum).totalNanoJoulesCalcSum)
%         ylabel('Caclculated Total NanoJoules')
    end
end
% 
% dx = 0.0;
% dy = 0.2;
% left = 0.1;
% bottom = 0.1;
% width = .5;
% height = .2;
% 
% figure('Units', 'normalized', 'Position', [left bottom width height]);
% hold on
% for i=1:length(profile)
%     plot(profile(i).hotelMilliVoltsDT, profile(i).hotelMilliVolts, 'b', profile(i).pumpMilliVoltsDT, profile(i).pumpMilliVolts + 1, 'r')
% end
% title('MilliVolts vs Time')
% legend('hotel', 'pump + 1 volt');
% 
% left = left+dx;
% bottom = bottom + dy;
% figure('Units', 'normalized', 'Position', [left bottom width height]);
% hold on
% for i=1:length(profile)
%     plot(profile(i).hotelAmpsDT, profile(i).hotelAmps, 'b', profile(i).pumpAmpsDT, profile(i).pumpAmps, 'r')
% end
% title('Amps vs Time')
% legend('hotel', 'pump');
% 
% left = left+dx;
% bottom = bottom + dy;
% figure('Units', 'normalized', 'Position', [left bottom width height]);
% hold on
% for i=1:length(profile)
%     plot(profile(i).hotelWattsDT, profile(i).hotelWatts, 'b', profile(i).pumpWattsDT, profile(i).pumpWatts, 'r')
% end
% title('Watts vs Time')
% legend('hotel', 'pump');
% 
% left = left+dx;
% bottom = bottom + dy;
% figure('Units', 'normalized', 'Position', [left bottom width height]);
% hold on
% for i=1:length(profile)
%     plot(profile(i).hotelNanoJoulesDT, profile(i).hotelNanoJoules, 'b', profile(i).pumpNanoJoulesDT, profile(i).pumpNanoJoules, 'r')
% end
% title('NanoJoules vs Time')
% legend('hotel', 'pump');

function processPVs
global tableData profile fileNum

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

notNaN = ~isnan(tableData.LPFVel);
profile(fileNum).LPFVel = tableData.LPFVel(notNaN);
profile(fileNum).LPFVelDT = profile(fileNum).DT(notNaN);

notNaN = ~isnan(tableData.DiffVel);
profile(fileNum).diffVel = tableData.DiffVel(notNaN);
profile(fileNum).diffVelDT = profile(fileNum).DT(notNaN);

notNaN = ~isnan(tableData.Diff2Vel);
profile(fileNum).diff2Vel = tableData.Diff2Vel(notNaN);
profile(fileNum).diff2VelDT = profile(fileNum).DT(notNaN);

notNaN = ~isnan(tableData.SlopeLPFPress);
profile(fileNum).slopeOfLPFPress = tableData.SlopeLPFPress(notNaN);
profile(fileNum).slopeOfLPFPressDT = profile(fileNum).DT(notNaN);
end

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

notNaN = ~isnan(tableData.LPFPress);
profile(fileNum).LPFPressure = tableData.LPFPress(notNaN);
profile(fileNum).LPFPressureDT = profile(fileNum).DT(notNaN);
end

function processDateTime()
global tableData profile fileNum

fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing date/time ...']);

notNaDate = ~isnat(tableData.Date);
notNaTime = ~isnat(tableData.Time);
validIndexes = min(notNaDate, notNaTime);
dt = tableData.Date(validIndexes) + timeofday(tableData.Time(validIndexes));

profile(fileNum).DT = datetime(dt, 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');
profile(fileNum).State = tableData.State;

%     notNaN = ~isnan(tableData.HotelMilliSeconds);
%     profile(fileNum).hotelSeconds = tableData.HotelMilliSeconds(notNaN) / 1.0e3;
%     profile(fileNum).hotelSecondsDT = profile(fileNum).DT(notNaN);
%     if(profile(fileNum).hotelMilliVoltsDT(1) < datetime(2000, 1, 1))
%         profile(fileNum).hotelMilliVoltsDT(1) = profile(fileNum).hotelMilliVoltsDT(2) - duration(0, 0, 1);
%end

%     notNaN = ~isnan(tableData.PumpMilliSeconds);
%     profile(fileNum).pumpSeconds = tableData.PumpMilliSeconds(notNaN) / 1.0e3;
%     profile(fileNum).pumpSecondsDT = profile(fileNum).DT(notNaN);
%     if(profile(fileNum).hotelMilliVoltsDT(1) < datetime(2000, 1, 1))
%         profile(fileNum).hotelMilliVoltsDT(1) = profile(fileNum).hotelMilliVoltsDT(2) - duration(0, 0, 1);
%    end

initProfileIndexes = find(strcmp(char(tableData.State), 'Init Profile'));
if ~isempty(initProfileIndexes)
    profile(fileNum).initProfileStartTime = profile(fileNum).DT(initProfileIndexes(1));
else
    profile(fileNum).initProfileStartTime = profile(fileNum).DT(1);
end
end

function processPID
global tableData profile fileNum
fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing PID ...']);
notNaN = ~isnan(tableData.pid_uCPS);
profile(fileNum).pid_uCPS = tableData.pid_uCPS(notNaN);
profile(fileNum).pid_uCPSDT = profile(fileNum).DT(notNaN);

if(~ismissing(tableData.pid_uCPS(1)))
    notNaN = ~isnan(tableData.pid_uCPS);
    profile(fileNum).pidUCps = tableData.pid_uCPS(notNaN);
    profile(fileNum).pidUCpsDT = profile(fileNum).DT(notNaN);
end
end

function processBellowPosition()
global tableData profile fileNum
fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing bellows position ...']);
notNaN = ~isnan(tableData.BelPos);
profile(fileNum).bellowsPosition = tableData.BelPos(notNaN);
profile(fileNum).bellowsPositionDT = profile(fileNum).DT(notNaN);
end

function findGlide()
global tableData glideOnMsgIndexes glideOffMsgIndexes
glideOnFind = strfind(tableData.Comment, 'glide on');
glideOnMsgIndexes = find(isempty(glideOnFind) == 0);

glideOffFind = strfind(tableData.Comment, 'glide off');
glideOffMsgIndexes = find(isempty(glideOffFind) == 0);
end

function checkHotelEnergy()
global tableData profile fileNum
global MilliVoltsScaleFactor NanoJoulesScaleFactor AmpsScaleFactor WattsScaleFactor SecondsScaleFactor CoulombsScaleFactor

fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Checking LTC2946 intermediate values ...']);

notNaN = ~isnan(tableData.HotelMilliVolts);
profile(fileNum).hotelMilliVolts = tableData.HotelMilliVolts(notNaN) * MilliVoltsScaleFactor;
profile(fileNum).hotelMilliVoltsDT = profile(fileNum).DT(notNaN);
if(profile(fileNum).hotelMilliVoltsDT(1) < datetime(2000, 1, 1))
    profile(fileNum).hotelMilliVoltsDT(1) = profile(fileNum).hotelMilliVoltsDT(2) - duration(0, 0, 1);
end

notNaN = ~isnan(tableData.HotelMilliamps);
profile(fileNum).hotelAmps = tableData.HotelMilliamps(notNaN) * AmpsScaleFactor;
profile(fileNum).hotelAmpsDT = profile(fileNum).DT(notNaN);
if(profile(fileNum).hotelAmpsDT(1) < datetime(2000, 1, 1))
    profile(fileNum).hotelAmpsDT(1) = profile(fileNum).hotelAmpsDT(2) - duration(0, 0, 1);
end

notNaN = ~isnan(tableData.HotelMilliSeconds);
profile(fileNum).hotelSeconds = tableData.HotelMilliSeconds(notNaN) * SecondsScaleFactor;
profile(fileNum).hotelSecondsDT = profile(fileNum).DT(notNaN);
if(profile(1).hotelSecondsDT(1) < datetime(2000, 1, 1))
    profile(1).hotelSecondsDT(1) = profile(1).hotelSecondsDT(2) - duration(0, 0, 1);
end

lastIndex = min(length(profile(fileNum).hotelMilliVolts), length(profile(fileNum).hotelAmps));
profile(fileNum).hotelWattsCheck = profile(fileNum).hotelMilliVolts(1:lastIndex) .* profile(fileNum).hotelAmps(1:lastIndex);
profile(fileNum).hotelWattsCheckDT = profile(fileNum).hotelMilliVoltsDT(1:lastIndex);
      
profile(fileNum).dtDiff = profile(fileNum).hotelWattsCheckDT(2:end) - profile(fileNum).hotelWattsCheckDT(1:end-1);
profile(fileNum).hotelNanoJoulesCheck = seconds(profile(fileNum).dtDiff) .* profile(fileNum).hotelWattsCheck(1:end-1);
profile(fileNum).hotelNanoJoulesCheckDT = profile(fileNum).hotelWattsCheckDT(1:end-1);
   
end

function processHotelEnergy()
global tableData profile fileNum
global MilliVoltsScaleFactor NanoJoulesScaleFactor AmpsScaleFactor WattsScaleFactor SecondsScaleFactor CoulombsScaleFactor

fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Calculating Hotel Power ...']);

notNaN = ~isnan(tableData.HotelMilliVolts);
profile(fileNum).hotelMilliVolts = tableData.HotelMilliVolts(notNaN) * MilliVoltsScaleFactor;
profile(fileNum).hotelMilliVoltsDT = profile(fileNum).DT(notNaN);
if(profile(fileNum).hotelMilliVoltsDT(1) < datetime(2000, 1, 1))
    profile(fileNum).hotelMilliVoltsDT(1) = profile(fileNum).hotelMilliVoltsDT(2) - duration(0, 0, 1);
end

notNaN = ~isnan(tableData.HotelMilliamps);
profile(fileNum).hotelAmps = tableData.HotelMilliamps(notNaN) * AmpsScaleFactor;
profile(fileNum).hotelAmpsDT = profile(fileNum).DT(notNaN);
if(profile(fileNum).hotelAmpsDT(1) < datetime(2000, 1, 1))
        profile(fileNum).hotelAmpsDT(1) = profile(fileNum).hotelAmpsDT(2) - duration(0, 0, 1);
    end
    
    notNaN = ~isnan(tableData.HotelMicroWatts);
    profile(fileNum).hotelWatts = tableData.HotelMicroWatts(notNaN) * WattsScaleFactor;
    profile(fileNum).hotelWattsDT = profile(fileNum).DT(notNaN);
    if(profile(1).hotelWattsDT(1) < datetime(2000, 1, 1))
        profile(1).hotelWattsDT(1) = profile(1).hotelWattsDT(2) - duration(0, 0, 1);
    end
    profile(fileNum).hotelEnergy = tableData.HotelMicroWatts(notNaN) * 65536 * 16.39543e-3 * 1e-3;

    notNaN = ~isnan(tableData.HotelMilliSeconds);
    profile(fileNum).hotelSeconds = tableData.HotelMilliSeconds(notNaN) * SecondsScaleFactor;
    profile(fileNum).hotelSecondsDT = profile(fileNum).DT(notNaN);
    if(profile(1).hotelSecondsDT(1) < datetime(2000, 1, 1))
        profile(1).hotelSecondsDT(1) = profile(1).hotelSecondsDT(2) - duration(0, 0, 1);
    end
    
    notNaN = ~isnan(tableData.HotelUCoulombs);
    profile(fileNum).hotelCoulombs = tableData.HotelUCoulombs(notNaN) * CoulombsScaleFactor;
    profile(fileNum).hotelCoulombsDT = profile(fileNum).DT(notNaN);
    if(profile(fileNum).hotelCoulombsDT(1) < datetime(2000, 1, 1))
        profile(fileNum).hotelCoulombsDT(1) = profile(fileNum).hotelCoulombsDT(2) - duration(0, 0, 1);
    end
    
    notNaN = ~isnan(tableData.HotelNanoJoules);
    profile(fileNum).hotelNanoJoules = tableData.HotelNanoJoules(notNaN) * NanoJoulesScaleFactor;
    profile(fileNum).hotelNanoJoulesDT = profile(fileNum).DT(notNaN);
    if(profile(fileNum).hotelNanoJoulesDT(1) < datetime(2000, 1, 1))
        profile(fileNum).hotelNanoJoulesDT(1) = profile(fileNum).hotelNanoJoulesDT(2) - duration(0, 0, 1);
    end
    
    profile(fileNum).totalHotelNanoJoules = profile(fileNum).hotelNanoJoules(end);
    profile(fileNum).totalHotelNanoJoulesDT = profile(fileNum).hotelNanoJoulesDT(end);
    
%     if(fileNum == 1)
%         profile(fileNum).missionHotelEnergyTotal = profile(fileNum).missionHotelEnergyTotal + profile(fileNum).totalHotelEnergy;
%     else
%         profile(fileNum).missionHotelEnergyTotal = profile(fileNum-1).missionHotelEnergyTotal + profile(fileNum).totalHotelEnergy;
%     end
%     
%     if(fileNum == 1)
%     profile(fileNum).missionEnergyTotal = profile(fileNum).missionEnergyTotal + profile(fileNum).totalPumpEnergy + profile(fileNum).totalHotelEnergy;
%     else
%         profile(fileNum).missionEnergyTotal = profile(fileNum-1).missionEnergyTotal + profile(fileNum).totalPumpEnergy + profile(fileNum).totalHotelEnergy;
%     end
%         
%     fprintf('\nTotal sum of hotel energy this profile = %.0f NanoJoules\n', profile(fileNum).totalHotelEnergy);
%     fprintf('Final value of hotel energy sum this profile = %.0f NanoJoules\n\n', profile(fileNum).hotelEnergyCalcSum(end));
end

function processPumpEnergy()
global tableData profile fileNum
global MilliVoltsScaleFactor NanoJoulesScaleFactor AmpsScaleFactor WattsScaleFactor SecondsScaleFactor CoulombsScaleFactor

    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Calculating Pump Power ...']);

    notNaN = ~isnan(tableData.PumpMilliVolts);
    profile(fileNum).pumpMilliVolts = tableData.PumpMilliVolts(notNaN) * MilliVoltsScaleFactor;
    profile(fileNum).pumpMilliVoltsDT = profile(fileNum).DT(notNaN);
    if(profile(1).pumpMilliVoltsDT(1) < datetime(2000, 1, 1))
        profile(1).pumpMilliVoltsDT(1) = profile(1).pumpMilliVoltsDT(2) - duration(0, 0, 1);
    end
    
    notNaN = ~isnan(tableData.PumpMilliamps);
    profile(fileNum).pumpAmps = tableData.PumpMilliamps(notNaN) * AmpsScaleFactor;
    profile(fileNum).pumpAmpsDT = profile(fileNum).DT(notNaN);
    if(profile(1).pumpAmpsDT(1) < datetime(2000, 1, 1))
        profile(1).pumpAmpsDT(1) = profile(1).pumpAmpsDT(2) - duration(0, 0, 1);
    end

    notNaN = ~isnan(tableData.PumpMicroWatts);
    profile(fileNum).pumpWatts = tableData.PumpMicroWatts(notNaN) * WattsScaleFactor;
    profile(fileNum).pumpWattsDT = profile(fileNum).DT(notNaN);
    if(profile(1).pumpWattsDT(1) < datetime(2000, 1, 1))
        profile(1).pumpWattsDT(1) = profile(1).pumpWattsDT(2) - duration(0, 0, 1);
    end

    notNaN = ~isnan(tableData.PumpMilliSeconds);
    profile(fileNum).pumpSeconds = tableData.PumpMilliSeconds(notNaN) / SecondsScaleFactor;
    profile(fileNum).pumpSecondsDT = profile(fileNum).DT(notNaN);
    if(profile(1).pumpSecondsDT(1) < datetime(2000, 1, 1))
        profile(1).pumpSecondsDT(1) = profile(1).pumpSecondsDT(2) - duration(0, 0, 1);
    end

    notNaN = ~isnan(tableData.PumpUCoulombs);
    profile(fileNum).pumpCoulombs = tableData.PumpUCoulombs(notNaN) * CoulombsScaleFactor;
    profile(fileNum).pumpCoulombsDT = profile(fileNum).DT(notNaN);
    if(profile(fileNum).pumpCoulombsDT(1) < datetime(2000, 1, 1))
        profile(fileNum).pumpCoulombsDT(1) = profile(fileNum).pumpCoulombsDT(2) - duration(0, 0, 1);
    end

   
    notNaN = ~isnan(tableData.PumpNanoJoules);
    profile(fileNum).pumpNanoJoules = tableData.PumpNanoJoules(notNaN) * NanoJoulesScaleFactor;
    profile(fileNum).pumpNanoJoulesDT = profile(fileNum).DT(notNaN);
    if(profile(fileNum).pumpNanoJoulesDT(1) < datetime(2000, 1, 1))
        profile(fileNum).pumpNanoJoulesDT(1) = profile(fileNum).pumpNanoJoulesDT(2) - duration(0, 0, 1);
    end
    profile(fileNum).totalPumpNanoJoules = profile(fileNum).pumpNanoJoules(end);
    
%
%     if(fileNum == 1)
%         profile(fileNum).missionPumpEnergyTotal = profile(fileNum).missionPumpEnergyTotal + profile(fileNum).totalPumpEnergy;
%     else
%         profile(fileNum).missionPumpEnergyTotal = profile(fileNum-1).missionPumpEnergyTotal + profile(fileNum).totalPumpEnergy;
%     end
%     
%     fprintf('\nTotal sum of pump energy this profile = %.0f NanoJoules\n', profile(fileNum).totalPumpEnergy);
%     fprintf('Final value of hotel energy sum this profile = %.0f NanoJoules\n\n', profile(fileNum).pumpEnergyCalcSum(end));
%         
%  
%     totalEnergy = profile(fileNum).totalHotelEnergy - totalHotelEnergyTransit + profile(fileNum).totalPumpEnergy - totalPumpEnergyTransit;
%     fprintf('Total energy this profile = %.0f NanoJoules', totalEnergy);
%     percentAvailable = 100 * totalEnergy / energyAvailable;
%     fprintf(' = %3.1f precent of total NanoJoules available\n\n', percentAvailable);
%       
%   
end

function calculateHotelEnergy()
global tableData profile fileNum
    lastIndex = min(length(profile(fileNum).hotelMilliVolts), length(profile(fileNum).hotelAmps));
    profile(fileNum).hotelPowerCalc = profile(fileNum).hotelMilliVolts(1:lastIndex) .* profile(fileNum).hotelAmps(1:lastIndex);
    profile(fileNum).hotelPowerCalcDT = profile(fileNum).hotelMilliVoltsDT(1:lastIndex);
      
    dtDiff = profile(fileNum).hotelPowerCalcDT(2:end) - profile(fileNum).hotelPowerCalcDT(1:end-1);
    profile(fileNum).hotelNanoJoulesCalc = seconds(dtDiff) .* profile(fileNum).hotelPowerCalc(1:end-1);
    profile(fileNum).hotelNanoJoulesCalcDT = profile(fileNum).hotelPowerCalcDT(1:end-1);
    
    profile(fileNum).hotelNanoJoulesCalcSum = cumsum(profile(fileNum).hotelNanoJoulesCalc);
    profile(fileNum).hotelNanoJoulesCalcSumDT = profile(fileNum).hotelNanoJoulesCalcDT;
    
    profile(fileNum).totalHotelNanoJoulesCalc = sum(profile(fileNum).hotelNanoJoulesCalc);
end

function calculatePumpEnergy()
    global tableData profile fileNum
    lastIndex = min(length(profile(fileNum).hotelMilliVolts), length(profile(fileNum).hotelAmps));
    profile(fileNum).pumpPowerCalc = profile(fileNum).pumpMilliVolts(1:lastIndex) .* profile(fileNum).pumpAmps(1:lastIndex);
    profile(fileNum).pumpPowerCalcDT = profile(fileNum).pumpMilliVoltsDT(1:lastIndex);
 
    dtDiff =  profile(fileNum).pumpPowerCalcDT(2:end) - profile(fileNum).pumpPowerCalcDT(1:end-1);
    profile(fileNum).pumpNanoJoulesCalc = seconds(dtDiff) .* profile(fileNum).pumpPowerCalc(1:end-1);
    profile(fileNum).pumpNanoJoulesCalcDT = profile(fileNum).pumpPowerCalcDT(1:end-1);
    
    profile(fileNum).pumpNanoJoulesCalcSum = cumsum(profile(fileNum).pumpNanoJoulesCalc);
    profile(fileNum).pumpNanoJoulesCalcSumDT = profile(fileNum).pumpNanoJoulesCalcDT;
    
    profile(fileNum).totalPumpNanoJoulesCalc = sum(profile(fileNum).pumpNanoJoulesCalc);
end


%    
%     
% 
%     %% Process PID uCPS
%   
%      
%      %% Process Twitter JV commands
%     fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing Twitter JV commands ...']);
%     
%     jvIndexes = contains(tableData.pumpMotorMsg, 'JV=');
%     jvStrings = split(tableData.pumpMotorMsg(jvIndexes), "=");
%     jvStrings = erase(jvStrings, ";");
%     profile(fileNum).JVs = str2double(jvStrings);
%     profile(fileNum).JVsDT = profile(fileNum).DT(jvIndexes);
%     
%     %% 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 = profile(fileNum).DT(notNaN);
%  
%     notNaN = ~isnan(tableData.SHT21Humid);
%     profile(fileNum).humidity = tableData.SHT21Humid(notNaN);
%     profile(fileNum).humidityDT = profile(fileNum).DT(notNaN);
%  
%     %% Process pump power

%     %% 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 = profile(fileNum).DT(ubxMsgIndexes);
%     index = 1;
%     for i = 1:length(profile(fileNum).ubxMsgs)
%         fields = split(profile(fileNum).ubxMsgs(i), [",", "*"]);
%         if(fields(2) == "00")
%             profile(fileNum).fixDT(index) = profile(fileNum).ubxMsgsDT(i);
%             profile(fileNum).latDDM(index) = fields(4);
%             profile(fileNum).latHemi(index) = fields(5);
%             profile(fileNum).lonDDM(index) = fields(6);
%             profile(fileNum).lonHemi(index) = fields(7);
%             profile(fileNum).numSats(index) = str2double(fields(19));
%             profile(fileNum).latDD(index) = latDDM2DD(fields(4), fields(5));
%             profile(fileNum).lonDD(index) = lonDDM2DD(fields(6), fields(7));
%             index = index + 1;
%         end
%     end
%         
% %     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);
% %% Process Twitter IQ and JV messages
% %     fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing Twitter IQ Messages ...']);
% %     
% %     iqIndex = find(tableData.pumpMotor == "IQ;");
% %     iq = tableData.pumpMotor(iqIndex + 1);
% %     profile(fileNum).iq = str2double(strip(iq,';'));
% %     profile(fileNum).iqDT = datetime(dt(iqIndex+1), 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');
% %     
% %     jvIndex = find(contains(tableData.pumpMotor, "JV="));
% %     jv = tableData.pumpMotor(jvIndex);
% %     jvFields = split(jv, ["=", ";"]);
% %     profile(fileNum).jv = str2double(jvFields(:,2));
% %     profile(fileNum).jvDT = datetime(dt(jvIndex), 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');
% %             
%     %% 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 = profile(fileNum).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
%  
% end
% %% Process mission totals
%     pumpTotalEnergyMinusTransit = profile(end).missionPumpEnergyTotal - totalPumpEnergyTransit;
%     fprintf('Total pump energy this mission = %.0f NanoJoules', pumpTotalEnergyMinusTransit);
%     percentAvailable = 100 * pumpTotalEnergyMinusTransit / energyAvailable;
%     fprintf(' = %3.1f precent of total NanoJoules available\n\n', percentAvailable);
% 
%     hotelTotalEnergyMinusTransit = profile(end).missionHotelEnergyTotal - totalHotelEnergyTransit;
%     fprintf('Total hotel energy this mission = %.0f NanoJoules', hotelTotalEnergyMinusTransit);
%     percentAvailable = 100 * hotelTotalEnergyMinusTransit / energyAvailable;
%     fprintf(' = %3.1f precent of total NanoJoules available\n\n', percentAvailable);
% 
%     missionEnergyMinusTransit = profile(end).missionEnergyTotal - totalPumpEnergyTransit - totalHotelEnergyTransit;
%     fprintf('Total energy this mission = %.0f NanoJoules', missionEnergyMinusTransit);
%     percentAvailable = 100 * missionEnergyMinusTransit / energyAvailable;
%     fprintf(' = %3.1f precent of total NanoJoules available\n\n', percentAvailable);
% 
%     allLatsDD = [];
%     allLonsDD = [];
%     for i=1:length(profile)
%         allLatsDD = [allLatsDD profile(i).latDD];
%         allLonsDD = [allLonsDD profile(i).lonDD];
%     end
%     [allEastings, allNorthings] = LatLon2UTM(allLatsDD, allLonsDD);
%     %% Read .msg files
%     msgFileList = ls([startingDir,'*.msg']);
%     numMsgFiles = size(fileList, 1);
%     
% %     for i=1:length(msgFileList)
% %         fullFileName = [startingDir msgFileList(i,:)];
% %         fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Importing .msg Filename: ' fullFileName 'file number: ' int2str(fileNum) ' of ' int2str(length(fileList))]);
% %         
% %         msg(i) = tdfread(fullFileName);
% %     end
% 
% %% plot individual profiles
% if(plotProfiles)
%     figure
%     plot(profile(fileNum).pidY1DT, profile(fileNum).pidY1, ...
%         profile(fileNum).LPFVelDT, profile(fileNum).LPFVel, ...
%         profile(fileNum).diffVelDT, profile(fileNum).diffVel, ...
%         profile(fileNum).diff2VelDT, profile(fileNum).diff2Vel, ...
%         profile(fileNum).slopeOfLPFPressDT, profile(fileNum).slopeOfLPFPress);
%     legend('pidY1', 'LPFVel', 'diffVel', 'diff2Vel', 'slopeOfLPFPress');
%     hold on
%     plotStateTransitions(tableData, profile(fileNum).DT);
%     hold off
%     xlabel('Time');
%     ylabel('Platform Velocity');
% end
% 
% if(false)
%     for  profileNum = 1:length(profile)
%         figure
%         title(['Pump and Hotel Power ' profile(profileNum).fileName]);
%         yyaxis left;
%         plot(profile(profileNum).pumpPowerCalcDT, profile(profileNum).pumpPowerCalc)
%         ylabel('Pump Power (milliMicroWattss)')
%         yyaxis right;
%         hold on
%         plot(profile(profileNum).hotelPowerCalcDT, profile(profileNum).hotelPowerCalc)
%         ylabel('Hotel power (milliMicroWattss)')
%         plotStateTransitions(tableData, profile(profileNum).DT);
%         xlabel('Time');
%         hold off
%         
%         figure
%         title(['Pump and Hotel Energy ' profile(profileNum).fileName]);
%         yyaxis left;
%         plot(profile(profileNum).pumpEnergyCalcSumDT, profile(profileNum).pumpEnergyCalcSum)
%         ylabel('Pump Energy (NanoJoules)')
%         yyaxis right;
%         hold on
%         plot(profile(profileNum).hotelEnergyCalcSumDT, profile(profileNum).hotelEnergyCalcSum)
%         ylabel('Hotel Energy (NanoJoules)')
%         plotStateTransitions(tableData, profile(profileNum).DT);
%         xlabel('Time');
%         hold off
%         
%         
%     end
%     
%     fig = figure('Name','Pump Energy (calculated)','NumberTitle','off');
%     title('Pump Bus Energy (calculated)')
%     hold on;
%     for profileNum = 1:length(profile)
%         for i=1:length(profile(profileNum).pumpEnergyCalcDT)
%             x(i) = profile(profileNum).pumpEnergyCalcDT(1);
%         end
%         y = profile(profileNum).pumpEnergyCalcDT - profile(profileNum).initProfileStartTime;
%         plot3(x, y, profile(profileNum).pumpEnergyCalc)
%         clear x;
%     end
%     set(gca, 'YDir', 'reverse');
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Pump Bus Energy (calculated NanoJoules)')
%     grid
%     view(3)
%     
% %     fig = figure('Name','Pump Power (calculated)','NumberTitle','off');
% %     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).initProfileStartTime;
% %         plot3(x, y, profile(profileNum).pumpPowerCalc)
% %         clear x;
% %     end
% %     set(gca, 'YDir', 'reverse');
% %     xlabel('Profile Start time');
% %     ylabel('Time from Profile Start')
% %     zlabel('Pump Bus Power (calculated MicroWatts)')
% %     grid
% %     view(3)
% % 
% %     fig = figure('Name','Hotel Power (calculated)','NumberTitle','off');
% %     title('Hotel Bus Power (calculated)')
% %     hold on;
% %     for profileNum = 1:length(profile)
% %         for i=1:length(profile(profileNum).hotelPowerCalcDT)
% %             x(i) = profile(profileNum).hotelPowerCalcDT(1);
% %         end
% %         y = profile(profileNum).hotelPowerCalcDT - profile(profileNum).initProfileStartTime;
% %         plot3(x, y, profile(profileNum).hotelPowerCalc)
% %         clear x;
% %     end
% %     set(gca, 'YDir', 'reverse');
% %     xlabel('Profile Start time');
% %     ylabel('Time from Profile Start')
% %     zlabel('Hotel Bus Power (calculated MicroWattss)')
% %     grid
% %     view(3)
% end
% 
% if(plotProfiles)
%     figure
%     yyaxis left
%     ylabel('Pressure (dBar)');
%     yyaxis right
%     ylabel('Slope of LPF Pressure (dBar/sec)');
%     title('Pressure and Platform Velocity');
%     
%     hold on
%     for i = 1:numFiles
%         yyaxis left
%         plot(profile(i).pressureDT, profile(i).pressure, 'b-')
%         yyaxis right
%         plot(profile(i).slopeOfLPFPressDT, profile(i).slopeOfLPFPress, 'r-')
%     end
% end
% 
% if(plotProfiles)
%     for i=1:numFiles
%         figLabel = 'Pressure and Bellows Position, Profile Num: ' + string(i);
%         plotPandBP(figLabel, i, profile, tableData);
%         
%         figLabel = 'Pressure and Jog Velocity, Profile Num: ' + string(i);
%         plotPandJV(figLabel, i, profile, tableData);
%         
%         figLabel = 'Pressure and energy sum, ProfileNum: ' + string(i);
%         plotPandEnergySum(figLabel, i, profile, tableData);
%     end
%     
%     plotAllFixes(profile, allEastings, allNorthings);
%     
%     figure
%     hold on
%     for i=1:length(profile)
%         plot(profile(i).pressureDT, profile(i).pressure)
%     end
%     
% end
% 
% %% Waterfall plots
% if(plotWaterfalls)
%     fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Plotting waterfalls ...']);
%     
%     figX = 0.05;
%     figXIncr = 0.05;
%     figY = 0.6;
%     figYIncr = -0.05;
%     figWidth = 0.3;
%     figHeight = 0.3;
%     
%     %% Depth
%     fig = figure('Name','Depth','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     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
%     set(gca, 'YDir', 'reverse');
%     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 figWidth figHeight];
%     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
%     set(gca, 'YDir', 'reverse');
%     zlim([0 100]);
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Bellows Position (IB % Full')
%     grid
%     view(3)
%     
%     %% Pump MilliVolts
%     fig = figure('Name','Pump MilliVolts','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     figX = figX + figXIncr;
%     figY = figY + figYIncr;
%     title('Pump Bus MilliVolts')
%     hold on;
%     
%     for profileNum = 1:length(profile)
%         for i=1:length(profile(profileNum).pumpMilliVoltsDT)
%             x(i) = profile(profileNum).pumpMilliVoltsDT(1);
%         end
%         y = profile(profileNum).pumpMilliVoltsDT - profile(profileNum).pumpMilliVoltsDT(1);
%         plot3(x, y, profile(profileNum).pumpMilliVolts)
%         clear x;
%     end
%     set(gca, 'YDir', 'reverse');
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Pump Bus MilliVolts')
%     grid
%     view(3)
% 
%     %% Internal Pressure
%     fig = figure('Name','Internal Pressure','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     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;
%     end
%     set(gca, 'YDir', 'reverse');
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Internal Pressure (mBar)')
%     grid
%     view(3)
%     
% 
% %Pump power
% fig = figure('Name','Pump Power (calculated)','NumberTitle','off');
% fig.Units = 'normalized';
% fig.Position = [figX figY figWidth figHeight];
% 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
% set(gca, 'YDir', 'reverse');
% xlabel('Profile Start time');
% ylabel('Time from Profile Start')
% zlabel('Pump Bus Power (calculated MicroWattss)')
% grid
% view(3)
% 
% %% Humidity
%     fig = figure('Name','Humidity','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     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
%     set(gca, 'YDir', 'reverse');
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Humidity (%RH)')
%     grid
%     view(3)
%   
%     %% Hotel Energy sum
%     fig = figure('Name','Hotel Cumul. Energy','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     figX = figX + figXIncr;
%     figY = figY + figYIncr;
%     title('Hotel Cumulative Energy (NanoJoules)')
%     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;
%     end
%     set(gca, 'YDir', 'reverse');
%     xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Hotel Cumulative Energy (NanoJoules')
%     grid
%     view(3)
%     
%     %% Pump Energy sum
%     fig = figure('Name','Pump Cumul. Energy','NumberTitle','off');
%     fig.Units = 'normalized';
%     fig.Position = [figX figY figWidth figHeight];
%     figX = figX + figXIncr;
%     figY = figY + figYIncr;
%     title('Pump Cumulative Energy (NanoJoules)')
%     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
%     set(gca, 'YDir', 'reverse');    xlabel('Profile Start time');
%     ylabel('Time from Profile Start')
%     zlabel('Pump Cumulative Energy (NanoJoules')
%     grid
%     view(3)
% 
% end