%
% DRIFTER_Z.M
% draw drifter position colored with depth        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   function drifter_z(nint,t_start)
%  NINT is time sampling interval,
%  which means each nint time step will be drown  
%  t_start - starting day of the run (JD)
%  HANDWORK: 1)set path (line 20); 2)title (line 77),
%  3)axis (line 70). 
%  how to call (example): call as: DRIFTER_Z(16,120)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load grid  
   bn=loadgrid('bank150');
% create a new grid structure (geographycal coordinate)   
   bb=bn;
   [bb.y,bb.x]=xy2ll(bb.x,bb.y);
   gridname='bank150';
% set path
   PTHFILE=['/whoinet/putnam/p2/globec/fcast_archives/FCAST_1.1B1/EN323_clim.06/CRUISE_Q4.1/DRIFTERS']
% load data from PTH file
   [gridname,ndrog,ndts,tsec,pth]=read_pth(PTHFILE,4);
   size(pth);
   ndts=ans(1)/ndrog;
   xdr=pth(:,1);
   xdr=reshape(xdr,ndrog,ndts);
   xdr=xdr';

   ydr=pth(:,2);
   ydr=reshape(ydr,ndrog,ndts);
   ydr=ydr';
   zdr=pth(:,3);
   zdr=reshape(zdr,ndrog,ndts);
   zdr=zdr';

   tdr=pth(:,4);
   tdr=reshape(tdr,ndrog,ndts);
   tdr=tdr';
% convert data to geographycal coordinates
   [y,x]=xy2ll(xdr,ydr);
   clear pth
   clear xdr
 
% this part is necessary to create a colorbar
   m=sqrt(ndrog);
   s1=reshape(x(ndts,:),m,m);
   s2=reshape(y(ndts,:),m,m);
   s3=reshape(zdr(ndts,:),m,m);
   clf
   contourf(s1,s2,s3)
% end of colorbar part
% set limits for drifter depth (or, can do it manually) 
   minz=min(zdr(:));
   maxz=max(zdr(:));
%   minz=-40;
%   maxz=-26;
   caxis([minz maxz])
% set number of grades R for colorbar ( if abs(maxz-minz)=14 (as above),
% choose number of grades equal to 14, or 7.   
   R=abs(maxz-minz);
   hsv1=hsv(R);
   colormap(hsv1)
   hh=colorbar
   set(hh,'Position',[0.831375 0.11 0.015 0.815])
   cla

   a=colormap;  
   RC=size(a,1);
% set axis, draw bnd, and h-contours
   axis([-68.5 -67. 40.2 41.4]) 
   hb=plotbnd(bb);
   set(hb,'color','k','Linewidth',2)
   hc=lcontour(bb,'z',[30:10:90])
   set(hc,'color','b')
   xlabel('Longitude','FontWeight','bold','FontSize',12)
   ylabel('Latitude','FontWeight','bold','FontSize',12)
   title('run EN323 FC1 clim.06/CRUISE Q4.3',...
'FontWeight','bold','FontSize',12)
   labcont
   grid on 
   drawnow
   h=ones(ndrog,1);
% find out  time  when drifters started to move
   ff=find(ydr(:,1)~=ydr(1,1));
   s=ff(1)-1;
% do frames
   for k=[s:nint:ndts]
   hold on 
   ncol=ceil((zdr(k,:)-minz)*RC/R);
   ncol=ncol';
   ncol(find(ncol==0))=1;
 
   for i=[1:ndrog]
   b=a(ncol(i),:);
   h(i)=plot(x(k,i),y(k,i),'o','markeredgecolor','k','markersize',10,'markerfacecolor',[b]); 
   end
   time=t_start+tdr(k,1)/86400;
   ht=text(-67.9,40.35,['time = ' num2str(time)]);
   set(ht,'Fontsize',12,'Fontweight','bold','color','b') 
   hold off
 
   j=sprintf('pth%i', k ) 
   eval(['print ' j '.ppm -dppm'])
   delete(h) 
   delete(ht) 
   end
