clear all
close all

startingDir = '\\atlas\ProjectLibrary\CPF\CPF003\Data\ParagonTests\2022-3-29T16-45-50\';

fileList = ls([startingDir,'*.eng']);
numFiles = size(fileList,1);

%% Variable names

varNames = {'stx', 'date', 'time', 	'state', 	'comment', 	...
    'pressure', 'bellowsPos', ...
    'pumpVolts', 	'pumpMilliamps', 'pumpCoulombs', 'pumpJoules', 'pumpMotor'...
    'hotelVolts', 	'hotelMilliamps', 'hotelCoulombs', 'hotelJoules', ...
    'CTDTemp', 	'CTDSal', 	...
    'RawPress', 	'LPFPress', 'LPFVel', 	'diffVel', 	'diff2Vel', 'slopeOfLPFPress', 	'CTDSamplePeriod', ...
    'LPS331Press', 	'LPS311Temp', 	'SHT21Humid', 	'SHT21Temp', ...
    'pidSP', 	'pidPV', 	'pidY1',	'pidY2', 'pidI', 	'pid_v', 'pid_u', 'pid_uCPS', 'pid_h',  ...
    'FLBB', 'Topt', 'Tphase', 'Rphase', 'NO3', 'pH_Vrs', ...
    'OCRtext', 'OCRCh1', 'OCRCh2', 'OCRCh3', 'OCRCh4', ...
    'GPS', 'IridModem', 'MSC'};
varTypes = {'string', 'string', 'string', 'string', 'string', ...
    'double', 'double', ...
    'double', 'double','double', 'double', 'string'...
    'double', 'double','double',  'double', ...
    'string', 'string', ...
    'double', 'double','double', 'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', ...
    'double', 'double','double', 'double', 'double', 'double', 'double','double', 'double', ...
    'string', 'string', 'string', 'string', 'string', 'string', ...
    'string', 'string', 'string', 'string', 'string', ...
    'string', 'string', 'string'};   

    dateIndex = 2;
    timeIndex = 3;
    stateIndex = 4;
    commentIndex = 5;
    pressureIndex = 6;
    bellowsPosIndex = 7;
    pumpVoltsIndex = 8;
    pumpAmpsIndex = 9;
    pumpJoulesIndex = 10;
    hotelVoltsIndex = 11;
    hotelAmpsIndex = 12;
    hotelJoulesIndex = 13;
    ctdIndex = 15;
    rawPressureIndex = 16;
    InternalPressureIndex = 23;
    IPTemperatureIndex = 20;
    HumidityIndex = 21;
    HumidityTemperatureIndex = 22;
    pumpSpeedIndex = 30;
    gpsIndex = 39;
    iridiumIndex = 40;

%% Process files
figX = 0.05;
figXIncr = 0.01;
figY = 0.3;
figYIncr = -0.02;

for fileNum = 1:numFiles
    %% Open and read file
    fullFileName = [startingDir fileList(fileNum,:)];
    fprintf('\n%s\n', [datestr(now, 'HH:MM:SS ') 'Importing Filename: ' fullFileName 'file number: ' int2str(fileNum) ' of ' int2str(length(fileList))]);
     
    opts = delimitedTextImportOptions('VariableNames', varNames, 'VariableTypes', varTypes, 'Delimiter', '\t');
    tableData = readtable(fullFileName, opts);
 
    profile(fileNum).fileName = fullFileName;
    
    %% Process date/time
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing date/time ...']);
   
    dt = tableData.date + tableData.time;
    profile(fileNum).DT= datetime(dt, 'InputFormat', 'dd-MMM-yyyyHH:mm:ss.SSS');

    %% Process pressure
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing pressure ...']);
    notNaN = ~isnan(tableData.RawPress);
    profile(fileNum).pressure = tableData.RawPress(notNaN);
    profile(fileNum).pressureDT = profile(fileNum).DT(notNaN);
    
    notNaN = ~isnan(tableData.LPFPress);
    profile(fileNum).LPFPressure = tableData.LPFPress(notNaN);
    profile(fileNum).LPFPressureDT = profile(fileNum).DT(notNaN);
    
    %% Process CTD messages
    fprintf('%s\n', [datestr(now, 'HH:MM:SS ') 'Processing CTD message ...']);
    ptsMsgIndexes = find(count(tableData.CTDSal, ',')== 2);
    profile(fileNum).ptsMsg = tableData.CTDSal(ptsMsgIndexes);
    profile(fileNum).ptsMsg = strrep(profile(fileNum).ptsMsg, 'S> ', '');
 
    ptsSplit = split(profile(fileNum).ptsMsg, ',');
    profile(fileNum).ptsP = str2double(ptsSplit(:,1));
    profile(fileNum).ptsT = str2double(ptsSplit(:,2));
    profile(fileNum).ptsS = str2double(ptsSplit(:,3));
 
    profile(fileNum).ptsDT = profile(fileNum).DT(ptsMsgIndexes);
    
    %% Process Optode messages
    optodeMsgIndexes = find(count(tableData.Topt, ',')== 10);
    profile(fileNum).optodeMsg = tableData.Topt(optodeMsgIndexes);
   
    optodeSplit = split(profile(fileNum).optodeMsg, ',');
    profile(fileNum).optodeT = str2double(optodeSplit(:,4));
 
    profile(fileNum).optodeDT = profile(fileNum).DT(optodeMsgIndexes);
 
end

timeOffset = duration(0, 0, 30);
figure
plot(profile(fileNum).ptsDT, profile(fileNum).ptsT, profile(fileNum).optodeDT, profile(fileNum).optodeT, 'x-');
figure
plot(profile(fileNum).ptsDT, profile(fileNum).ptsT, profile(fileNum).optodeDT - timeOffset, profile(fileNum).optodeT, 'o-');
