Index: trunk/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90
===================================================================
--- trunk/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90	(revision 3918)
+++ trunk/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90	(revision 4009)
@@ -185,71 +185,121 @@
       INTEGER  :: ji,jj,jk
       INTEGER  :: ispongearea, ilci, ilcj
-      REAL(wp) :: z1spongearea
-      REAL(wp), POINTER, DIMENSION(:,:) :: zlocalviscsponge
+      LOGICAL  :: ll_spdone
+      REAL(wp) :: z1spongearea, zramp
+      REAL(wp), POINTER, DIMENSION(:,:) :: ztabramp
 
 #if defined SPONGE || defined SPONGE_TOP
-
-      CALL wrk_alloc( jpi, jpj, zlocalviscsponge )
-
-      ispongearea  = 2 + 2 * Agrif_irhox()
-      ilci = nlci - ispongearea
-      ilcj = nlcj - ispongearea 
-      z1spongearea = 1._wp / REAL( ispongearea - 2 )
-      spbtr2(:,:) = 1. / ( e1t(:,:) * e2t(:,:) )
+      ll_spdone=.TRUE.
+      IF (( .NOT. spongedoneT ).OR.( .NOT. spongedoneU )) THEN
+         ! Define ramp from boundaries towards domain interior
+         ! at T-points
+         ! Store it in ztabramp
+         ll_spdone=.FALSE.
+
+         CALL wrk_alloc( jpi, jpj, ztabramp )
+
+         ispongearea  = 2 + 2 * Agrif_irhox()
+         ilci = nlci - ispongearea
+         ilcj = nlcj - ispongearea 
+         z1spongearea = 1._wp / REAL( ispongearea - 2 )
+         spbtr2(:,:) = 1. / ( e1t(:,:) * e2t(:,:) )
+
+         ztabramp(:,:) = 0.
+
+         IF( (nbondi == -1) .OR. (nbondi == 2) ) THEN
+            DO jj = 1, jpj
+               IF ( umask(2,jj,1) == 1._wp ) THEN
+                 DO ji = 2, ispongearea                  
+                    ztabramp(ji,jj) = ( ispongearea-ji ) * z1spongearea
+                 END DO
+               ENDIF
+            ENDDO
+         ENDIF
+
+         IF( (nbondi == 1) .OR. (nbondi == 2) ) THEN
+            DO jj = 1, jpj
+               IF ( umask(nlci-2,jj,1) == 1._wp ) THEN
+                  DO ji = ilci+1,nlci-1
+                     zramp = (ji - (ilci+1) ) * z1spongearea
+                     ztabramp(ji,jj) = MAX( ztabramp(ji,jj), zramp )
+                  ENDDO
+               ENDIF
+            ENDDO
+         ENDIF
+
+         IF( (nbondj == -1) .OR. (nbondj == 2) ) THEN
+            DO ji = 1, jpi
+               IF ( vmask(ji,2,1) == 1._wp ) THEN
+                  DO jj = 2, ispongearea
+                     zramp = ( ispongearea-jj ) * z1spongearea
+                     ztabramp(ji,jj) = MAX( ztabramp(ji,jj), zramp )
+                  END DO
+               ENDIF
+            ENDDO
+         ENDIF
+
+         IF( (nbondj == 1) .OR. (nbondj == 2) ) THEN
+            DO ji = 1, jpi
+               IF ( vmask(ji,nlcj-2,1) == 1._wp ) THEN
+                  DO jj = ilcj+1,nlcj-1
+                     zramp = (jj - (ilcj+1) ) * z1spongearea
+                     ztabramp(ji,jj) = MAX( ztabramp(ji,jj), zramp )
+                  END DO
+               ENDIF
+            ENDDO
+         ENDIF
+
+      ENDIF
 
       ! Tracers
       IF( .NOT. spongedoneT ) THEN
-         zlocalviscsponge(:,:) = 0.
          spe1ur(:,:) = 0.
          spe2vr(:,:) = 0.
 
          IF( (nbondi == -1) .OR. (nbondi == 2) ) THEN
