function[cpP,cpT,cpS,flbbFL,flbbBB700,flbbBB470,flbbDepth,optodeO2Conc,optodeDepth,cpDateTimeNum] = parseSciData( inFile )

MAXi=50000;

cpP = NaN(1,MAXi);
cpT = NaN(1,MAXi);
cpS = NaN(1,MAXi);
flbbFL = NaN(1,MAXi);
flbbBB700 = NaN(1,MAXi);
flbbBB470 = NaN(1,MAXi);
flbbDepth = NaN(1,MAXi);
optodeO2Conc = NaN(1,MAXi);
optodeDepth = NaN(1,MAXi);
cpDateTimeNum = NaN;

inFID = fopen(inFile);
%outFID = fopen(strcat(inFile, 'SciData.txt'), 'w');
header = ['Date\tTime\tState\tComment\tDepth\t' ...
            'CTD Pressure\tCTD Temperature\tCTD Salinity\t' ...
            'FLBB FL\tFLBB BB\t' ...
            'O2 Conc (uM)\tO2 Air Sat (%%)\tO2 Temperature (degC)\tO2 Cal Phase (Deg)\t' ...
            'O2 TCPhase (deg)\tO2 C1RPh (deg)\tO2 C2RPh (deg)\tO2 C1 Amp(mV)\t' ...
            'O2 C2Amp (mV)\tO2 RawTEmp (mV)\r\n'];
%fprintf(outFID, header);                

profileIndex= 1;
optodeIndex = 1;
ptsIndex = 1;
cpIndex = 1;
flbbIndex = 1;

%get the header line
inString = fgets(inFID);
    
