%			REFM4Quoddy
% This routine converts the ADCP data to vertically averaged form
% and loads the position, time, depth range, water depth, and vertically
% averaged velocities into a single array called aobs.  The routine was
% scavenged from a routine of Chris Naimie

% CNF 12/15/98
% CNF 1/7/99 modified method of computing the binsize during the ztop, zbot
%	computation
% CNF 1/11/99 modified to eliminate ensembles when bottom tracking off

% Eliminate ensembles taken when bottom tracking was turned off

ibton = find(~isnan(bt_depth));
ABS_U = ABS_U(:,ibton);
ABS_V = ABS_V(:,ibton);
Sv = Sv(:,ibton);
zc = zc(:,ibton);
nav = nav(:,ibton);
bt_depth = bt_depth(ibton);
time = time(ibton);


% vertically integrate non-nan velocity observations

nobs=0;
unan=isnan(ABS_U);
vnan=isnan(ABS_V);
for i=1:length(time)
   for j=1:length(zc(:,i))	% find the first bin of data and set ztop to
      if unan(j,i) == 0		% upper bin boundary
         jtop=j;
         ztop=zc(jtop,i)-0.5*(zc(2,i)-zc(1,i));
         break
      end
   end
   if j == length(zc(:,i));	% do nothing
   else;			% move usable data into aobs and perform vertical
      nobs=nobs+1;		% average
      aobs(nobs,1)=time(i);
      aobs(nobs,2)=nav(1,i);
      aobs(nobs,3)=nav(2,i);
      aobs(nobs,8)=bt_depth(i);
      aobs(nobs,6)=ztop;
      jbot=length(zc(:,i));
      aobs(nobs,7)=zc(jbot,i)+2.0;
      for j=jtop+1:length(zc(:,i))	% determine the depth of the bottom of
         if unan(j,i) == 1		% the last usable bin
            jbot=j-1;
            aobs(nobs,7)=zc(jbot,i)+0.5*(zc(2,i)-zc(1,i));
            break
         end
      end
      usum=0.0;vsum=0.0;
      for j=jtop:jbot		% compute the vertically averaged velocity
         usum=usum+ABS_U(j,i);	% between ztop and zbot
         vsum=vsum+ABS_V(j,i);
      end
      aobs(nobs,4)=usum/(jbot-jtop+1);
      aobs(nobs,5)=vsum/(jbot-jtop+1);
   end;
end

% Convert observational coordinates to (x,y) adjust vertical coordinates

%[aobs(:,2),aobs(:,3)]=ll2xy(aobs(:,3),aobs(:,2)); % VK

