% getCPFeng.m
% Extract engineering data from engineering file

fname    = '2016-1-29T18-22-32';
data_dir = '\\atlas.shore.mbari.org\projectLibrary\CPF\Data\ParagonTests\2016Jan29\';

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	LPF PVel	
% Diff PVel 	LPF PAcc	Press Avg	Press Var	PID SP	PID PV	
% PID Y1	PID Y2	PID I	PID v	PID u	PID uCPS	
% 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' 'Pressure' 'LPS331 Press' 'LPS311 Temp' ...
             'SHT21 Humid' 'SHT21 Temp' 'LPF PVel' 'Diff PVel' ...
             'LPF PAcc' 'Press Avg' 'Press Var' 'PID SP' 'PID PV' ...
             'PID Y1' 'PID Y2' 'PID I' 'PID v' 'PID u' 'PID uCPS' ...
             'Batt Amps' '12V Amps'	'3.3V Amps'};

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,:) =[];

% QUICK SMOOTH
Pdata = [data(t2,1),data(t2,2)];
rows  = size(Pdata ,1);
win   = 7;

Pavg   = ones(7,rows-win+1);
SDNavg = ones(7,rows-win+1);
for i = 1:win
    Pavg(i,:) = Pdata(i:rows-win+i,2);
    SDNavg(i,:) = Pdata(i:rows-win+i,1);
end
SDNavg = mean(SDNavg,1);
Pstd    = std(Pavg,1);



% *************************************************************************
% PLOT
% *************************************************************************
figure
set(gcf,'units','normalized','Position',[0.25 0.093 0.50 0.83])
a1(1) = subplot(3,1,1);
x_limits = [min(data(:,1)) max(data(:,1))];
plot(data(:,1),data(:,2),'b-','LineWidth',2)
title(fname,'Fontsize',16)
ylabel(saved_vars{2},'Fontsize',14)
xlim(x_limits);
datetick('x','keeplimits', 'keepticks')
hold on
%plot(data(:,1),data(:,10),'r-','LineWidth',2)
plot(data(:,1),data(:,10),'ro','MarkerSize',2,'MarkerFaceColor','r')
hold off
legend('Pressure', 'Avg Pres')
set(gca,'Fontsize',14)

a1(2) = subplot(3,1,2);
x_limits = [min(data(:,1)) max(data(:,1))];
%plot(data(:,1),data(:,11),'ro','MarkerSize',2,'MarkerFaceColor','r')
plot(data(:,1),data(:,11),'ro','MarkerSize',2,'MarkerFaceColor','r')
ylabel(saved_vars{11},'Fontsize',14)
xlim(x_limits);
datetick('x','keeplimits', 'keepticks')
set(gca,'Fontsize',14)

a1(3) = subplot(3,1,3);
plot(dPdt(:,1),dPdt(:,2),'b-','LineWidth',2)
title('Pressure has been filtered')
ylabel('dP/dt','Fontsize',14)
xlim(x_limits);
datetick('x','keeplimits', 'keepticks')
hold on
plot(dPavgdt(:,1),dPavgdt(:,2),'ro','MarkerSize',2,'MarkerFaceColor','r')
plot(data(:,1),-data(:,7),'go','MarkerSize',2,'MarkerFaceColor','r')
hold off
legend('dPdt', 'dPavgdt', 'LPF PVel')
set(gca,'Fontsize',14)

linkaxes(a1,'x')
      