while ~feof(inFID)
    inString = fgets(inFID);
    inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
    
    if strcmp(inFields{5}, 'SBE41 Message');
        numCommas = length(strfind(inFields{10}, ','));
        if(numCommas == 0 || numCommas == 2)
            profileDate(profileIndex, :) = inFields{2};
            profileTime(profileIndex, :) = inFields{3};
            profileDateTimeNum(profileIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
            profileState{profileIndex} = inFields{4};
            profileComment{profileIndex} = inFields{5};
            profileDepth(profileIndex) = -str2double(inFields{6});
            profileIndex = profileIndex + 1;
        end
        
        if(numCommas == 2)
            ptsFields = strsplit(inFields{10}, ',', 'CollapseDelimiters', false);
            ptsDate(ptsIndex, :) = inFields{2};
            ptsTime(ptsIndex, :) = inFields{3};
            ptsDateTimeNum(ptsIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
            ptsState{ptsIndex} = inFields{4};
            ptsComment{ptsIndex} = inFields{5};
            ptsP(ptsIndex) = str2double(ptsFields{1});
            ptsT(ptsIndex) = str2double(ptsFields{2});
            ptsS(ptsIndex) = str2double(ptsFields{3});

%             if(~strcmp(ptsState{ptsIndex}, 'Ascend'))
%                 fprintf(outFID, '%s\t%s\t%s\t%s\t%f\t%f\t%f\t%f\r\n', ...
%                     ptsDate(ptsIndex,:), ptsTime(ptsIndex,:), ptsState{ptsIndex}, ptsComment{ptsIndex}, ...
%                     profileDepth(profileIndex - 1), ptsP(ptsIndex), ptsT(ptsIndex), ptsS(ptsIndex));
%             end
            
            ptsIndex = ptsIndex + 1;
        end
    end
        
    if ( strcmp(inFields{5}, 'Optode Message') && strncmp(inFields{4},'Ascend',6) ) 
        if(strfind(inFields{16}, '4330') > 0)
            optodeFields = strsplit(inFields{16}, ',', 'CollapseDelimiters', false);
%             optodeDate(optodeIndex, :) = inFields{2};
%             optodeTime(optodeIndex, :) = inFields{3};
%             optodeDateTimeNum(optodeIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
%             optodeState{optodeIndex} = inFields{4};
%             optodeComment{optodeIndex} = inFields{5};
            optodeDepth(optodeIndex) = -str2double(inFields{6});
            optodeO2Conc(optodeIndex) = str2double(optodeFields{4});
            optodeAirSat(optodeIndex) = str2double(optodeFields{6});
            optodeTemp(optodeIndex) = str2double(optodeFields{8});
%             optodeCalPhase(optodeIndex) = str2double(optodeFields{11});
%             optodeTCPhase(optodeIndex) = str2double(optodeFields{13});
%             optodeC1RPh(optodeIndex) = str2double(optodeFields{15});
%             optodeC2RPh(optodeIndex) = str2double(optodeFields{17});
%             optodeC1Amp(optodeIndex) = str2double(optodeFields{19});
%             optodeC2Amp(optodeIndex) = str2double(optodeFields{21});
%             optodeRawTemp(optodeIndex) = str2double(optodeFields{23});
            
%             fprintf(outFID, '%s\t%s\t%s\t%s\t%f\t\t\t\t\t\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\r\n', ...
%                 optodeDate(optodeIndex,:), optodeTime(optodeIndex,:), optodeState{optodeIndex}, optodeComment{optodeIndex}, ...
%                 optodeDepth(optodeIndex), optodeO2Conc(optodeIndex), optodeAirSat(optodeIndex), ...
%                 optodeTemp(optodeIndex), optodeCalPhase(optodeIndex), optodeTCPhase(optodeIndex), ...
%                 optodeC1RPh(optodeIndex), optodeC2RPh(optodeIndex), optodeC1Amp(optodeIndex), ...
%                 optodeC2Amp(optodeIndex), optodeRawTemp(optodeIndex));
            
            optodeIndex = optodeIndex + 1;
        end
    end
    
    if strcmp(strtrim(inFields{5}), 'FLBB Message')
        numCommas = length(strfind(inFields{15}, ','));
        if(numCommas == 7)
            flbbFields = strsplit(inFields{15}, ',', 'CollapseDelimiters', false);
            flbbDate(flbbIndex, :) = inFields{2};
            flbbTime(flbbIndex, :) = inFields{3};
            flbbDateTimeNum(flbbIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
            %flbbState{flbbIndex} = inFields{4};
            %flbbComment{flbbIndex} = inFields{5};
            flbbDepth(flbbIndex) = -str2double(inFields{6});
            flbbFL(flbbIndex) = str2double(flbbFields{7});
            flbbBB700(flbbIndex) = str2double(flbbFields{6});
            flbbBB470(flbbIndex) = str2double(flbbFields{4});
                      
%             fprintf(outFID, '%s\t%s\t%s\t%s\t%f\t\t\t\t%f\t%f\r\n', ...
%                 flbbDate(flbbIndex,:), flbbTime(flbbIndex,:), flbbState{flbbIndex}, flbbComment{flbbIndex}, ...
%                 flbbDepth(flbbIndex), flbbFL(flbbIndex), flbbBB(flbbIndex));
            
            flbbIndex = flbbIndex + 1;
        end
    end
    
    if (strfind(inFields{4}, 'Dumping CP Data') > 0);
        %disp(inString);
        if(length(inFields) >= 10)
            numCommas = length(strfind(inFields{10}, ','));
            if(numCommas == 2)
                
                cpFields = strsplit(inFields{10}, ',', 'CollapseDelimiters', false);
                %cpDate(cpIndex, :) = inFields{2};
                %cpTime(cpIndex, :) = inFields{3};
                cpDateTimeNum = datenum(strcat(inFields{2}, 'T', inFields{3}));
                cpState(cpIndex, :) = inFields{4};
                cpComment(cpIndex, :) = inFields{5};
                cpDepth(cpIndex) = -str2double(inFields{6});
                cpP(cpIndex) = str2double(cpFields{1});
                if(cpP(cpIndex)<5 || cpP(cpIndex)>600)
                    cpP(cpIndex)=double(nan);
                    cpT(cpIndex)=double(nan);
                    cpS(cpIndex)=double(nan);
                else
                    cpT(cpIndex) = str2double(cpFields{2});
                    cpS(cpIndex) = str2double(cpFields{3});
                end
%                 fprintf(outFID, '%s\t%s\t%s\t%s\t%f\t%f\t%f\t%f\r\n', ...
%                     cpDate(cpIndex,:), cpTime(cpIndex,:), cpState(cpIndex,:), cpComment(cpIndex,:), ...
%                     cpDepth(cpIndex), cpP(cpIndex), cpT(cpIndex), cpS(cpIndex));
                cpIndex = cpIndex + 1;
            end
        end
    end
    
end

goingtoplot=0;
if(goingtoplot)
yLimits = [10*floor(min(profileDepth)/10) 0];


subplot(2,2,1), plot(profileDateTimeNum, profileDepth);
set(gcf, 'units', 'normalized', 'Position', [.1, .1, .5, .5]);
set(gca, 'Ylim', yLimits);
grid;
datetick('x');
title('Profile Depth vs. Time');
ylabel('dBar');

subplot(2,2,2);
line(cpT, -cpP, 'Color', 'k')
ylabel('dBar');
set(gca, 'YLim', yLimits);
ax1 = gca; % current axes
ax1.XColor = 'k';
ax1.YColor = 'k';

ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position', ax1_pos,...
    'XAxisLocation', 'top', ...
    'YAxisLocation', 'right', ...
    'XColor', 'r', 'YColor', 'r', ...
    'Color', 'none');
set(gca, 'Ylim', yLimits);
line(cpS, -cpP, 'Parent', ax2, 'Color', 'r')

grid;
set(ax1.XLabel, 'String', 'CP Temperature');
set(ax2.XLabel, 'String', 'CP Salinity');
set(ax1.YLabel, 'String', 'dBar');
set(ax2.YLabel, 'String', 'dBar');

subplot(2,2,3), plot(optodeO2Conc(2:end), optodeDepth(2: end));
set(gca, 'Ylim', yLimits);
grid;
title('O2 Concentration vs Depth');

subplot(2,2,4)
line(flbbFL(3:end), flbbDepth(3:end), 'Color', 'g')
% set(gca, 'Ylim', yLimits);
 ax1 = gca; % current axes
% ax1.XColor = 'g';
% ax1.YColor = 'g';
% 
% ax1_pos = ax1.Position; % position of first axes
% ax2 = axes('Position', ax1_pos,...
%     'XAxisLocation', 'top', ...
%     'YAxisLocation', 'right', ...
%     'XColor', 'r', 'YColor', 'r', ...
%     'Color', 'none');
% set(gca, 'Ylim', yLimits);
% line(flbbBB(3:end), flbbDepth(3:end), 'Parent', ax2, 'Color', 'r')
% 
 grid;
% set(ax1.XLabel, 'String', 'FLBB chl');
% set(ax2.XLabel, 'String', 'FLBB Blue');
set(ax1.YLabel, 'String', 'dBar');
set(ax2.YLabel, 'String', 'dBar');
title('FLBB2F chl signal vs Depth');


ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');

text(0.5, 1,'\bf Dive #1 36.8003N 121.8218W 10FEB16 1036 PST','HorizontalAlignment' ,'center','VerticalAlignment', 'top');

end %end ifgoingtoplot


fclose(inFID);
%fclose(outFID);

end