clear all
close all

dirName = 'X:\CPF\Data\CPF-P2\Paragon Tests\2017Jul12-14\EngrLogs\';

dirData = dir(dirName);
fileDatenums = dirData.date
[sortedTime, i] = sort([dirData.datenum]);
sortedDirData = dirData(i);

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

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;
processedFileNum = 0;
plotFilename = true;

for fileNum = 1:length(sortedDirData)
    if (length(sortedDirData(fileNum).name) >= 4)
        if ( (strcmp(sortedDirData(fileNum).name(end-3:end), '.eng')) && (sortedDirData(fileNum).bytes > 100000) )
            processedFileNum = processedFileNum + 1;
            fname = strtrim(sortedDirData(fileNum).name);
            fprintf('%s',[datestr(now, 'HH:MM:SS ') 'Importing file: ' [dirName sortedDirData(fileNum).name] ' ...\n']);
            
            fid = fopen([dirName sortedDirData(fileNum).name]);
            
            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 ...\n']);
            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 ...\n']);
            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(dateTime(~isnan(pressure)), pressure(~isnan(pressure)));
            if plotFilename
                text(datenum(dateTime(1)), 0, sortedDirData(fileNum).name, 'rotation', 270, 'HorizontalAlignment', 'left');
            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 ...\n']);
            a = cellfun(@str2num, cellData{bellowsPosIndex}, 'UniformOutput', false);
            emptyCellIndexes = cellfun('isempty', a);
            a(emptyCellIndexes) = {nan};
            bellowsPos = cell2mat(a);
            
            figure(bellowsPosFig);
            plot(dateTime(~isnan(bellowsPos)), bellowsPos(~isnan(bellowsPos)));
            
            %fprintf([datestr(now, 'HH:MM:SS ') 'Calculating Power ...\n']);
            clear a
            a = cellfun(@str2num, cellData{batteryVoltsIndex}, 'UniformOutput', false);
            emptyCellIndexes = cellfun('isempty', a);
            a(emptyCellIndexes) = {nan};
            batteryVolts = cell2mat(a);
            
            figure(batteryVoltsFig);
            plot(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(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
        end
    end
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(energyHistoryTime, energyHistoryEnergy, '+-');
title('Energy vs Time')

figure
plot(numSatsTime, numSats, '+-');
title('Number of GPS Satellites vs Time')

figure
plot(numSBDIXTime, numSBDIX, '+-');
title('Number of SBDIX Attempts vs Time')

