function []=vecplt5(w,wll,wind,windll,runoff,runoffll,jd)
% VECPLT5 steps through vector currents on a map 
%         using mouse buttons as controls
%
%      right mouse button advances in time
%      middle mouse quits
%      left mouse button goes back in time
%
%  click farther right on the plot to take greater time steps
%
% plots surface, mid and deep as different colors
% 0-10 m red
% 10-30 m green
% 30-60 m blue
% > 60 m  yellow
%
%
%  USAGE vecplt5(w,wll,wind,windll,runoff,runoffll,jd)
%        w  matrix where each column is a complex velocity time series
%        wll two-column matrix [lon lat] for velocity stations
%        w  complex wind vector
%        run real runoff vector
%
%        if it's too slow, try increasing nsub
%
nsub=1;
[n,m]=size(w);
w(:,m+1)=wind;
w(:,m+2)=runoff;
w=replace(w,NaN,0.);  %replace NaN with zeros to increase speed of plotting

wll(m+1,:)=windll;
wll(m+2,:)=runoffll;
%
%  get coastline and 40 m isobath
load coast
load bath40
%
%  set up plot
lat_mid=42.3;
lat_max=42.6;
lon_mid=-70.4;
dlon=1.4;
lat_fac=cos(lat_mid*pi/180);
dlat=dlon*lat_fac;
%
% factor for arrows
%
 yfac=24*3600/1.e5/111;   % length particles would travel in one day :
 xfac=yfac/lat_fac;
%
% 111 km/ deg lat;
[n,m]=size(w);
lon=wll(:,1);
lat=wll(:,2);
%  Do movie until user says no!
axis('square');
s='y';
   is=[1 3 6 12 14 16 19 21 24];
   im=[2 4 7 13 15 17 20 22 25];
   inb=[5 8 9 23];
   ib=[10 11 ];
   iw=m-1;
   ir=m;
while(s~='n')
 iclear=24*28;   % clear screen every 2 weeks
 greg1=gregorian(jd(1));
 i=nsub;
 mb=1
 k=0;
 while(mb~=0),
   k=k+1;
   [x,y,mb]=ginput(1,'sc');
   if(mb==112),  % print out plot if user hits "p"
     hold off
     axis([lon_mid-.5*dlon lon_mid+.5*dlon lat_mid-.5*dlat lat_mid+.5*dlat]);
     plot(coast(:,1),coast(:,2));
     greg=gregorian(jd(i));
     title(greg2str(greg));
     hold on
     mplot(bath40,'r.')
     arrows(lon(is),lat(is),u(is),v(is),xfac,yfac,'c2-');
     arrows(lon(im),lat(im),u(im),v(im),xfac,yfac,'c3-');
     arrows(lon(inb),lat(inb),u(inb),v(inb),xfac,yfac,'c4-');
     arrows(lon(ib),lat(ib),u(ib),v(ib),xfac,yfac,'c5-');
     arrows(lon(iw),lat(iw),u(iw),v(iw),xfac,yfac,'c6-');
     arrows(lon(ir),lat(ir),u(ir),v(ir),xfac,yfac,'c7-');
     print
   end 
   mb=mb-2;
   if(mb==0),return,end;
   i=i+50*x*mb*nsub;
   i=rem(i,n);
   if(i<0),i=i+n,end;
   if (rem(k-1,iclear)==0),  % clear screen every iclear button hits
     hold off
     axis([lon_mid-.5*dlon lon_mid+.5*dlon lat_mid-.5*dlat lat_mid+.5*dlat]);
     plot(coast(:,1),coast(:,2));
     greg2=gregorian(jd(min(i+iclear,n)));
     title(date_lbl(greg1,greg2));
     greg1=greg2;
     hold on
     mplot(bath40,'r.')
     u=zeros(1,m);
     v=zeros(1,m);
   end
   greg2=gregorian(jd(i));
   str1=greg2str(greg2)
   arrows(lon(is),lat(is),u(is),v(is),xfac,yfac,'i-');
   arrows(lon(im),lat(im),u(im),v(im),xfac,yfac,'i-');
   arrows(lon(inb),lat(inb),u(inb),v(inb),xfac,yfac,'i-');
   arrows(lon(ib),lat(ib),u(ib),v(ib),xfac,yfac,'i-');
   arrows(lon(iw),lat(iw),u(iw),v(iw),xfac,yfac,'i-');
   arrows(lon(ir),lat(ir),u(ir),v(ir),xfac,yfac,'i-');
   u=real(w(i,:));
   v=imag(w(i,:));
   arrows(lon(is),lat(is),u(is),v(is),xfac,yfac,'c2-');
   arrows(lon(im),lat(im),u(im),v(im),xfac,yfac,'c3-');
   arrows(lon(inb),lat(inb),u(inb),v(inb),xfac,yfac,'c4-');
   arrows(lon(ib),lat(ib),u(ib),v(ib),xfac,yfac,'c5-');
   arrows(lon(iw),lat(iw),u(iw),v(iw),xfac,yfac,'c6-');
   arrows(lon(ir),lat(ir),u(ir),v(ir),xfac,yfac,'c7-');
 end
end
