Index: NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps/src/OCE/BDY/bdylib.F90
===================================================================
--- NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps/src/OCE/BDY/bdylib.F90	(revision 10957)
+++ NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps/src/OCE/BDY/bdylib.F90	(revision 11822)
@@ -15,4 +15,5 @@
    USE bdy_oce        ! ocean open boundary conditions
    USE phycst         ! physical constants
+   USE bdyini
    !
    USE in_out_manager !
@@ -75,5 +76,4 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::   phia  ! tracer trend
       !!
-      REAL(wp) ::   zwgt           ! boundary weight
       INTEGER  ::   ib, ik, igrd   ! dummy loop indices
       INTEGER  ::   ii, ij         ! 2D addresses
@@ -92,5 +92,5 @@
 
 
-   SUBROUTINE bdy_orl( idx, phib, phia, dta, ll_npo )
+   SUBROUTINE bdy_orl( idx, phib, phia, dta, lrim0, ll_npo )
       !!----------------------------------------------------------------------
       !!                 ***  SUBROUTINE bdy_orl  ***
@@ -104,4 +104,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::   phib  ! before tracer field
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::   phia  ! tracer trend
+      LOGICAL                 , OPTIONAL,  INTENT(in) ::   lrim0   ! indicate if rim 0 is treated
       LOGICAL,                             INTENT(in) ::   ll_npo  ! switch for NPO version
       !!
@@ -111,10 +112,10 @@
       igrd = 1                       ! Everything is at T-points here
       !
-      CALL bdy_orlanski_3d( idx, igrd, phib(:,:,:), phia(:,:,:), dta, ll_npo )
+      CALL bdy_orlanski_3d( idx, igrd, phib(:,:,:), phia(:,:,:), dta, lrim0, ll_npo )
       !
    END SUBROUTINE bdy_orl
 
 
-   SUBROUTINE bdy_orlanski_2d( idx, igrd, phib, phia, phi_ext, ll_npo )
+   SUBROUTINE bdy_orlanski_2d( idx, igrd, phib, phia, phi_ext, lrim0, ll_npo )
       !!----------------------------------------------------------------------
       !!                 ***  SUBROUTINE bdy_orlanski_2d  ***
@@ -132,4 +133,5 @@
       REAL(wp), DIMENSION(:,:), INTENT(inout) ::   phia     ! model after 2D field (to be updated)
       REAL(wp), DIMENSION(:)  , INTENT(in   ) ::   phi_ext  ! external forcing data
+      LOGICAL, OPTIONAL,        INTENT(in   ) ::   lrim0    ! indicate if rim 0 is treated
       LOGICAL ,                 INTENT(in   ) ::   ll_npo   ! switch for NPO version
       !
@@ -140,4 +142,5 @@
       INTEGER  ::   ii_offset, ij_offset                   ! offsets for mask indices
       INTEGER  ::   flagu, flagv                           ! short cuts
+      INTEGER  ::   ibeg, iend                             ! length of rim to be treated (rim 0 or rim 1 or both)
       REAL(wp) ::   zmask_x, zmask_y1, zmask_y2
       REAL(wp) ::   zex1, zex2, zey, zey1, zey2
@@ -146,7 +149,7 @@
       REAL(wp) ::   zdy_1, zdy_2, zsign_ups
       REAL(wp), PARAMETER :: zepsilon = 1.e-30                 ! local small value
-      REAL(wp), POINTER, DIMENSION(:,:)          :: pmask      ! land/sea mask for field
-      REAL(wp), POINTER, DIMENSION(:,:)          :: pmask_xdif ! land/sea mask for x-derivatives
-      REAL(wp), POINTER, DIMENSION(:,:)          :: pmask_ydif ! land/sea mask for y-derivatives
+      REAL(wp), POINTER, DIMENSION(:,:)          :: zmask      ! land/sea mask for field
+      REAL(wp), POINTER, DIMENSION(:,:)          :: zmask_xdif ! land/sea mask for x-derivatives
+      REAL(wp), POINTER, DIMENSION(:,:)          :: zmask_ydif ! land/sea mask for y-derivatives
       REAL(wp), POINTER, DIMENSION(:,:)          :: pe_xdif    ! scale factors for x-derivatives
       REAL(wp), POINTER, DIMENSION(:,:)          :: pe_ydif    ! scale factors for y-derivatives
