function[p,bp,battv,batti,cpDateTimeNum,PID_u,PID_uCPS,state,sedtrap,numSBDIX,band,lat,lon,latlontime]= parseEngData( inFile ,sedfile)

%inFile = '../BOG3/2023MAY15/raw/2023-5-15T19-3-24.eng';
%sedfile = 'sedfile.dump';


inFID = fopen(inFile);
%outFID = fopen(strcat(inFile, 'EngData.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);                

%fpdeb = fopen("debug.txt","a");
fpsed = fopen(sedfile,'a');
lat = 0; lon=0;latlontime=0;
profileIndex= 1;
PIDIndex = 1;
SEDhomeIndex=1;
SEDnextIndex=1;

doplots=0;

MAXi=500000;
MAXSED=120;

p = NaN(1,MAXi);
bp = NaN(1,MAXi);
battv = NaN(1,MAXi);
batti = NaN(1,MAXi);
%cpDate = NaN(1,MAXi);
%cpTime = NaN(1,MAXi);
cpDateTimeNum = NaN(1,MAXi);
format longg,cpDateTimeNum;
PID_u = NaN(1,MAXi);
PID_uCPS = NaN(1,MAXi);
state = NaN(1,MAXi);
band = NaN(1,MAXi);
numSBDIX = 0;


sedtrap = struct('advp',NaN(1,MAXSED),'homep',NaN(1,MAXSED),'sedhome',NaN(1,MAXSED),'sedadv',NaN(1,MAXSED),'sedno',NaN(1,MAXSED));

%sedhome = NaN(1,MAXSED);
format longg,sedtrap.sedhome;
%sednext = NaN(1,MAXSED);
format longg,sedtrap.sedadv;

%get the header line
inString = fgets(inFID);
    
while ~feof(inFID)
    inString = fgets(inFID);
    %fprintf('%s\n',inString);
    inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
    %disp(inString);
    %get date, time, and state
    if( strfind(inString,'Date') )
    else
        %disp(PIDIndex);
        %disp(inFields);
        state(PIDIndex) = state_finder_BOG2(inFields{4});
        cpDateTimeNum(PIDIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
    end
    
    if (strfind(inFields{5}, 'SBE41 Message') > 0);
       p(PIDIndex) = -1*str2double(inFields{11}); 
       if p(PIDIndex) < -250
           p(PIDIndex) = NaN;
       end
       %fprintf(fpdeb,inString);
       %fprintf(fpdeb,'p(%d) = %f\n',PIDIndex,p(PIDIndex));
    end
 
    if(length(inFields) >= 39)
        if (strfind(inFields{39}, 'AT+SBDIX') > 0);
         numSBDIX=numSBDIX+1; 
        end
        if(strfind(inFields{38},'UBX') > 0)
            A = sscanf(inFields{38},'UBX,%d,%f,%f,%c,%f');
            latlontime = cpDateTimeNum(PIDIndex);
            temp = sprintf('%f',A(3));
            latf = sscanf(temp,'%2f%f');
            lat = latf(1) + latf(2)/60;
            temp = sprintf('%f',A(5));
            lonf = sscanf(temp,'%3f%f');
            lon = lonf(1) + lonf(2)/60;
            fprintf('UBX, %f, %f, %f\n',latlontime,lat,lon);
            
        end
    end
    
    
      if (strfind(inFields{5}, 'Scend ') > 0);
        if(length(inFields) >= 8)
            %disp(inString);
            %disp(inFields{26});
            p(PIDIndex) = -1*str2double(inFields{6});
            bp(PIDIndex) = str2double(inFields{7});
            battv(PIDIndex) = str2double(inFields{8});
            batti(PIDIndex) = str2double(inFields{9});
            i12v(PIDIndex) = str2double(inFields{35});
            
            if (strfind(inFields{5}, 'Outside') > 0)
                band(PIDIndex)=3;
            end
            if (strfind(inFields{5}, 'Approach') > 0)
                band(PIDIndex)=2;
            end
            if (strfind(inFields{5}, 'In Band') > 0)
                band(PIDIndex)=1;
            end
            
            %PID_SP(PIDIndex) = str2double(inFields{26});
            %PID_PV(PIDIndex) = str2double(inFields{27});
            %PID_Y1(PIDIndex) = str2double(inFields{28});
            %PID_Y2(PIDIndex) = str2double(inFields{29});
            %PID_I(PIDIndex) = str2double(inFields{30});
            %PID_v(PIDIndex) = str2double(inFields{31});
            %PID_u(PIDIndex) = str2double(inFields{32});
            %disp(PID_u(PIDIndex));
            %PID_uCPS(PIDIndex) = str2double(inFields{33});
            
            %cpDate(PIDIndex, :) = inFields{2};
            %cpTime(PIDIndex, :) = inFields{3};
            
            
            
           % numCommas = length(strfind(inFields{10}, ','));
           % if(numCommas == 2)
%                 cpFields = strsplit(inFields{10}, ',', 'CollapseDelimiters', false);

%                 cpState(cpIndex, :) = inFields{4};
%                 cpComment(cpIndex, :) = inFields{5};
%                 cpDepth(cpIndex) = -str2double(inFields{6});
%                 cpP(cpIndex) = str2double(cpFields{1});
%                 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
    if (strfind(inFields{5},'Vel PID') > 0)
        %disp('found PID line');
       if(length(inFields) >= 8)
           PID_u(PIDIndex) = str2double(inFields{29});
           PID_uCPS(PIDIndex) = str2double(inFields{30});
           %disp(inFields);disp(PID_u(PIDIndex));
       end
    end

    if (strfind(inFields{5},'SedTrap Message') > 0)
        disp(inString);
        fprintf(fpsed,'%s',inString);
       if(strfind(inFields{40},'ADVANCING') > 0)
           inSed = strsplit(inFields{40}, ' ', 'CollapseDelimiters', false);         
           sedtrap.sedno(SEDnextIndex) = str2double(inSed{8});
           sedtrap.sedadv(SEDnextIndex) = cpDateTimeNum(PIDIndex);
           disp(sedtrap.sedno(SEDnextIndex));
           sedtrap.advp(SEDnextIndex) = -1*str2double(inFields{6});
           SEDnextIndex = SEDnextIndex+1;
       else
           sedtrap.sedhome(SEDhomeIndex) = cpDateTimeNum(PIDIndex);
           sedtrap.homep(SEDhomeIndex) = -1*str2double(inFields{6});
           SEDhomeIndex = SEDhomeIndex+1;      
       end
    end
    
    PIDIndex = PIDIndex+1;
    
end

if (doplots)
 [ax,h1,h2]=plotyy(1:size(p,2),p,1:size(bp,2),bp);
 ylabel('Depth(m)');           % puts a label on the left axis
 axes(ax(2))                  % makes the 2nd yaxis the current axis.
 ylabel('Bellows Position');    % puts a label on the right axis.
 grid on;
 
 figure(2);
  [ax,h1,h2]=plotyy(1:size(p,2),p,1:size(battv,2),battv);
 ylabel('Depth(m)');           % puts a label on the left axis
 axes(ax(2))                  % makes the 2nd yaxis the current axis.
 ylabel('BattV');    % puts a label on the right axis.
 grid on;
 
  figure(3);
  [ax,h1,h2]=plotyy(1:size(p,2),p,1:size(batti,2),batti);
 ylabel('Depth(m)');           % puts a label on the left axis
 axes(ax(2))                  % makes the 2nd yaxis the current axis.
 ylabel('BattI');    % puts a label on the right axis.
 grid on;
 
   figure(4);
  [ax,h1,h2]=plotyy(1:size(p,2),p,1:size(i12v,2),i12v);
 ylabel('Depth(m)');           % puts a label on the left axis
 axes(ax(2))                  % makes the 2nd yaxis the current axis.
 ylabel('12V I');    % puts a label on the right axis.
 grid on;
end

fclose(fpsed);
%fclose(fpdeb);
end 

