! $Id: wnbqijk_bc.F 1458 2014-02-03 15:01:25Z gcambon $
!
!======================================================================
! CROCO is a branch of ROMS developped at IRD and INRIA, in France
! The two other branches from UCLA (Shchepetkin et al) 
! and Rutgers University (Arango et al) are under MIT/X style license.
! CROCO specific routines (nesting) are under CeCILL-C license.
! 
! CROCO website : http://www.croco-ocean.org
!======================================================================
!
#ifndef CHILD
!
# include "cppdefs.h"
# if defined NBQ && defined NBQ_IJK
      subroutine wnbqijk_bc_tile (Istr,Iend,Jstr,Jend,grad)
#  ifdef AGRIF      
      use AGRIF_Util
      integer Istr,Iend,Jstr,Jend
      real grad(PRIVATE_2D_SCRATCH_ARRAY)
      if (AGRIF_Root()) then
        call wnbqijk_bc_parent_tile (Istr,Iend,Jstr,Jend,grad)
      else
        call wnbqijk_bc_child_tile (Istr,Iend,Jstr,Jend,grad)
      endif
      return
      end
!
! PARENT
!
      subroutine wnbqijk_bc_parent_tile (Istr,Iend,Jstr,Jend,grad)
#  endif
!
! Set lateral boundary conditions for W-component momentum
! qdm_nbq_a(W_index,2) for the parent grid.
!
# endif /* NBQ */
#else
# if defined NBQ && defined NBQ_IJK
!
! CHILD
!
      subroutine wnbqijk_bc_child_tile (Istr,Iend,Jstr,Jend,grad)
!
! Set lateral boundary conditions for W-component momentum
! qdm_nbq_a(W_index,2) for the child grid.
!
# endif /* NBQ */
#endif /* CHILD */
# if defined NBQ && defined NBQ_IJK
!
! Common Code
!
# include "set_obc_definitions.h"
!
!
      implicit none
# include "param.h"
# include "grid.h"
# include "ocean3d.h"
# include "nbq.h"
# include "climat.h"
# include "scalars.h"
# include "boundary.h"  
# ifdef MRL_WCI
#  include "forces.h"
# endif
      integer Istr,Iend,Jstr,Jend, i,j,k,
     &        l_b_j,l_b_jm1,l_bm1_j,l_bm1_jm1,l_bm2_j,
     &        l_b_i,l_b_im1,l_bm1_i,l_bm1_im1,l_bm2_i,
     &        l_c,l_c_im1,l_c_jm1
      real    grad(PRIVATE_2D_SCRATCH_ARRAY)
      real    cff,eps,
     &        cx,cy, dft,dfx,dfy, tau,tau_in,tau_out
      parameter (eps=1.E-20)
!
# include "compute_auxiliary_bounds.h"
!
! Interpolations of the parent values to get wnbqbry_east or wnbqclm
!
# ifdef CHILD
      call wnbqijk_bc_interp_tile(Istr,Iend,Jstr,Jend)
# endif
!
# if defined NBQ_FRC_BRY || defined NBQNUDGING
      tau_in=dt*tauM_in
      tau_out=dt*tauM_out
# endif
!
!# define nnew vnnew_nbq
!# define nstp vnstp_nbq
!
#
# ifndef EW_COM_PERIODIC
!
!====================================================================
!                            WESTERN BC
!====================================================================


      if (WESTERN_EDGE) then
#  ifdef OBC_COM_WEST
#   ifdef OBC_COM_NBQORLANSKI
        do k=0,N                             ! Western edge radiation
          do j=Jstr,Jend+1                   ! ======= ==== =========

            l_b_j     = ijk2lmom_nh(Istr-1,j  ,k,3)
            l_b_jm1   = ijk2lmom_nh(Istr-1,j-1,k,3)
            l_bm1_j   = ijk2lmom_nh(Istr  ,j  ,k,3)
            l_bm1_jm1 = ijk2lmom_nh(Istr  ,j-1,k,3)

            grad(Istr-1,j)=(qdm_nbq_a(l_b_j  )
     &                     -qdm_nbq_a(l_b_jm1))