-            DO ji = 2, ispongearea
-               zlocalviscsponge(ji,:) = visc_tra * ( ispongearea-ji ) * z1spongearea
-            ENDDO
-            spe1ur(2:ispongearea-1,:      ) = 0.5 * ( zlocalviscsponge(2:ispongearea-1,:      )   &
-               &                         +            zlocalviscsponge(3:ispongearea  ,:      ) ) &
-               &                         * e2u(2:ispongearea-1,:      ) / e1u(2:ispongearea-1,:      )
-            spe2vr(2:ispongearea  ,1:jpjm1) = 0.5 * ( zlocalviscsponge(2:ispongearea  ,1:jpjm1)   &
-               &                         +            zlocalviscsponge(2:ispongearea,2  :jpj  ) ) &
-               &                         * e1v(2:ispongearea  ,1:jpjm1) / e2v(2:ispongearea  ,1:jpjm1)
+            spe1ur(2:ispongearea-1,:       ) = visc_tra                                        &
+               &                             *    0.5 * (  ztabramp(2:ispongearea-1,:      )   &
+               &                                         + ztabramp(3:ispongearea  ,:      ) ) &
+               &                             * e2u(2:ispongearea-1,:) / e1u(2:ispongearea-1,:)
+
+            spe2vr(2:ispongearea  ,1:jpjm1 ) = visc_tra                                        &
+               &                             *    0.5 * (  ztabramp(2:ispongearea  ,1:jpjm1)   &
+               &                                         + ztabramp(2:ispongearea,2  :jpj  ) ) &
+               &                             * e1v(2:ispongearea,1:jpjm1) / e2v(2:ispongearea,1:jpjm1)
          ENDIF
 
          IF( (nbondi == 1) .OR. (nbondi == 2) ) THEN
-            DO ji = ilci+1,nlci-1
-               zlocalviscsponge(ji,:) = visc_tra * (ji - (ilci+1) ) * z1spongearea
-            ENDDO
-  
-            spe1ur(ilci+1:nlci-2,:      ) = 0.5 * (  zlocalviscsponge(ilci+1:nlci-2,:)    & 
-               &                          +          zlocalviscsponge(ilci+2:nlci-1,:) )  &
-               &                          * e2u(ilci+1:nlci-2,:) / e1u(ilci+1:nlci-2,:)
-
-            spe2vr(ilci+1:nlci-1,1:jpjm1) = 0.5 * (  zlocalviscsponge(ilci+1:nlci-1,1:jpjm1)    & 
-               &                            +        zlocalviscsponge(ilci+1:nlci-1,2:jpj  )  ) & 
-               &                                   * e1v(ilci+1:nlci-1,1:jpjm1) / e2v(ilci+1:nlci-1,1:jpjm1)
+            spe1ur(ilci+1:nlci-2,:        ) = visc_tra                                   &
+               &                            * 0.5 * (  ztabramp(ilci+1:nlci-2,:      )   & 
+               &                                     + ztabramp(ilci+2:nlci-1,:      ) ) &
+               &                            * e2u(ilci+1:nlci-2,:) / e1u(ilci+1:nlci-2,:)
+
+            spe2vr(ilci+1:nlci-1,1:jpjm1  )  = visc_tra                                  &
+               &                            * 0.5 * (  ztabramp(ilci+1:nlci-1,1:jpjm1)   & 
+               &                                     + ztabramp(ilci+1:nlci-1,2:jpj  ) ) & 
+               &                            * e1v(ilci+1:nlci-1,1:jpjm1) / e2v(ilci+1:nlci-1,1:jpjm1)
          ENDIF
 
          IF( (nbondj == -1) .OR. (nbondj == 2) ) THEN
-            DO jj = 2, ispongearea
-               zlocalviscsponge(:,jj) = visc_tra * ( ispongearea-jj ) * z1spongearea
-            ENDDO
-            spe1ur(1:jpim1,2:ispongearea  ) = 0.5 * ( zlocalviscsponge(1:jpim1,2:ispongearea  ) & 
-               &                            +         zlocalviscsponge(2:jpi  ,2:ispongearea) ) &
+            spe1ur(1:jpim1,2:ispongearea  ) = visc_tra                                     &
+               &                            * 0.5 * (  ztabramp(1:jpim1,2:ispongearea  )   & 
+               &                                     + ztabramp(2:jpi  ,2:ispongearea  ) ) &
                &                            * e2u(1:jpim1,2:ispongearea) / e1u(1:jpim1,2:ispongearea)
    
