function d = get_ph_cal_stats()
% Parse the pH log xlxs file & generate a stucture of data & info used to
% plot current cal in relation to historical cal

% PARSE pH CAL SUMMARY LIST
d    = get_ph_version();
hdr  = d.hdr;
data = d.data;
clear d

% GET INDICES
iSN     = strcmp(hdr,'DF ID');
iMSC    = strcmp(hdr,'MSC');
iPHV    = strcmp(hdr,'verion notes');
iUW     = strcmp(hdr,'UW ID');
iK0_HCL = strcmp(hdr,'k0 HCl');
iK0_ON  = strcmp(hdr,'k0 pON');
iK0_OFF = strcmp(hdr,'k0 pOFF');
iK2     = strncmp(hdr,'k2',2);
iFP     = strncmp(hdr,'f(P)',4);
iVER    = strncmp(hdr,'sensor version',4);

% Remove lines with no f(p)
tnan  = cellfun(@isnan, data(:,iFP));
tnan  = all(tnan,2);
data  = data(~tnan,:);
rdata = size(data,1);

% MAKE LOGICAL FLAGS FOR PLOTTING SENSOR TYPES & COUNT ARRAY
% GDF = version 7
% SBE = version 0
tf_SBE = cell2mat(data(:,iVER)) == 0;
tf_GDF = cell2mat(data(:,iVER)) == 7;
tf_V6  = cell2mat(data(:,iVER)) == 6;
tf_V5  = cell2mat(data(:,iVER)) == 5;
tf_V4  = cell2mat(data(:,iVER)) == 4;
tf_V3  = cell2mat(data(:,iVER)) == 3;
tf_V2  = cell2mat(data(:,iVER)) == 2;
tf_V1  = fix(cell2mat(data(:,iVER))) == 1;

ct     = (1:rdata)'; % float count for ploting

% GET DATA OUT OF CELL ARRAY
MSC    = cell2mat(data(:,iMSC));
k0_ON  = cell2mat(data(:,iK0_ON));
k0_OFF = cell2mat(data(:,iK0_OFF));
k0_HCL = cell2mat(data(:,iK0_HCL));
k2     = cell2mat(data(:,iK2 ));
SV     = cell2mat(data(:,iVER));
UW     = cell2mat(data(:,iUW ));
fP     = cell2mat(data(:,iFP ));
fP(:,end) = 0;

% BUILD f(P) & k2_(P) matricies for ploting
P     = [0:5:100,125:25:400,450:50:2000];
FP    = ones(rdata,size(P,2))*NaN;
k2fP  = fP;
for i = 1:rdata
    pfP  = fP(i,:);
    tnan = isnan(pfP);
    FP(i,:) = polyval(pfP(~tnan),P);
end

% GET STRING VALUES
SN     = cellfun(@num2str, data(:,iSN),'UniformOutput',false);
VNOTES = data(:,iPHV);

% ASIGN INFO TO STRUCTURE
d.hdr    = hdr;
d.data   = data;
d.flt_ct = ct;

d.tf.SBE = tf_SBE; % logical arrays
d.tf.GDF = tf_GDF;
d.tf.V6  = tf_V6;
d.tf.V5  = tf_V5;
d.tf.V4  = tf_V4;
d.tf.V3  = tf_V3;
d.tf.V2  = tf_V2;
d.tf.V1  = tf_V1;

d.MSC = MSC;
d.k2     = k2;  % calibration values
d.k0_ON  = k0_ON;
d.k0_OFF = k0_OFF;
d.k0_HCL = k0_HCL;
d.SV     = SV; % sensor version
d.UW     = UW;
d.P      = P; % for plotting f(p)
d.FP     = FP;

d.SN     = SN; % string values
d.VNOTES = VNOTES;

d.uSV    = unique(fix(d.SV)); % group all V1.X's as one
d.colors = [128 128 128; 0 0 204; 51 153 255; 0 204 204; ...
            0 153 76; 255 128 0; 204 0 0; 255 255 0]/256;



    

