%
%    function  pltdrft(lat,lon,opt,[tint2],[yr],[yd],[ntl],[l1v],[l1i],[l2v],[l2i],[l3v],[l3i]);
%
%      standard plot of drifters with annotation as specified
%
%      lat,lon should be in decimal degrees and negative lon
%
%      opt=positive using yearday
%         =negative using some other variable
%
%          (note: the absolute value of "opt" is the color code)
%
%      For "opt=pos" case:
%      tint2 is the number of seconds between annotated fixes
%      yr is the 2-digit year code (95, for example)
%      yd is the yearday (0.5 for noon Jan1)
%
%      For "opt=neg" case:
%      ntl= number of variables to label track
%      l1v= label #1 variable ("day" for example)
%      l1i= label #1 interval (if "-1" then just label at change)

function pltdrft(lat,lon,opt,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10);


%   plot all fixes with dots or lines
%   lw=input('Line width? ');
lw=3;
%disp(['linewidth hardcoded to ',num2str(lw)]);
%global lw
%disp('note: linewidth "lw" must be set globally from the calling routine.')
ms=lw*8;

% start with "opt=1" case of using yearday
% load colorcodes

cp=computer;
if cp(1:3)=='SGI';
  load /home3/ocn/jmanning/atsee/ccodemat;
else;
   load ccodemat
  %load e:\atsee\ccodemat

end;
if opt>0;
  tint2=v1;
  yr=v2;
  yd=v3;


  %define linestyle

  if tint2<0;lsty='.';else;lsty='-';end;

  tint2=abs(tint2);
  plot(lon,lat,lsty,'color',ccode(abs(opt),:),'linewidth',lw);
%  plot(lon,lat,lsty,'color',ccode(abs(opt),:),'markersize',20);
%  plot(lon,lat,'g.');
  hold on

  if tint2==0;return;end;

% calculate average time between fixes in seconds
  tint=mean(diff(yd))*3600*24;
 
%   calculate a displacement of label from "fix"
  dyd=(maxnnan(lat)-minnnan(lat))/20;

%   annotate where requested in a loop
  if nargin<3;return;end;

  for i=1:floor(tint2/tint):length(lon);

%   post a red dot where annotating
%    plot(lon(i),lat(i),'.','color','r','markersize',24)

%   figure out the date and post it
    if ~isnan(i);
      date=calday(yr,yd(i));
      h2=text(lon(i),lat(i)+dyd,[num2str(date(2)),'/',num2str(date(3))]);
      set(h2,'horizontalalignment','center','color',ccode(abs(opt),:));

%     case where hourly lables needed
      if abs(tint2)<3600*24;
        h3=text(lon(i)+1.5*dyd,lat(i)+dyd,['(',num2str(date(4)*100+date(5)),'GMT)']);
        set(h3,'fontsize',8,'color',ccode(abs(opt),:));
      end;
    end;  
  end;

%   if hourly labels are not included then put one on the 1st fix for
%   reference
   disp('Hardcoded to not write out gmt time of first fix')
%  if tint2>=3200*24;
%    date=calday(yr,yd(1));
%    h3=text(lon(1)+1.5*dyd,lat(1)+dyd,['(',num2str(date(4)*100+date(5)),'GMT)']);
%    set(h3,'fontsize',8,'color',['c',num2str(abs(opt))])
%  end;

% fix axis
  set(gca,'aspectratio',[1 .78]);
  ylabel('Latitude (N/S)');
  xlabel('Longitude (E/W)');


% NOW FOR "OPT<0" CASE OF USING SOME OTHER VARIABLE TO LABEL

elseif opt<0;
  ntl=v1;

  

  %linestyle determined by sign of ntl

  if ntl<0;lsty='.';else;lsty='-';end;

  ntl=abs(ntl);
  if ntl>=1;l1v=v2;l1i=v3;skip1=v4;end;
  if ntl>=2;l2v=v5;l2i=v6;skip2=v7;end;

  plot(lon,lat,lsty,'color',ccode(abs(opt),:),...
       'markersize',ms,'linewidth',lw);

  for k=1:abs(ntl);

%   case of text at change only
    eval(['ths=l',num2str(k),'i;']);
    if ths==-1;
      eval(['id=find(diff(l',num2str(k),'v)~=0);']);
      skip=eval(['skip',num2str(k)]);
      for m=1:skip:length(id);
         eval(['h=text(lon(id(m)),lat(id(m)),num2str(l',num2str(k),'v(id(m))));']);

         ccode(abs(opt),:)
        set(h,'color',ccode(abs(opt),:),'fontsize',6);
      end;
    
% case of text at specified interval
    elseif ths~=-1;
      for n=1:length(lon);
%        eval(['if rem(l',num2str(k),'v(n),l',num2str(k),'i)==0;']);
        if rem(l1v(n),l1i)==0;
          strng=num2str(eval(['l',num2str(k),'v(n)']));
          text(lon(n),lat(n),strng,'color',ccode(abs(opt),:));
        end;
      end;
    end;
  end;
elseif opt==0;
  plot(lon,lat,'-','color',[.5 .5 0],'markersize',6);
end;
