%% bin_missionNST.m : called from process_auvmissionNST.m
function [T1,T2,C1,C2,S1,S2,D1,D2,OBSbin,O2,FLOW1,FLOW2,depthnav,depth1,depth2,pitch,roll,yaw,Xpos,Ypos] = ...
    bin_missionNST(t1,t2,c1,c2,s1,s2,d1,d2,obs,o2,flw1,flw2,dpth1,dpth2, ...
                   ndepth,npitch,nroll,nyaw,nposx,nposy,mtime,c1time,c2time,navtime);

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

for N=1:length(mtime)
 %%------
 %% CTD1
 %%------
 idx = find(c1time >= mtime(N)-buf & c1time < mtime(N)+buf);
if (isempty(idx) == 0)
  T1(N,1)=median(t1(idx));
  C1(N,1)=median(c1(idx));
  S1(N,1)=median(s1(idx));
  D1(N,1)=median(d1(idx));
  OBSbin(N,1)=median(obs(idx));
  O2(N,1)=median(o2(idx));
  FLOW1(N,1)=median(flw1(idx));
  depth1(N,1)=median(dpth1(idx));
end
  
 %%------
 %% CTD2
 %%------
 idxi = find(c2time >= mtime(N)-buf & c2time < mtime(N)+buf);
if (isempty(idxi) == 0)
  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



