% getCPFeng.m
% Extract engineering data from engineering file

fname    = '2016-2-26T16-10-51';
data_dir = '\\atlas.shore.mbari.org\projectLibrary\CPF\Data\ParagonTests\2016Feb26\';

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
% hdr_vars =     '$'    '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'
%     'Diff Vel'    'Slope of LPF Press.'    'CTD Sample Period'    ''    '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' 'Pressure' 'Bel Pos' 'Batt Volt' ...
             'LPS331 Press' 'LPS311 Temp'  'SHT21 Humid' 'SHT21 Temp'  ...
             'Raw Press'    'LPF Press'    'LPF Vel' 'Diff Vel'    'Slope of LPF Press.'  ...
             '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
% *************************************************************************
keepIndex = ~isnan(data(:,11));
lpfPVel = data(keepIndex,11);
lpfPVelDate = data(keepIndex,1);
pressure = data(keepIndex,2);
pressureDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,12));
diffPVel = data(keepIndex,12);
diffPVelDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,12));
diffPVel = data(keepIndex,12);
diffPVelDate = data(keepIndex,1);

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

keepIndex = ~isnan(data(:,13));
slope = data(keepIndex,13);
slopeDate = data(keepIndex,1);

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

keepIndex = ~isnan(data(:,14));
pidPV = data(keepIndex,14);
pidPVDate = data(keepIndex,1);

keepIndex = ~isnan(data(:,3));
bellowsPos = data(keepIndex,3);
bellowsPosDate = 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,lpfPressureDate,lpfPressure);
legend('Pressure', 'LPF Pressure');
grid
datetick('x', 15)

ax2 = subplot(2,1,2);
plot(lpfPVelDate, lpfPVel, slopeDate, slope, pidY1Date, pidY1);
legend('LPF Velocity',   'Slope', 'PID Y1')
grid
datetick('x', 15)

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

% *************************************************************************
% 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')
%       
