% getCPFeng.m
% Extract engineering data from engineering file

fname    = '2016-2-10T20-6-39';
data_dir = '\\atlas.shore.mbari.org\projectLibrary\CPF\Data\TankTests2016\';

fig_switch =[1 0 0 0 0];

% ***********************************************************************
% FORMAT STRINGS


       
% FLBB_format = '99/99/99,99:99:99,%*f,%f,%*f,%f,%f';
% gps_format  = '%*s %*s %*s %f %s %f %s';

% ***********************************************************************
% ***********************************************************************
% BUILD FORMAT STRING BASED ON VARIABLE LIST FROM HEADER DESIRED VARIABLES
% DETERMINED FROM "keep_vars"
% HEADER
%    '$'    'Date'    'Time'    'State'    'Comment'    'Pressure'    'Bel Pos'    'Batt Volt'    'CTD Temp'    'CTD Sal'
%     'LPS331 Press'    'LPS311 Temp'    'SHT21 Humid'    'SHT21 Temp'    'FLBB'    'Topt'    'Tphase'
%     'Rphase'    'NO3'    'pH'    'Raw Press'    'LPF Press'    'LPF Vel'    'Difference Vel'    'CTD Sample Period'
%     'PID SP'    'PID PV'    'PID Y1'    'PID Y2'    'PID I'    'PID v'    'PID u'    'PID uCPS'    'PID h'    'Batt Amps'
%     '12V Amps'    '3.3V Amps'    'Debug'    'GPS'    'Irid Modem'
    % ***********************************************************************
fid       = fopen([data_dir,fname]);

tline     = fgetl(fid); % 1st line of data is always header line
hdr_vars  = textscan(tline,'%s','Delimiter','\t');
hdr_vars  = hdr_vars{1,1};

% keep_vars = { 'Date' 'Time' ...
%     'State'  'Comment' ...
%     'Pressure' 'Bel Pos' ...
%     'Batt Volt' 'Batt Amps' ...
%     'CTD Temp' 'CTD Sal' ...
%     'LPS331 Press' 'LPS311 Temp' 'SHT21 Humid' 'SHT21 Temp' ...
%     'FLBB' ...
%     'Topt' 'Tphase' 'Rphase' ...
%     'NO3' 'pH' ....
%     'Raw Press' 'LPF Press' 'LPF Vel' 'Difference Vel' 'CTD Sample Period' ...
%     'PID SP' 'PID PV' 'PID Y1' 'PID Y2' 'PID I'  'PID v' 'PID u' 'PID uCPS' 'PID h' ...
%     'Debug' ...
%     'GPS' 'Irid Modem'
%     };

keep_vars = { 'Date' 'Time' ...
    'Pressure' 'Bel Pos' ...
    'Batt Volt' 'Batt Amps' ...
    'Raw Press' 'LPF Press' 'LPF Vel' 'Difference Vel' 'CTD Sample Period' ...
    'PID SP' 'PID PV' 'PID Y1' 'PID Y2' 'PID I'  'PID v' 'PID u' 'PID uCPS' 'PID h' ...
    };

ct = 0;
txt_format = '';
for i = 1:length(hdr_vars) % Finish building format string
    hdr_test = strcmp(hdr_vars{i,1},keep_vars);
    if sum(hdr_test) == 0
        txt_format = [txt_format,' %*s']; % Un-wanted variables
    else
        txt_format = [txt_format,' %s'];  % desired variables
        ct = ct+1;
        saved_vars(ct,1) = hdr_vars(i,1); %Header for discovered vars
    end
end

txt_data = textscan(fid, txt_format, 'Delimiter','\t');

% *************************************************************************
% *************************************************************************
%                         NOW CONVERT TEXT TO NUMBERS
% *************************************************************************
% *************************************************************************
[rows,~] = size(txt_data{1,2}); % Will be sdn and decimal seconds
[~,cols] = size(txt_data); % number of data cols
NaNfill  = ones(rows,1)*NaN;
time_stamp  = ones(rows,2)*NaN; %predimmension
data = ones(rows,cols-1)*NaN;
saved_vars(2) = []; % remove 'Time'

