function functionTest
% 
% clear all
% close all

startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/TestTank2018May23-/';
[fileName, pathName] = uigetfile([startingDir '*.eng']);
fullFileName = [pathName fileName];

init

pressureFig = figure;
title('Pressure vs Time');
hold on;
bellowsPosFig = figure;
title('Bellows Position vs Time');
hold on;
batteryVoltsFig = figure;
title('Battery Voltage vs Time');
hold on;
powerFig = figure;
title('Power vs Time');
hold on;

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


for processedFileNum = 1:1
    
    F(processedFileNum) = figure(1);
    F(processedFileNum).Units = 'normalized';
    F(processedFileNum).Position = [0.1141 0.4425 0.5441 0.4175];
    AX(1) = subplot(2,3,1);
    AX(2) = subplot(2,3,2);
    AX(3) = subplot(2,3,3);
    AX(4) = subplot(2,3,4);
    AX(5) = subplot(2,3,5);
    AX(6) = subplot(2,3,6);
    
    fprintf('%s\n',[datestr(now, 'HH:MM:SS ') 'Importing file: ' fullFileName ' ...']);
    
    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);
    
    %fprintf([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;
    
    %fprintf([datestr(now, 'HH:MM:SS ') 'Processing pressure ...']);
    a = cellfun(@str2num, cellData{pressureIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    pressure = cell2mat(a);
    
    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
    
    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
   
    %figure(pressureFig);
    plot(AX(1), dateTime(~isnan(pressure)), pressure(~isnan(pressure)));
    if plotFilename
        text(datenum(dateTime(1)), 0, fullFileName, 'rotation', 270, 'HorizontalAlignment', 'left', 'interpreter', 'none');
    end
    
    if length(glideOnMsgIndexes) > 0
        plot(glideOnTime, glideOnDepth, 'gO')
    end
    glideOnMsgIndexes = [];
    if length(glideOffMsgIndexes) > 0
        plot(glideOffTime, glideOffDepth, 'rX')
    end
    glideOffMsgIndexes = [];
    
    %fprintf([datestr(now, 'HH:MM:SS ') 'Processing bellows position ...']);
    a = cellfun(@str2num, cellData{bellowsPosIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    bellowsPos = cell2mat(a);
    
    %figure(bellowsPosFig);
    plot(AX(2),dateTime(~isnan(bellowsPos)), bellowsPos(~isnan(bellowsPos)));
    
    %fprintf([datestr(now, 'HH:MM:SS ') 'Calculating Power ...']);
    clear a
    a = cellfun(@str2num, cellData{batteryVoltsIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    batteryVolts = cell2mat(a);
    
    %figure(batteryVoltsFig);
    plot(AX(3), dateTime(~isnan(batteryVolts)), batteryVolts(~isnan(batteryVolts)));
    
    clear a
    a = cellfun(@str2num, cellData{batteryAmpsIndex}, 'UniformOutput', false);
    emptyCellIndexes = cellfun('isempty', a);
    a(emptyCellIndexes) = {nan};
    batteryAmps = cell2mat(a);
    power = batteryVolts .* batteryAmps;
    
    %figure(powerFig);
    plot(AX(4), dateTime(~isnan(power)), power(~isnan(power)));
    
    %fprintf([datestr(now, 'HH:MM:SS ') 'Calculating Energy ... \t']);
    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 = sum(energy);
    energyHistoryTime(processedFileNum) = dateTime(end);
    energyHistoryEnergy(processedFileNum) = totalEnergy;
    fprintf('Total Energy this profile = %.0f Joules\n\n', totalEnergy);
    
    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
    
    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
    
    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', processedFileNum );
    
    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);
    
    %figure
    plot(AX(5), energyHistoryTime, energyHistoryEnergy, '+-');
    title('Energy vs Time')
    
%         figure
%     try
%         plot(numSatsTime, numSats, '+-');
%         title('Number of GPS Satellites vs Time')
%     catch me
%         title('Number of GPS Satellites not processed\n');
%     end
%     
%         figure
%     try
%         plot(numSBDIXTime, numSBDIX, '+-');
%         title('Number of SBDIX Attempts vs Time')
%     catch me
%         title('Number of SBDIX Attempts not processed\n');
%     end

end

    function init
        dateIndex = 2;
        timeIndex = 3;
        stateIndex = 4;
        commentIndex = 5;
        pressureIndex = 6;
        bellowsPosIndex = 7;
        batteryVoltsIndex = 8;
        batteryAmpsIndex = 9;
        ctdIndex = 11;
        rawPressureIndex = 12;
        gpsIndex = 38;
        iridiumIndex = 39;
        
        
    end

end




