% classifyEpochs.m
%
% Vivek Nadkarni
% July 9, 2004
% Trimble

function [segRonly, segGonly, segRcommon, segGcommon, ...
  skipRov, skipGie] = ...
  classifyEpochs (timeR, timeG, startTime, endTime, Ts)

skipRov = (isempty(timeR));
skipGie = (isempty(timeG));

dt = Ts*1000; % dt is in milliseconds
epsForTime = (1000*Ts)/10; % milliseconds
startTimeMsec = startTime*1000;
endTimeMsec = endTime*1000;

if ( skipGie && skipRov )
  % Clear out all the segments
  segRonly = [];
  segGonly = [];
  segRcommon = [];
  segGcommon = [];
elseif (~skipGie && skipRov)
  segRonly = [];
  segGonly = 1:length(timeG);
  segRcommon = [];
  segGcommon = [];
elseif (skipGie && ~skipRov)
  segRonly = 1:length(timeR);
  segGonly = [];
  segRcommon = [];
  segGcommon = [];
elseif (~skipGie && ~skipRov)
  % records present in both GIE and Rover data.
  % select common epochs.

  %% Loop through data to find common and uncommon segments.
  idxG = 1;
  idxR = 1;
  lenG = length(timeG);
  lenR = length(timeR);
  segGcommon = [];
  segRcommon = [];
  segGonly = [];
  segRonly = [];
  
  t = startTimeMsec; % Set up t in integer milliseconds
  while (t < endTimeMsec)

    if (idxG <= lenG)
      % Compare the timestamp to t 
      matchG = (timeG(idxG) == t); 
      % matchG = ( abs(timeG(idxG) - t*1000) < epsForTime );
    else
      matchG = 0;
    end
    if (idxR <= lenR)
      % Compare the timestamps to t
      matchR = (timeR(idxR) == t); 
      % matchR = ( abs(timeR(idxR) - t*1000) < epsForTime );
    else
      matchR = 0;
    end

    if (matchG && matchR)
      segGcommon = [segGcommon; idxG];
      segRcommon = [segRcommon; idxR];
      idxG = idxG+1;
      idxR = idxR+1;
    elseif (matchG)
      segGonly = [segGonly; idxG];
      idxG = idxG+1;
    elseif (matchR)
      segRonly = [segRonly; idxR];
      idxR = idxR+1;
    end

    t = t+dt;
  end
end % (~skipGie && ~skipRov)


return;
