function SBE = parse_cal_pdf(pdf_file)
%
% This function tries to find various calibration coefficients for
% profiling floats stored in pdf files
%
% A file exchange function by Derek Wood is used to extract info:
% https://www.mathworks.com/matlabcentral/fileexchange/
% 63615-read-text-from-a-pdf-document
% Before pdf reader can work add a couple lines to startup.m to
% switch to the dir where the mfile is found and execute the following line:
% javaaddpath('iText-4.2.0-com.itextpdf.jar')
% it only needs to be executed once upon the start of a matlab session
%
% Several possible pdf calibration file types: 
% APEX:  FLBB, pH(if SBE stem on APEX)
% NAVIS: MCOMS, SUNA, pH, SBE63, SBE83(soon?)

% ************************************************************************
% TESTING
%pdf_file = 'C:\temp\1114-12157-10374 Float Calsheet.pdf'; % pH
%pdf_file = 'C:\temp\SBE B5 CalSheet.pdf'; % pH

%pdf_file = 'C:\temp\SBE 10399 CalSheet.pdf'
%pdf_file = 'C:\temp\11018 Stem Calsheet.pdf'

%pdf_file = 'C:\temp\MCOMS-0286 FDOM Char Sheet.pdf'; % FDOM
%pdf_file = 'C:\temp\MCOMS_034_CDOM.pdf'; %CDOM

%pdf_file = 'C:\temp\FLBBAP2-5534_(CHL)_CharSheet.pdf';
%pdf_file = 'C:\temp\MCOMS-0286 CHL Char Sheet.pdf';    % CHL
%pdf_file = 'C:\temp\MCOMSB-129_(CHL)CharSheet.pdf';

%pdf_file = 'C:\temp\FLBBAP2-5534_(700nm)_CharSheet.pdf'; % BBP
%pdf_file = 'C:\temp\FLBBAP2-6333_CharSheets.pdf'; %CHL & BBP in one pdf
%pdf_file = 'C:\temp\MCOMS-0286 700nm Char Sheet.pdf'; %BBP
%pdf_file = 'C:\temp\10952 Stem pH Calibration Sheet.pdf';
%pdf_file = 'C:\temp\10954 Stem pH Calibration Sheet.pdf';

%pdf_file = 'C:\temp\FLBBAP2-6121_CharSheets.pdf'

%pdf_file = 'C:\temp\11045 Stem Calsheet.pdf';
%pdf_file = 'C:\temp\11022 Stem Calsheet.pdf';
% ************************************************************************

tmp = pdfRead(pdf_file); % get text from pdf file

% THE NEXT FEW LINES ARE IMPORTANT! The pdf reader pulls in some odd
% hidden characters. This reg exp line only grabs common visible
% characters & leaves the hidden ones behind via indexing
pdf_txt = tmp{1,1};
inds    = regexp(pdf_txt,'[\w.-() /=]'); % this removes hidden chars in str
pdf_txt = pdf_txt(inds);

% CHL & BBP cals in same pdf(Ex floats 19072 and 19018)
% {1,1) is CHL & {1,2} is BBP
if size(tmp,2) == 2 
    pdf_txt2 = tmp{1,2};
    inds     = regexp(pdf_txt2,'[\w.-() /=]'); % this removes hidden chars in str
    pdf_txt2 = pdf_txt2(inds);
end

% *************************************************************************
% pH
% *************************************************************************
%if size(regexp(pdf_txt,'f\(P)|k0|k2'),2) == 3 % pH cal sheet
% jp 09/01/21 k in k2 k0 can change case ie "11045 Stem Calsheet.pdf"
if size(regexpi(pdf_txt,'f\(P)|k0|k2'),2) == 3 
    SBE.pH.SN = '';
    SBE.pH.k0 = [];
    SBE.pH.k2 = [];
    SBE.pH.fP = [];
    
    % GET pH Serial number