% % ************************************************************************
% % ************************************************************************
% F1          = figure(1);
% F1.Units    = 'Normalized';
% F1.Position = [0.2000 0.2158 0.4641 0.7133];
% 
% % ***********
% ax(1) = subplot(4,1,1);
% ax(1).Position = ax(1).Position + [0.025 0 -0.1 0];
% ax_pos1 = ax(1).Position;
% 
% S1 = scatter(ct, k0_ON, 25 ,SV, 'filled','o');
% %xlabel('Sensor Count')
% ylabel({'k0 (pump ON)' 'Volts'})
% 
% CB1 = colorbar;
% CB1.Label.String = 'pH Stem Version #';
% CB1.Label.FontSize = 12;
% ax(1).Position = ax_pos1; % get axes size back to original, cb moves auto
% 
% % ax(1).XTick = ct;
% % ax(1).XTickLabel = UW;
% 
% ax(1).FontSize = 14;
% 
% 
% % ***********
% ax(2) = subplot(4,1,2);
% plot(ct, k0_ON, 'ko','MarkerFaceColor','r');
% %xlabel('Sensor Count')
% ylabel({'k0' 'Volts'})
% hold on
% plot(ct, k0_OFF, 'ko','MarkerFaceColor','b');
% plot(ct, k0_HCL, 'ko','MarkerFaceColor','g');
% hold off
% ax_pos2 = ax(2).Position;
% ax(2).Position = [ax_pos1(1), ax_pos2(2),ax_pos1(3), ax_pos2(4)];
% 
% L2 = legend('Pump ON','Pump OFF', 'HCL');
% L2.NumColumns = 2;
% L2.Location = 'NorthWest';
% ax(2).FontSize = 14;
% 
% 
% % ***********
% ax(3) = subplot(4,1,3);
% plot(ct, (k0_ON - k0_OFF)*1e3, 'ko','MarkerFaceColor','r');
% %xlabel('Sensor Count')
% ylabel({'k0 difference' 'milliVolts'})
% hold on
% plot(ct, (k0_ON - k0_HCL)*1e3, 'ko','MarkerFaceColor','b');
% 
% hold off
% ax_pos3 = ax(3).Position;
% ax(3).Position = [ax_pos1(1), ax_pos3(2),ax_pos1(3), ax_pos3(4)];
% 
% L3 = legend('Pump ON - OFF','Pump ON - HCL');
% L3.NumColumns = 2;
% L3.Location = 'SouthWest';
% ax(3).FontSize = 14;
% 
% % ***********
% ax(4) = subplot(4,1,4);
% plot(ct, (k0_ON - k0_OFF)*1e6, 'ko','MarkerFaceColor','r');
% xlabel('Sensor Count')
% ylabel({'k0 pON - k0 pOFF' 'uVolts'})
% ax_pos4 = ax(4).Position;
% ax(4).Position = [ax_pos1(1), ax_pos4(2),ax_pos1(3), ax_pos4(4)];
% ax(4).FontSize = 14;
% 
% % ************************************************************************
% % ************************************************************************
% F2 = figure(2);
% F2.Units = 'Normalized';
% F2.Position = [0.2000 0.2775 0.4646 0.6517];
% 
% % BUILD COLOR ARRAYS FOR PLOTTING
% CMAP   = colormap;
% uVER   = unique(SV);
% clr_ct = linspace(min(uVER),max(uVER), size(CMAP,1));
% pCMAP  = interp1(clr_ct,CMAP,SV);
% 
% % ***********
% ax2(1) = subplot(3,2,1:2);
% ax2(1).Position = ax(1).Position + [0.025 0 -0.1 0];
% ax2_pos1 = ax2(1).Position;
% S2 = scatter(ct, k2(:,end), 25 ,SV, 'filled','o');
% xlabel('Sensor Count')
% ylabel({'k2' 'Press = 0'})
% ax_pos = ax2(1).Position;
% 
% CB2 = colorbar;
% CB2.Label.String = 'pH Stem Version #';
% CB2.Label.FontSize = 12;
% ax2(1).Position = ax2_pos1; % get axes size back to original, cb moves auto
% ax2(1).FontSize = 14;
% 
% % Plot f(P)'s
% ax2(2) = subplot(3,2,[3,5]);
% p2 = plot(FP', P');
% ax2(2).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% colororder(ax2(2), pCMAP)
% ax2(2).FontSize = 14;
% 
% 
% % ********************************************
% % Plot f(P)'s
% ax2(3) = subplot(3,2,[4,6]);
% 
% mV6 = median(FP(tf_V6,:),1,'omitnan')';
% sV6 = std(FP(tf_V6,:),[],1,'omitnan')';
% fillV6  = [[mV6-1*sV6;flipud(mV6+1*sV6)],[P';flipud(P')]];
% 
% mV0 = median(FP(tf_SBE,:),1,'omitnan')';
% sV0 = std(FP(tf_SBE,:),[],1,'omitnan')';
% fillV0  = [[mV0-1*sV0;flipud(mV0+1*sV0)],[P';flipud(P')]];
% 
% mV7 = median(FP(tf_GDF,:),1,'omitnan')';
% sV7 = std(FP(tf_GDF,:),[],1,'omitnan')';
% fillV7  = [[mV7-1*sV7;flipud(mV7+1*sV7)],[P';flipud(P')]];
% 
% mV5 = median(FP(tf_V5,:),1,'omitnan')';
% sV5 = std(FP(tf_V5,:),[],1,'omitnan')';
% fillV5  = [[mV5-1*sV5;flipud(mV5+1*sV5)],[P';flipud(P')]];
% 
% mV4 = median(FP(tf_V4,:),1,'omitnan')';
% sV4 = std(FP(tf_V4,:),[],1,'omitnan')';
% fillV4  = [[mV4-1*sV4;flipud(mV4+1*sV4)],[P';flipud(P')]];
% 
% 
% p21 = plot(mV6, P', '-b','LineWidth',3);
% ax2(3).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% hold on
% FL01 = patch(fillV6(:,1),fillV6(:,2),'b');
% FL01.FaceAlpha = 0.1';
% FL01.LineStyle = 'none';
% 
% hold on
% 
% 
% p22 = plot(mV0, P', '-r','LineWidth',3);
% ax2(3).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% hold on
% FL02 = patch(fillV0(:,1),fillV0(:,2),'r');
% FL02.FaceAlpha = 0.1; 
% FL02.LineStyle = 'none';
% 
% clr = [0 153 76]/256;
% p23 = plot(mV7, P', '-','LineWidth',3,'Color',clr);
% ax2(3).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% hold on
% FL03 = patch(fillV7(:,1),fillV7(:,2),clr);
% FL03.FaceAlpha = 0.25; 
% FL03.LineStyle = 'none';
% 
% clr = [255 120 0]/256;
% p24 = plot(mV5, P', '-','LineWidth',3,'Color',clr);
% ax2(3).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% hold on
% FL04 = patch(fillV5(:,1),fillV5(:,2),clr);
% FL04.FaceAlpha = 0.25; 
% FL04.LineStyle = 'none';
% 
% clr = [153 0 153]/256;
% p25 = plot(mV4, P', '-','LineWidth',3,'Color',clr);
% ax2(3).YDir = 'Reverse';
% xlabel('f(P)')
% ylabel('Pressure')
% hold on
% FL05 = patch(fillV4(:,1),fillV4(:,2),clr);
% FL05.FaceAlpha = 0.25; 
% FL05.LineStyle = 'none';
% 
% 
% 
% 
% L23 = legend([p21,p22,p23,p24,p25], ...
%     'MBARI V6', 'SBS', 'GDF','MBARI V5','MBARI V4','Location', 'NorthEast');
% ax2(3).FontSize = 14;
% ax2(3).XLim = ax2(2).XLim;
% 
% 
% 
% 
% 
% 