-            spe2vr(:      ,2:ispongearea-1) = 0.5 * ( zlocalviscsponge(:,2:ispongearea-1)       &
-               &                            +         zlocalviscsponge(:,3:ispongearea  )     ) &
+            spe2vr(:      ,2:ispongearea-1) = visc_tra                                     &
+               &                            * 0.5 * (  ztabramp(:      ,2:ispongearea-1)   &
+               &                                     + ztabramp(:      ,3:ispongearea  ) ) &
                &                            * e1v(:,2:ispongearea-1) / e2v(:,2:ispongearea-1)
          ENDIF
 
          IF( (nbondj == 1) .OR. (nbondj == 2) ) THEN
-            DO jj = ilcj+1,nlcj-1
-               zlocalviscsponge(:,jj) = visc_tra * (jj - (ilcj+1) ) * z1spongearea
-            ENDDO
-            spe1ur(1:jpim1,ilcj+1:nlcj-1) = 0.5 * ( zlocalviscsponge(1:jpim1,ilcj+1:nlcj-1)   &
-               &                          +         zlocalviscsponge(2:jpi  ,ilcj+1:nlcj-1) ) &
+            spe1ur(1:jpim1,ilcj+1:nlcj-1) = visc_tra                                   &
+               &                          * 0.5 * (  ztabramp(1:jpim1,ilcj+1:nlcj-1)   &
+               &                                   + ztabramp(2:jpi  ,ilcj+1:nlcj-1) ) &
                &                                * e2u(1:jpim1,ilcj+1:nlcj-1) / e1u(1:jpim1,ilcj+1:nlcj-1)
-            spe2vr(:      ,ilcj+1:nlcj-2) = 0.5 * ( zlocalviscsponge(:,ilcj+1:nlcj-2      )   &
-               &                          +         zlocalviscsponge(:,ilcj+2:nlcj-1)     )   &
+
+            spe2vr(:      ,ilcj+1:nlcj-2) = visc_tra                                   &
+               &                          * 0.5 * (  ztabramp(:      ,ilcj+1:nlcj-2)   &
+               &                                   + ztabramp(:      ,ilcj+2:nlcj-1) ) &
                &                                * e1v(:,ilcj+1:nlcj-2) / e2v(:,ilcj+1:nlcj-2)
          ENDIF
@@ -259,46 +309,41 @@
       ! Dynamics
       IF( .NOT. spongedoneU ) THEN
-         zlocalviscsponge(:,:) = 0.
          spe1ur2(:,:) = 0.
          spe2vr2(:,:) = 0.
 
          IF( (nbondi == -1) .OR. (nbondi == 2) ) THEN
-            DO ji = 2, ispongearea
-               zlocalviscsponge(ji,:) = visc_dyn * ( ispongearea-ji ) * z1spongearea
-            ENDDO
-            spe1ur2(2:ispongearea-1,:      ) = 0.5 * ( zlocalviscsponge(2:ispongearea-1,:      ) &
-                                             &     +   zlocalviscsponge(3:ispongearea,:    ) )
-            spe2vr2(2:ispongearea  ,1:jpjm1) = 0.5 * ( zlocalviscsponge(2:ispongearea  ,1:jpjm1) &
-                                             &     +   zlocalviscsponge(2:ispongearea,2:jpj) ) 
+            spe1ur2(2:ispongearea-1,:      ) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(2:ispongearea-1,:      ) &
+               &                                      + ztabramp(3:ispongearea  ,:      ) )
+            spe2vr2(2:ispongearea  ,1:jpjm1) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(2:ispongearea  ,1:jpjm1) &
+               &                                      + ztabramp(2:ispongearea  ,2:jpj  ) ) 
          ENDIF
 
          IF( (nbondi == 1) .OR. (nbondi == 2) ) THEN
