!--------------------------------------------------------------------------------------------------
!  DOXYGEN
!> @brief SUPPLY ALL BOUNDARIES
!> @details SUPPLY ALL BOUNDARIES WITH V POINTS.
!> @author ORIGINATOR - ????? 
!> @date ??-??-?? \n
!> @author LUCCI 
!> @date 18-03-20 \n
!> @version V1.1.0
!> @details MODERNIZATION OF THE CODE, INCLUDING:
!!                      * F77 TO F90/F95
!!                      * INDENTATION & UNIFORMIZATION CODE
!!                      * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!!                      * DOCUMENTATION WITH DOXYGEN
!!                      * OPENMP FUNCTIONALITY 
!<
!> @param[in] LMAX - Significado de LMAX
!> @param[in] LN - Significado de LN
!> @param[inout] UARRAY - Significado de UARRAY
!> @param[inout] VARRAY - Significado de VARRAY
!> @details <b>Use Module:</b>
!! @arg @c DOM
!! @arg @c F77KINDS
!! @arg @c MPPSTAFF
!! @arg @c NEBMAP
!! @arg @c PARMETA
!> @details <b>Driver:</b> 
!<
!> @details <b>Calls:</b> 
!! @arg @c MPI_IRECV
!! @arg @c MPI_ISEND
!! @arg @c MPI_WAIT
!! @arg @c ROTCCW90
!! @arg @c ROTCW90
!! @arg @c ROT180
!! @arg @c SWAPUV_LN
!<
!--------------------------------------------------------------------------------------------------
   SUBROUTINE BOCOVMPI_LN(UARRAY, VARRAY, LMAX,LN)
!--------------------------------------------------------------------------------------------------
! SUBROUTINE BOCOVMPI_LN
! 
! SUBPROGRAM: BOCOVMPI_LN - SUPPLY ALL BOUNDARIES WITH V POINTS
! PROGRAMMER: ????? 
! ORG: W/NP22
! DATE: ??-??-??
!
! ABSTRACT:  
! SUPPLY ALL BOUNDARIES WITH V POINTS: ANY TOPOLOGY WITH MPI VERSION
!
! PROGRAM HISTORY LOG:
! ??-??-??  ?????   - ORIGINATOR
! 18-03-20  LUCCI   - MODERNIZATION OF THE CODE, INCLUDING:
!                     * F77 TO F90/F95
!                     * INDENTATION & UNIFORMIZATION CODE
!                     * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!                     * DOCUMENTATION WITH DOXYGEN
!                     * OPENMP FUNCTIONALITY 
!
!
! INPUT ARGUMENT LIST:
! IMAX - 
! JMAX - 
! LMAX - 
! ARR1 -
!
! OUTPUT ARGUMENT LIST:
! ARR2 - 
!
! INPUT/OUTPUT ARGUMENT LIST:
! NONE
!
! INPUT FILES:
! NONE
!
! OUTPUT FILES:
! NONE
!
! USE MODULES: DOM
!              F77KINDS
!              MPPSTAFF
!              NEBMAP
!              PARMETA
!
! DRIVER     : 
!
! CALLS      : MPI_IRECV
!              MPI_ISEND
!              MPI_WAIT
!              ROTCCW90
!              ROTCW90
!              ROT180
!              SWAPUV_LN           
!--------------------------------------------------------------------------------------------------
    USE DOM
    USE F77KINDS
    USE MPPSTAFF
    USE NEBMAP
    USE PARMETA
!
    IMPLICIT NONE

    INCLUDE "mpif.h"
!
    INTEGER(KIND=I4)                                                                            ::&
    & LMAX    , LMAX2   , LN
!
    INTEGER(KIND=I4)                                                                            ::&
    & I       , J       , L
!
    INTEGER(KIND=I4)                                                                            ::&
    & NDATA   , NEBPE   , IERR    , IAERR   , IDERR
!
    INTEGER(KIND=I4)                                                                            ::&
    & ISEND   , IRECV
!
    INTEGER(KIND=I4)    , PARAMETER :: IMP1  = IM  + 1
    INTEGER(KIND=I4)    , PARAMETER :: JMP1  = IMP1
    INTEGER(KIND=I4)    , PARAMETER :: IM1X2 = IM1 * 2
    INTEGER(KIND=I4)    , PARAMETER :: JM1X2 = IM1X2
