Index: branches/2016/dev_NOC_CMCC_merge_2016/NEMOGCM/NEMO/OPA_SRC/DYN/wet_dry.F90
===================================================================
--- branches/2016/dev_NOC_CMCC_merge_2016/NEMOGCM/NEMO/OPA_SRC/DYN/wet_dry.F90	(revision 7385)
+++ branches/2016/dev_NOC_CMCC_merge_2016/NEMOGCM/NEMO/OPA_SRC/DYN/wet_dry.F90	(revision 7397)
@@ -33,5 +33,4 @@
    !! ---------------------------------------------------------------------
 
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:) ::   wduflt, wdvflt !: u- and v- filter
    REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:) ::   wdmask         !: u- and v- limiter 
 
@@ -46,4 +45,5 @@
    PUBLIC   wad_lmt                   ! routine called by sshwzv.F90
    PUBLIC   wad_lmt_bt                ! routine called by dynspg_ts.F90
+   PUBLIC   wad_istate                ! routine called by istate.F90 and domvvl.F90
 
    !! * Substitutions
@@ -87,5 +87,5 @@
 
       IF(ln_wd) THEN
-         ALLOCATE( wduflt(jpi,jpj), wdvflt(jpi,jpj), wdmask(jpi,jpj), STAT=ierr )
+         ALLOCATE( wdmask(jpi,jpj), STAT=ierr )
          IF( ierr /= 0 ) CALL ctl_stop('STOP', 'wad_init : Array allocation error')
       ENDIF
@@ -145,6 +145,6 @@
         ! Horizontal Flux in u and v direction
         DO jk = 1, jpkm1  
-           DO jj = 1, jpjm1
-              DO ji = 1, jpim1
+           DO jj = 1, jpj
+              DO ji = 1, jpi
                  zflxu(ji,jj) = zflxu(ji,jj) + e3u_n(ji,jj,jk) * un(ji,jj,jk) * umask(ji,jj,jk)
                  zflxv(ji,jj) = zflxv(ji,jj) + e3v_n(ji,jj,jk) * vn(ji,jj,jk) * vmask(ji,jj,jk)
@@ -156,6 +156,7 @@
         zflxv(:,:) = zflxv(:,:) * e1v(:,:)
        
-        DO jj = 2, jpjm1
-           DO ji = 2, jpim1 
+        wdmask(:,:) = 1
+        DO jj = 2, jpj
+           DO ji = 2, jpi 
 
              IF(tmask(ji, jj, 1) < 0.5_wp) CYCLE   ! we don't care about land cells
@@ -168,7 +169,8 @@
 
               zdep2 = bathy(ji,jj) + sshb1(ji,jj) - rn_wdmin1
-              IF(zdep2 < 0._wp) THEN  !add more safty, but not necessary
+              IF(zdep2 .le. 0._wp) THEN  !add more safty, but not necessary
                 !zdep2 = 0._wp
                 sshb1(ji,jj) = rn_wdmin1 - bathy(ji,jj)
+                wdmask(ji,jj) = 0._wp
               END IF
            ENDDO
@@ -183,8 +185,7 @@
            zflxv1(:,:) = zflxv(:,:) * zwdlmtv(:,:)
           
-           DO jj = 2, jpjm1
-              DO ji = 2, jpim1 
+           DO jj = 2, jpj
+              DO ji = 2, jpi 
         
-                 wdmask(ji,jj) = 0
                  IF(tmask(ji, jj, 1) < 0.5_wp) CYCLE 
                  IF(bathy(ji,jj) > zdepwd) CYCLE
@@ -202,5 +203,5 @@
                  IF(zdep1 > zdep2) THEN
                    zflag = 1
-                   wdmask(ji, jj) = 1
+                   wdmask(ji, jj) = 0
                    zcoef = ( ( zdep2 - rn_wdmin2 ) * ztmp - zzflxn * z2dt ) / ( zflxp(ji,jj) * z2dt )
                    zcoef = max(zcoef, 0._wp)
@@ -209,5 +210,5 @@
                    IF(zflxu1(ji-1,jj) < 0._wp) zwdlmtu(ji-1,jj) = zcoef
                    IF(zflxv1(ji,  jj) > 0._wp) zwdlmtv(ji  ,jj) = zcoef
