function gb_print99dvel(id,tstart,tstop)
% gb_print99dvel(id,tstart,tstop) computes and 
% saves drifter mean and max speed and velocity
% for the 40-m deep drifters in id in 5-day blocks
% for the time between tstart and tstop.
% 4/5/01

% find number of drifters 
id=sort(id);
Nid=length(id);

%load data
load gb40m99
z=zd; clear zd

% convert to drifter days
tstart=yd2dd(tstart,1999);
tstop=yd2dd(tstop,1999);

% set start, stop times
dt=5;
t1=[tstart:dt:tstop-dt];
t2=t1+dt;
N=length(t1);

speed=ones(1,7);

% start loop
for n=1:N
   st1=dd2yd(t1(n));
   st2=dd2yd(t2(n));
   speed=[speed;[st1,st2,0,0,0,0,0]];
   for nid=1:Nid
      nn=find(z(:,8)==id(nid));
      zz=z(nn,:);
      % find subarray
      t=zz(:,1);
      err=0.005;
      I=find(t1(n)-err<=t & t<=t2(n)+err);II=length(I);
      t=zz(I,1);
      u=zz(I,4);
      v=zz(I,5);
      spd=100.*sqrt(u.^2+v.^2);
      um=mean(u);vm=mean(v);
      vspd=100*sqrt(um.^2+vm.^2);
      vdir=180*atan2(vm,um)./pi;
      tp=dd2yd(t); 
      spdmax=max(spd);
      nspm=find(spd==spdmax);
      tpmax=tp(nspm(1,:));
      spdmean=mean(spd);
      drstat=[id(nid) spdmean spdmax tpmax vspd vdir II];
      speed=[speed;drstat];
   end
end
dspeed=0.1*round(10.*speed);
save speed dspeed
clear
load speed
delete drifter_speed_stats.txt
diary('drifter_speed_stats.txt')
dspeed
diary off
delete speed.mat
