close all

%startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/TankTests2019/Feb07/';
%startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/BenchTopSimulatorTests2019/Feb12/';
startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/TankTests2019/Feb12/';

engFileNames = dir('*.eng');

figX = 0.1;
figY = 0.1;

i = 2;
%for i=1:length(engFileNames)
    clearvars -except engFileNames i figX figY
    
    fid = fopen(engFileNames(i).name);
    
    headerLine = fgetl(fid);
    header  = textscan(headerLine,'%s','Delimiter','\t');
    header  = header{1,1};
    
    formatString = '';
    for j = 1:length(header)
        if strcmp(header{j}, '$')
            formatString = [formatString,' %s'];
            header{j} = 'STX';
        elseif strcmp(header{j}, 'Pressure') || strcmp(header{j}, 'Bel Pos') || strcmp(header{j}, 'Batt Volt') || strcmp(header{j}, 'Batt Amps') || strcmp(header{j}, 'CTD Temp')  ...
                || strcmp(header{j}, 'Raw Press') || strcmp(header{j}, 'LPF Press')  || strcmp(header{j}, 'Diff Vel') ||  strcmp(header{j}, 'Deiff 2 Vel') ...
                || strcmp(header{j}, 'Slope of LPF Press.') || strcmp(header{j}, 'CTD Sample Period')  || strcmp(header{j}, 'LPS331 Press') || strcmp(header{j}, 'LPS311 Temp') ...
                || strcmp(header{j}, 'SHT21 Humid')  || strcmp(header{j}, 'SHT21 Temp') || strcmp(header{j}, 'PID SP') || strcmp(header{j}, 'PID PV') ...
                || strcmp(header{j}, 'PID Y1') || strcmp(header{j}, 'PID Y2') || strcmp(header{j}, ' PID I')  || strcmp(header{j}, 'PID v') || strcmp(header{j}, 'PID u') ...
                || strcmp(header{j}, 'PID uCPS') || strcmp(header{j}, 'PID h') || strcmp(header{j}, 'NO3') || strcmp(header{j}, 'pH Vrs')
            formatString = [formatString, ' %f'];
        else
            formatString = [formatString,' %s'];
        end
            %fprintf(1, '%s %s\r\n', header{j}, formatString)
        end
    
    cellData = textscan(fid, formatString, 'Delimiter','\t');
    fclose(fid);
    
    vNames = genvarname(header);
    tableData = cell2table(cellData, 'VariableNames', vNames);
    
    d = strcat(cell2mat(tableData.Date{:}), cell2mat(tableData.Time{:}) );
    dt = datetime(d, 'InputFormat', 'dd-MM-yyyyHH:mm:ss.SSS');
    
    p = cell2mat(tableData.Pressure);
    plot(dt, p);

%     if(length(cellData)  > 30)
%         dateIndex = 2;
%         timeIndex = 3;
%         rawPressureIndex = 12;
%         profileStartTimeIndex = 1;
%         
%         %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{rawPressureIndex}, 'UniformOutput', false);
%         emptyCellIndexes = cellfun('isempty', a);
%         a(emptyCellIndexes) = {nan};
%         pressure = cell2mat(a);
%         
%         figure('Units', 'normalized', 'Position', [figX, figY, .25, .25]);
%         figX = figX + 0.025;
%         figY = figY + 0.025;
%         pressureDT = dateTime(~isnan(pressure));
%         pressure = pressure(~isnan(pressure));
%         plot(pressureDT, pressure);
%         title(engFileNames(i).name);
%         ylabel( 'Pressure (dBar)');
%     else
%             fprintf(1, 'Skipping file %s  with %d columns \r\n', engFileNames(i).name, length(cellData))
%     end
% end
% 
% 
