function SBE = parse_SBEpHcal_doc(fp)

% fp = path to microsoft word doc

% ************************************************************************
% TESTING
% fn ='10742 Stem Calsheet.docx';
% %fn ='10590 Stem Cal Sheet.docx';
% fn = '11085 Stem Calsheet.docx';
% % 
% dirs.temp = 'C:\temp\';
% fp = [dirs.temp,fn];
%************************************************************************


% ************************************************************************
% VERIFY FILE EXISTANCE & TYPE & THAT THE CALL COMES FROM A WINDOWS COMPUTER
% Create a temp dir if not already there
if ~isfile(fp)
    fprtintf('WARNING: FILE NOT FOUND: %s\n', fp);
    return
end

if ~strncmp(computer,'PCWIN',5)
    fprtintf('WARNING: THIS IS NOT A WINDOWS PC: %s\n', computer);
    return
end

if isempty(regexp(fp,'docx$','once'))
    fprtintf('WARNING: THIS IS NOT A MICROSOFT WORD DOC: %s\n', fp);
    return
end

temp_dir = 'C:\temp\';
if ~isfolder(temp_dir)
    mkdir(temp_dir)
end

% ************************************************************************
% PREP FOR LOCAL COPY & PROCESS
[fpath, fname, fext] = fileparts(fp);
local_fp = [temp_dir, fname, fext];
if ~strcmp(fp, local_fp)
    copyfile(fp, local_fp);
end



% USE ACTIVEX
word     = actxserver('Word.Application');
wdoc     = word.Documents.Open(local_fp);
wdoc_txt = wdoc.Content.Text;
word.Quit()
delete(word)
clear word wdoc



% ************************************************************************
% EXTRACT CALIBRATION DATA FROM TEXT STRING

% THE NEXT FEW LINES ARE IMPORTANT! 
% Some odd characters need to be removed
% as well as \r. This reg exp line only grabs common visible characters
wdoc_txt = regexprep(wdoc_txt,'\r',' '); % replace carriage return with a space
wdoc_txt = regexprep(wdoc_txt,'[^\w.-() /=]',''); %grab relavent characters

% GET pH Serial number
SN = regexp(wdoc_txt,'(?<=Serial Number\s*[SBE]*\s*)\w+(?=\s*Calib)',...
    'once','match');

if ~isempty(SN)
    SBE.pH.SN = SN;
end

% GET PCOEF FIT ORDER
poly_order = str2double(regexp(wdoc_txt,'\d{1}(?=th\s*order)', ...
    'once','match'));

% GET PCOEF K0 & K2 STRINGS
Pcoef_str = regexp(wdoc_txt,'(?<=f0\s*)[\-\d\.\e\s]+','match','once');
k0_str   = regexpi(wdoc_txt,'(?<=k0\s*\(HCl\)\s*)[\-\d\.]+','match','once');
k2_str   = regexpi(wdoc_txt,'(?<=k2\s*\(HCl\)\s*)[\-\d\.]+','match','once');

if isempty(Pcoef_str) || isempty(k0_str) || isempty(k2_str)
    disp('Not all cooficient lines were properly extracted!')
    disp(['Pcoef str: ', Pcoef_str]);
    disp(['k0_str: ', k0_str]);
    disp(['k2_str: ', k2_str]);
end

% GET NUMBERS FROM STRINGS
coefs  = sscanf(Pcoef_str,'%f');
SBE.pH.fP = coefs(1:poly_order)';

k0_HCL  = str2double(k0_str);
SBE.pH.k0 = k0_HCL;

k2       = str2double(k2_str);
SBE.pH.k2 = k2;