@@ -159,7 +162,7 @@
       SELECT CASE(igrd)
          CASE(1)
-            pmask      => tmask(:,:,1)
-            pmask_xdif => umask(:,:,1)
-            pmask_ydif => vmask(:,:,1)
+            zmask      => tmask(:,:,1)
+            zmask_xdif => umask(:,:,1)
+            zmask_ydif => vmask(:,:,1)
             pe_xdif    => e1u(:,:)
             pe_ydif    => e2v(:,:)
@@ -167,7 +170,7 @@
             ij_offset = 0
          CASE(2)
-            pmask      => umask(:,:,1)
-            pmask_xdif => tmask(:,:,1)
-            pmask_ydif => fmask(:,:,1)
+            zmask      => umask(:,:,1)
+            zmask_xdif => tmask(:,:,1)
+            zmask_ydif => fmask(:,:,1)
             pe_xdif    => e1t(:,:)
             pe_ydif    => e2f(:,:)
@@ -175,7 +178,7 @@
             ij_offset = 0
          CASE(3)
-            pmask      => vmask(:,:,1)
-            pmask_xdif => fmask(:,:,1)
-            pmask_ydif => tmask(:,:,1)
+            zmask      => vmask(:,:,1)
+            zmask_xdif => fmask(:,:,1)
+            zmask_ydif => tmask(:,:,1)
             pe_xdif    => e1f(:,:)
             pe_ydif    => e2t(:,:)
@@ -185,7 +188,15 @@
       END SELECT
       !
-      DO jb = 1, idx%nblenrim(igrd)
+      IF( PRESENT(lrim0) ) THEN
+         IF( lrim0 ) THEN   ;   ibeg = 1                       ;   iend = idx%nblenrim0(igrd)   ! rim 0
+         ELSE               ;   ibeg = idx%nblenrim0(igrd)+1   ;   iend = idx%nblenrim(igrd)    ! rim 1
+         END IF
+      ELSE                  ;   ibeg = 1                       ;   iend = idx%nblenrim(igrd)    ! both
+      END IF
+      !
+      DO jb = ibeg, iend
          ii  = idx%nbi(jb,igrd)
          ij  = idx%nbj(jb,igrd) 
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )   CYCLE
          flagu = int( idx%flagu(jb,igrd) )
          flagv = int( idx%flagv(jb,igrd) )
@@ -203,12 +214,12 @@
          !
          ! Calculate scale factors for calculation of spatial derivatives.        
