c    @(#)bndmix.f	1.4
c
c
c
      subroutine bndmix
c
c
c
c
c   c   leonard walstad, june 6, 1986
c
c
c
c
c   routine to setup for mixing at each horizontal grid point.
c
c
c
c_______________________________________________________________________________
c
c include statement follows
c 
      include'mladv.inc'
c
c
c_______________________________________________________________________________
c
c
      common/parm/kz,m,mm1,mm2,mp1,msq,tim,tmax,tstart,hy,rea,dt,
     +   xbasin,yy(mx0),icalc,kzp1,kzm1,iftop,ifbot,iftvv,ifrel,theta,
     +   ifsbl,ifeva
                

c
      common /extpar/ rlat0,rlng0,t0,v0,dhor,ht,time0,r0,f0star
c
c----------------------------------------------------------------------c
c
c
      bbmiss=0.0
      enttot=0.0
c
c
      do 100 ibnd=1,mbnd
        ip=ipofb(ibnd)
        hmx=hmax(ip)
        entr=bndbe(ibnd)*qgts
        enttot=entot+max(0.0,entr)
        h=bnddpth(ibnd)
c
        do 20 k=1,l
          b1d(k)=bndbuoy(ibnd,k)+bqgofk(ip,k)
          t1d(k)=bndtemp(ibnd,k)+tqgofk(ip,k)
          do 21 i=1,ntrace
             icurtrace = i
             trc1d(k,i)=bndtrace(ibnd,k,i)+trcqgofk(ip,k)
   21      continue
   20   continue
c
c
        call mix
c
        call findkml(h,zml,l,kml)

        call diffus(b1d,l,kml+2,dc,qgts,dzml(1))
        call diffus(t1d,l,kml+2,dc,qgts,dzml(1))
        do 25 i=1,ntrace
           call diffus(trc1d(1,i),l,kml+2,dc,qgts,dzml(1))
  25     continue
 

        bndmld(ibnd)=max(h,dmix)
        bbmiss=bbmiss+entmiss
        bnddpth(ibnd)=h
        bndtbar(ibnd)=mltint
        bndbbar(ibnd)=mlbint
c
        do 30 k=1,l
          bndbuoy(ibnd,k)=b1d(k)-bqgofk(ip,k)
          bndtemp(ibnd,k)=t1d(k)-tqgofk(ip,k)
          do 31 i=1,ntrace
             icurtrace = i
             bndtrace(ibnd,k,i)=trc1d(k,i)-trcqgofk(ip,k)
   31      continue
   30   continue
c
c
  100 continue
c
c  determine frac missed      
      if(enttot.ne.0.0)bbmiss=bbmiss/enttot          
      if(enttot.eq.0.0 .and. bbmiss.eq.0.0)bbmiss=0.0
      if(enttot.eq.0.0 .and. bbmiss.ne.0.0)bbmiss=1.0
c
c
      return
      end