-            DO ji = ilci+1,nlci-1
-               zlocalviscsponge(ji,:) = visc_dyn * (ji - (ilci+1) ) * z1spongearea
-            ENDDO
-            spe1ur2(ilci+1:nlci-2,:      ) = 0.5 * (  zlocalviscsponge(ilci+1:nlci-2,:) &
-                                           &        + zlocalviscsponge(ilci+2:nlci-1,:) )  
-            spe2vr2(ilci+1:nlci-1,1:jpjm1) = 0.5 * (  zlocalviscsponge(ilci+1:nlci-1,1:jpjm1) &
-                                           &        + zlocalviscsponge(ilci+1:nlci-1,2:jpj  )  ) 
+            spe1ur2(ilci+1:nlci-2  ,:      ) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(ilci+1:nlci-2, :       ) &
+               &                                      + ztabramp(ilci+2:nlci-1, :       ) )                      
+            spe2vr2(ilci+1:nlci-1  ,1:jpjm1) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(ilci+1:nlci-1,1:jpjm1  ) &
+               &                                      + ztabramp(ilci+1:nlci-1,2:jpj    ) ) 
          ENDIF
 
          IF( (nbondj == -1) .OR. (nbondj == 2) ) THEN
-            DO jj = 2, ispongearea
-               zlocalviscsponge(:,jj) = visc_dyn * ( ispongearea-jj ) * z1spongearea
-            ENDDO
-            spe1ur2(1:jpim1,2:ispongearea  ) = 0.5 * ( zlocalviscsponge(1:jpim1,2:ispongearea) &
-                                             &      + zlocalviscsponge(2:jpi,2:ispongearea) ) 
-            spe2vr2(:      ,2:ispongearea-1) = 0.5 * ( zlocalviscsponge(:,2:ispongearea-1)     &
-                                             &      + zlocalviscsponge(:,3:ispongearea)     )
+            spe1ur2(1:jpim1,2:ispongearea  ) = visc_dyn                                   &  
+               &                             * 0.5 * (  ztabramp(1:jpim1,2:ispongearea  ) &
+               &                                      + ztabramp(2:jpi  ,2:ispongearea  ) ) 
+            spe2vr2(:      ,2:ispongearea-1) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(:      ,2:ispongearea-1) &
+               &                                      + ztabramp(:      ,3:ispongearea  ) )
          ENDIF
 
          IF( (nbondj == 1) .OR. (nbondj == 2) ) THEN
-            DO jj = ilcj+1,nlcj-1
-               zlocalviscsponge(:,jj) = visc_dyn * (jj - (ilcj+1) ) * z1spongearea
-            ENDDO
-            spe1ur2(1:jpim1,ilcj+1:nlcj-1) = 0.5 * ( zlocalviscsponge(1:jpim1,ilcj+1:nlcj-1) &
-                                           &         + zlocalviscsponge(2:jpi,ilcj+1:nlcj-1) ) 
-            spe2vr2(:      ,ilcj+1:nlcj-2) = 0.5 * ( zlocalviscsponge(:,ilcj+1:nlcj-2      ) &
-                                           &         + zlocalviscsponge(:,ilcj+2:nlcj-1)     )
+            spe1ur2(1:jpim1,ilcj+1:nlcj-1  ) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(1:jpim1,ilcj+1:nlcj-1  ) &
+               &                                      + ztabramp(2:jpi  ,ilcj+1:nlcj-1  ) ) 
+            spe2vr2(:      ,ilcj+1:nlcj-2  ) = visc_dyn                                   &
+               &                             * 0.5 * (  ztabramp(:      ,ilcj+1:nlcj-2  ) &
+               &                                      + ztabramp(:      ,ilcj+2:nlcj-1  ) )
          ENDIF
          spongedoneU = .TRUE.
@@ -306,5 +351,5 @@
       ENDIF
       !
-      CALL wrk_dealloc( jpi, jpj, zlocalviscsponge )
+      IF (.NOT.ll_spdone) CALL wrk_dealloc( jpi, jpj, ztabramp )
       !
 #endif