-         zex1 = ( abs(iibm1-iibm2) * pe_xdif(iibm1+ii_offset,ijbm1          )         &
-        &       + abs(ijbm1-ijbm2) * pe_ydif(iibm1          ,ijbm1+ij_offset) ) 
-         zex2 = ( abs(iibm1-iibm2) * pe_xdif(iibm2+ii_offset,ijbm2          )         &
-        &       + abs(ijbm1-ijbm2) * pe_ydif(iibm2          ,ijbm2+ij_offset) ) 
-         zey1 = ( (iibm1-iibm1jm1) * pe_xdif(iibm1jm1+ii_offset,ijbm1jm1          )  & 
+         zex1 = ( abs(iibm1-iibm2) * pe_xdif(iibm1   +ii_offset,ijbm1             )   &
+        &       + abs(ijbm1-ijbm2) * pe_ydif(iibm1             ,ijbm1   +ij_offset) ) 
+         zex2 = ( abs(iibm1-iibm2) * pe_xdif(iibm2   +ii_offset,ijbm2             )   &
+        &       + abs(ijbm1-ijbm2) * pe_ydif(iibm2             ,ijbm2   +ij_offset) ) 
+         zey1 = ( (iibm1-iibm1jm1) * pe_xdif(iibm1jm1+ii_offset,ijbm1jm1          )   & 
         &      +  (ijbm1-ijbm1jm1) * pe_ydif(iibm1jm1          ,ijbm1jm1+ij_offset) ) 
-         zey2 = ( (iibm1jp1-iibm1) * pe_xdif(iibm1+ii_offset,ijbm1)                  &
-        &      +  (ijbm1jp1-ijbm1) * pe_ydif(iibm1          ,ijbm1+ij_offset) ) 
+         zey2 = ( (iibm1jp1-iibm1) * pe_xdif(iibm1   +ii_offset,ijbm1             )   &
+        &      +  (ijbm1jp1-ijbm1) * pe_ydif(iibm1             ,ijbm1   +ij_offset) ) 
          ! make sure scale factors are nonzero
          if( zey1 .lt. rsmall ) zey1 = zey2
@@ -217,11 +228,11 @@
          zey1 = max(zey1,rsmall); zey2 = max(zey2,rsmall); 
          !
-         ! Calculate masks for calculation of spatial derivatives.        
-         zmask_x = ( abs(iibm1-iibm2) * pmask_xdif(iibm2+ii_offset,ijbm2          )         &
-        &          + abs(ijbm1-ijbm2) * pmask_ydif(iibm2          ,ijbm2+ij_offset) ) 
-         zmask_y1 = ( (iibm1-iibm1jm1) * pmask_xdif(iibm1jm1+ii_offset,ijbm1jm1          )  & 
-        &          +  (ijbm1-ijbm1jm1) * pmask_ydif(iibm1jm1          ,ijbm1jm1+ij_offset) ) 
-         zmask_y2 = ( (iibm1jp1-iibm1) * pmask_xdif(iibm1+ii_offset,ijbm1)                  &
-        &          +  (ijbm1jp1-ijbm1) * pmask_ydif(iibm1          ,ijbm1+ij_offset) ) 
+         ! Calculate masks for calculation of spatial derivatives.
+         zmask_x  = ( abs(iibm1-iibm2) * zmask_xdif(iibm2   +ii_offset,ijbm2               )   &
+        &           + abs(ijbm1-ijbm2) * zmask_ydif(iibm2             ,ijbm2   +ij_offset) ) 
+         zmask_y1 = ( (iibm1-iibm1jm1) * zmask_xdif(iibm1jm1+ii_offset,ijbm1jm1            )   & 
+        &          +  (ijbm1-ijbm1jm1) * zmask_ydif(iibm1jm1          ,ijbm1jm1+ij_offset) ) 
+         zmask_y2 = ( (iibm1jp1-iibm1) * zmask_xdif(iibm1   +ii_offset,ijbm1               )   &
+        &          +  (ijbm1jp1-ijbm1) * zmask_ydif(iibm1             ,ijbm1   +ij_offset) ) 
 
          ! Calculation of terms required for both versions of the scheme. 
@@ -231,8 +242,8 @@
          ! Note no rdt factor in expression for zdt because it cancels in the expressions for 
          ! zrx and zry.
-         zdt = phia(iibm1,ijbm1) - phib(iibm1,ijbm1)
-         zdx = ( ( phia(iibm1,ijbm1) - phia(iibm2,ijbm2) ) / zex2 ) * zmask_x 
+         zdt   =     phia(iibm1   ,ijbm1   ) - phib(iibm1   ,ijbm1   )
+         zdx   = ( ( phia(iibm1   ,ijbm1   ) - phia(iibm2   ,ijbm2   ) ) / zex2 ) * zmask_x 
          zdy_1 = ( ( phib(iibm1   ,ijbm1   ) - phib(iibm1jm1,ijbm1jm1) ) / zey1 ) * zmask_y1    
-         zdy_2 = ( ( phib(iibm1jp1,ijbm1jp1) - phib(iibm1   ,ijbm1)    ) / zey2 ) * zmask_y2 
+         zdy_2 = ( ( phib(iibm1jp1,ijbm1jp1) - phib(iibm1   ,ijbm1   ) ) / zey2 ) * zmask_y2 
          zdy_centred = 0.5 * ( zdy_1 + zdy_2 )
 !!$         zdy_centred = phib(iibm1jp1,ijbm1jp1) - phib(iibm1jm1,ijbm1jm1)
@@ -265,5 +276,5 @@
            &                    + zwgt * ( phi_ext(jb) - phib(ii,ij) ) ) / ( 1. + zrx ) 
          end if