-                   IF(zflxv1(ji,jj-1) < 0._wp) zwdlmtv(ji-1,jj) = zcoef
+                   IF(zflxv1(ji,jj-1) < 0._wp) zwdlmtv(ji,jj-1) = zcoef
                  END IF
               END DO ! ji loop
@@ -231,4 +232,9 @@
         CALL lbc_lnk( un, 'U', -1. )
         CALL lbc_lnk( vn, 'V', -1. )
+      !
+        un_b(:,:) = un_b(:,:) * zwdlmtu(:, :)
+        vn_b(:,:) = vn_b(:,:) * zwdlmtv(:, :)
+        CALL lbc_lnk( un_b, 'U', -1. )
+        CALL lbc_lnk( vn_b, 'V', -1. )
        
         IF(zflag == 1 .AND. lwp) WRITE(numout,*) 'Need more iterations in wad_lmt!!!'
@@ -291,6 +297,4 @@
         zflxp(:,:)   = 0._wp
         zflxn(:,:)   = 0._wp
-        !zflxu(:,:)   = 0._wp
-        !zflxv(:,:)   = 0._wp
 
         zwdlmtu(:,:)  = 1._wp
@@ -299,9 +303,6 @@
         ! Horizontal Flux in u and v direction
        
-        !zflxu(:,:) = zflxu(:,:) * e2u(:,:)
-        !zflxv(:,:) = zflxv(:,:) * e1v(:,:)
-       
-        DO jj = 2, jpjm1
-           DO ji = 2, jpim1 
+        DO jj = 2, jpj
+           DO ji = 2, jpi 
 
              IF(tmask(ji, jj, 1) < 0.5_wp) CYCLE   ! we don't care about land cells
@@ -314,8 +315,4 @@
 
               zdep2 = bathy(ji,jj) + sshn_e(ji,jj) - rn_wdmin1
-              IF(zdep2 < 0._wp) THEN  !add more safty, but not necessary
-                !zdep2 = 0._wp
-               sshn_e(ji,jj) = rn_wdmin1 - bathy(ji,jj)
-              END IF
            ENDDO
         END DO
@@ -329,8 +326,7 @@
            zflxv1(:,:) = zflxv(:,:) * zwdlmtv(:,:)
           
-           DO jj = 2, jpjm1
-              DO ji = 2, jpim1 
+           DO jj = 2, jpj
+              DO ji = 2, jpi 
         
-                 wdmask(ji,jj) = 0
                  IF(tmask(ji, jj, 1) < 0.5_wp) CYCLE 
                  IF(bathy(ji,jj) > zdepwd) CYCLE
@@ -349,5 +345,4 @@
                  IF(zdep1 > zdep2) THEN
                    zflag = 1
-                   !wdmask(ji, jj) = 1
                    zcoef = ( ( zdep2 - rn_wdmin2 ) * ztmp - zzflxn * z2dt ) / ( zflxp(ji,jj) * z2dt )
                    zcoef = max(zcoef, 0._wp)
@@ -356,5 +351,5 @@
                    IF(zflxu1(ji-1,jj) < 0._wp) zwdlmtu(ji-1,jj) = zcoef
                    IF(zflxv1(ji,  jj) > 0._wp) zwdlmtv(ji  ,jj) = zcoef
-                   IF(zflxv1(ji,jj-1) < 0._wp) zwdlmtv(ji-1,jj) = zcoef
+                   IF(zflxv1(ji,jj-1) < 0._wp) zwdlmtv(ji,jj-1) = zcoef
                  END IF
               END DO ! ji loop
@@ -379,6 +374,4 @@
         IF(zflag == 1 .AND. lwp) WRITE(numout,*) 'Need more iterations in wad_lmt_bt!!!'
        
-        !IF( ln_rnf      )   CALL sbc_rnf_div( hdivn )          ! runoffs (update hdivn field)
-        !IF( nn_cla == 1 )   CALL cla_div    ( kt )             ! Cross Land Advection (update hdivn field)
         !
         !
@@ -390,3 +383,138 @@
       IF( nn_timing == 1 )  CALL timing_stop('wad_lmt')
    END SUBROUTINE wad_lmt_bt
