%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Plot DVL data.
% AUTHOR:  Rob McEwen
% DATE:    01/3/15
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

  if exist('dataDvl') ~= 1, return, end

  str = [' ',titleStr,' Plotted on ',myDate];
  PaperPosn = [.5 .5 3.75 2.5];
  FigurePosn = [5 30 815 760];
  i = 0;


  if exist( 'dvlFig' ) ~= 1
    dvlFig = figure;
%   winpos( position(1,:) );
  else
    figure(dvlFig);
  end
  set( dvlFig, 'Name', 'DVL Plot', 'DeleteFcn', 'clear dvlFig' );
  set(gcf,'position',position)

  nstrips = 7;
  ErrorVal = -32.768;
%
% The RDI Dvl substitutes a -32768 for the velocity components for
% any ping that returns invalid data.  The purpose of the find/indx
% logic below is to drop these values from the plots because they
% shrink the scale too much.
%
  subplot(nstrips,1,1)
  [xx,dvlBx]  = stairs( dataDvl(:,1), dataDvl(:,dvlBotVelx) );
  [xx,dvlWx]  = stairs( dataDvl(:,1), dataDvl(:,dvlWatVelx) );
  indxBx = find( dvlBx == ErrorVal );
  dvlBx(indxBx) = 0*ones(length(indxBx),1);
  indxWx = find( dvlWx == ErrorVal );
  dvlWx(indxWx) = 0*ones(length(indxWx),1);
  plot(xx-dataDvl(1,1), [dvlBx dvlWx ] );
  ylabel('Body X')
  title(['Dvl Measurement; Blue is wrto Bottom ',str]);
  grid on
  zoom on
  
  subplot(nstrips,1,2)
  [xx,dvlBy]  = stairs( dataDvl(:,1), dataDvl(:,dvlBotVely) );
  [xx,dvlWy]  = stairs( dataDvl(:,1), dataDvl(:,dvlWatVely) );
  indxBy = find( dvlBy == ErrorVal );
  dvlBy(indxBy) = 0*ones(length(indxBy),1);
  indxWy = find( dvlWy == ErrorVal );
  dvlWy(indxWy) = 0*ones(length(indxWy),1);
  plot(xx-dataDvl(1,1), [dvlBy dvlWy ] );
  ylabel('Body Y')
  grid on;
  zoom on;
  
  subplot(nstrips,1,3)
  [xx,dvlBz]  = stairs( dataDvl(:,1), dataDvl(:,dvlBotVelz) );
  [xx,dvlWz]  = stairs( dataDvl(:,1), dataDvl(:,dvlWatVelz) );
  indxBz = find( dvlBz == ErrorVal );
  dvlBz(indxBz) = 0*ones(length(indxBz),1);
  indxWz = find( dvlWz == ErrorVal );
  dvlWz(indxWz) = 0*ones(length(indxWz),1);
  plot(xx-dataDvl(1,1), [dvlBz dvlWz ] );
  ylabel('Body Z')
  grid on;
  zoom on;
  
  subplot(nstrips,1,4)
  [xx,dvlBz]  = stairs( dataDvl(:,1), dataDvl(:,dvlBotVele) );
  [xx,dvlWz]  = stairs( dataDvl(:,1), dataDvl(:,dvlWatVele) );
  indxBz = find( dvlBz == ErrorVal );
  dvlBz(indxBz) = 0*ones(length(indxBz),1);
  indxWz = find( dvlWz == ErrorVal );
  dvlWz(indxWz) = 0*ones(length(indxWz),1);
  plot(xx-dataDvl(1,1), [dvlBz dvlWz ] );
  ylabel('E Vel')
  grid on;
  zoom on;
%
% The problem here is that the various vectors dvlBx, dvlBy, etc., may be
% of different lengths.  Thus it isn't possible to use them directly to
% compute the magnitude.  So, start over.
%
%  indxB = intersect(intersect(indxBx,indxBy),indxBz);
%  indxW = intersect(intersect(indxWx,indxWy),indxWz);
   indxB = indxBx;
   indxW = indxWx;

  BotVelMag = sqrt( dataDvl(:,dvlBotVelx).^2 + ...
		    dataDvl(:,dvlBotVely).^2 + ...
		    dataDvl(:,dvlBotVelz).^2 );

  WatVelMag = sqrt( dataDvl(:,dvlWatVelx).^2 + ...
		    dataDvl(:,dvlWatVely).^2 + ...
		    dataDvl(:,dvlWatVelz).^2 );

  indxB = find(BotVelMag > 10);
  BotVelMag(indxB) = 0*ones(length(indxB),1);
  indxW = find(WatVelMag > 10);
  WatVelMag(indxW) = 0*ones(length(indxW),1);

  subplot(nstrips,1,5)
  [xx,yyB]  = stairs( dataDvl(:,1), BotVelMag );
  [xx,yyW]  = stairs( dataDvl(:,1), WatVelMag );
  plot(xx-dataDvl(1,1), [yyB yyW ] );
  ylabel('|Vel|')
  grid on;
  zoom on;

  subplot(nstrips,1,6)
  [xx,yyB]  = stairs( dataDvl(:,1), dataDvl(:,dvlBotVelStat) );
  [xx,yyW]  = stairs( dataDvl(:,1), dataDvl(:,dvlWatVelStat) );
  plot(xx-dataDvl(1,1), [yyB yyW ] );
  ylabel('Bot & Wat Stat')
  grid on;
  zoom on;

  subplot(nstrips,1,7)
  [xx,yy]  = stairs( dataDvl(:,1), dataDvl(:,dvlTemp) );
  plot(xx-dataDvl(1,1), yy );
  ylabel('Celsius')
  xlabel('Elapsed Time (s)');
  grid on;
  zoom on;
  return