-         phia(ii,ij) = phia(ii,ij) * pmask(ii,ij)
+         phia(ii,ij) = phia(ii,ij) * zmask(ii,ij)
       END DO
       !
@@ -271,5 +282,5 @@
 
 
-   SUBROUTINE bdy_orlanski_3d( idx, igrd, phib, phia, phi_ext, ll_npo )
+   SUBROUTINE bdy_orlanski_3d( idx, igrd, phib, phia, phi_ext, lrim0, ll_npo )
       !!----------------------------------------------------------------------
       !!                 ***  SUBROUTINE bdy_orlanski_3d  ***
@@ -287,4 +298,5 @@
       REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::   phia     ! model after 3D field (to be updated)
       REAL(wp), DIMENSION(:,:)  , INTENT(in   ) ::   phi_ext  ! external forcing data
+      LOGICAL, OPTIONAL,          INTENT(in   ) ::   lrim0    ! indicate if rim 0 is treated
       LOGICAL ,                   INTENT(in   ) ::   ll_npo   ! switch for NPO version
       !
@@ -295,4 +307,5 @@
       INTEGER  ::   ii_offset, ij_offset                   ! offsets for mask indices
       INTEGER  ::   flagu, flagv                           ! short cuts
+      INTEGER  ::   ibeg, iend                             ! length of rim to be treated (rim 0 or rim 1 or both)
       REAL(wp) ::   zmask_x, zmask_y1, zmask_y2
       REAL(wp) ::   zex1, zex2, zey, zey1, zey2
@@ -301,7 +314,7 @@
       REAL(wp) ::   zdy_1, zdy_2,  zsign_ups
       REAL(wp), PARAMETER :: zepsilon = 1.e-30                 ! local small value
-      REAL(wp), POINTER, DIMENSION(:,:,:)        :: pmask      ! land/sea mask for field
-      REAL(wp), POINTER, DIMENSION(:,:,:)        :: pmask_xdif ! land/sea mask for x-derivatives
-      REAL(wp), POINTER, DIMENSION(:,:,:)        :: pmask_ydif ! land/sea mask for y-derivatives
+      REAL(wp), POINTER, DIMENSION(:,:,:)        :: zmask      ! land/sea mask for field
+      REAL(wp), POINTER, DIMENSION(:,:,:)        :: zmask_xdif ! land/sea mask for x-derivatives
+      REAL(wp), POINTER, DIMENSION(:,:,:)        :: zmask_ydif ! land/sea mask for y-derivatives
       REAL(wp), POINTER, DIMENSION(:,:)          :: pe_xdif    ! scale factors for x-derivatives
       REAL(wp), POINTER, DIMENSION(:,:)          :: pe_ydif    ! scale factors for y-derivatives
@@ -314,7 +327,7 @@
       SELECT CASE(igrd)
          CASE(1)
-            pmask      => tmask(:,:,:)
-            pmask_xdif => umask(:,:,:)
-            pmask_ydif => vmask(:,:,:)
+            zmask      => tmask(:,:,:)
+            zmask_xdif => umask(:,:,:)
+            zmask_ydif => vmask(:,:,:)
             pe_xdif    => e1u(:,:)
             pe_ydif    => e2v(:,:)
@@ -322,7 +335,7 @@
             ij_offset = 0
          CASE(2)
-            pmask      => umask(:,:,:)
-            pmask_xdif => tmask(:,:,:)
-            pmask_ydif => fmask(:,:,:)
+            zmask      => umask(:,:,:)
+            zmask_xdif => tmask(:,:,:)
+            zmask_ydif => fmask(:,:,:)
             pe_xdif    => e1t(:,:)
             pe_ydif    => e2f(:,:)
@@ -330,7 +343,7 @@
             ij_offset = 0
          CASE(3)
