function [data]=ReadMsgFile_mc(path_data,msg_file);

% Purpose: Read MSG file and extract LR profile data as well as BGC data
% from Anchor Points (i.e. when CPF is parked at depth).

% clear all
% close all
% path_data='./';
% filename='CPF003.001r1.msg.txt'; % test file from Gene, no modifications needed from original .msg file
%

% fid = fopen([path_data,filename]);
addpath(genpath('/Users/Mau/Documents/MATLABhelp_new'));

filename=msg_file;
% % clear all
% msg_file ='/Volumes/CPF/CPF003/Data/ParagonTests/2022-3-14T16-30-13/CPF003.004.msg'; % MC, profiles that have Anchor data (~6h), 001,002,,003,005,013

msg_file=[path_data,filename]; % note, redefining msg_file here to include the path
fid = fopen(msg_file);

% extract date stamp from msg_file name
ddate=regexp(msg_file,['(?<year>\d{4})-(?<month>\d+)-(?<day>\d+)'],'names');
dirs.date=[ddate.year,'-',ddate.month,'-',ddate.day]
cast=regexp(msg_file,'\d+(?=\.msg)','once','match');

rowIndex = 1;
getHeaderLine = true;
while ~feof(fid)
    inLine = fgetl(fid);
    if(getHeaderLine)
        if strncmp(inLine, "Comment", 7)
            dataLines{rowIndex} = vertcat(strsplit(inLine, '\t'));
            rowIndex = rowIndex + 1;
            getHeaderLine = false;
        end
    end

    if(strncmp(inLine, "AirCal", 6) || ...
            strncmp(inLine, "AnchorPt", 8) || ...
            strncmp(inLine, "AscendPt", 8))
        dataLines{rowIndex} = vertcat(strsplit(inLine, '\t'));
        rowIndex = rowIndex + 1;
    end
end

fclose(fid);

dataLines = dataLines';

% MC, modified the following lines to account for different columns for
% AscendPt/AnchorPt and AirCal data. Gene will eventually add NaNs to the
% two columns that have missing data (blanks)

for f=1:length(dataLines)
    tsize(f,:)=size(dataLines{f});
end

dataLines = vertcat(dataLines{find(tsize(:,2)==17)});
dataTable = array2table(dataLines(tsize(:,2)==17,:), 'VariableNames', dataLines(1,:));

dataTable.dt = datetime(dataTable.DateTime, 'InputFormat', 'yyyy-MMM-dd''T''HH:mm:ss''Z');
% plot(dataTable.dt, str2double(dataTable.pres_dBar))

%% Create index for AnchorPt lines
C=dataTable(:,1);
indA=zeros(size(C));

%% Will do this here, but eventually have to add to calc_BGC.m or create a function calc_PHYS.m
mTime=dataTable(:,2);
Pres=dataTable(:,3);
Temp=dataTable(:,4);
Sal=dataTable(:,5);
Topt=dataTable(:,6);
TPhase=dataTable(:,7);
RPhase=dataTable(:,8);
pH=dataTable(:,9);
NO3=dataTable(:,10);
FSig=dataTable(:,11);
BbSig=dataTable(:,12);
TSig=dataTable(:,13);
OCRCh1=dataTable(:,14);
OCRCh2=dataTable(:,15);
OCRCh3=dataTable(:,16);
OCRCh4=dataTable(:,17);

dtime=datenum(dataTable.dt);

nM=size(Pres); nM=nM(1);

for m=2:nM-1
    if strcmp(cell2mat(C{m,1}),'AnchorPt:')

        D(m-1,2)=str2num(cell2mat(Pres{m,1}));
        D(m-1,3)=str2num(cell2mat(Temp{m,1}));
        D(m-1,4)=str2num(cell2mat(Sal{m,1}));
        D(m-1,5)=str2num(cell2mat(Topt{m,1}));
        D(m-1,6)=str2num(cell2mat(TPhase{m,1}));
        D(m-1,7)=str2num(cell2mat(RPhase{m,1}));
        D(m-1,8)=str2num(cell2mat(pH{m,1}));
        D(m-1,9)=str2num(cell2mat(NO3{m,1}));
        D(m-1,10)=str2num(cell2mat(FSig{m,1}));
        D(m-1,11)=str2num(cell2mat(BbSig{m,1}));
        D(m-1,12)=str2num(cell2mat(TSig{m,1}));
        D(m-1,13)=str2num(cell2mat(OCRCh1{m,1}));
        D(m-1,14)=str2num(cell2mat(OCRCh2{m,1}));
        D(m-1,15)=str2num(cell2mat(OCRCh3{m,1}));
        D(m-1,16)=str2num(cell2mat(OCRCh4{m,1}));

        mtime=dtime(m,1);
        D(m-1,1)=mtime;

        dt(m-1,1)=dtime(m,1);
    elseif strcmp(cell2mat(C{m,1}),'AscendPt:')
        break
    end
end



VariableNames=dataTable(1,:); % comment line
data.park_hdr=table2cell(VariableNames(1,2:end-1))';
data.park_d=D;

% nlines=size(D,1);
% if nlines<=1 % if no park data
%     data.park_d=[];
%     data.park_hdr={};
%     return
% end

% NOTE: if only 1 AnchorPt, no real good Anchor data (that line should be
% removed from msg files)


% Saving data structure
% park_hdr: {15×1 cell}
% park_d: [6×15 double]

% NOTE: data structure spit out by parse_CPFmsg_v5.m
%            lr_hdr: {15×1 cell}
%              lr_d: [6×15 double]
%            hr_hdr: {4×1 cell}
%              hr_d: [17×4 double]
%           ocr_hdr: []
%             ocr_d: []
%              cast: 1
%               sdn: 7.3860e+05
%               gps: [-121.9399 36.8884]
%               air: []
%            aircal: [5×4 double]
%             FwRev: []
%     CpActivationP: []
%          FlbbMode: NaN
%           CTDtype: ''
%             CTDsn: ''
%               EOT: 0