#    ifdef MASKING
     &                              *vmask(Istr-1,j)
#    endif
            grad(Istr  ,j)=(qdm_nbq_a(l_bm1_j  )
     &                     -qdm_nbq_a(l_bm1_jm1))
#    ifdef MASKING
     &                                  *vmask(Istr,j)
#    endif
          enddo
          do j=Jstr,Jend

            l_b_j     = ijk2lmom_nh(Istr-1,j,k,3)
            l_bm1_j   = ijk2lmom_nh(Istr  ,j,k,3)
            l_bm2_j   = ijk2lmom_nh(Istr+1,j,k,3)

            dft=qdm_nbq_a(l_bm1_j)-qdm_nbq_a(l_bm1_j)
            dfx=qdm_nbq_a(l_bm1_j)-qdm_nbq_a(l_bm2_j)

            if (dfx*dft .lt. 0.) then
              dft=0.                       ! <-- cancel cx, if inflow
#    if defined NBQ_FRC_BRY || defined NBQNUDGING
              tau=tau_in
            else
              tau=tau_out
#    endif
            endif
 
            if (dft*(grad(Istr,j)+grad(Istr,j+1)) .gt. 0.) then
              dfy=grad(Istr,j)
            else
              dfy=grad(Istr,j+1)
            endif
 
#    ifdef OBC_COM_RAD_NORMAL
            dfy=0.
#    endif
            cff=max(dfx*dfx+dfy*dfy, eps)
            cx=dft*dfx
#    ifdef OBC_COM_RAD_NPO
            cy=0.
#    else
            cy=min(cff,max(dft*dfy,-cff))
#    endif
 
            qdm_nbq_a(l_b_j)=( cff*qdm_nbq_a(l_b_j  )
     &                              +cx*qdm_nbq_a(l_bm1_j)
     &                             -max(cy,0.)*grad(Istr-1,j  )
     &                             -min(cy,0.)*grad(Istr-1,j+1)
     &                                               )/(cff+cx)
#    if defined NBQ_FRC_BRY  || defined NBQNUDGING 
            qdm_nbq_a(l_b_j)=(1.-tau)*qdm_nbq_a(l_b_j)
#     ifdef NBQ_FRC_BRY
     &                                    +tau*wnbqbry_west(j,k)
#     else     
     &                                  +tau*wnbqclm(Istr-1,j,k)
#     endif
#    endif
#    ifdef MASKING
            qdm_nbq_a(l_b_j)=qdm_nbq_a(l_b_j)
     &                                            *rmask(Istr-1,j)
#    endif
#    ifdef WET_DRY
            qdm_nbq_a(l_b_j)=qdm_nbq_a(l_b_j)
     &                                          *rmask_wet(Istr-1,j)
#    endif
          enddo
        enddo
!
#   elif defined OBC_COM_NBQSPECIFIED
!                                            Western edge Specified BC
!                                            ======= ==== ========= ==
        do k=0,N
          do j=Jstr,Jend
            l_b_j     = ijk2lmom_nh(Istr-1,j  ,k,3)
#    ifdef NBQ_FRC_BRY
            qdm_nbq_a(l_b_j)=wnbqbry_west(j,k)         ! specified
#    else
            qdm_nbq_a(l_b_j)=wnbqclm(Istr-1,j,k)
#    endif
#    ifdef MASKING
     &                               *rmask(Istr-1,j)
#    endif
#    ifdef WET_DRY
     &                           *rmask_wet(Istr-1,j)
#    endif
          enddo
        enddo
#   else
!                                            Western edge gradient BC
!                                            ======= ==== ======== ==
        do k=0,N
          do j=Jstr,Jend
!           qdmw_nbq(Istr-1,j,k)=qdmw_nbq(Istr,j,k)
!    &                                     *rmask(Istr,j)
             qdmw_nbq(Istr-1,j,k)=wz(Istr-1,j,k,nrhs)
     &          *hzw_half_nbq(istr-1,j,k)
