
%define vehicle, deployment, data directory
 veh = 'wgTiny';
 dep = '20161031';
 mydir = ['//atlas.shore.mbari.org/WaveGlider/deployment_data/' ...
    veh '/' dep '/QC/'];

%load position, waypoint data
 pos = csvread([mydir 'Position.csv'],1,0);
 way = csvread([mydir 'Waypoint.csv'],1,0);

%plot
 figure; hold on;
 plot(pos(:,2),pos(:,3),'k.-');

 BOGLine = [way(:,1), zeros(size(way(:,2)))];

%find onshore-to-offshore BOG lines
 f = find( way(1:end-1,2)==41 & way(2:end,2)==42 ) + 1;
 for n=1:length(f)
   ct1 = f(n);
   while way(ct1+1,2)==42
     ct1 = ct1+1;
   end
   if ct1 > f(n) %if there were any 42's
     ct2 = ct1;
     while way(ct2+1,2)==43
       ct2 = ct2+1;
     end
     if ct2 > ct1 %if there were any 43's
       BOGLine( f(n):ct2,2 ) = 1;
     end
     clear ct2;
   end
   clear ct1;
 end
 clear f n;

 figure; hold on;
 plot(pos(:,2),'k.-');
 f = find(BOGLine(:,2));
 plot(f,pos(f,2),'g.');
 
%find offshore-to-onshore BOG lines
 f = find( way(1:end-1,2)==43 & way(2:end,2)==44 ) + 1;
 for n=1:length(f)
   ct1 = f(n);
   while way(ct1+1,2)==44
     ct1 = ct1+1;
   end
   if ct1 > f(n) %if there were any 44's
     ct2 = ct1;
     while way(ct2+1,2)==41
       ct2 = ct2+1;
     end
     if ct2 > ct1 %if there were any 41's
       BOGLine( f(n):ct2,2 ) = 1;
     end
     clear ct2;
   end
   clear ct1;
 end
 clear f n;
 
% Two problems with this method:
% 1) Sometimes the targetWaypoint is 255, even though wg is clearly
%    heading towards waypoint X.
% 2) Sometimes wg goes from waypoint 44 to 42 before heading to 41.
% 
% Wondering if I don't even want to use targetWaypoint?

