  SUBROUTINE cas_wetdrying

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE cas_wetdrying  ***
!&E
!&E ** Purpose : definition of a test case for wetting and drying
!&E
!&E ** Description :
!&E
!&E ** Called by : cas_init
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays :
!&E
!&E ** Modified variables : hx, hy, h0, sal, temp, dy, dhx, dhy, dsigu, dsigw
!&E                         surf, fisac, gwac, dgac, dfiac, fu, fv, sig, sigw
!&E                         tfiusr, tfivsr, goulet, govlet, ig, id, jb, jh
!&E                         ssh,u,v,uz,vz
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2005-10-13  Original code
!&E       !  2008-12    (V. Garnier) shift of sigma values : -1 < sig < 0
!&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used
   USE parameters, ONLY : pi,valmanq,rsh,rlg,riolg,imax,jmin,jmax,kmax,ljmin,ljminm2,ljmaxp2, &
                          limin,liminm2,liminp2,limax,limaxp2
   USE comvars2d,  ONLY : fisac,gwac,fu,fv,dtmin,dtmax,dt,halfdt,suffix,h0fond,hx_g,hy_g,h0_g, &
                          dhx,dhx_g,dhy,dhy_g,hm,icon,l_modele2d,vish_phi,vish_xe,vis2d, &
                          qmax,h0,t,tdeb,kx,ky,frot_lin
   USE comvarp2d,  ONLY : ssh,u
   USE comvars3d,  ONLY : saliref_lin,kzinit,nzinit,turb_nbeq
   USE comvarp3d,  ONLY : uz,ect
   USE comvars3d,  ONLY : l_stateeq_lin,saliref,kzinit,nzinit,qmaxz,bgdiff
   USE obccombine, ONLY : l_obc_east,l_obc_mars,l_obc_tide
   USE output,     ONLY : pasor,l_out_temp,l_out_precdate,l_out_demer
   USE comfluxdo,  ONLY : phieau
   USE comturb,    ONLY : ectmin

   USE comdiag,    ONLY : l_diag
   USE toolmpi,ONLY : ex_i_rsh
   USE casinit,    ONLY : cas_init_param,cas_init_grid,cas_init_bathy, &
                          cas_init_io,cas_init_obc,cas_init_meteo,     &
                          cas_init_visc,cas_init_turb,cas_init_tspar,  &
                          cas_init_progvar,cas_init_misc,cas_init_river


   IMPLICIT NONE

!! * Arguments

!! * Local declarations
   CHARACTER(LEN=19) :: date_start,date_end,date_start3d,date_startagrif
   INTEGER           :: ni_wall_e,ni_wall_w,nj_wall_n,nj_wall_s
   INTEGER           :: i,j,k
   REAL(KIND=rsh)    :: L1,L2,dxval,dyval,ht
   REAL(KIND=rsh)    :: z0bottom,z0surface,depthcst

!!----------------------------------------------------------------------
!! * Executable part

! general parametrization (each parameter can be reassigned)
! -----------------------

! dynamics
   CALL cas_init_param

! over-assignement
   h0fond=0.01_rsh
   hm=0.3_rsh

! tracers : among others, saliref=saliref_lin=35.5_rsh, tetaref=tetaref_lin=10.0_rsh
   CALL cas_init_tspar

! over-assignement
   icon=0
   kx=1.0_rsh
   ky=1.0_rsh
   qmax=8.0_rsh
   qmaxz=10.0_rsh
   l_stateeq_lin=.FALSE.
   saliref=30.0_rsh ! pour la condition a la limite


! Bathymetry
! ----------

! assignement : input variables for cas_init_bathy routine
   depthcst = 0.0_rsh
   ni_wall_w = 2  ! land over ni_wall_w cells (from imin column) at the western part of the domain
   nj_wall_s = 2  ! land over nj_wall_s cells (from jmin row) at the southern part of the domain
   nj_wall_n = 1  ! land over nj_wall_n cells at the northern part of the domain



! As depthcst is null, specify your own bathymetry

   DO j=jmin+1,jmax
     IF(j>=60.AND.j<=90) ht=10.0_rsh
! bas
!--------
     IF(j>=50.AND.j<60 ) ht=10.0_rsh+REAL(j-60,rsh)/10.0_rsh*9.0_rsh
     IF(j<50 )           ht= 1.0_rsh+REAL(j-50,rsh)/50.0_rsh*3.0_rsh
! haut
!--------
     IF(j>90.AND.j<=100) ht=10.0_rsh-REAL(j- 90,rsh)/10.0_rsh*9.0_rsh
     IF(j>100)           ht= 1.0_rsh-REAL(j-100,rsh)/50.0_rsh*3.0_rsh

     hx_g(:,j)=ht
     hy_g(:,j)=ht
     h0_g(:,j)=ht
   END DO

   DO i=1,60
     IF(i>=50.AND.i<60 ) ht=10.0_rsh+REAL(i-60,rsh)/10.0_rsh*9.0_rsh
     IF(i<50 )           ht= 1.0_rsh+REAL(i-50,rsh)/50.0_rsh*3.0_rsh
     DO j=jmin,jmax
       hx_g(i,j)=MIN(ht,hx_g(i,j))
       hy_g(i,j)=MIN(ht,hy_g(i,j))
       h0_g(i,j)=MIN(ht,h0_g(i,j))
     END DO
   END DO

   hy_g(imax-10:imax,:)=-valmanq-h0fond
   hx_g(imax-11:imax,:)=-valmanq-h0fond
   h0_g(imax-10:imax,:)=-valmanq
   h0_g(imax-11:imax,61:89)=10.0_rsh
   hx_g(imax-11:imax,61:89)=10.0_rsh
   hy_g(imax-11:imax,61:88)=10.0_rsh

