%% Import data from text file.
% Script for importing data from the following text file:
%
%    \\atlas.shore.mbari.org\projectLibrary\CPF\Data\TankTests2015\TankTest2015Nov11\2015-11-10T19-36-6
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2015/11/11 13:46:24

files = dir;
fileDatenums = [files.datenum].';
[sortedFileDatenums, index] = sort(fileDatenums);

for i = 1:length(index)
    
    %% Initialize variables.
    %filename = '\\atlas.shore.mbari.org\projectLibrary\CPF\Data\TankTests2015\TankTest2015Nov11\2015-11-10T19-36-6';
    filename = files(index(i)).name;
    delimiter = '\t';
    
    if( strncmp(filename,'2015', 4) )
        fprintf('Processing File number = %d   name = %s\r\n', i, filename);
        
        
        %% Format string for each line of text:
        %   column1: text (%s)
        %	column2: text (%s)
        %   column3: text (%s)
        %	column4: text (%s)
        %   column5: text (%s)
        %	column6: text (%s)
        %   column7: text (%s)
        %	column8: text (%s)
        %   column9: text (%s)
        %	column10: text (%s)
        %   column11: text (%s)
        %	column12: text (%s)
        %   column13: text (%s)
        %	column14: text (%s)
        %   column15: text (%s)
        %	column16: text (%s)
        %   column17: text (%s)
        %	column18: text (%s)
        %   column19: text (%s)
        %	column20: text (%s)
        %   column21: text (%s)
        %	column22: text (%s)
        %   column23: text (%s)
        %	column24: text (%s)
        %   column25: text (%s)
        %	column26: text (%s)
        %   column27: text (%s)
        %	column28: text (%s)
        %   column29: text (%s)
        %	column30: text (%s)
        %   column31: text (%s)
        %	column32: text (%s)
        %   column33: text (%s)
        %	column34: text (%s)
        %   column35: text (%s)
        %	column36: text (%s)
        %   column37: text (%s)
        %	column38: text (%s)
        %   column39: text (%s)
        % For more information, see the TEXTSCAN documentation.
        formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
        
        %% Open the text file.
        fileID = fopen(filename,'r');
        
        %% Read columns of data according to format string.
        % This call is based on the structure of the file used to generate this
        % code. If an error occurs for a different file, try regenerating the code
        % from the Import Tool.
        dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);
        
        %% Close the text file.
        fclose(fileID);
        
        %% Post processing for unimportable data.
        % No unimportable data rules were applied during the import, so no post
        % processing code is included. To generate code which works for
        % unimportable data, select unimportable cells in a file and regenerate the
        % script.
        
        %% Allocate imported array to column variable names
        VarName1 = dataArray{:, 1};
        Date = dataArray{:, 2};
        Time = dataArray{:, 3};
        State = dataArray{:, 4};
        Comment = dataArray{:, 5};
        Pressure = dataArray{:, 6};
        BelPos = dataArray{:, 7};
        BattVolt = dataArray{:, 8};
        CTDTemp = dataArray{:, 9};
        CTDSal = dataArray{:, 10};
        LPS331Press = dataArray{:, 11};
        LPS311Temp = dataArray{:, 12};
        SHT21Humid = dataArray{:, 13};
        SHT21Temp = dataArray{:, 14};
        FLBB = dataArray{:, 15};
        Topt = dataArray{:, 16};
        Tphase = dataArray{:, 17};
        Rphase = dataArray{:, 18};
        NO3 = dataArray{:, 19};
        pH = dataArray{:, 20};
        LPFPVel = dataArray{:, 21};
        DiffPVel = dataArray{:, 22};
        LPFPAcc = dataArray{:, 23};
        PressAvg = dataArray{:, 24};
        PressVar = dataArray{:, 25};
        PIDSP = dataArray{:, 26};
        PIDPV = dataArray{:, 27};
        PIDY1 = dataArray{:, 28};
        PIDY2 = dataArray{:, 29};
        PIDI = dataArray{:, 30};
        PIDv = dataArray{:, 31};
        PIDu = dataArray{:, 32};
        PIDuCPS = dataArray{:, 33};
        BattAmps = dataArray{:, 34};
        VarName35 = dataArray{:, 35};
        VarName36 = dataArray{:, 36};
        Debug = dataArray{:, 37};
        GPS = dataArray{:, 38};
        IridModem = dataArray{:, 39};
        
        indexes = cellfun(@(x)~isnan(str2double(x)), Pressure);
        tempPressures = cellfun(@str2double, Pressure(indexes));
        tempDatenum = datenum(strcat(Date(indexes), 'T', Time(indexes)), 'dd-mm-yyyyTHH:MM:SS');
        
        if(i == 3)
            pressures = tempPressures';
            datenums = tempDatenum';
        else
            pressures = [pressures tempPressures'];
            datenums = [datenums tempDatenum'];
        end
        
        
        
        
        %% Clear temporary variables
        clearvars filename delimiter formatSpec fileID dataArray ans;
    end
end

close all
plot(datenums, -pressures);
