c    @(#)mlfilt.f	1.6
      subroutine mlfilt
c
c
c
      include'mladv.inc'
c
c
      external bqgofk
      external tqgofk
      external trcqgofk

      parameter (div=10.0)
      dimension hfil(mnmax)

      common/unit/iin,iout,idff,idif,iorr,iowr,iplt,ifndd
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
                
      common/cnn/n,nm1,nm2,np1      
c
      data icall/0/
c
      if(icall.eq.0) then
        write(iout,'('' mixed layer filtering is '',i2,''-1-1'')')nord
        icall=1
      endif

      if(nord.eq.0)return

      call advset

      if (nfrq .gt. 0) then

         call filter(buoy(1,1,mlcur),m,n,nord)

         call filtml(buoy(1,1,mlcur),bqgofk)

         if (kmlmin.ne.kmlmax) then
            call advtrans(buoy(1,1,mlcur),2)
            do 5 k=kmlmin,kmlmax
               call advfix(k,2)
  5          continue
           endif

        endif

      do 10 k=kmlmax+1,l
         call filter(buoy(1,k,mlcur),m,n,nord)
   10 continue

      if (nfrq .gt. 0) then

         call filter(temp(1,1,mlcur),m,n,nord)
         call filtml(temp(1,1,mlcur),tqgofk)

         if (kmlmin.ne.kmlmax) then
            call advtrans(temp(1,1,mlcur),2)
            do 6 k=kmlmin,kmlmax
               call advfix(k,2)
  6          continue
           endif

       endif

      do 11 k=kmlmax+1,l
         call filter(temp(1,k,mlcur),m,n,nord)
   11 continue

      do 13 icurtrace = 1 , ntrace

         if (nfrq .gt. 0) then

            call filter(trace(1,1,mlcur,icurtrace),m,n,nord)
            call filtml(trace(1,1,mlcur,icurtrace),trcqgofk)

            if (kmlmin.ne.kmlmax) then
               call advtrans(trace(1,1,mlcur,icurtrace),2)
               do 9 k=kmlmin,kmlmax
                  call advfix(k,2)
 9              continue
              endif

          endif

      do 12 k=kmlmax+1,l
         call filter(trace(1,k,mlcur,icurtrace),m,n,nord)
   12 continue

   13 continue


c
c mixed layer depth
c

      do 1 ip=1,msq
         hfil(ip) = hdpth(ip,mlcur)
    1 continue

      call filter(hdpth(1,mlcur),m,n,nord)

      do 2 ip=1,msq

         call findkml(hfil(ip),zml,l,k1)
         call findkml(hdpth(ip,mlcur),zml,l,k2)

         if (abs(k1-k2) .gt. 1) then
            write(*,*)'Filter CFL violation',mltime,ip,k1,k2,
     .                               hfil(ip),hdpth(ip,mlcur)
            if (k1 .lt. k2) then
               k2 = k1 + 1
               hdpth(ip,mlcur) = -zml(k2) - (dzml(k2) / div)
              else
               k2 = k1 - 1
               hdpth(ip,mlcur) = -zml(k2) + (dzml(k2) / div)
             endif
            write(*,*)'Resolved',k1,k2,hfil(ip),hdpth(ip,mlcur)
          endif

         if (k1 .ne. k2) then

            if (k1.lt.k2) then
               idir = -1
              else
               idir =  1
             endif

            call adjust(ip,k1,hfil,idir,buoy(1,1,mlcur))
            call adjust(ip,k1,hfil,idir,temp(1,1,mlcur))

            do 65 itrace=1,ntrace
               call adjust(ip,k1,hfil,idir,trace(1,1,mlcur,itrace))
  65         continue

            write(*,*)'Filter transgression',mltime,ip,k1,k2,
     .                                       hdpth(ip,mlcur),hfil(ip)

          endif

    2 continue

      dzt=-zml(1)

      do 7 ip=1,msq
        if(hdpth(ip,mlcur).lt.dzt)hdpth(ip,mlcur)=dzt
    7 continue
c
c
c
      return
      end
