% phase 3
% make a synthetic tidal velocity for these times
% (note: we add 4 hours to make gmt and add 24 hours for '96 leap)
  disp('Detiding...')
  [ut,vt]=detide(lo,la,jd*24+(yr-94)*365*24+24);

% subtract the tide
  uf=ubar'-ut;
  vf=vbar'-vt;

% Now, lets detide the "detided" with pl33
% First we need to make an hourly time series
  hts=[min(jd):1/24:max(jd)];
  idnn=find(isnan(uf)~=1); 
  ufh=interp1(jd(idnn),uf(idnn),hts);
  vfh=interp1(jd(idnn),vf(idnn),hts);
  loh=interp1(jd(idnn),lo(idnn),hts);
  lah=interp1(jd(idnn),la(idnn),hts);
  ufhpl33=plfilt(ufh);
  vfhpl33=plfilt(vfh);

% plot the time series
  clf;
  plot(jd,vbar,jd,vt,jd,vf,hts,vfhpl33)
  legend('vbar','vt','vf','vfhpl33');

% plot detided detided vectors
  figure(2);
  id=find(isnan(ufhpl33)~=1);
  loh=loh(id);lah=lah(id);ufhpl33=ufhpl33(id);vfhpl33=vfhpl33(id);hts=hts(id);
  plot(mean(loh),mean(lah))
  vecplot2(loh,lah,ufhpl33,vfhpl33,1,'m/s',min(loh)+rangejm(loh)/10,min(lah)+rangejm(lah)/10,'g');
  disp('press return for specific time period...');
  pause;

% redo for julio detided specific time
% mist=input('Desired start time in yeardays? ');
% mast=input('Desired end time in yeardays? ');
%% find the adcp times that fit within this
% id=find(jd>mist&jd<mast);
% plot(mean(lo(id)),mean(la(id)));
% vecplot2(lo(id),la(id),uf(id),vf(id),1,'m/s',...
%  min(lo(id))+rangejm(lo(id))/10,min(la(id))+rangejm(la(id))/10,'r');
%  pause(3);
% legend off


% redo vector plot for detided detided (pl33) results
  mist2=input('Desired start time in yeardays? ');
  mast2=input('Desired end time in yeardays? ');
% find the adcp times that fit within this
  id=find(hts>mist2&hts<mast2);
  plot(mean(loh(id)),mean(lah(id)));
  vecplot2(loh(id),lah(id),ufhpl33(id),vfhpl33(id),.2,'m/s',...
  min(loh(id))+rangejm(loh(id))/10,min(lah(id))+rangejm(lah(id))/10,'g');

% find depth averaged 
  [nr,nc]=size(va);
  for j=1:nc;  
%   find the good vels
    vbar2(j)=meannnan(va(:,j));
    ubar2(j)=meannnan(ua(:,j));
%   mz(j)=depth(min(find(isnan(va(:,j))==1)));
  end;

%plot(jd,mz);
  
%subtract the tide
  uf2=ubar2'-ut;
  vf2=vbar2'-vt;

% plot the time series
  clf;
  plot(jd,vbar2,jd,vt,jd,vf2)
  legend('vbar2','vt','vf2');
