function [out2,prr2]=binpre(dat,pr,d1,d2,delx,ich)
%
%
%   Program to bin on pressure
%     dat=data array to bin
%     pr=pressure array
%     d1=starting depth
%     d2=ending depth
%     delx=binsize
%     ich= 1= median bin, 2=mean bin
%
%  Written by Andrew Barnard, OSU, 1996.
%
del=delx/2;
markr=d1;
ikk=1;
out1=dat;
info=size(dat);
ivar=info(1);
prr=pr;
mpr=max(prr);
if d2 < mpr
  mpr=d2;
end

while markr <= mpr

  hh=markr+del;
  ll=markr-del;

  indx=find(pr >= ll & pr < hh);

  if isempty(indx) == 0
   for i=1:ivar
    if ich == 1
     out1(i,ikk)=nanmedian(dat(i,indx));
    else
     out1(i,ikk)=nanmean(dat(i,indx));
    end
   end
    prr(1,ikk)=markr;
   ikk=ikk+1;
  end
  markr=markr+delx;

end
ikk=ikk-1;

out2=zeros(ivar,ikk);
out2(:,:)=out1(:,1:ikk);
prr2=zeros(1,ikk);
prr2(:,:)=prr(1,1:ikk);
