SUBROUTINE bocov
  !
  ! $Author: CPTEC $
  ! $Date: 2000/02/24 13:15:43 $
  ! $Revision: 1.0 $
  !                .      .    .     
  ! subprogram:    bocov       update wind points on boundary
  !   prgrmmr: janjic          org: w/nmc2     date: 94-03-08
  !     
  ! abstract:
  !     u and v components of the wind are updated on the
  !     domain boundary by applying the pre-computed
  !     tendencies at each time step.  an extrapolation from
  !     inside the domain is used for the component tangential
  !     to the boundary if the normal component is outward.
  !     
  ! program history log:
  !   87-??-??  mesinger   - originator
  !   95-03-25  black      - conversion from 1-d to 2-d in horizontal
  !     
  ! usage: call bocoh from main program ebu
  !   input argument list:
  !     none
  !  
  !   output argument list: 
  !     none
  !     
  !   output files:
  !     none
  !     
  !   subprograms called:
  !  
  !     unique: none
  !  
  !     library: none
  !  
  !   common blocks: ctlblk
  !                  masks
  !                  vrbls
  !                  boco
  !   
  IMPLICIT NONE

  !*** problem size

  INCLUDE "parmeta.f90"
  INTEGER, PARAMETER :: lb=2*im+jm-3

  !*** global variables

  INCLUDE "ctlblk.f90" ! dt(in)
  INCLUDE "masks.f90"  ! vtm(in)
  INCLUDE "vrbls.f90"  ! u(inout); v(inout)
  INCLUDE "boco.f90"   ! ub(inout); vb(inout)

  !*** local variables

  REAL, PARAMETER :: h1=1.e0
  REAL, PARAMETER :: h4=4.e0
  REAL, PARAMETER :: d06666=.06666666e0
  REAL, PARAMETER :: d25=.25e0
  REAL, PARAMETER :: d00=0.e0
  INTEGER         :: i, j, l, n

  !*** time interpolation of u and v at the outer boundary

  DO l=1,lm
     n=1
     DO i=1,im-1
        ub(n,l,1)=ub(n,l,1)+ub(n,l,2)*dt
        vb(n,l,1)=vb(n,l,1)+vb(n,l,2)*dt
        u(i,1,l)=ub(n,l,1)
        v(i,1,l)=vb(n,l,1)
        n=n+1
     END DO
     DO i=1,im-1
        ub(n,l,1)=ub(n,l,1)+ub(n,l,2)*dt
        vb(n,l,1)=vb(n,l,1)+vb(n,l,2)*dt
        u(i,jm,l)=ub(n,l,1)
        v(i,jm,l)=vb(n,l,1)
        n=n+1
     END DO
     DO j=2,jm-1,2
        ub(n,l,1)=ub(n,l,1)+ub(n,l,2)*dt
        vb(n,l,1)=vb(n,l,1)+vb(n,l,2)*dt
        u(1,j,l)=ub(n,l,1)
        v(1,j,l)=vb(n,l,1)
        n=n+1
     END DO
     DO j=2,jm-1,2
        ub(n,l,1)=ub(n,l,1)+ub(n,l,2)*dt
        vb(n,l,1)=vb(n,l,1)+vb(n,l,2)*dt
        u(im,j,l)=ub(n,l,1)
        v(im,j,l)=vb(n,l,1)
        n=n+1
     END DO
  END DO

  !*** extrapolation of tangential velocity at outflow points

  DO l=1,lm
     DO i=2,im-2
        IF(v(i,1,l).LT.d00)u(i,1,l)=(vtm(i,5,l)+h1)*u(i,3,l) &
             -vtm(i,5,l)    *u(i,5,l)
     END DO
     DO i=2,im-2
        IF(v(i,jm,l).GT.d00)u(i,jm,l)=(vtm(i,jm-4,l)+h1)*u(i,jm-2,l) &
             -vtm(i,jm-4,l)    *u(i,jm-4,l)
     END DO
     DO j=4,jm-3,2
        IF(u(1,j,l).LT.d00)v(1,j,l)=(vtm(3,j,l)+h1)*v(2,j,l) &
             -vtm(3,j,l)    *v(3,j,l)
     END DO
     DO j=4,jm-3,2
        IF(u(im,j,l).GT.d00)v(im,j,l)=(vtm(im-2,j,l)+h1)*v(im-1,j,l) &
             -vtm(im-2,j,l)    *v(im-2,j,l)
     END DO
  END DO

  !*** space interpolation of u and v at the inner boundary

  DO l=1,lm
     u(2,2,l)=d06666*(h4*(u(1,1,l)+u(2,1,l)+u(2,3,l)) &
          + u(1,2,l)+u(1,4,l)+u(2,4,l))
     v(2,2,l)=d06666*(h4*(v(1,1,l)+v(2,1,l)+v(2,3,l)) &
          +v(1,2,l)+v(1,4,l)+v(2,4,l))
     u(im-1,2,l)=d06666*(h4*(u(im-2,1,l)+u(im-1,1,l)+u(im-2,3,l)) &
          +u(im,2,l)+u(im,4,l)+u(im-1,4,l))
     v(im-1,2,l)=d06666*(h4*(v(im-2,1,l)+v(im-1,1,l)+v(im-2,3,l)) &
          +v(im,2,l)+v(im,4,l)+v(im-1,4,l))
     u(2,jm-1,l)=d06666*(h4*(u(1,jm,l)+u(2,jm,l)+u(2,jm-2,l)) &
          +u(1,jm-1,l)+u(1,jm-3,l)+u(2,jm-3,l))
     v(2,jm-1,l)=d06666*(h4*(v(1,jm,l)+v(2,jm,l)+v(2,jm-2,l)) &
          +v(1,jm-1,l)+v(1,jm-3,l)+v(2,jm-3,l))
     u(im-1,jm-1,l)= &
          d06666*(h4*(u(im-2,jm,l)+u(im-1,jm,l)+u(im-2,jm-2,l)) &
          +u(im,jm-1,l)+u(im,jm-3,l)+u(im-1,jm-3,l))
     v(im-1,jm-1,l)= &
          d06666*(h4*(v(im-2,jm,l)+v(im-1,jm,l)+v(im-2,jm-2,l)) &
          +v(im,jm-1,l)+v(im,jm-3,l)+v(im-1,jm-3,l))
     DO i=3,im-2
        u(i,2,l)=(u(i-1,1,l)+u(i,1,l)+u(i-1,3,l)+u(i,3,l))*d25
        v(i,2,l)=(v(i-1,1,l)+v(i,1,l)+v(i-1,3,l)+v(i,3,l))*d25
     END DO
     DO i=3,im-2
        u(i,jm-1,l)=(u(i-1,jm-2,l)+u(i,jm-2,l)+u(i-1,jm,l)+u(i,jm,l))*d25
        v(i,jm-1,l)=(v(i-1,jm-2,l)+v(i,jm-2,l)+v(i-1,jm,l)+v(i,jm,l))*d25
     END DO
     DO j=3,jm-2,2
        u(1,j,l)=(u(1,j-1,l)+u(2,j-1,l)+u(1,j+1,l)+u(2,j+1,l))*d25
        v(1,j,l)=(v(1,j-1,l)+v(2,j-1,l)+v(1,j+1,l)+v(2,j+1,l))*d25
     END DO
     DO j=3,jm-2,2
        u(im-1,j,l)= &
             d25*(u(im-1,j-1,l)+u(im,j-1,l)+u(im-1,j+1,l)+u(im,j+1,l))
        v(im-1,j,l)= &
             d25*(v(im-1,j-1,l)+v(im,j-1,l)+v(im-1,j+1,l)+v(im,j+1,l))
     END DO
  END DO
END SUBROUTINE bocov
