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/';
startingDir = '//atlas.shore.mbari.org/ProjectLibrary/CPF/Data/SN003/TankTests/2020Oct21-22/';

engFileNames = dir([startingDir '*.eng']);

figX = 0.1;
figY = 0.1;

%i = 2;
%for i=1:length(engFileNames)
for i=5:6
    clearvars -except engFileNames i pressure bellowsPosition figX figY depthTight timeTight depthLoose timeLoose
    
    fid = fopen([engFileNames(i).folder '\' 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);
    pressure{i,:} = p(~isnan(p));
    bp = cell2mat(tableData.BelPos);
    bellowsPosition{i,:} = bp(~isnan(bp));
    
    figure('Units', 'normalized', 'Position', [figX, figY, .25, .25]);
    figX = figX + 0.025;
    figY = figY + 0.025;
    %yyaxis left
    x = dt(~isnan(p));
    y = cell2mat(pressure(i));
    if(i==5)
        depthLoose = y;
        timeLoose = x;
        times = 24 * 3600 * datenum(timeLoose(188:240));
        p = polyfit(times, depthLoose(188:240), 1);
        fitDepth = (p(2) + (p(1) * times));
        plot(x(1:length(x)- 9), y(1:length(x)-9), x(188:240), fitDepth)
        title(engFileNames(i).name, 'fontsize', 20)
        ylabel('Pressure (dBar)', 'fontsize', 20);
        ylim([-1 42]);
        set(gca, 'YDir','reverse')
        set(gca,'FontSize', 16)
        grid;
        figure
        
        plot(x(170:295), y(170:295), x(188:240), fitDepth)
        title(engFileNames(i).name, 'fontsize', 20)
        ylabel('Pressure (dBar)', 'fontsize', 20);
        ylim([-1 42]);
        set(gca, 'YDir','reverse')
        set(gca,'FontSize', 16)
        grid;
    end
    if(i==6)
        depthTight = y;
        timeTight = x;
        figure
        plot(x(1:length(x)-45), y(1:length(x)-45))
        title(engFileNames(i).name, 'fontsize', 20)
        ylabel('Pressure (dBar)', 'fontsize', 20);
        ylim([-1 42]);
        set(gca, 'YDir','reverse')
        set(gca,'FontSize', 16)
        grid;
    end
    
    %    yyaxis right
    %    y = cell2mat(bellowsPosition(i))
    %    plot(x, y);
    
    %     uCPS = cell2mat(tableData.PIDUCPS);
    %     uCPS = uCPS(~isnan(uCPS));
    %
    %     figure
    %     yyaxis left
    %     plot(dt(~isnan(uCPS)), uCPS);
    %     yyaxis right
    %     plot(dt(~isnan(bellowsPosition)), bellowsPosition);
end