#    ifdef MASKING
     &                                     *rmask(Istr,j)
#    endif
#    ifdef WET_DRY
     &                                 *rmask_wet(Istr,j)
#    endif
          enddo
        enddo
#   endif
!
!#  else           /* alternative to open */
!        do k=1,N                                     ! Western edge closed
!          do j=jstr,jend                             ! ======= ==== ======
!            l_b_j     = ijk2lmom_nh(Istr-1,j  ,k,3)  !  (no-slip: default)
!            qdm_nbq_a(l_b_j)=0.           
!          enddo
!        enddo
#  endif              /* OBC_COM_WEST */
      endif         !<-- WESTERN_EDGE
!
!====================================================================
!                            EASTERN BC
!====================================================================
      if (EASTERN_EDGE) then
#  ifdef OBC_COM_EAST
#   ifdef OBC_COM_NBQORLANSKI
        do k=0,N                             ! Eastern edge radiation
          do j=Jstr,Jend+1                   ! ======= ==== =========

            l_b_j       = ijk2lmom_nh(Iend+1,j  ,k,3)
            l_b_jm1     = ijk2lmom_nh(Iend+1,j-1,k,3)
            l_bm1_j     = ijk2lmom_nh(Iend  ,j  ,k,3)
            l_bm1_jm1   = ijk2lmom_nh(Iend  ,j-1,k,3)

            grad(Iend  ,j)=(qdm_nbq_a(l_bm1_j  )
     &                     -qdm_nbq_a(l_bm1_jm1))
#    ifdef MASKING
     &                                *vmask(Iend  ,j)
#    endif
            grad(Iend+1,j)=(qdm_nbq_a(l_b_j  )
     &                     -qdm_nbq_a(l_b_jm1))
#    ifdef MASKING
     &                              *vmask(Iend+1,j)
#    endif
          enddo
          do j=Jstr,Jend

            l_b_j     = ijk2lmom_nh(Iend+1,j,k,3)
            l_bm1_j   = ijk2lmom_nh(Iend  ,j,k,3)
            l_bm2_j   = ijk2lmom_nh(Iend-1,j,k,3)

            dft=qdm_nbq_a(l_bm1_j)-qdm_nbq_a(l_bm1_j)
            dfx=qdm_nbq_a(l_bm1_j)-qdm_nbq_a(l_bm2_j)
 
            if (dfx*dft .lt. 0.) then
              dft=0.                      ! <-- cancel cx, if inflow
#    if defined NBQ_FRC_BRY || defined NBQNUDGING
              tau=tau_in
            else
              tau=tau_out
#    endif
            endif
 
            if (dft*(grad(Iend,j)+grad(Iend,j+1)) .gt. 0.) then
              dfy=grad(Iend,j)
            else
              dfy=grad(Iend,j+1)
            endif
 
#    ifdef OBC_COM_RAD_NORMAL
            dfy=0.
#    endif
            cff=max(dfx*dfx+dfy*dfy, eps)
            cx=dft*dfx
#    ifdef OBC_COM_RAD_NPO
            cy=0.
#    else
            cy=min(cff,max(dft*dfy,-cff))
#    endif
 
            qdm_nbq_a(l_b_j)=( cff*qdm_nbq_a(l_b_j  )
     &                              +cx*qdm_nbq_a(l_bm1_j)
     &                             -max(cy,0.)*grad(Iend+1,j  )
     &                             -min(cy,0.)*grad(Iend+1,j+1)
     &                                               )/(cff+cx)
#    if defined NBQ_FRC_BRY  || defined NBQNUDGING 
            qdm_nbq_a(l_b_j)=(1.-tau)*qdm_nbq_a(l_b_j)
#     ifdef NBQ_FRC_BRY
     &                                    +tau*wnbqbry_east(j,k)
#     else     
     &                                  +tau*wnbqclm(Iend+1,j,k)
