function [u,v,spd,jdn]=ll2uv(jd,lat,lon,wa,code);

% function [u,v,spd,jdn]=ll2uv(jd,lat,lon,[wa],[code]);
%
% routine to calculate u & v from julian day and lat &lon
%
% where jd =  some form of yearday
%  lat,lon =  dec degrees
%  u,v,spd =  cm/sec
%      jdn =  the new yearday with mid-time intervals
%  wa      =  number of points to preform window average filter
%  code    =  1 to add plots
%          =  0 not to (default)

if nargin<5;
  code=0;
end;


% calculate time interval vector in seconds
dt=diff(jd)*24*60*60;
for i=1:length(lat)-1;
  [dkm,bear]=dist(lat(i),lon(i),lat(i+1),lon(i+1));
  [u(i),v(i)]=sd2uv(dkm*100000/dt(i),bear);
  spd(i)=sqrt(u(i)^2+v(i)^2);
  jdn(i)=dt(i)/2/24/60/60+jd(i);
end;

if nargin>3&wa~=0;
  u=runave(jdn',u',wa,0,0,0);
  v=runave(jdn',v',wa,0,0,0);
end;

if code==1;

  % plot u velocity
%  plot(jdn,u,'linewidth',2);
  hold on;

   % add plot of filtered
   ti=min(jdn):1/24:max(jdn);
   uhr=interp1(jdn,u,ti);
   vhr=interp1(jdn,v,ti);
   uf=plfilt(uhr);
   vf=plfilt(vhr);
   plot(ti,uf,'g--','linewidth',2);
   hold on;
   plot(ti,vf,'b--','linewidth',2);

   % add wind
   ww=menu('Which wind?','buoy','model');
   if ww==1;
     load /data5/jmanning/wind/stress/b11dec98ws.dat
     b=b11dec98ws;
     plot(b(:,1),b(:,2)*10,'r-',b(:,1),b(:,3)*10,'c-','linewidth',2);
     % alternatively load "wind.dat"
     %id=find(w(:,1)>=min(jd)&w(:,1)<=max(jd)&w(:,6)==8);
     %plot(w(id,1),w(id,2),'r*',w(id,1),w(id,3),'g*');
   else;
     load /home3/ocn/jmanning/gbs99/rtda/q42/modwstr.dat
     m=modwstr;
     % generate ydts
     for i=1:length(m);
       jdw(i)=julian(m(i,2),m(i,1),m(i,3)-1900,m(i,4)*100);
     end;
     plot(jdw,m(:,6)*10,'r-',jdw,m(:,7)*10,'c-','linewidth',2);
   end;

   % fix axis 
     axis([min(jdn) max(jdn) -10 10]);
%   yd2calab(98);
%   legend('U-vel','detided U','detided V','N/S Buoy Stress*10','E/W Buoy Stress*10')
%   legend('detided U @15m','detided V @15m','E/W Buoy Stress*10','N/S Buoy Stress*10')

%  subplot(2,1,2);
%  plottraj;
end;