! walls
   CALL cas_init_bathy(depthcst,ni_wall_w=ni_wall_w,nj_wall_n=nj_wall_n, &
                                nj_wall_s=nj_wall_s)

! over-assignement
   dhx(:,:)=0.2_rsh
   dhy(:,:)=0.2_rsh
   dhx_g(:,:)=0.2_rsh
   dhy_g(:,:)=0.2_rsh


! grid parametrization
! --------------------

! assignement : input variables for cas_init_grid routine
   L1=10000.0_rsh
   L2=10000.0_rsh
   dxval = L1/(REAL(imax,rsh)-1.5_rsh)
   dyval = L2/(REAL(jmax,rsh)-1.5_rsh)
   fisac=0.0_riolg
   gwac= 0.0_riolg



   CALL cas_init_grid(dxval,dyval)

! over-assignement
   fu(:,:)=0.0_rsh
   fv(:,:)=0.0_rsh


! IO parametrization
! ------------------

! assignement : input variables for cas_init_io routine
   date_start=   '01/01/1900 00:00:00'
   date_end=   '02/01/1900 00:00:00'
   date_start3d=  '01/01/1900 00:00:00'
   date_startagrif='01/01/1900 00:00:00'
   dtmin=50.0_rlg
   dtmax=50.0_rlg
   dt=50.0_rlg

   halfdt=dt*0.5_rlg

   suffix='wetdry'

   CALL cas_init_io(date_start,date_end,date_start3d,date_startagrif,suffix)

! over-assignement
   l_modele2d=.TRUE.
   pasor(1)=600.0_rlg

   l_diag=.TRUE.
   l_out_precdate(1)=.TRUE.
   l_out_temp(1)=.FALSE.


! open boundaries
! ---------------

! no open boundary
   CALL cas_init_obc

! over-assignement
! geometry (where are the open boundaries ?)
   l_obc_east=.TRUE.
! used scheme to manage obc
   l_obc_mars = .TRUE.
! physics introduced into the simulated area (tide or ogcm solution)
   l_obc_tide=.TRUE.


! viscosity
!-----------

! none
   CALL cas_init_visc

! over-assignement
   vish_xe(:,:,:) = 5.0_rsh*0.01_rsh*dyval**1.15_rsh
   vish_phi(:,:,:)= 5.0_rsh*0.01_rsh*dyval**1.15_rsh
   vis2d(:,:)= 5.0_rsh*0.01_rsh*dyval**1.15_rsh


! turbulence
! ----------

   z0bottom   =0.005_rsh
   z0surface  =0.0_rsh
   nzinit=0.0028_rsh
   kzinit=0.0_rsh

! Gaspard parametrization
   CALL cas_init_turb(kzinit,nzinit,z0surface,z0bottom)

! over-assignement
   turb_nbeq=1
   frot_lin=0.0008_rsh
   ect(:,:,:)=0.000001_rsh
   ectmin=0.000001_rsh
   bgdiff=0.000001_rsh


! meteorology
! -----------

! no forcing
   CALL cas_init_meteo


! River and outflows
! -------------------

! no river
   CALL cas_init_river


! initialization of the pronostic variables
! -----------------------------------------

! set to 0,tetaref,saliref
   CALL cas_init_progvar

! over-assignement

! Sea surface height
!-------------------

   ssh(liminm2:limaxp2,ljminm2:ljmaxp2) = MAX(REAL(3.0_rlg*COS(2.0_rsh*pi*(t-tdeb)/44712.0_rlg),rsh), &
                                              -h0(liminm2:limaxp2,ljminm2:ljmaxp2))
   WHERE(h0(liminm2:limaxp2,ljminm2:ljmaxp2)==-valmanq) ssh(liminm2:limaxp2,ljminm2:ljmaxp2)=valmanq

! Velocities
!-----------

   DO i=liminp2,limax
     IF (ljmin == jmin) &
     u(i,2)    = 3.0_rsh*2.0_rsh*pi/44712.0_rsh/hx_g(i,2)*i*dxval &
       *SIN(2.0_rsh*pi*REAL((t-tdeb)/44712.0_rlg,rsh))
     IF (ljmin == jmin) &
     uz(:,i,2) = u(i,2)
   END DO

   CALL ex_i_rsh(-2,2,kmax,liminm2,limaxp2,ljminm2,ljmaxp2,uz(:,liminm2:limaxp2,ljminm2:ljmaxp2))
!CALL ex_j_rsh(-2,2,kmax,liminm2,limaxp2,ljminm2,ljmaxp2,uz(:,liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,u(liminm2:limaxp2,ljminm2:ljmaxp2))


! other forcings
!---------------

! river discharge
   phieau(:,:,:)=0.0_rsh



! Trajectories
! ------------



! initialization of vertical scale factor (part relative to dc*sds)
!------------------------------------------------------------------

! Domain of calculation
! Percent of wet surface on each cell
   CALL cas_init_misc

  END SUBROUTINE cas_wetdrying