!
    REAL   (KIND=R4)    , DIMENSION(1-LN:IM+LN, 1-LN:JM+LN, LMAX)                               ::&
    & UARRAY  , VARRAY
!
    REAL   (KIND=R4)    , DIMENSION(:,:,:)  , ALLOCATABLE                                       ::&
    & SBUF1   , SBUF2   , SBUF3   , SBUF4   ,                                                     &
    & RBUF1   , RBUF2   , RBUF3   , RBUF4   ,                                                     &
    & TMPBUF
!  
    REAL   (KIND=R4)    , DIMENSION(:,:)    , ALLOCATABLE                                       ::&
    & SBUF1_C , SBUF2_C , SBUF3_C , SBUF4_C ,                                                     &
    & RBUF1_C , RBUF2_C , RBUF3_C , RBUF4_C
!
    INTEGER(KIND=I4)    , DIMENSION(8)                                                          ::&
    & SHANDLE , RHANDLE
!
    INTEGER(KIND=I4)    , DIMENSION(MPI_STATUS_SIZE)                                            ::&
    & ISTAT
!
    REAL   (KIND=R4)    , DIMENSION(LMAX)                                                       ::&
    & DUMMY
!----------------
! SEND BOUNDARIES
!----------------
      NDATA = IM1X2 * LMAX * LN
!-------------
! TOWARD NORTH
!-------------
!
    IF (MY_NEB(1) >= 0) THEN
        NEBPE = MY_NEB(1)
!
        ALLOCATE (TMPBUF(IM1, LN*2, LMAX ), STAT = IAERR)
!
        TMPBUF(:, 1:LN     , 1:LMAX) = UARRAY(1:IM1, JM-LN:JM1, 1:LMAX) * ISGNU_NEB(1)
        TMPBUF(:, LN+1:2*LN, 1:LMAX) = VARRAY(1:IM1, JM-LN:JM1, 1:LMAX) * ISGNV_NEB(1)
!
        IF (.NOT. LINV_NEB(1) .AND. .NOT. LEXC_NEB(1)) THEN 
            ALLOCATE (SBUF1(IM-1, LN*2, LMAX), STAT = IAERR)
!
            SBUF1 = TMPBUF
!
        ELSE IF (.NOT. LINV_NEB(1) .AND. LEXC_NEB(1)) THEN
!
            ALLOCATE (SBUF1(LN*2, IM-1, LMAX), STAT = IAERR)
!
            CALL ROTCCW90(TMPBUF, SBUF1, IM-1, LN*2, LMAX)
!
        ELSE IF (LINV_NEB(1) .AND. .NOT. LEXC_NEB(1)) THEN
!
            ALLOCATE (SBUF1(IM-1, LN*2, LMAX), STAT = IAERR)
!
            CALL ROT180   (TMPBUF,SBUF1,IM-1, LN*2, LMAX   )
            CALL SWAPUV_LN(SBUF1 ,IM-1 ,LN*2, LMAX, .FALSE.)
!
        ELSE
!
            ALLOCATE (SBUF1(LN*2, IM-1, LMAX), STAT = IAERR)
!
            CALL ROTCW90  (TMPBUF, SBUF1, IM-1, LN*2, LMAX  )
            CALL SWAPUV_LN(SBUF1 , LN*2 , IM-1, LMAX, .TRUE.)  ! TRUE FOR SWAP LEFT AND RIGHT
!
        END IF
!
        DEALLOCATE (TMPBUF)
!
        CALL MPI_ISEND(SBUF1, NDATA, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(1), ISEND)
!
    END IF
!-------------       
! TOWARD EAST 
!------------
    IF (MY_NEB(2) >= 0) THEN
        NEBPE = MY_NEB(2)
!
        ALLOCATE (TMPBUF(LN*2, IM1, LMAX ), STAT = IAERR)
!
        TMPBUF(1:LN     , :, 1:LMAX) = UARRAY(IM-LN:IM1, 1:JM1, 1:LMAX ) * ISGNU_NEB(2)
        TMPBUF(LN+1:2*LN, :, 1:LMAX) = VARRAY(IM-LN:IM1, 1:JM1, 1:LMAX ) * ISGNV_NEB(2)