#     endif
#    endif
#    ifdef MASKING
            qdm_nbq_a(l_b_j)=qdm_nbq_a(l_b_j)
     &                                             *rmask(Iend+1,j)
#    endif
#    ifdef WET_DRY
            qdm_nbq_a(l_b_j)=qdm_nbq_a(l_b_j)
     &                                             *rmask_wet(Iend+1,j)
#    endif
          enddo
        enddo
!
#   elif defined OBC_COM_NBQSPECIFIED
!                                            Eastern edge Specified BC
!                                            ======= ==== ========= ==
        do k=0,N
          do j=Jstr,Jend
            l_b_j     = ijk2lmom_nh(Iend+1,j,k,3)
#    ifdef NBQ_FRC_BRY
             qdm_nbq_a(l_b_j)=wnbqbry_east(j,k)       ! specified
#    else
             qdm_nbq_a(l_b_j)=wnbqclm(Iend+1,j,k)
#    endif
#    ifdef MASKING
     &                                *rmask(Iend+1,j)
#    endif
#    ifdef WET_DRY
     &                            *rmask_wet(Iend+1,j)
#    endif
          enddo
        enddo
#   else
!                                            Eastern edge gradient BC
!                                            ======= ==== ======== ==
        do k=0,N
          do j=Jstr,Jend
!           qdmw_nbq(Iend+1,j,k)=qdmw_nbq(Iend,j,k)
!    &                                   *rmask(Iend+1,j)
             qdmw_nbq(Iend+1,j,k)=wz(Iend+1,j,k,nrhs)
     &          *hzw_half_nbq(Iend+1,j,k)
#    ifdef MASKING
     &                                   *rmask(Iend+1,j)
#    endif
#    ifdef WET_DRY
     &                               *rmask_wet(Iend+1,j)
#    endif
          enddo
        enddo
#   endif
!
!#  else
!        do k=1,N                                    ! Eastern edge closed
!          do j=jstr,jend                            ! ======= ==== ======
!            l_b_j     = ijk2lmom_nh(Iend+1,j,k,3)   !  (no-slip: default)
!             qdm_nbq_a(l_b_j)=0.
!          enddo
!        enddo
#  endif            /* OBC_COM_EAST */
      endif         !<-- EASTERN_EDGE
# endif             /* !EW_COM_PERIODIC */
 
# ifndef NS_COM_PERIODIC
!
!====================================================================
!                            SOUTHERN BC
!====================================================================
      if (SOUTHERN_EDGE) then
#  ifdef OBC_COM_SOUTH
#   ifdef OBC_COM_NBQORLANSKI
        do k=0,N                            ! Southern edge radiation
          do i=Istr,Iend+1                  ! ======== ==== =========

            l_b_i     = ijk2lmom_nh(i  ,Jstr-1,k,3)
            l_b_im1   = ijk2lmom_nh(i-1,Jstr-1,k,3)
            l_bm1_i   = ijk2lmom_nh(i  ,Jstr  ,k,3)
            l_bm1_im1 = ijk2lmom_nh(i-1,Jstr  ,k,3)

            grad(i,Jstr-1)=(qdm_nbq_a(l_b_i  )
     &                     -qdm_nbq_a(l_b_im1))
#    ifdef MASKING
     &                              *pmask(i  ,Jstr)
#    endif
            grad(i,Jstr  )=(qdm_nbq_a(l_bm1_i  )
     &                     -qdm_nbq_a(l_bm1_im1))
#    ifdef MASKING
     &                                *pmask(i,Jstr+1)
#    endif
          enddo
          do i=Istr,Iend

            l_b_i     = ijk2lmom_nh(i  ,Jstr-1,k,3)
            l_bm1_i   = ijk2lmom_nh(i  ,Jstr  ,k,3)
            l_bm2_i   = ijk2lmom_nh(i  ,Jstr+1,k,3)

            dft=qdm_nbq_a(l_bm1_i)-qdm_nbq_a(l_bm1_i)
            dfx=qdm_nbq_a(l_bm1_i)-qdm_nbq_a(l_bm2_i)

            if (dfx*dft .lt. 0.) then
              dft=0.                      ! <-- cancel cx, if inflow
