clear all
close all

startingDir = '\\atlas\ProjectLibrary\CPF\CPF003\Data\ParagonTests\2022-2-23T16-59-6\';
fileList = ls([startingDir,'*.msg']);
numFiles = size(fileList, 1);

for fileNum = 1:numFiles
    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);
    
    rowIndex = 1;
    getHeaderLine = true;
    while ~feof(fid)
        inLine = fgetl(fid);
        if(getHeaderLine)
            if strncmp(inLine, "Comment", 7)
                inLine = strrep(inLine,'/','per');
                dataLines{rowIndex} = strsplit(inLine, '\t');
                rowIndex = rowIndex + 1;
                getHeaderLine = false;
            end
        end
        
        if(strncmp(inLine, "AirCal", 6) || ...
                strncmp(inLine, "AnchorPt", 8) || ...
                strncmp(inLine, "AscendPt", 8))
            temp = strsplit(inLine, '\t','CollapseDelimiters',false);
            tempSize = size(temp);
            if(tempSize(2)==17)
                dataLines{rowIndex} = strsplit(inLine, '\t','CollapseDelimiters',false);
            else
                fprintf('Error in input line: %s\n', inLine);
            end
        end
        
        rowIndex = rowIndex + 1;
    end
    
    fclose(fid);
    
    dataLines = dataLines';
    dataLines = vertcat(dataLines{:});

    tempDataTable = array2table(dataLines(2:end,:), 'VariableNames', dataLines(1,:));
    writetable(tempDataTable, 'dataTable.txt')
    opts{fileNum} = detectImportOptions('dataTable.txt');
    dataTable = readtable('dataTable.txt', 'Format', '%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f')

    dataTable.dt = datetime(dataTable.DateTime, 'InputFormat', 'yyyy-MMM-dd''T''HH:mm:ss''Z')
    
    allTables{fileNum} = dataTable;
    
    if(fileNum ~= 0)
        figure
    end
    plot(allTables{fileNum}.dt, allTables{fileNum}.pres_dBar)
   
    clear dataLines dataTable;
end


    
    