%     SN = regexp(pdf_txt,'(?<=Serial Number SBE\s*)\w+(?=\s*Calib)', ...
%         'once','match');
% "SBE" not alsways in SN string blockso do "zero or more",ie "11045 Stem Calsheet.pdf"
    SN = regexp(pdf_txt,'(?<=Serial Number (SBE)*\s*)\w+(?=\s*Calib)', ...
        'once','match');
    if ~isempty(SN)
        SBE.pH.SN = SN;
    end
    
    % GET pH COEFFICIENTS
    poly_order = str2double(regexp(pdf_txt,'\d{1}(?=th\s*order)','once','match'));

    coef_str   = regexp(pdf_txt,'(?<=f0\s*)\-*\d{1}.+\d{1}(?=\s*Sea)', ...
                 'match','once');    
    
    coefs      = str2double(regexp(coef_str,' ','split'));
    
    % JP fix for non standard SBE pdf file 09/01/21
    tnan       = isnan(coefs);
    if sum(tnan,2) > 0
        disp('WARNING: non standard SBE pH Calibration file!')
        disp('Removing NaNs in coefs array & trying to procede.....')
        coefs = coefs(~tnan);
    end
    
    if size(coefs,2) == poly_order + 3 % f(p) f0 + k0 + k2 ALL GOOD
        SBE.pH.fP = coefs(1:poly_order);
        SBE.pH.k0 = coefs(poly_order+2);
        SBE.pH.k2 = coefs(poly_order+3);
    else
        disp('pH coeficient count is ambiguous')
    end
end

% *************************************************************************
% CHLOROPHYLL
% *************************************************************************
if size(regexpi(pdf_txt,'chlorophyll'),2) > 0 % chl calibration
    chl_text      = pdf_txt;
    SBE.Chl.SN    = '';
    SBE.Chl.Scale = [];
    SBE.Chl.DC    = [];
    
    % Get serial number
    SN = regexp(chl_text ,'(?<=S/N.+-)\d+','once','match');
    if ~isempty(SN)
        SBE.Chl.SN = SN;
    end
    
    % Get dark counts
    DC = regexp(chl_text ,'(?<=Date\s*)\d+(?=\s*counts)','once','match');
    if ~isempty(DC)
        SBE.Chl.DC = str2double(DC);
    end
    
    % Get Scale factor
    if regexp(chl_text,'FLBB','once')
        SF = regexp(chl_text ,'(?<=counts\s*)[\dE\.\-]+(?=\s*.g/l/count)', ...
            'once','match');
        if ~isempty(SF)
            SBE.Chl.Scale = str2double(SF);
        end
    elseif regexp(chl_text ,'MCOMS','once') % 2 flavors for MCOMS (at least!)
        SF = regexp(chl_text ,'(?<=equation)[\dE\.\-]+','once','match');
        if isempty(SF)
            SF = regexp(chl_text,'[\dE\.\-]+(?=\s*.g CHL l-1)','once','match');
        end
        if ~isempty(SF)
            SBE.Chl.Scale = str2double(SF);
        end
    end
end
clear Dc SF SN chl_txt

% *************************************************************************
% BBP
% *************************************************************************
if exist('pdf_txt2','var')
    bbp_txt = pdf_txt2;
else
    bbp_txt = pdf_txt;
end

if size(regexpi(bbp_txt,'Wavelength 700'),2) > 0 % bbp700 calibration

    SBE.bbp700.SN    = '';
    SBE.bbp700.Scale = [];
    SBE.bbp700.DC    = [];
    
    % Get serial number
    SN = regexp(bbp_txt,'(?<=FLBB.+-|MCOMS.+-)\d+','once','match');
    if ~isempty(SN)
        SBE.bbp700.SN = SN;
    end
    
    % Get dark counts 1st occurance of pattern is DC
    DC = regexp(bbp_txt,'\d+(?=\s*counts)','once','match');
    if ~isempty(DC)
        SBE.bbp700.DC = str2double(DC);
    end
    
    % Get Scale factor
    SF = regexp(bbp_txt ,'[\dE\.\-]+(?=\s*\(m-1sr-1\)|\s*m-1 sr-1)', ...
        'once','match');
    if ~isempty(SF)
        SBE.bbp700.Scale = str2double(SF);
    end
end
clear bbp_txt

% *************************************************************************
% CDOM or FDOM
% *************************************************************************

if size(regexpi(pdf_txt,'FDOM|CDOM'),2) > 0 % FDOM calibration
    fdom_txt = pdf_txt;
    
    SBE.FDOM.SN    = '';
    SBE.FDOM.Scale = [];
    SBE.FDOM.DC    = [];
    
    % Get serial number
    SN = regexp(fdom_txt,'(?<=MCOMS.+-)\d+','once','match');
    if ~isempty(SN)
        SBE.FDOM.SN = SN;
    end
    
    % Get dark counts 1st occurance of pattern is DC
    DC = regexp(fdom_txt,'\d+(?=\s*counts)','once','match');
    if ~isempty(DC)
        SBE.FDOM.DC = str2double(DC);
    end
    
    % Get Scale factor
    SF = regexp(fdom_txt,'[\dE\.\-]+(?=\s*ppb)', ...
        'once','match');
    if ~isempty(SF)
        SBE.FDOM.Scale = str2double(SF);
    end
end
clear fdom_txt DC SF