-            pmask      => vmask(:,:,:)
-            pmask_xdif => fmask(:,:,:)
-            pmask_ydif => tmask(:,:,:)
+            zmask      => vmask(:,:,:)
+            zmask_xdif => fmask(:,:,:)
+            zmask_ydif => tmask(:,:,:)
             pe_xdif    => e1f(:,:)
             pe_ydif    => e2t(:,:)
@@ -339,10 +352,18 @@
          CASE DEFAULT ;   CALL ctl_stop( 'unrecognised value for igrd in bdy_orlanksi_2d' )
       END SELECT
-
+      !
+      IF( PRESENT(lrim0) ) THEN
+         IF( lrim0 ) THEN   ;   ibeg = 1                       ;   iend = idx%nblenrim0(igrd)   ! rim 0
+         ELSE               ;   ibeg = idx%nblenrim0(igrd)+1   ;   iend = idx%nblenrim(igrd)    ! rim 1
+         END IF
+      ELSE                  ;   ibeg = 1                       ;   iend = idx%nblenrim(igrd)    ! both
+      END IF
+      !
       DO jk = 1, jpk
          !            
-         DO jb = 1, idx%nblenrim(igrd)
+         DO jb = ibeg, iend
             ii  = idx%nbi(jb,igrd)
             ij  = idx%nbj(jb,igrd) 
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )   CYCLE
             flagu = int( idx%flagu(jb,igrd) )
             flagv = int( idx%flagv(jb,igrd) )
@@ -360,12 +381,12 @@
             !
             ! Calculate scale factors for calculation of spatial derivatives.        