!
    IF (.NOT. LINV_NEB(2) .AND. .NOT. LEXC_NEB(2)) THEN 
!
        ALLOCATE (SBUF2(LN*2, IM-1, LMAX), STAT = IAERR)
!
        SBUF2 = TMPBUF
!
    ELSE IF (.NOT. LINV_NEB(2) .AND. LEXC_NEB(2)) THEN
!
        ALLOCATE (SBUF2(IM-1, LN*2, LMAX), STAT = IAERR)
!
        CALL ROTCW90(TMPBUF, SBUF2, LN*2, IM-1, LMAX)
!
    ELSE IF (LINV_NEB(2) .AND. .NOT. LEXC_NEB(2)) THEN
!
        ALLOCATE (SBUF2(LN*2, IM-1, LMAX), STAT = IAERR)
!
        CALL ROT180(TMPBUF, SBUF2, LN*2, IM-1, LMAX)
!
    ELSE
!
        ALLOCATE (SBUF2(IM-1, LN*2, LMAX), STAT = IAERR)
!
        CALL  ROTCCW90(TMPBUF, SBUF2, LN*2, IM-1, LMAX   )
        CALL SWAPUV_LN(SBUF2 , LN*2 , IM-1, LMAX, .FALSE.)
!
    END IF
!
    DEALLOCATE (TMPBUF)
!
    CALL MPI_ISEND(SBUF2, NDATA, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(2), ISEND)
!
    END IF
!-------------
! TOWARD SOUTH
!-------------
    IF (MY_NEB(3) >= 0) THEN
!
    NEBPE = MY_NEB(3)
!
        ALLOCATE (TMPBUF(IM1,LN*2,LMAX), STAT = IAERR)
!
        TMPBUF(:, 1:LN     , 1:LMAX) = UARRAY(1:IM1, 1:LN, 1:LMAX) * ISGNU_NEB(3)
        TMPBUF(:, LN+1:2*LN, 1:LMAX) = VARRAY(1:IM1, 1:LN, 1:LMAX) * ISGNV_NEB(3)
!
        IF (.NOT. LINV_NEB(3) .AND. .NOT. LEXC_NEB(3)) THEN 
!
            ALLOCATE (SBUF3(IM-1,LN*2,LMAX), STAT = IAERR)
!
            SBUF3 = TMPBUF
!
        ELSE IF (.NOT. LINV_NEB(3) .AND. LEXC_NEB(3)) THEN
!
            ALLOCATE (SBUF3(LN*2,IM-1,LMAX), STAT = IAERR)
!
            CALL ROTCCW90(TMPBUF, SBUF3, IM-1, LN*2, LMAX)
!
        ELSE IF (LINV_NEB(3) .AND. .NOT. LEXC_NEB(3)) THEN
!
            ALLOCATE (SBUF3(IM-1,LN*2,LMAX), STAT = IAERR)
!
            CALL ROT180(TMPBUF, SBUF3, IM-1, LN*2, LMAX)
!
            CALL SWAPUV_LN(SBUF3, IM-1, LN*2, LMAX, .FALSE.)
!
        ELSE
!
            ALLOCATE (SBUF3(LN*2,IM-1,LMAX), STAT = IAERR)
!
            CALL ROTCW90(TMPBUF, SBUF3, IM-1, LN*2, LMAX)
!
            CALL SWAPUV_LN(SBUF3, LN*2, IM-1, LMAX, .TRUE.)       ! TRUE FOR SWAP LEFT AND RIGHT
!
        END IF
!
        DEALLOCATE (TMPBUF)
!
        CALL MPI_ISEND(SBUF3, NDATA, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(3), ISEND)
    END IF
!------------
! TOWARD WEST
!------------
    IF (MY_NEB(4) >= 0 ) THEN
!
    NEBPE = MY_NEB(4)
!
        ALLOCATE (TMPBUF(LN*2,IM1,LMAX ), STAT = IAERR)
!
        TMPBUF(1:LN     , :, 1:LMAX) = UARRAY(1:LN, 1:JM1, 1:LMAX) * ISGNU_NEB(4)
        TMPBUF(LN+1:2*LN, :, 1:LMAX) = VARRAY(1:LN, 1:JM1, 1:LMAX) * ISGNV_NEB(4)
