  SUBROUTINE cas_kelvin

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE cas_kelvin  ***
   !&E
   !&E ** Purpose : test case for Kelvin waves
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : cas_init
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&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---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters, ONLY : rsh,rlg,riolg,kmax,grav,pi,imin,imax,jmin,jmax, &
                          limin,liminp1,limax,ljmin,ljminp1,ljminp2,ljmax, &
                          ljmaxm1,ljminm2,ljmaxp2,liminm2,limaxp2, &
                          limaxp1,ljmaxp1,valmanq, &
                          ishift_obc_west,ishift_obc_east,jshift_obc_south,jshift_obc_north
   USE comvars2d, ONLY : fisac,gwac,fu,fv,dtmin,dtmax,dt,halfdt,suffix,l_obc_cycl_x, &
                         l_obc_cycl_y,icon,qmax,t,l_modele2d,h0,hx,h0fond, &
                         jh,jb,ig,id
   USE comvarp2d, ONLY : ssh,u,v
   USE comvarp3d, ONLY : uz,vz
   USE comvars3d, ONLY : kzinit,nzinit,qmaxz
   USE obccombine, ONLY : l_obc_cycl
   USE_MPI toolmpi, ONLY : ex_i_rsh,ex_j_rsh,ex_peri_x_rsh,ex_peri_y_rsh
   USE output,    ONLY : pasor,l_out_temp,l_out_precdate
#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
   CHARACTER(LEN=19) :: date_start,date_end,date_start3d,date_startagrif
   INTEGER           :: i,j
   INTEGER           :: nj_wall_n,nj_wall_s
   REAL(KIND=rsh)    :: dxval,dyval
   REAL(KIND=rsh)    :: radi,x,y,k0,omega0,amplitude,Ro,lambda,fuval
   REAL(KIND=rsh)    :: z0bottom,z0surface,depthcst


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

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

   ! dynamics
   CALL cas_init_param

   ! over-assignement
   l_modele2d=.TRUE.
#ifdef key_caskelvin3d
   l_modele2d=.FALSE.
#endif

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

   ! over-assignement
   icon=0
   qmax=8.0_rsh
   qmaxz=10.0_rsh


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

   ! assignement : input variables for cas_init_bathy routine
   depthcst = (100.0_rsh/4.47_rsh)**2/grav
   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
#if defined key_siggen || defined key_gencoord
   hc= 5.0_rsh
#endif


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

   ! over-assignement

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

   ! assignement : input variables for cas_init_grid routine
   dxval=20000.0_rsh
   dyval=20000.0_rsh
   fisac=45.0_riolg
   gwac=20.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


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

   ! assignement : input variables for cas_init_io routine
   date_start=   '01/01/1900 00:00:00'
   date_end=   '10/01/1900 00:00:00'
   date_start3d=  '01/01/1900 00:00:00'
   date_startagrif='01/01/1900 00:00:00'
   dtmin=1000.0_rlg
   dtmax=1000.0_rlg
   dt=1000.0_rlg
   halfdt=dt*0.5_rlg

   suffix='kelvin'

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

   ! over-assignement
   pasor(1)=3600.0_rlg
   l_out_temp(1)=.FALSE.
   l_out_precdate(1)=.TRUE.


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

   ! no open boundary
   CALL cas_init_obc

   ! over-assignement
   ! used scheme to manage obc
   l_obc_cycl = .TRUE.
   l_obc_cycl_x=.TRUE.
   ! physics introduced into the simulated area (tide or ogcm solution)

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

   ! none
   CALL cas_init_visc


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

   z0bottom   =1.0e-13
   z0surface  =0.0_rsh
   kzinit=0.0_rsh
   nzinit=0.000001_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
   ! dynamics

   radi=57.29578_rsh
   fuval=0.0001458_rsh*SIN(45.0_rsh/radi)
   Ro=SQRT(grav*depthcst)/fuval
   lambda=SQRT(grav*depthcst)*44700.0_rsh
   k0=2.0_rsh*pi/lambda
   omega0=2.0_rsh*pi/44700.0_rsh
   amplitude=0.5_rsh
   PRINT*,Ro,lambda,omega0

   DO j=ljminp2,ljmaxm1
     y=REAL(j-15-jshift_obc_south,rsh)*dyval
     DO i=liminp1,limax
       IF (h0(i,j)/=-valmanq) THEN
         x=REAL(i-1-ishift_obc_west,rsh)*dxval
         ssh(i,j)=amplitude*(EXP(-y/Ro)*COS(k0*x-omega0*t)+ &
           EXP(y/Ro)*COS(k0*x+omega0*t))
       ELSE
         ssh(i,j)=valmanq
       END IF
     END DO
   END DO
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))

   DO j=ljminp1,ljmax
     y=REAL(j-15-jshift_obc_south,rsh)*dyval
     DO i=liminp1,limax
       IF (hx(i,j)/=-valmanq-h0fond) THEN
         x=(REAL(i-1-ishift_obc_west,rsh)+0.5_rsh)*dxval
         u(i,j)=amplitude*SQRT(grav/hx(i,j))* &
           (EXP(-y/Ro)*COS(k0*x-omega0*t)- &
           EXP(y/Ro)*COS(k0*x+omega0*t))
         uz(:,i,j)=amplitude*SQRT(grav/hx(i,j))* &
           (EXP(-y/Ro)*COS(k0*x-omega0*t)- &
           EXP(y/Ro)*COS(k0*x+omega0*t))
       END IF
     END DO
   END DO

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



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

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

   ! over-assignement
   id(:)=id(:)-ishift_obc_east
   ig(:)=ig(:)+ishift_obc_west
   jh(:)=jh(:)-jshift_obc_north
   jb(:)=jb(:)+jshift_obc_south

   ! Periodic conditions use ig,id,jb,jh which are defined in cas_init_misc

   IF (l_obc_cycl_x) THEN
#ifdef key_MPI_2D
     CALL_MPI ex_peri_x_rsh(2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))
#else
     DO j=ljminm2,ljmaxp2
       ssh(ig(j)-1,j)=ssh(id(j)-2,j)
       ssh(ig(j),j)=ssh(id(j)-1,j)
       ssh(id(j),j)=ssh(ig(j)+1,j)
     END DO
#endif
   ELSE IF (l_obc_cycl_y) THEN
#ifdef key_MPI_2D
     CALL_MPI ex_peri_y_rsh(2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))
#else
     DO i=liminm2,limaxp2
       ssh(i,jb(i)-1)=ssh(i,jh(i)-2)
       ssh(i,jb(i))=ssh(i,jh(i)-1)
       ssh(i,jh(i))=ssh(i,jb(i)+1)
     END DO
#endif
   END IF

  END SUBROUTINE cas_kelvin