% *************************************************************************
% PARSE TIME STAMP
% *************************************************************************
time_stamp(:,1) =  datenum(txt_data{1,1}, 'dd-mmm-yyyy'); % Convert day to SDN
for i = 1:rows
    t_tmp = sscanf(txt_data{1,2}{i},'%f:%f:%f');
    time_stamp(i,2) = t_tmp(1)*3600 + t_tmp(2)*60 + t_tmp(3) ; %seconds in day
end
time_stamp(:,1) = time_stamp(:,1) + time_stamp(:,2) ./ 24 /3600;
data(:,1) = time_stamp(:,1);

% *************************************************************************
% PARSE THE REST OF THE DATA
% *************************************************************************
for i = 3: cols % step through variables
    tmp_cell = txt_data{1,i};
    tmp_num  = NaNfill;
    for j = 1:rows %step through lines of data
        if regexp(tmp_cell{j},'\d+','once')
            tmp_num(j) = str2double(tmp_cell{j});
        else
            tmp_num(j) = NaN;  
        end
    end
data(:,i-1) =tmp_num;
clear tmp_cell tmp_num
close all
end

% *************************************************************************
% CLEAN UP
% *************************************************************************
clear i j time_stamp txt_data ct txt_format tline t_tmp NaNfill fid rows
clear cols
t1 = sum(~isnan(data(:,2:end)),2) == 0;
data(t1,:) =[];
disp([num2str(sum(t1)),' empty rows removed'])

% *************************************************************************
% DERIVED QUANTITIES
% *************************************************************************
filter_value = 0.5; % dp/sec
t1 = ~isnan(data(:,2));
dPdt = diff(data(t1,2)) ./ (diff(data(t1,1))*24*3600);
sdn_dP = (data(t1,1));
sdn_dP = (sdn_dP(1:end-1) + sdn_dP(2:end))./2;
dPdt   = [sdn_dP, dPdt];
t2     = abs(dPdt(:,2)) > filter_value; % filter spikes
dPdt(t2,:) =[];

t1 = ~isnan(data(:,10));
dPavgdt = diff(data(t1,10)) ./ (diff(data(t1,1))*24*3600);
sdn_dPavg = (data(t1,1));
sdn_dPavg = (sdn_dPavg(1:end-1) + sdn_dPavg(2:end))./2;
dPavgdt   = [sdn_dPavg, dPavgdt];
t3        = abs(dPavgdt(:,2)) > filter_value; % filter spikes
dPavgdt(t3,:) =[];

keepIndex = ~isnan(data(:,2));
pressure = data(keepIndex,2);
pressureDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,5));
rawPressure = data(keepIndex,5);
rawPressureDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,6));
lpfPressure = data(keepIndex,6);
lpfPressureDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,7));
lpfVelocity = data(keepIndex,7);
lpfVelocityDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,8));
diffVelocity = data(keepIndex,8);
diffVelocityDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,12));
pidY1 = data(keepIndex,12);
pidY1Date = data(keepIndex,1);

figure
set(gcf,'units','normalized','Position',[0.25 0.093 0.50 0.83]);

ax1 = subplot(2,1,1);
plot(pressureDate, pressure, rawPressureDate, rawPressure, lpfPressureDate, lpfPressure);
legend('Pressure', 'LPF Raw Pressure', 'LPF Pressure', 'Location', 'NorthWest');
grid
datetick('x', 13)

ax2 = subplot(2,1,2);
plot(lpfVelocityDate, lpfVelocity, diffVelocityDate, diffVelocity, pidY1Date, pidY1);
legend('LPF Velocity',   'Velocity from PAvg', 'PID Y1', 'Location', 'NorthWest')
grid
datetick('x', 13)

linkaxes([ax1, ax2], 'x');
