%
%  main adcp process routine (may call several other m-files)


ph=menu('What phase?','vertically averaged vectors on map',...
                      'plot time series of u & v',...
                      'plot profiles')

%%%%%%%%%%%%%%%%%%%  plotting vertically averaged  %%%%%%%%%%%%%

if ph==1;

  wo=menu('Which option?','hourly','more frequent');
  if wo==1;
%   load data
    load /data5/jmanning/adcp/tranhsj9507
    djul=(th-(365*24.))/24.0;
    
% detide?
    dt=menu('Detide with pl33?','y','n');
    if dt==1;
      [uth]=plfilt(uth);
      [vth]=plfilt(vth);
    end;

    st=input('Enter Start Yearday:');
    et=input('Enter End time:');
    id=find(djul>=st&djul<=et);
%    georbat;

%   force axis same as codvsbott
%    axis([-68.73 -67.322 40.417 41.417])

%   draw vectors  
    for k=1:length(id);
      arrowold(lonh(id(k)),lath(id(k)),uth(id(k)),vth(id(k)),1,1,1,[-68.73  -67.322 40.417 41.417],'r')
    end;

%   add text & legendcd
    text(-68.25,41.15,'ADCP (vbar)','color','r')  
    arrowold(-68.25,41.12,1,0,1,1,1,[-68.73  -67.322 40.417 41.417],'r')
    text(-68.15,41.11,'= 100 cm/s','color','r')  

  elseif wo==2;
%    load other more frequent data
     st=input('Enter Start Yearday:');
     et=input('Enter End time:');
     k=0;

%read files until this is covered
     for j=1:8;
       eval(['load /data5/jmanning/adcp/transj9507',num2str(j)]);
       djul=djul-365;

%      case to skip this file
       if st>max(djul)|et<min(djul);
         disp([num2str(j)]);

%      case to add this file to time series
       else
         k=k+1;
         if k==1;
           yd=djul;
           u=ut;
           v=vt;
           y=lat;
           x=lon;
         else;
           yd=[yd; djul];
           u=[u; ut];
           v=[v; vt];
           y=[y; lat];
           x=[x; lon];
         end;
       end;
     end;


%    find just that asked for
     id=find(yd>=et&yd<=et);

%    make vector plot of filterd
     for k=1:length(uf);
       arrowold(x(k),y(k),u(k),v(k),.1,1,1,[-69 -67 40 41.5],'r')
     end;
  
% end the option wo=2 of more frequent vectors
  end;

% end phase 1 plotting vertically averaged vector maps
end;      

%%%%%%%%%%%%%%%%%%%%%%%  plotting time series of u & v

if ph==2;

  wo=menu('Which option?','hourly','more frequent');
  if wo==1;
%   load data
    load /data5/jmanning/adcp/tranhsj9507
    djul=(th-(365*24.))/24.0;
    
    st=input('Enter Start Yearday:');
    et=input('Enter End time:');
    id=find(djul>=st&djul<=et);

% detide?
    dt=menu('detide adcp?', 'pl33','detide','none');
    if dt==1;
      [uthf]=plfilt(uth);
      [vthf]=plfilt(vth);
      plot(djul(id),uthf(id),djul(id),vthf(id));
    elseif dt==2;
      [ut,vt]=detide(uth,vth,th);
      uthf=uth'-ut; vthf=vth'-vt;
      plot(djul(id),uthf(id),djul(id),vthf(id));
    else;
      plot(djul(id),uth(id),djul(id),vth(id));
    end;


    hold on;

% add wind?
    aw=menu('wind?','y','n')
    if aw==1;
      load /data4/jmanning/wind/stress/b1195ws
      idw=find(t>=st&t<=et);
      plot(t(idw),taux(idw),'g',t(idw),tauy(idw),'r');
    end;

% add morring currents
    hold on;
    dt=menu('detided?','y','n');
    if dt==1;
      load /data4/jmanning/moor/moor95/st2f
      idw=find(st21f(:,1)>=st&st21f(:,1)<=et);
      plot(st22f(idw,1),st22f(idw,2)/100,'--b',st22f(idw,1),st22f(idw,3)/100,'--c');
    elseif dt==2;
      load /data4/jmanning/moor/moor95/vmcm15m_450.mtl;
      st21=vmcm15m_450;
      idw=find(st21(:,2)>=st&st21(:,2)<=et);
      plot(st21(idw,2),st21(idw,3)/100,'--b',st21(idw,2),st21(idw,4)/100,'--c');
    end;

% add legend
    if aw==1;
      legend('adcp-u','adcp-v','taux  ','tauy  ','st2-u','st2-v ')
    else;
      legend('adcp-u','adcp-v','st2-u','st2-v ')
    end;
  end;
end;                
   
%%%%%%%%%%%%%%%%%%%%%     plot profiles of ADCP   %%%%%%%%%%%%%%%%%%%

if ph==3;

  mm=menu('Making a movie loop?','y','n');

  st=input('Enter Start day in month (ie 15 for may 15th, 16 for may 16th etc.):');
  hr=input('start Hour?');
%     minu=input('minute?');
  minu=0;
  et=st;
  ih=hr;

% loop through several hours
  for ih=hr:23;
    k=0;

%read files until this is covered
    for j=1:8;
       if k~=99;
         k=k+1;
         eval(['load /data5/jmanning/adcp/sj9507hd',num2str(j)]);

%      case to skip this file
         if st>max(h(:,5))|et<min(h(:,5));
           disp([num2str(j)]);

%       case to add this file to time series
%       and find the particular profile(s) of interest
         else
           eval(['load /data5/jmanning/adcp/sj9507pf',num2str(j)]);
           id=find(h(:,5)==st&h(:,6)==ih&(h(:,7)>minu-1&h(:,7)<minu+1));
           if id~=[];
             id2=find(p(:,1)==h(id(1),1));
             x=h(id,25);
             y=h(id,24);
             z=p(id2,2);
%             u=p(id2,3)/p(id2,10);
%             v=p(id2,4)/p(id2,10);
             u=p(id2,3)/h(id(1),20);
             v=p(id2,4)/h(id(1),20);
             k=99;
           end;
         end;
       end;
     end;

% done finding data now
% make profile of good points
  id3=find(abs(u)<200);
  clf;  
  plot(u(id3),-1*z(id3),'r.','markersize',20)
  hold;
  plot(v(id3),-1*z(id3),'g.','markersize',20)
  title([num2str(ih),' hours @',num2str(y(1)),'N and ',num2str(x(1)),'W']);
  ylabel(['Depth (m) (bottom depth = ',num2str(h(id(1),20)),'m)']);
  xlabel('E/W (red) and N/S (green) current m/sec')
  axis([-10 10 -100 0]);

% save maximums for this time in a file
  yy=[x(1) y(1) ih mean(u(id3)) mean(v(id3))];
  fploopy(yy,'maxdat.dat','a')

  if mm==1;
% CAPTURE THIS IMAGE
    
    disp(' ');
    disp('Click Figure Window!!');
    eval(['!xwd -nobdrs  | imconv -xwd -infile - -gif -outfile ',num2str(k),'.gif']);
    !exit
  elseif mm==2;
    drawnow;
  end;



% end loop trhough several hours
  end;

% end profiling phase
end;  
