c    @(#)mixall.f	1.6
c
c
c
      subroutine mixall
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
      bimiss=0.0
      enttot=0.0
c
c
      do 100 ip=1,msq
        hmx=hmax(ip)
        entr=be(ip)*qgts
        enttot=enttot+max(0.,entr)
        entmiss=0.0
        h=hdpth(ip,mlcur)
c
        do 20 k=1,l
          b1d(k)=buoy(ip,k,mlcur)+bqgofk(ip,k)
          t1d(k)=temp(ip,k,mlcur)+tqgofk(ip,k)
          do 21 i=1,ntrace
             icurtrace=i
             trc1d(k,i)=trace(ip,k,mlcur,i)+trcqgofk(ip,k)
   21      continue
   20   continue
c
c
        call mix
c
        call findkml(h,zml,l,kml)

        if ((dc .gt. 0.0) .and. (kml+2 .lt. l-1)) then
           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
         endif

c       out(ip)=max(h,dmix)
        bimiss=bimiss+entmiss
        hdpth(ip,mlcur)=h
        tbar(ip)=mltint
        bbar(ip)=mlbint
c
        do 30 k=1,l
          buoy(ip,k,mlcur)=b1d(k)-bqgofk(ip,k)
          temp(ip,k,mlcur)=t1d(k)-tqgofk(ip,k)
          do 31 i=1,ntrace
             icurtrace=i
             trace(ip,k,mlcur,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)bimiss=bimiss/enttot           
      if(enttot.eq.0.0 .and. bimiss.eq.0.0)bimiss=0.0
      if(enttot.eq.0.0 .and. bimiss.ne.0.0)bimiss=1.0
c
c
      return
      end