-            zex1 = ( abs(iibm1-iibm2) * pe_xdif(iibm1+ii_offset,ijbm1          )         &
-           &       + abs(ijbm1-ijbm2) * pe_ydif(iibm1          ,ijbm1+ij_offset) ) 
-            zex2 = ( abs(iibm1-iibm2) * pe_xdif(iibm2+ii_offset,ijbm2          )         &
-           &       + abs(ijbm1-ijbm2) * pe_ydif(iibm2          ,ijbm2+ij_offset) ) 
-            zey1 = ( (iibm1-iibm1jm1) * pe_xdif(iibm1jm1+ii_offset,ijbm1jm1          )  & 
+            zex1 = ( abs(iibm1-iibm2) * pe_xdif(iibm1   +ii_offset,ijbm1             )   &
+           &       + abs(ijbm1-ijbm2) * pe_ydif(iibm1             ,ijbm1+ij_offset   ) ) 
+            zex2 = ( abs(iibm1-iibm2) * pe_xdif(iibm2   +ii_offset,ijbm2             )   &
+           &       + abs(ijbm1-ijbm2) * pe_ydif(iibm2             ,ijbm2+ij_offset   ) ) 
+            zey1 = ( (iibm1-iibm1jm1) * pe_xdif(iibm1jm1+ii_offset,ijbm1jm1          )   & 
            &      +  (ijbm1-ijbm1jm1) * pe_ydif(iibm1jm1          ,ijbm1jm1+ij_offset) ) 
-            zey2 = ( (iibm1jp1-iibm1) * pe_xdif(iibm1+ii_offset,ijbm1)                  &
-           &      +  (ijbm1jp1-ijbm1) * pe_ydif(iibm1          ,ijbm1+ij_offset) ) 
+            zey2 = ( (iibm1jp1-iibm1) * pe_xdif(iibm1   +ii_offset,ijbm1             )   &
+           &      +  (ijbm1jp1-ijbm1) * pe_ydif(iibm1             ,ijbm1+ij_offset   ) ) 
             ! make sure scale factors are nonzero
             if( zey1 .lt. rsmall ) zey1 = zey2
@@ -375,10 +396,10 @@
             !
             ! Calculate masks for calculation of spatial derivatives.        
-            zmask_x = ( abs(iibm1-iibm2) * pmask_xdif(iibm2+ii_offset,ijbm2          ,jk)          &
-           &          + abs(ijbm1-ijbm2) * pmask_ydif(iibm2          ,ijbm2+ij_offset,jk) ) 
-            zmask_y1 = ( (iibm1-iibm1jm1) * pmask_xdif(iibm1jm1+ii_offset,ijbm1jm1          ,jk)   & 
-           &          +  (ijbm1-ijbm1jm1) * pmask_ydif(iibm1jm1          ,ijbm1jm1+ij_offset,jk) ) 
-            zmask_y2 = ( (iibm1jp1-iibm1) * pmask_xdif(iibm1+ii_offset,ijbm1          ,jk)         &
-           &          +  (ijbm1jp1-ijbm1) * pmask_ydif(iibm1          ,ijbm1+ij_offset,jk) ) 
+            zmask_x  = ( abs(iibm1-iibm2) * zmask_xdif(iibm2   +ii_offset,ijbm2             ,jk)   &
+           &           + abs(ijbm1-ijbm2) * zmask_ydif(iibm2             ,ijbm2   +ij_offset,jk) ) 
+            zmask_y1 = ( (iibm1-iibm1jm1) * zmask_xdif(iibm1jm1+ii_offset,ijbm1jm1          ,jk)   & 
+           &          +  (ijbm1-ijbm1jm1) * zmask_ydif(iibm1jm1          ,ijbm1jm1+ij_offset,jk) ) 
+            zmask_y2 = ( (iibm1jp1-iibm1) * zmask_xdif(iibm1   +ii_offset,ijbm1             ,jk)   &
+           &          +  (ijbm1jp1-ijbm1) * zmask_ydif(iibm1             ,ijbm1   +ij_offset,jk) ) 
             !
             ! Calculate normal (zrx) and tangential (zry) components of radiation velocities.
@@ -386,6 +407,6 @@
             ! Centred derivative is calculated as average of "left" and "right" derivatives for 
             ! this reason. 
-            zdt = phia(iibm1,ijbm1,jk) - phib(iibm1,ijbm1,jk)
-            zdx = ( ( phia(iibm1,ijbm1,jk) - phia(iibm2,ijbm2,jk) ) / zex2 ) * zmask_x                  
+            zdt   =     phia(iibm1   ,ijbm1   ,jk) - phib(iibm1   ,ijbm1   ,jk)
+            zdx   = ( ( phia(iibm1   ,ijbm1   ,jk) - phia(iibm2   ,ijbm2   ,jk) ) / zex2 ) * zmask_x                  
             zdy_1 = ( ( phib(iibm1   ,ijbm1   ,jk) - phib(iibm1jm1,ijbm1jm1,jk) ) / zey1 ) * zmask_y1  
             zdy_2 = ( ( phib(iibm1jp1,ijbm1jp1,jk) - phib(iibm1   ,ijbm1   ,jk) ) / zey2 ) * zmask_y2      
@@ -421,5 +442,5 @@
               &                       + zwgt * ( phi_ext(jb,jk) - phib(ii,ij,jk) ) ) / ( 1. + zrx ) 
             end if
-            phia(ii,ij,jk) = phia(ii,ij,jk) * pmask(ii,ij,jk)
+            phia(ii,ij,jk) = phia(ii,ij,jk) * zmask(ii,ij,jk)
          END DO
          !
@@ -428,5 +449,5 @@
    END SUBROUTINE bdy_orlanski_3d
 
-   SUBROUTINE bdy_nmn( idx, igrd, phia )
+   SUBROUTINE bdy_nmn( idx, igrd, phia, lrim0 )
       !!----------------------------------------------------------------------
       !!                 ***  SUBROUTINE bdy_nmn  ***
@@ -434,63 +455,69 @@
       !! ** Purpose : Duplicate the value at open boundaries, zero gradient.
       !! 
-      !!----------------------------------------------------------------------
-      INTEGER,                    INTENT(in)     ::   igrd     ! grid index
-      REAL(wp), DIMENSION(:,:,:), INTENT(inout)  ::   phia     ! model after 3D field (to be updated)
-      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      !!
+      !! ** Method  : - take the average of free ocean neighbours
+      !!
+      !!      ___   !   |_____|   !   ___|    !   __|x o   !   |_   _|     ! |      
+      !!   __|x     !      x      !     x o   !      o     !     |_|       ! |x o   
+      !!      o     !      o      !     o     !            !    o x o      ! |x_x_ 
+      !!                                                   !      o      
+      !!----------------------------------------------------------------------
+      INTEGER,                    INTENT(in   )  ::   igrd     ! grid index
+      REAL(wp), DIMENSION(:,:,:), INTENT(inout)  ::   phia     ! model after 3D field (to be updated), must be masked
+      TYPE(OBC_INDEX),            INTENT(in   )  ::   idx      ! OBC indices
+      LOGICAL, OPTIONAL,          INTENT(in   )  ::   lrim0    ! indicate if rim 0 is treated
       !! 
