function  comets3(lh,x,y,lb,chead,cbody,ctail,lshead,lsbody,lstail,linewid,sd)

% COMETS3  Primitive for the COMETS command
%     COMETS(LH,X,Y,LB,CHEAD,CBODY,CTAIL,LSHEAD,LSBODY,LSTAIL,LINEWID)
%       Makes animated lines with handles LH and coordinates stored in matrices
%       X and Y and partial body length LB. Other arguments are:
%     CHEAD - [size(x,2) by 1] - color of the comet head
%     CBODY - color of the body, CTAIL - color of the tail,
%     LSHEAD - head linestyle, LSBODY - body linestyle, LSTAIL - tail linestyle,
%     LINEWID - [size(x,2) by 3] linewidths of heads, bodies, tails.
%
%     See also COMETS (front end to this program), CMTSDEMO, FIREWORKS, 
%     COMET, COMET3

%  Kirill K. Pankratov, kirill@plume.mit.edu
%  July 18, 1994
%  Modified Richard Limeburner May 1995 to show yearday on Georges Bank Drifters

%Label the Startday on the animation
	ydh=text(-65.6,40.15,int2str(sd));
	set(ydh,'color','c6')
	set(ydh,'fontname','times');
	set(ydh,'fontsize',22);
	set(ydh,'fontw','bold');
	c=sd;
	cgw=[.9 .9 .9];
	cg5=([.4 .4 .4]);

head = lh(1,:);     % Head handle(s)
body = lh(2,:);     % Body handle(s)
tail = lh(3,:);     % Tail handle(s)
lcom = size(x,1);   % Comet length

if size(lh,2)==1   % If only one line ..............................................
  if any(lshead(1)=='x*o+.'), word = 'markersize';
  else, word = 'linewidth'; end

  % Initialize comets ........................................
  set(head,'color',chead(1,:),'linestyle',lshead(1,:));
  set(head,word,linewid(1,1),'erase','xor','xdata',x(1,:),'ydata',y(1,:));
  set(body,'color',cbody(1,:),'linestyle',lsbody(1,:));
  set(tail,'color',ctail(1,:),'linestyle',lstail(1,:));

  % Separate with NaN's
  xbody = x(ones(3,1),:); ybody = y(ones(3,1),:);
  xbody(3,:) = xbody(3,:)*nan; ybody(3,:) = ybody(3,:)*nan;
  xtail = x(ones(3,1),:); ytail = y(ones(3,1),:);
  xtail(3,:) = xtail(3,:)*nan; ytail(3,:) = ytail(3,:)*nan;

  set(body,'linewidth',linewid(1,2),'erase','none','xdata',xbody(:),'ydata',ybody(:));
  set(tail,'linewidth',linewid(1,3),'erase','none','xdata',xtail(:),'ydata',ytail(:));
  set(ydh,'erase','none');
  
  % Primary loop ..............................................
  for i = 2:lcom+lb;
     num = i-1:i;
     numb = min(max(num,1),lcom);
     numt = min(max(num-lb,1),lcom);
     num = min(i,lcom);
     xhead = x(num,:);
     yhead = y(num,:);
     xbody(1:2,:) = x(numb,:);
     ybody(1:2,:) = y(numb,:);
     xtail(1:2,:) = x(numt,:);
     ytail(1:2,:) = y(numt,:);
     set(head,'xdata',xhead(:),'ydata',yhead(:))
     set(body,'xdata',xbody(:),'ydata',ybody(:))
     set(tail,'xdata',xtail(:),'ydata',ytail(:))
     c=c+1;
     cc=c/4;
	%Reset the Yearday handle at integer timesteps
	if floor(cc)==cc
		  Yearday=cc;
		  set(ydh,'color',cg5);
		  set(ydh,'string',num2str(Yearday))
		  set(ydh,'color','c6')
	end

	drawnow
end

else   % If many lines .............................................................

  % Initialize comets ........................................
  for jl = 1:size(x,2)
    cch = chead(jl,:);
    ccb = cbody(jl,:);
    cct = ctail(jl,:);
    clh = lshead(jl,:);
    if any(clh(1)=='x*o+.'), word = 'markersize';
    else, word = 'linewidth'; end
    clb = lsbody(jl,:);
    clt = lstail(jl,:);
    set(head(jl),'color',cch,'linestyle',clh,word,linewid(jl,1));
    set(head(jl),'erase','xor','xdata',x(1,jl),'ydata',y(1,jl))
    set(body(jl),'color',ccb,'linestyle',clb,'linewidth',linewid(jl,2));
    set(body(jl),'erase','none','xdata',[],'ydata',[])
    set(tail(jl),'color',cct,'linestyle',clt,'linewidth',linewid(jl,3));
    set(tail(jl),'erase','none','xdata',[],'ydata',[]);
%set(r,'erase','none','xdata',[],'ydata',[]);
end

  % Primary loop ..............................................
  for i = 2:lcom+lb
    num = i-1:i;
    numb = min(max(num,1),lcom);
    numt = min(max(num-lb,1),lcom);
    num = min(i,lcom);
    for jl = 1:size(x,2)
      set(head(jl),'xdata',x(num,jl),'ydata',y(num,jl))
      set(body(jl),'xdata',x(numb,jl),'ydata',y(numb,jl))
      set(tail(jl),'xdata',x(numt,jl),'ydata',y(numt,jl))
    end
    drawnow
  end

end  % End ........................................................................