#    if defined NBQ_FRC_BRY || defined NBQNUDGING
              tau=tau_in
            else
              tau=tau_out
#    endif
            endif
 
            if (dft*(grad(i,Jstr)+grad(i+1,Jstr)) .gt. 0.) then
              dfy=grad(i,Jstr)
            else
              dfy=grad(i+1,Jstr)
            endif
 
#    ifdef OBC_COM_RAD_NORMAL
            dfy=0.
#    endif
            cff=max(dfx*dfx+dfy*dfy, eps)
            cx=dft*dfx
#    ifdef OBC_COM_RAD_NPO
            cy=0.
#    else
            cy=min(cff,max(dft*dfy,-cff))
#    endif
 
            qdm_nbq_a(l_b_i)=( cff*qdm_nbq_a(l_b_i  )
     &                              +cx*qdm_nbq_a(l_bm1_i)
     &                             -max(cy,0.)*grad(i  ,Jstr-1)
     &                             -min(cy,0.)*grad(i+1,Jstr-1)
     &                                               )/(cff+cx)
#    if defined NBQ_FRC_BRY  || defined NBQNUDGING 
            qdm_nbq_a(l_b_i)=(1.-tau)*qdm_nbq_a(l_b_i)
#     ifdef NBQ_FRC_BRY
     &                                   +tau*wnbqbry_south(i,k)
#     else     
     &                                  +tau*wnbqclm(i,Jstr-1,k)
#     endif
#    endif
#    ifdef MASKING
            qdm_nbq_a(l_b_i)=qdm_nbq_a(l_b_i)*rmask(i,Jstr-1)
#    endif
#    ifdef WET_DRY
            qdm_nbq_a(l_b_i)=qdm_nbq_a(l_b_i)
     &                                             *rmask_wet(i,Jstr-1)
#    endif
          enddo
        enddo
#   elif defined OBC_COM_NBQSPECIFIED
!                                           Southern edge Specified BC
!                                           ======== ==== ========= ==
        do k=0,N
          do i=Istr,Iend
            l_b_i     = ijk2lmom_nh(i  ,Jstr-1,k,3)
#    ifdef NBQ_FRC_BRY
            qdm_nbq_a(l_b_i)=wnbqbry_south(i,k)        ! specified
#    else
            qdm_nbq_a(l_b_i)=wnbqclm(i,Jstr-1,k)
#    endif
#    ifdef MASKING
     &                               *rmask(i,Jstr-1)
#    endif
#    ifdef WET_DRY
     &                           *rmask_wet(i,Jstr-1)
#    endif
          enddo
        enddo
#   else
!                                           Southern edge gradient BC
!                                           ======== ==== ======== ==
        do k=0,N
          do i=Istr,Iend
!            qdmw_nbq(i,Jstr-1,k)=qdmw_nbq(i,Jstr,k)
             qdmw_nbq(i,Jstr-1,k)=wz(i,Jstr-1,k,nrhs)
     &          *hzw_half_nbq(i,Jstr-1,k)
#    ifdef MASKING
     &                                   *rmask(i,Jstr-1)
#    endif
#    ifdef WET_DRY
     &                               *rmask_wet(i,Jstr-1)
#    endif
          enddo
        enddo
#   endif
!#  else
!        do k=1,N                                     ! Southern edge closed
!          do i=Istr,Iend                             ! ======== ==== ======
!            l_b_i     = ijk2lmom_nh(i  ,Jstr-1,k,3)  !  (no-slip: default)
!            qdm_nbq_a(l_b_i)=0.
!          enddo
!        enddo
#  endif              /* OBC_COM_SOUTH */
      endif         !<-- SOUTHERN_EDGE