-      REAL(wp) ::   zcoef, zcoef1, zcoef2
-      REAL(wp), POINTER, DIMENSION(:,:,:)        :: pmask      ! land/sea mask for field
-      REAL(wp), POINTER, DIMENSION(:,:)        :: bdypmask      ! land/sea mask for field
+      REAL(wp) ::   zweight
+      REAL(wp), POINTER, DIMENSION(:,:,:)      :: zmask         ! land/sea mask for field
       INTEGER  ::   ib, ik   ! dummy loop indices
-      INTEGER  ::   ii, ij, ip, jp   ! 2D addresses
-      !!----------------------------------------------------------------------
+      INTEGER  ::   ii, ij   ! 2D addresses
+      INTEGER  ::   ipkm1    ! size of phia third dimension minus 1
+      INTEGER  ::   ibeg, iend                          ! length of rim to be treated (rim 0 or rim 1 or both)
+      INTEGER  ::   ii1, ii2, ii3, ij1, ij2, ij3, itreat
+      !!----------------------------------------------------------------------
+      !
+      ipkm1 = MAX( SIZE(phia,3) - 1, 1 ) 
       !
       SELECT CASE(igrd)
-         CASE(1)
-            pmask => tmask(:,:,:)
-            bdypmask => bdytmask(:,:)
-         CASE(2)
-            pmask => umask(:,:,:)
-            bdypmask => bdyumask(:,:)
-         CASE(3)
-            pmask => vmask(:,:,:)
-            bdypmask => bdyvmask(:,:)
+         CASE(1)   ;   zmask => tmask(:,:,:)
+         CASE(2)   ;   zmask => umask(:,:,:)
+         CASE(3)   ;   zmask => vmask(:,:,:)
          CASE DEFAULT ;   CALL ctl_stop( 'unrecognised value for igrd in bdy_nmn' )
       END SELECT
