  SUBROUTINE cas_lock

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE cas_lock  ***
   !&E
   !&E ** Purpose : test case of lock exchange
   !&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
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters,ONLY : rsh,rlg,riosh,riolg,imax,liminm1,limaxp2
   USE comvars2d, ONLY : fisac,gwac,fu,fv,dtmin,dtmax,dt,halfdt,suffix, &
                         qmax,h0,kx,ky,vish_xe,vish_phi,ierrorlog
   USE comvarp2d, ONLY : ssh,u,v,sal,temp
   USE comvars3d, ONLY : rhoref,saliref,saliref_lin,tetaref,tetaref_lin,kzinit,nzinit,qmaxz,sig,dsigu,l_stateeq_lin
   USE comvarp3d, ONLY : uz,vz
   USE output, ONLY : pasor,l_out_sal
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : theta_sig,b_sig,hc
#endif
   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
   LOGICAL ::    l_nc4par
   CHARACTER(LEN=19) :: date_start,date_end,date_start3d,date_startagrif
   INTEGER           :: i,j,k,IERR_MPI
   INTEGER           :: ni_wall_e,ni_wall_w,nj_wall_n,nj_wall_s
   REAL(KIND=rsh)    :: dxval,dyval
   REAL(KIND=rsh)    :: z0bottom,z0surface,depthcst,limit

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

   !--------------------------------------------
   !--------------------------------------------
   !!  second test case : lock exchange       !!
   !--------------------------------------------
   !--------------------------------------------

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

   ! dynamics
   CALL cas_init_param

   ! over-assignement

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

   ! over-assignement
   qmax=12.0_rsh
   qmaxz=10.0_rsh
   rhoref=1000.0_rsh
   saliref=35.0_rsh
   tetaref=20.0_rsh
   saliref_lin=saliref
   tetaref_lin=tetaref
   l_stateeq_lin=.TRUE.
   kx= 0.0_rsh
   ky= 0.0_rsh


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

   ! assignement : input variables for cas_init_bathy routine
   depthcst = 20.0_rsh
   ni_wall_e = 4  ! land over ni_wall_e cells at the eastern part of the domain
   ni_wall_w = 5  ! land over ni_wall_w cells (from imin column) at the western part of the domain
   nj_wall_s = 5  ! land over nj_wall_s cells (from jmin row) at the southern part of the domain
   nj_wall_n = 4  ! land over nj_wall_n cells at the northern part of the domain
#if defined key_siggen || defined key_gencoord
   hc= 5.0_rsh
#endif


   ! flat bottom, plus walls
   CALL cas_init_bathy(depthcst,ni_wall_e=ni_wall_e,ni_wall_w=ni_wall_w, &
                                nj_wall_n=nj_wall_n,nj_wall_s=nj_wall_s)

   ! over-assignement


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

   ! assignement : input variables for cas_init_grid routine
 
   ! parametres de forme
   !---------------------
#if defined key_caslock_HR
   dxval = 125.0_rsh
   dyval = 125.0_rsh
#elif defined key_caslock_MR
   dxval = 500.0_rsh
   dyval = 500.0_rsh
#elif defined key_caslock_LR
   dxval = 2000.0_rsh
   dyval = 2000.0_rsh
#else
   WRITE(ierrorlog,*) 'You must choose a resolution :'
   WRITE(ierrorlog,*) 'key_caslock_HR (125m) or key_caslock_MR (500m) or key_caslock_LR (2km)'
   WRITE(ierrorlog,*) 'The simulation is stopped'
   CALL_MPI MPI_FINALIZE(IERR_MPI)
   STOP
#endif

   fisac=1.0_riolg
   gwac=1.0_riolg

#if defined key_siggen || defined key_gencoord
   theta_sig= 4.0_rsh ! entre 0 et 20
   theta_sig= 0.0000001_rsh ! entre 0 et 20
   b_sig= 1.0_rsh     ! entre 0 et 1
#endif

   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=   '01/01/1900 20:00:00'
   date_end=   '01/01/1900 17:00:00'
   date_start3d=  '01/01/1900 00:00:00'
   date_startagrif='01/01/1900 00:00:00'
#if defined key_caslock_HR
   dtmin=10.0_rlg
   dtmax=10.0_rlg
   dt=10.0_rlg
#elif defined key_caslock_MR
   dtmin=50.0_rlg
   dtmax=50.0_rlg
   dt=50.0_rlg
#elif defined key_caslock_LR
   dtmin=150.0_rlg
   dtmax=150.0_rlg
   dt=150.0_rlg
#endif
   halfdt=dt*0.5_rlg
! mettre 1s de pas de temps !!!!!!

   suffix='gravity'
   l_nc4par = .TRUE.

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

   ! over-assignement
   pasor(1)=900.0_rlg
   l_out_sal(1)=.FALSE.

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

   ! no open boundary
   CALL cas_init_obc


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

   ! none
   CALL cas_init_visc

   ! over-assignement
#if defined key_caslock_HR
   vish_xe(:,:,:) = 100.0_rsh
   vish_phi(:,:,:)= 100.0_rsh
#elif defined key_caslock_MR
   vish_xe(:,:,:) = 100.0_rsh
   vish_phi(:,:,:)= 100.0_rsh
#elif defined key_caslock_LR
   vish_xe(:,:,:) = 100.0_rsh
   vish_phi(:,:,:)= 100.0_rsh
#endif


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

   z0bottom   =1.0e-13
   z0surface  =0.0_rsh
   nzinit=1.e-6_rsh
   kzinit=1.e-6_rsh
   nzinit=0.0_rsh
   kzinit=0.0_rsh

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

   ! over-assignement


   ! 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

   ! tracers
   !--------

   DO i=liminm1,limaxp2
#if defined key_caslock_th
     temp(:,i,:)=7.5_rsh/0.28_rsh + 2.5_rsh/0.28_rsh * TANH(REAL(i-0.5_rsh-imax/2,rsh)*0.001_rsh*dxval)
#else
     IF(i<=imax/2) THEN 
       temp(:,i,:)=5.0_rsh/0.28_rsh
     ELSE 
       temp(:,i,:)=10.0_rsh/0.28_rsh
     END IF
#endif
   END DO


   ! Miscellaneous
   ! -------------

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

  END SUBROUTINE cas_lock