!
!====================================================================
!                            NORTHERN BC
!====================================================================
      if (NORTHERN_EDGE) then
#  ifdef OBC_COM_NORTH
#   ifdef OBC_COM_NBQORLANSKI
        do k=0,N                            ! Northern edge radiation
          do i=Istr,Iend+1                  ! ======== ==== =========

            l_b_i     = ijk2lmom_nh(i  ,Jend+1,k,3)
            l_b_im1   = ijk2lmom_nh(i-1,Jend+1,k,3)
            l_bm1_i   = ijk2lmom_nh(i  ,Jend  ,k,3)
            l_bm1_im1 = ijk2lmom_nh(i-1,Jend  ,k,3)

            grad(i,Jend  )=(qdm_nbq_a(l_bm1_j  )
     &                     -qdm_nbq_a(l_bm1_im1))
#    ifdef MASKING
     &                                  *umask(i,Jend)
#    endif
            grad(i,Jend+1)=(qdm_nbq_a(l_b_j  )
     &                     -qdm_nbq_a(l_b_im1))
#    ifdef MASKING
     &                              *umask(i,Jend+1)
#    endif
          enddo
          do i=Istr,Iend

            l_b_i     = ijk2lmom_nh(i  ,Jend+1,k,3)
            l_bm1_i   = ijk2lmom_nh(i  ,Jend  ,k,3)
            l_bm2_i   = ijk2lmom_nh(i  ,Jend-1,k,3)

            dft=qdm_nbq_a(l_bm1_i)-qdm_nbq_a(l_bm1_i)
            dfx=qdm_nbq_a(l_bm1_i)-qdm_nbq_a(l_bm2_i)

            if (dfx*dft .lt. 0.) then
              dft=0.                       ! <-- cancel cx, if inflow
#    if defined NBQ_FRC_BRY || defined NBQNUDGING
              tau=tau_in
            else
              tau=tau_out
#    endif
            endif
 
            if (dft*(grad(i,Jend)+grad(i+1,Jend)) .gt. 0.) then
              dfy=grad(i,Jend)
            else
              dfy=grad(i+1,Jend)
            endif
 
#    ifdef OBC_COM_RAD_NORMAL
            dfy=0.
#    endif
            cff=max(dfx*dfx+dfy*dfy, eps)
            cx=dft*dfx
#    ifdef OBC_COM_RAD_NPO
            cy=0.
#    else
            cy=min(cff,max(dft*dfy,-cff))
#    endif
 
            qdm_nbq_a(l_b_i)=( cff*qdm_nbq_a(l_b_i)
     &                              +cx*qdm_nbq_a(l_bm1_i)
     &                             -max(cy,0.)*grad(i  ,Jend+1)
     &                             -min(cy,0.)*grad(i+1,Jend+1)
     &                                               )/(cff+cx)
#    if defined NBQ_FRC_BRY  || defined NBQNUDGING 
            qdm_nbq_a(l_b_i)=(1.-tau)*qdm_nbq_a(l_b_i)
#     ifdef NBQ_FRC_BRY
     &                                   +tau*wnbqbry_north(i,k)
#     else     
     &                                  +tau*wnbqclm(i,Jend+1,k)
#     endif
#    endif
#    ifdef MASKING
          qdm_nbq_a(l_b_i)=qdm_nbq_a(l_b_i)*rmask(i,Jend+1)
#    endif
#    ifdef WET_DRY
          qdm_nbq_a(l_b_i)=qdm_nbq_a(l_b_i)
     &                                           *rmask_wet(i,Jend+1)
#    endif
          enddo
        enddo
!
#   elif defined OBC_COM_NBQSPECIFIED
!                                           Northern edge Specified BC
!                                           ======== ==== ========= ==
        do k=0,N
          do i=Istr,Iend
            l_b_i     = ijk2lmom_nh(i  ,Jend+1,k,3)
#    ifdef NBQ_FRC_BRY
            qdm_nbq_a(l_b_i)=wnbqbry_north(i,k)      ! specified