-      DO ib = 1, idx%nblenrim(igrd)
+      !
+      IF( PRESENT(lrim0) ) THEN
+         IF( lrim0 ) THEN   ;   ibeg = 1                       ;   iend = idx%nblenrim0(igrd)   ! rim 0
+         ELSE               ;   ibeg = idx%nblenrim0(igrd)+1   ;   iend = idx%nblenrim(igrd)    ! rim 1
+         END IF
+      ELSE                  ;   ibeg = 1                       ;   iend = idx%nblenrim(igrd)    ! both
+      END IF
+      !
+      DO ib = ibeg, iend
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
-         DO ik = 1, jpkm1
-            ! search the sense of the gradient
-            zcoef1 = bdypmask(ii-1,ij  )*pmask(ii-1,ij,ik) +  bdypmask(ii+1,ij  )*pmask(ii+1,ij,ik)
-            zcoef2 = bdypmask(ii  ,ij-1)*pmask(ii,ij-1,ik) +  bdypmask(ii  ,ij+1)*pmask(ii,ij+1,ik)
-            IF ( nint(zcoef1+zcoef2) == 0) THEN
-               ! corner **** we probably only want to set the tangentail component for the dynamics here
-               zcoef = pmask(ii-1,ij,ik) + pmask(ii+1,ij,ik) +  pmask(ii,ij-1,ik) +  pmask(ii,ij+1,ik)
-               IF (zcoef > .5_wp) THEN ! Only set none isolated points.
-                 phia(ii,ij,ik) = phia(ii-1,ij  ,ik) * pmask(ii-1,ij  ,ik) + &
-                   &              phia(ii+1,ij  ,ik) * pmask(ii+1,ij  ,ik) + &
-                   &              phia(ii  ,ij-1,ik) * pmask(ii  ,ij-1,ik) + &
-                   &              phia(ii  ,ij+1,ik) * pmask(ii  ,ij+1,ik)
-                 phia(ii,ij,ik) = ( phia(ii,ij,ik) / zcoef ) * pmask(ii,ij,ik)
-               ELSE
-                 phia(ii,ij,ik) = phia(ii,ij  ,ik) * pmask(ii,ij  ,ik)
-               ENDIF
-            ELSEIF ( nint(zcoef1+zcoef2) == 2) THEN
-               ! oblique corner **** we probably only want to set the normal component for the dynamics here
-               zcoef = pmask(ii-1,ij,ik)*bdypmask(ii-1,ij  ) + pmask(ii+1,ij,ik)*bdypmask(ii+1,ij  ) + &
-                   &   pmask(ii,ij-1,ik)*bdypmask(ii,ij -1 ) +  pmask(ii,ij+1,ik)*bdypmask(ii,ij+1  )
-               phia(ii,ij,ik) = phia(ii-1,ij  ,ik) * pmask(ii-1,ij  ,ik)*bdypmask(ii-1,ij  ) + &
-                   &            phia(ii+1,ij  ,ik) * pmask(ii+1,ij  ,ik)*bdypmask(ii+1,ij  )  + &
-                   &            phia(ii  ,ij-1,ik) * pmask(ii  ,ij-1,ik)*bdypmask(ii,ij -1 ) + &
-                   &            phia(ii  ,ij+1,ik) * pmask(ii  ,ij+1,ik)*bdypmask(ii,ij+1  )
- 
-               phia(ii,ij,ik) = ( phia(ii,ij,ik) / MAX(1._wp, zcoef) ) * pmask(ii,ij,ik)
-            ELSE
-               ip = nint(bdypmask(ii+1,ij  )*pmask(ii+1,ij,ik) - bdypmask(ii-1,ij  )*pmask(ii-1,ij,ik))
-               jp = nint(bdypmask(ii  ,ij+1)*pmask(ii,ij+1,ik) - bdypmask(ii  ,ij-1)*pmask(ii,ij-1,ik))
-               phia(ii,ij,ik) = phia(ii+ip,ij+jp,ik) * pmask(ii+ip,ij+jp,ik)
-            ENDIF
-         END DO
+         itreat = idx%ntreat(ib,igrd)
+         CALL find_neib( ii, ij, itreat, ii1, ij1, ii2, ij2, ii3, ij3 )   ! find free ocean neighbours
+         SELECT CASE( itreat )
+         CASE( 1:8 )
+            IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
+            DO ik = 1, ipkm1
+               IF( zmask(ii1,ij1,ik) /= 0. )   phia(ii,ij,ik) = phia(ii1,ij1,ik)  
+            END DO
+         CASE( 9:12 )
+            IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
+            IF( ii2 < 1 .OR. ii2 > jpi .OR. ij2 < 1 .OR. ij2 > jpj )   CYCLE
+            DO ik = 1, ipkm1
+               zweight = zmask(ii1,ij1,ik) + zmask(ii2,ij2,ik)
+               IF( zweight /= 0. )   phia(ii,ij,ik) = ( phia(ii1,ij1,ik) + phia(ii2,ij2,ik) ) / zweight
+            END DO
+         CASE( 13:16 )
+            IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
+            IF( ii2 < 1 .OR. ii2 > jpi .OR. ij2 < 1 .OR. ij2 > jpj )   CYCLE
+            IF( ii3 < 1 .OR. ii3 > jpi .OR. ij3 < 1 .OR. ij3 > jpj )   CYCLE
+            DO ik = 1, ipkm1
+               zweight = zmask(ii1,ij1,ik) + zmask(ii2,ij2,ik) + zmask(ii3,ij3,ik)
+               IF( zweight /= 0. )   phia(ii,ij,ik) = ( phia(ii1,ij1,ik) + phia(ii2,ij2,ik) + phia(ii3,ij3,ik) ) / zweight
+            END DO
+         END SELECT
       END DO
       !