!
        IF (.NOT. LINV_NEB(4) .AND. .NOT. LEXC_NEB(4)) THEN 
!
            ALLOCATE (SBUF4(LN*2,IM-1,LMAX), STAT = IAERR)
!
            SBUF4 = TMPBUF
!
        ELSE IF (.NOT. LINV_NEB(4) .AND. LEXC_NEB(4)) THEN
!
            ALLOCATE (SBUF4(IM-1,LN*2,LMAX), STAT = IAERR)
!
            CALL ROTCW90(TMPBUF, SBUF4, LN*2, IM-1, LMAX)
!
        ELSE IF (LINV_NEB(4) .AND. .NOT. LEXC_NEB(4)) THEN
!
            ALLOCATE (SBUF4(LN*2,IM-1,LMAX), STAT = IAERR)
!
            CALL ROT180(TMPBUF, SBUF4, LN*2, IM-1, LMAX)
        ELSE
!
            ALLOCATE (SBUF4(IM-1,LN*2,LMAX), STAT = IAERR)
!
            CALL ROTCCW90(TMPBUF, SBUF4, LN*2, IM-1, LMAX)
!
            CALL SWAPUV_LN(SBUF4, LN*2, IM-1, LMAX, .FALSE.)
!
        END IF
!
    DEALLOCATE (TMPBUF)
!
    CALL MPI_ISEND(SBUF4, NDATA, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(4), ISEND)
!
    END IF
!-------------------
! RECEIVE BOUNDARIES
!-------------------
!
!-----------
! FROM NORTH
!-----------
    IF (MY_NEB(1) >= 0 ) THEN
!
        NEBPE = MY_NEB(1)