#    else
            qdm_nbq_a(l_b_i)=wnbqclm(i,Jend+1,k)
#    endif
#    ifdef MASKING
     &                               *rmask(i,Jend+1)
#    endif
#    ifdef WET_DRY
     &                           *rmask_wet(i,Jend+1)
#    endif
          enddo
        enddo
#   else
        do k=0,N
          do i=Istr,Iend
!                                           Northern edge gradient BC
!                                           ======== ==== ======== ==
!            qdmw_nbq(i,Jend+1,k)=qdmw_nbq(i,Jend,k)
             qdmw_nbq(i,Jend+1,k)=wz(i,Jend+1,k,nrhs)
     &          *hzw_half_nbq(i,Jend+1,k)
#    ifdef MASKING
     &                                   *rmask(i,Jend+1)
#    endif
#    ifdef WET_DRY
     &                               *rmask_wet(i,Jend+1)
#    endif
          enddo
        enddo
#   endif
!#  else
!        do k=1,N                                      ! Northern edge closed
!          do i=Istr,Iend                              ! ======== ==== ======
!            l_b_i     = ijk2lmom_nh(i  ,Jend+1,k,3)   !   (no-slip: default)
!            qdm_nbq_a(l_b_i)=0.
!          enddo
!        enddo
#  endif
      endif     !<--  NORTHERN_EDGE
# endif          /* !NS_COM_PERIODIC */
 
                           ! Corners between adjacent open boundaries
                           ! ======= ======= ======== ==== ==========
 
# if defined OBC_COM_SOUTH && defined OBC_COM_WEST
      if (WESTERN_EDGE .and. SOUTHERN_EDGE) then
        do k=0,N
          qdmw_nbq(Istr-1,Jstr-1,k)=0.5*(qdmw_nbq(Istr,Jstr-1,k)
     &                                  +qdmw_nbq(Istr-1,Jstr,k))
#  ifdef MASKING
     &                                *rmask(Istr-1,Jstr-1)
#  endif
        enddo
      endif
# endif
# if defined OBC_COM_SOUTH && defined OBC_COM_EAST
      if (EASTERN_EDGE .and. SOUTHERN_EDGE) then
        do k=0,N
          qdmw_nbq(Iend+1,Jstr-1,k)=0.5*(qdmw_nbq(Iend,Jstr-1,k)
     &                                  +qdmw_nbq(Iend+1,Jstr,k))     
#  ifdef MASKING
     &                                *rmask(Iend+1,Jstr-1)
#  endif
        enddo
      endif
# endif
# if defined OBC_COM_NORTH && defined OBC_COM_WEST
      if (WESTERN_EDGE .and. NORTHERN_EDGE) then
        do k=0,N
          qdmw_nbq(Istr-1,Jend+1,k)=0.5*(qdmw_nbq(Istr  ,Jend+1,k)
     &                                  +qdmw_nbq(Istr-1,Jend,k)) 
#  ifdef MASKING
     &                                *rmask(Istr-1,Jend+1)
#  endif
        enddo
      endif
# endif
# if defined OBC_COM_NORTH && defined OBC_COM_EAST
      if (EASTERN_EDGE .and. NORTHERN_EDGE) then
        do k=0,N
          qdmw_nbq(Iend+1,Jend+1,k)=0.5*(qdmw_nbq(Iend  ,Jend+1,k)
     &                                  +qdmw_nbq(Iend+1,Jend,k))     
#  ifdef MASKING
     &                                *rmask(Iend+1,Jend+1)
#  endif
        enddo
      endif
# endif

# undef nnew
# undef nstp

      return
      end
#else
# ifndef CHILD
      subroutine wnbqijk_bc_parent_empty
      end
# else
      subroutine wnbqijk_bc_child_empty
      end
# endif
#endif /* NBQ */
#ifndef CHILD
# define CHILD
# ifdef AGRIF
#  include "wnbqijk_bc.F"
# endif
# undef CHILD
#endif  /* !CHILD */