+
+   SUBROUTINE wad_istate
+      !!----------------------------------------------------------------------
+      !!                   ***  ROUTINE wad_istate  ***
+      !! 
+      !! ** Purpose :   Initialization of the dynamics and tracers for WAD test
+      !!      configurations (channels or bowls with initial ssh gradients)
+      !!
+      !! ** Method  : - set temperature field
+      !!              - set salinity field
+      !!              - set ssh slope (needs to be repeated in domvvl_rst_init to
+      !!                               set vertical metrics )
+      !!----------------------------------------------------------------------
+      !
+      INTEGER  ::   ji, jj            ! dummy loop indices
+      REAL(wp) ::   zi, zj
+      !!----------------------------------------------------------------------
+      !
+      ! Uniform T & S in all test cases
+      tsn(:,:,:,jp_tem) = 10._wp
+      tsb(:,:,:,jp_tem) = 10._wp
+      tsn(:,:,:,jp_sal) = 35._wp
+      tsb(:,:,:,jp_sal) = 35._wp
+      SELECT CASE ( jp_cfg ) 
+         !                                        ! ====================
+         CASE ( 1 )                               ! WAD 1 configuration
+            !                                     ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Closed box with EW linear bottom slope'
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            do ji = 1,jpi
+             sshn(ji,:) = ( -5.5_wp + 5.5_wp*FLOAT(mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+            end do
+            !                                     ! ====================
+         CASE ( 2 )                               ! WAD 2 configuration
+            !                                     ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Parobolic EW channel, mid-range initial ssh slope'
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            do ji = 1,jpi
+             sshn(ji,:) = ( -5.5_wp + 3.9_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+            end do
+            !                                     ! ====================
+         CASE ( 3 )                               ! WAD 3 configuration
+            !                                     ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Parobolic EW channel, extreme initial ssh slope' 
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            do ji = 1,jpi
+             sshn(ji,:) = ( -7.5_wp + 6.9_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+            end do
+
+            !
+            !                                     ! ====================
+         CASE ( 4 )                               ! WAD 4 configuration
+            !                                     ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Parobolic bowl, mid-range initial ssh slope' 
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            DO ji = 1, jpi
+               zi = MAX(1.0-FLOAT((mig(ji)-25)**2)/400.0, 0.0 )
+               DO jj = 1, jpj
+                  zj = MAX(1.0-FLOAT((mjg(jj)-17)**2)/144.0, 0.0 )
+                  sshn(ji,jj) = -8.5_wp + 8.5_wp*zi*zj
+               END DO
+            END DO
+
+            !
+            !                                    ! ===========================
+         CASE ( 5 )                              ! WAD 5 configuration
+            !                                    ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Double slope with shelf'
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            ! Needed rn_wdmin2 increased to 0.01 for this case?
+            do ji = 1,jpi
+             sshn(ji,:) = ( -5.5_wp + 9.0_wp*FLOAT(mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+            end do
+
+            !
+            !                                     ! ===========================
+         CASE ( 6 )                               ! WAD 6 configuration
+            !                                     ! ====================
+            !
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'istate_wad : Parobolic EW channel with gaussian ridge' 
+            IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
+            !
+            do ji = 1,jpi
+             !6a
+             sshn(ji,:) = ( -5.5_wp + 9.0_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+             !Some variations in initial slope that have been tested
+             !6b
+             !sshn(ji,:) = ( -5.5_wp + 6.5_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+             !6c
+             !sshn(ji,:) = ( -5.5_wp + 7.5_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+             !6d
+             !sshn(ji,:) = ( -4.5_wp + 8.0_wp*FLOAT(jpidta - mig(ji))/FLOAT(jpidta-1))*tmask(ji,:,1)
+            end do
+
+            !
+            !                                    ! ===========================
+         CASE DEFAULT                            ! NONE existing configuration
+            !                                    ! ===========================
+            WRITE(ctmp1,*) 'WAD test with a ', jp_cfg,' option is not coded'
+            !
+            CALL ctl_stop( ctmp1 )
+            !
+      END SELECT
+      !
+      ! Apply minimum wetdepth criterion
+      !
+      do jj = 1,jpj
+         do ji = 1,jpi
+            IF( bathy(ji,jj) + sshn(ji,jj) < rn_wdmin1 ) THEN
+               sshn(ji,jj) = tmask(ji,jj,1)*( rn_wdmin1 - bathy(ji,jj) )
+            ENDIF
+         end do
+      end do
+      sshb = sshn
+      ssha = sshn
+      !
+   END SUBROUTINE wad_istate
+
+   !!=====================================================================
 END MODULE wet_dry
