%% bin_mission1bNST.m : called from process_auvmissionNST.m
function [P2,T2,C2,S2,D2,FLOW2,depthnav,depth2,pitch,roll,yaw,Xpos,Ypos] = ...
    bin_mission1bNST(p2,t2,c2,s2,d2,flw2,dpth2,ndepth,npitch,nroll,nyaw, ...
    nposx,nposy,mtime,c2time,navtime);

buf=median(diff(mtime))/2;

for N=1:length(mtime)
 %%------
 %% CTD2
 %%------
 idxi = find(c2time >= mtime(N)-buf & c2time < mtime(N)+buf);
if (isempty(idxi) == 0)
  P2(N,1)=median(p2(idxi));
  T2(N,1)=median(t2(idxi));
  C2(N,1)=median(c2(idxi));
  S2(N,1)=median(s2(idxi));
  D2(N,1)=median(d2(idxi));
  FLOW2(N,1)=median(flw2(idxi));
  depth2(N,1)=median(dpth2(idxi));
end

 %%----------
 %% Spatial 
 %%----------
 idx = find(navtime >= mtime(N)-buf & navtime < mtime(N)+buf);
  depthnav(N,1)=median(ndepth(idx));
  pitch(N,1)=median(npitch(idx));
  roll(N,1)=median(nroll(idx));
  yaw(N,1)=median(nyaw(idx));
  Xpos(N,1)=median(nposx(idx));
  Ypos(N,1)=median(nposy(idx));
end