!
        ALLOCATE (RBUF1(1:IM1,LN*2, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF1, NDATA, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(1), IRECV)
!
        CALL MPI_WAIT(RHANDLE(1), ISTAT, IERR)
!
        UARRAY(1:IM1, JM:JM+LN-1, 1:LMAX) = RBUF1(1:IM1, 1:LN     , 1:LMAX )
        VARRAY(1:IM1, JM:JM+LN-1, 1:LMAX) = RBUF1(1:IM1, LN+1:2*LN, 1:LMAX )
!
        DEALLOCATE (RBUF1, STAT = IDERR)
!
    END IF
!----------
! FROM EAST
!----------
    IF (MY_NEB(2) >= 0) THEN
!
        NEBPE = MY_NEB(2)
!
        ALLOCATE (RBUF2(LN*2,1:IM1, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF2, NDATA, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(2), IRECV)
!
        CALL MPI_WAIT(RHANDLE(2), ISTAT, IERR)
!
        UARRAY(IM:IM+LN-1, 1:JM1, 1:LMAX) = RBUF2(1:LN     , :, :)
        VARRAY(IM:IM+LN-1, 1:JM1, 1:LMAX) = RBUF2(LN+1:LN*2, :, :)
!
        DEALLOCATE(RBUF2, STAT = IDERR)
!
    END IF
!-----------
! FROM SOUTH 
!-----------
    IF (MY_NEB(3) >= 0) THEN
!
        NEBPE = MY_NEB(3)
!
        ALLOCATE (RBUF3(1:IM1,LN*2, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF3, NDATA, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(3), IRECV)
!
        CALL MPI_WAIT( RHANDLE(3), ISTAT, IERR)
!
        UARRAY(1:IM1, 1-LN:0, 1:LMAX ) = RBUF3(:, 1:LN     , 1:LMAX)
        VARRAY(1:IM1, 1-LN:0, 1:LMAX ) = RBUF3(:, LN+1:LN*2, 1:LMAX)
!
        DEALLOCATE (RBUF3, STAT = IDERR)
!
    END IF
!----------
! FROM WEST 
!----------
    IF (MY_NEB(4) >= 0) THEN
!
        NEBPE = MY_NEB(4)
!
        ALLOCATE (RBUF4(LN*2, 1:IM1, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF4, NDATA, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(4), IRECV)
!
        CALL MPI_WAIT(RHANDLE(4), ISTAT, IERR)
!
        UARRAY(1-LN:0, 1:JM1, 1:LMAX) = RBUF4(1:LN     , :, 1:LMAX)
        VARRAY(1-LN:0, 1:JM1, 1:LMAX) = RBUF4(LN+1:LN*2, :, 1:LMAX)
!
        DEALLOCATE (RBUF4, STAT = IDERR)
!
    END IF
!---------------------
! DEALLOCATE SBUFFERES
!---------------------
    IF (MY_NEB(1) >= 0) THEN
        CALL MPI_WAIT(SHANDLE(1), ISTAT, IERR)
!
        DEALLOCATE (SBUF1, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(2) >= 0) THEN
        CALL MPI_WAIT(SHANDLE(2), ISTAT, IERR)
!
        DEALLOCATE (SBUF2, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(3) >= 0 ) THEN
        CALL MPI_WAIT(SHANDLE(3), ISTAT, IERR)
!
        DEALLOCATE (SBUF3, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(4) >= 0) THEN
        CALL MPI_WAIT (SHANDLE(4), ISTAT, IERR)
!
        DEALLOCATE (SBUF4, STAT = IERR)
!
    END IF
!-------------
! SEND CORNERS
!-------------
    LMAX2 = LMAX * 2
!------------------
! TOWARD NORTH-EAST
!------------------
    IF  (MY_NEB(5) >= 0) THEN
!
        NEBPE = MY_NEB(5)
!
        ALLOCATE (SBUF1_C(1:2, 1:LMAX), STAT = IAERR)
!
        SBUF1_C(1, 1:LMAX) = UARRAY( IM1, JM1, 1:LMAX) * ISGNU_NEB(5)
        SBUF1_C(2, 1:LMAX) = VARRAY( IM1, JM1, 1:LMAX) * ISGNV_NEB(5)
!
        IF (LEXC_NEB(5))  THEN
            DUMMY(:)     = SBUF1_C(1,:)
            SBUF1_C(1,:) = SBUF1_C(2,:)
            SBUF1_C(2,:) = DUMMY(:)
        END IF
!
        CALL MPI_ISEND(SBUF1_C, LMAX2, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(5), ISEND)
!     
    END IF
!------------------       
! TOWARD SOUTH-EAST 
!------------------
      IF (MY_NEB(6) >= 0) THEN
!
        NEBPE = MY_NEB(6)
!
        ALLOCATE (SBUF2_C(1:2, 1:LMAX), STAT = IAERR)
!
        SBUF2_C(1,1:LMAX) = UARRAY(IM1, 1, 1:LMAX) * ISGNU_NEB(6)
        SBUF2_C(2,1:LMAX) = VARRAY(IM1, 1, 1:LMAX) * ISGNV_NEB(6)
!
        IF (LEXC_NEB(6)) THEN
              DUMMY(:)   = SBUF2_C(1,:)
            SBUF2_C(1,:) = SBUF2_C(2,:)
            SBUF2_C(2,:) = DUMMY(:)
        END IF
!
        CALL MPI_ISEND(SBUF2_C, LMAX2, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(6), ISEND)
!
    END IF
!------------------
! TOWARD SOUTH-WEST
!------------------ 
    IF (MY_NEB(7) >= 0) THEN
!
        NEBPE = MY_NEB(7)
!
        ALLOCATE (SBUF3_C(1:2, 1:LMAX), STAT = IAERR)
!
        SBUF3_C(1,:) = UARRAY(1, 1, :) * ISGNU_NEB(7)
        SBUF3_C(2,:) = VARRAY(1, 1, :) * ISGNV_NEB(7)
!
        IF (LEXC_NEB(7)) THEN
            DUMMY(:)     = SBUF3_C(1,:)
            SBUF3_C(1,:) = SBUF3_C(2,:)
            SBUF3_C(2,:) = DUMMY(:)
        END IF
!
        CALL MPI_ISEND(SBUF3_C, LMAX2, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(7), ISEND)
!
    END IF
!------------------
! TOWARD NORTH-WEST
!------------------
    IF (MY_NEB(8) >= 0) THEN
!
        NEBPE = MY_NEB(8)
!
        ALLOCATE (SBUF4_C(1:2, 1:LMAX), STAT = IAERR)
!
        SBUF4_C(1,:) = UARRAY(1, JM1, :) * ISGNU_NEB(8)
        SBUF4_C(2,:) = VARRAY(1, JM1, :) * ISGNV_NEB(8)
!
        IF (LEXC_NEB(8)) THEN
              DUMMY(:)   = SBUF4_C(1,:)
            SBUF4_C(1,:) = SBUF4_C(2,:)
            SBUF4_C(2,:) = DUMMY(:)
        END IF
!
        CALL MPI_ISEND(SBUF4_C, LMAX2, ITYPE, NEBPE, MYPE, MPI_COMM_WORLD, SHANDLE(8), ISEND)
!
    END IF
!----------------
! RECEIVE CORNERS
!----------------
!
!----------------
! FROM NORTH-EAST  
!----------------
    IF (MY_NEB(5) >= 0) THEN
!
        NEBPE = MY_NEB(5)
!
        ALLOCATE (RBUF1_C(1:2,1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF1_C, LMAX2, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(5), IRECV)
!
        CALL MPI_WAIT(RHANDLE(5), ISTAT, IERR)
!
        UARRAY(IM, JM, :) = RBUF1_C(1, :)
        VARRAY(IM, JM, :) = RBUF1_C(2, :)
!
        DEALLOCATE (RBUF1_C, STAT = IERR)
!
    END IF
!----------------
! FROM SOUTH-EAST
!----------------
    IF (MY_NEB(6) >= 0) THEN
!
        NEBPE = MY_NEB(6)
!
        ALLOCATE (RBUF2_C(1:2, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF2_C, LMAX2, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(6), IRECV)
!
        CALL MPI_WAIT(RHANDLE(6), ISTAT, IERR)
!
        UARRAY(IM, 0, :) = RBUF2_C(1, :)
        VARRAY(IM, 0, :) = RBUF2_C(2, :)
!
        DEALLOCATE(RBUF2_C, STAT = IERR)
!
    END IF
!----------------
! FROM SOUTH-WEST
!----------------
      IF (MY_NEB(7) >= 0) THEN
!
        ALLOCATE (RBUF3_C(1:2, 1:LMAX), STAT = IAERR)
!
        NEBPE = MY_NEB(7)
!
        CALL MPI_IRECV(RBUF3_C, LMAX2, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(7), IRECV)
!
        CALL MPI_WAIT(RHANDLE(7), ISTAT, IERR)
!
        UARRAY(0, 0 , :) = RBUF3_C(1, :)
        VARRAY(0, 0 , :) = RBUF3_C(2, :)
!
        DEALLOCATE (RBUF3_C, STAT = IERR)
!
    END IF
!----------------
! FROM NORTH-WEST
!----------------
    IF (MY_NEB(8) >= 0) THEN
!
        NEBPE = MY_NEB(8)
!
        ALLOCATE (RBUF4_C(1:2, 1:LMAX), STAT = IAERR)
!
        CALL MPI_IRECV(RBUF4_C, LMAX2, ITYPE, NEBPE, NEBPE, MPI_COMM_WORLD, RHANDLE(8), IRECV)
!
        CALL MPI_WAIT(RHANDLE(8), ISTAT, IERR)
!
        UARRAY(0, JM, :) = RBUF4_C(1, :)
        VARRAY(0, JM, :) = RBUF4_C(2, :)
!
        DEALLOCATE (RBUF4_C, STAT = IERR)
!
    END IF
!---------------------
! DEALLOCATE SBUFFERES
!---------------------
    IF (MY_NEB(5) >= 0) THEN
!
        CALL MPI_WAIT(SHANDLE(5), ISTAT, IERR)
!
        DEALLOCATE (SBUF1_C, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(6) >= 0 ) THEN
!
        CALL MPI_WAIT(SHANDLE(6), ISTAT, IERR)
!
        DEALLOCATE (SBUF2_C, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(7) >= 0) THEN
!
        CALL MPI_WAIT(SHANDLE(7), ISTAT, IERR)
!
        DEALLOCATE(SBUF3_C, STAT = IERR)
!
    END IF
!
    IF (MY_NEB(8) >= 0) THEN
!
        CALL MPI_WAIT(SHANDLE(8), ISTAT, IERR)
!
        DEALLOCATE(SBUF4_C, STAT = IERR)
!
    END IF
!
    END SUBROUTINE BOCOVMPI_LN
