%Author: B. Kieft
%
%Note: Used to process typical event data from a BED
%
%Copyright (c) 2013 MBARI
%MBARI Proprietary Information.  All Rights Reserved
%
function []= processBEDData(fileName)
close all;

addpath ./include

% if( exist('fileName') )
%   BED = fileName;
% else
%   BED = input('Please enter a filename including path in single quotes (or set var fileName prior to running):');
% end

BED = fileName;
if( exist('samplePeriod') )
  Ts = samplePeriod;
else
  Ts = .02;   %Sample Period; 50 Hz sample rate. 
end

   
fid = fopen(BED);
i=1;

% Read file one line at a time...
while feof(fid) < 1,
  currLine = fgets(fid);
  % Check for IMU data
  if length(currLine) > 35 & strcmp(currLine(1:8),'Inertial') == 1, % The line is long enough and seems to have inertial data in it 
    %To obtain intertial data, a line as below is expected 
    %"Inertial Data  acc: -0.00047 -0.00081 -0.02039  quat: -0.29102 -0.79346 -0.04529  0.53271"
    
    % Grab accelerations
    [a,count] = sscanf(currLine,'%*s %*s  %*s %f %f %f quat: %*f %*f %*f %*f');
      if count == 3
        %X is 1, Y is 2, Z is 3
        %acc_wrld(i) = a'
        %accelerations come out in g's
        acc_wrld(i,1) = a(1);
        acc_wrld(i,2) = a(2);
        acc_wrld(i,3) = a(3);
      end
    % Grab quaternion
    [a,count] = sscanf(currLine,'%*s %*s  %*s %*f %*f %*f quat: %f %f %f %f');
      if count == 4
        quat(i,1) = a(1);
        quat(i,2) = a(2);
        quat(i,3) = a(3);
        quat(i,4) = a(4);
  
      end      
  else  % Otherwise it's time or just junk. We want time.
    if length(currLine) > 30 & strcmp(currLine(1:6),'Second') == 1,
      %To obtain time data, a line as below is expected 
      %"Second Marker 03/18/2013 12:54:00"
%       [t,count] = sscanf(currLine,'%*s %*s %*f/%*f/%*f %f:%f:%f');
%       if count == 3
%        dataTime(i)= t(1)*3600+t(2)*60+t(3);  
%       
%       end
      [t,r]=strtok(currLine,'Second Marker');
      dataTime(i)=datenum(strcat(t,r));
      % The line right after time is pressure and looks like:
      % "Ext Pressure 24519 counts  0.94 volts 23.986 bar"
      % Or, now that we have A/D data:
      % "Sys Rcd 04/04/2013 07:00:07 Batt 26.37V Vmodem 25.13V ExtPress  1.212 bar IntPress 15.10 psia Temp 17.0 degC Hum 15.1"
      currLine = fgets(fid);          
      i = i + 1;
      
      % Check for full data line first
      if strcmp(currLine(1:3),'Sys') == 1,
        [a,count] = sscanf(currLine,'%*s %*s %*f/%*f/%*f %*f:%*f:%*f Batt %*fV Vmodem %*fV ExtPress  %f bar IntPress %*f psia Temp %*f degC Hum %*f');
        if count == 1
          pressure(i) = a(1);
        end               
      else % Or grab partial data instead
       % [a,count] = sscanf(currLine,'%*s %*s %*f counts  %*f volts %f bar');
        [a,count] = sscanf(currLine,'%*s %*s %*f counts  %f volts');
        if count == 1
          pressure(i) = a(1);
        end         
      end
    end
  end
  i = i+1; 
end
fclose(fid);

plotBEDs;
%close all;
%save(fileName, 'AllData');
