  SUBROUTINE cas_intwave
   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE cas_intwave  ***
   !&E
   !&E ** Purpose : test case of instability waves
   !&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---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters
   USE comvars2d
   USE comvarp2d, ONLY : ssh,u,v,sal,temp
   USE comvarp3d, ONLY : uz,vz,ect
   USE comvars3d
   USE output, ONLY : fileresu,tdebsor,tfinsor,nb_out,tinter,tsort,pasor,   &
                      l_out_precdate,l_out_demer,l_out_ssh,l_out_ubt,l_out_u3d, &
                      l_out_sal,l_out_temp,l_out_wind,l_out_buoy,           &
                      l_out_visc,l_out_turb,l_out_off,l_out_atmsflx,nbatm,  &
                      iextmin,iextmax,l_out_nc4par,l_out_pack,              &
                      jextmin,jextmax,kextmin,kextmax,stepi,stepj,stepk,    &
                      igextmin,igextmax,jgextmin,jgextmax,l_filebydate,     &
                      l_out_zaxis_w
   USE obcogcm, ONLY : obc_coefrel
   USE cometdyn
   USE comdiag, ONLY : l_diag,tdebdiag,tfindiag,file_diag
   USE cometemp
   USE comfluxdo
   USE comaree
   USE comsiggen
   USE comturb
   USE obccombine
   USE_MPI toolmpi, ONLY: ADD_TAG,ex_i_rsh,ex_j_rsh,MASTER,min_all_mpi_rsh,max_all_mpi_rsh
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   CHARACTER(LEN=19) :: date_start,date_end,date_start3d,date_startagrif
   CHARACTER(LEN=19) :: date_startout,date_endout
   CHARACTER(LEN=19) :: tool_sectodat
   CHARACTER(LEN=lchain) :: logfilename
   INTEGER           :: i,j,k
   REAL(KIND=rsh),DIMENSION(imin:imax,jmin:jmax) :: hx_t,hy_t
   REAL(KIND=rsh)    :: x1,d,x,dtemp,hplat,x0,hplaine,dxmarge,dhmarge,omega,z,immersion
   REAL(KIND=rsh)    :: zm,zp,hz,dxval,dyval,hc_sigval
   REAL(KIND=rlg)    :: tool_datosec
   REAL(KIND=rsh)    :: value

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

   ! parametres de forme
   !---------------------
   dxval = 1000.0_rsh
   dyval = 1000.0_rsh
   fisac=0.0_riolg
   gwac=0.0_riolg
   dfiac=0.01_riolg
   dgac= 0.01_riolg
   DO j = ljmin,ljmax
     DO i = limin,limax
       dx(i,j)=dxval
       dx_u(i,j)=dxval
       dx_v(i,j)=dxval
       dx_f(i,j)=dxval
       dy(i,j) =dyval
       dy_u(i,j)=dyval
       dy_v(i,j)=dyval
       dy_f(i,j)=dyval
       surf(i,j)=dy(i,j)**2
       fu(i,j)=0.0_rsh
       fv(i,j)=0.0_rsh
       tfiusr(i,j)=0.0_rsh
       tfivsr(i,j)=0.0_rsh
       goulet(i,j)=1.0_rsh
       govlet(i,j)=1.0_rsh
     ENDDO
   ENDDO

OMPMPI barrier
OMPMPI master
   CALL_MPI ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,govlet(limin:limax,ljminm1:ljmax))
   CALL_MPI ex_i_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dx(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL_MPI ex_j_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dx(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL_MPI ex_i_rsh(0,1,1,limin,limaxp1,ljminm1,ljmax,dx_v(limin:limaxp1,ljminm1:ljmax))
   CALL_MPI ex_j_rsh(-1,0,1,limin,limaxp1,ljminm1,ljmax,dx_v(limin:limaxp1,ljminm1:ljmax))
   CALL_MPI ex_i_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dy(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL_MPI ex_j_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dy(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL_MPI ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmaxp1,dy_u(liminm1:limax,ljmin:ljmaxp1))
   CALL_MPI ex_j_rsh(0,1,1,liminm1,limax,ljmin,ljmaxp1,dy_u(liminm1:limax,ljmin:ljmaxp1))
   CALL_MPI ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmax,dx_u(liminm1:limax,ljmin:ljmax))
   CALL_MPI ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmax,dx_f(liminm1:limax,ljmin:ljmax))
   CALL_MPI ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,dy_v(limin:limax,ljminm1:ljmax))
   CALL_MPI ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,dy_f(limin:limax,ljminm1:ljmax))
OMPMPI end master
OMPMPI barrier
OMPMPI flush(govlet,dx,dx_v,dy,dy_u,dx_u,dx_f,dy_v,dy_f)

   jb(:)=jmin+1
   jh(:)=jmax
   ig(:)=imin+1
   id(:)=imax
   dhx(:,:)=0.0_rsh
   dhy(:,:)=0.0_rsh
   dhx_g(:,:)=0.0_rsh
   dhy_g(:,:)=0.0_rsh

   DO k=1,kmax
     sig(k)=(REAL(k-kmax,rsh)-0.5_rsh)/REAL(kmax,rsh)
   END DO
   sig(kmax+1)=-sig(kmax)
   sig(0)=ABS(sig(1))-2.0_rsh

   DO k=1,kmax
     dsigw(k)=sig(k+1)-sig(k)
     sigw(k)=0.5_rsh*(sig(k)+sig(k+1))
   END DO
   sigw(kmax+1)=-sigw(kmax)
   sigw(0)=-1.0_rsh

   DO k=1,kmax
     dsigu(k)=sigw(k)-sigw(k-1)
   END DO

#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
   hc_sig(:,:)= 5.0_rsh
   hc_sig_g(:,:)= 5.0_rsh
   hcx_sig(:,:)= 5.0_rsh
   hcy_sig(:,:)= 5.0_rsh
   hc_sigval = 5.0_rsh
   ! hc_sig(:,:)= 0.0_rsh
   DO k=0,kmax+1
     Csu_sig(k)=(1.0_rsh-b_sig)*SINH(theta_sig*sig(k))/SINH(theta_sig) &
       + 0.5_rsh*b_sig*(TANH(theta_sig*(sig(k)+0.5_rsh))-TANH(0.5_rsh*theta_sig))/TANH(theta_sig/2.0_rsh)
   END DO
   DO k=0,kmax
     Csw_sig(k)=(1.0_rsh-b_sig)*SINH(theta_sig*sigw(k))/SINH(theta_sig) &
       + 0.5_rsh*b_sig*(TANH(theta_sig*(sigw(k)+0.5_rsh))-TANH(0.5_rsh*theta_sig))/TANH(theta_sig/2.0_rsh)
   END DO

   DO k=1,kmax
     dcusds(k)=(Csw_sig(k)-Csw_sig(k-1))/(sigw(k)-sigw(k-1))
   END DO

   DO k=1,kmax-1
     dcwsds(k)=(Csu_sig(k+1)-Csu_sig(k))/(sig(k+1)-sig(k))
   END DO

   ! profondeur au niveau k-1 (interface)
   zm=hc_sigval*sig(k)+(1000.-hc_sigval)*csw_sig(k)
   DO k=1,kmax+1
     ! print*,k,sigw(k),csw_sig(k),csu_sig(k),dcusds(k)
     ! profondeur au niveau k (interface)
     zp=hc_sigval*sig(k)+(1000.-hc_sigval)*csw_sig(k)
     ! coef de contraction des epaisseurs de couches
     hz=hc_sigval+(1000.-hc_sigval)*dcusds(k)
     PRINT*,' '
     PRINT*,'pour un fond h = 1000 m'
     PRINT*,k,'profondeur de l interface en sigma =',sigw(k)*1000.0,' en sig gen =',hc_sigval*sig(k)+(1000.-hc_sigval)*csw_sig(k)
     PRINT*,'epaisseur des couches zp-zm : ',zp-zm,'hz*dsigu',hz*dsigu(k),zp-zm-hz*dsigu(k)
     zm=zp
   END DO

   PRINT*,'sum(dcusds(1:kmax)*dsigu(1:kmax))'
   PRINT*,SUM(dcusds(1:kmax)*dsigu(1:kmax))
#endif

   ! longitude latitude
   ! ------------------

   lon2d_g(:,0)=(/ (dxval *(i-1), i=0,imax) /)
   DO j=1,jmax
     lon2d_g(:,j)=lon2d_g(:,0)
   END DO
   lon2du_g(:,:)=lon2d_g(:,:)+dxval/2.0_rsh
   lon2dv_g(:,:)=lon2d_g(:,:)
   lat2d_g(0,:)=(/ (dyval *(j-1), j=0,jmax) /)
   DO i=1,imax
     lat2d_g(i,:)=lat2d_g(0,:)
   END DO
   lat2du_g(:,:)=lat2d_g(:,:)
   lat2dv_g(:,:)=lat2d_g(:,:)+dyval/2.0_rsh

   lon2du(liminm1:limax,ljmin:ljmaxp1)=lon2du_g(liminm1:limax,ljmin:ljmaxp1)
   lon2d(limin:limax,ljmin:ljmax)=lon2d_g(limin:limax,ljmin:ljmax)
   lon2dv(limin:limax,ljmin:ljmax)=lon2dv_g(limin:limax,ljmin:ljmax)

   lat2dv(limin:limax,ljminm1:ljmax)=lat2dv_g(limin:limax,ljminm1:ljmax)
   lat2d(limin:limax,ljmin:ljmax)=lat2d_g(limin:limax,ljmin:ljmax)
   lat2du(limin:limax,ljmin:ljmax)=lat2du_g(limin:limax,ljmin:ljmax)

   ! dates
   ! -----

   date_start=   '01/01/1900 00:00:00'
   date_end=   '20/01/1900 00:00:00'
   date_start3d=  '01/01/1900 00:00:00'
   date_startagrif='01/01/1900 00:00:00'
   date_ref = '01-Jan-1900 00:00:00'
   tdeb     = tool_datosec(date_start) 
   t=tdeb
   tfin     = tool_datosec(date_end)
   tfindiag=tfin
   tdebdiag=tdeb
   file_diag='diagnostic.dat'
   tdeb3d   = tool_datosec(date_start3d)
   tdebagrif = tool_datosec(date_startagrif)
   date_startshift= 0.0
   dtmin=10.0
   dtmax=100.0
   dt=100.0
   l_modele2d=.FALSE.
   cflcrt=.6
   tobs=12.0*3600.d0
   tdebobs=t
   cflumaxmax=0.0
   cflvmaxmax=0.0

   l_initfromfile=.FALSE.
   l_saverestart_1file=.FALSE.
   l_saverestart_bydate=.FALSE.
   file_saverestart='save.nc'
   saverestart_step=5.0_rlg*3600.0_rlg   ! in hours
   name_in_dt='dt'
   name_bathy_h0='h0'
   name_bathy_hx='hx'
   name_bathy_hy='hy'
   name_in_xev='xev'
   name_in_u='u'
   name_in_v='v'
   name_in_uz='uz'
   name_in_vz='vz'
   name_in_wz='wz'
   name_in_temp='temp'
   name_in_sal='sal'
   name_in_ect='ect'
   name_in_psi='psi'
   name_in_kz='kz'
   name_in_nz='nz'
   name_in_vis2d='name_in_vis2d'
   name_in_vis3d_xe='name_in_vis3d_xe'
   name_in_vis3d_phi='name_in_vis3d_phi'

   ! outputs
   ! -------

   nb_out=1
   ALLOCATE( fileresu(nb_out),tdebsor(nb_out),tfinsor(nb_out),tinter(nb_out) )
   ALLOCATE( tsort(nb_out),pasor(nb_out),l_filebydate(nb_out),l_out_precdate(nb_out) )
   ALLOCATE( l_out_off(nb_out),l_out_atmsflx(nb_out),l_out_visc(nb_out),l_out_turb(nb_out) )
   ALLOCATE( l_out_buoy(nb_out), l_out_zaxis_w(nb_out) )
   ALLOCATE( l_out_demer(nb_out),l_out_ubt(nb_out),l_out_u3d(nb_out) )
   ALLOCATE( l_out_sal(nb_out),l_out_temp(nb_out),l_out_ssh(nb_out),l_out_wind(nb_out) )
   ALLOCATE( iextmin(nb_out),iextmax(nb_out),jextmin(nb_out),jextmax(nb_out) )
   ALLOCATE( igextmin(nb_out),igextmax(nb_out),jgextmin(nb_out),jgextmax(nb_out) )
   ALLOCATE( kextmin(nb_out),kextmax(nb_out),stepi(nb_out),stepj(nb_out),stepk(nb_out) )
   fileresu(1)='results.nc'
   suffix='mint'
   date_startout='01/01/1900 00:00:00'
   date_endout='20/01/1900 00:00:00'
   l_maxu=.TRUE.
   tdebsor(1)=tool_datosec(date_startout)
   tfinsor(1)=tool_datosec(date_endout)
   pasor(1)=3600.d0
   l_filebydate(1)=.FALSE.
   IF_MPI ( .NOT. l_filebydate(1) ) THEN
     CALL_MPI ADD_TAG(fileresu(1),LEN(fileresu(1)))
   ENDIF_MPI
   l_out_off(1)=.FALSE.
   l_out_precdate(1)=.FALSE.
   l_out_demer(1)=.FALSE.
   igextmin(1)=imin
   igextmax(1)=imax
   jgextmin(1)=jmin
   jgextmax(1)=jmax
   iextmin(1)=limin
   iextmax(1)=limax
   jextmin(1)=ljmin
   jextmax(1)=ljmax
   kextmin(1)=1
   kextmax(1)=kmax
   stepi(1)=1
   stepj(1)=1
   stepk(1)=1
   l_out_ssh(1)=.TRUE.
   l_out_ubt(1)=.TRUE.
   l_out_u3d(1)=.TRUE.
   l_out_sal(1)=.TRUE.
   l_out_temp(1)=.TRUE.
   l_out_wind(1)=.FALSE.
   l_out_atmsflx(1)=.FALSE.
   l_out_visc(1)=.FALSE.
   l_out_turb(1)=.FALSE.
   l_out_buoy(1)=.FALSE.
   l_out_zaxis_w(1)=.FALSE.
   l_obc2drank_save=.FALSE.
   l_incbotstress=.FALSE.
   botstressmax=100.0
   l_stability=.FALSE.
   iscreen=12
   iscreenlog=18
   ierrorlog=17
   iwarnlog=16
   l_out_nc4par = .false.
   l_out_pack = .false.

   IF_MPI (MASTER) THEN
   logfilename= 'simu_'//TRIM(suffix)//'.log'
   IF (iscreenlog/=6) OPEN(unit=iscreenlog,file=logfilename,status="replace")
   logfilename='error_'//TRIM(suffix)//'.log'
   IF (ierrorlog/=6) OPEN(unit=ierrorlog,file=logfilename,status="replace")
   logfilename='warning_'//TRIM(suffix)//'.log'
   IF (iwarnlog/=6) OPEN(unit=iwarnlog,file=logfilename,status="replace")
   ENDIF_MPI

   ! meteo
   !-------

   l_meteo_stat=.TRUE.
   l_meteo_hom=.TRUE.
   wind_veloc=0.0
   wind_dir=0.0
   !cds=0.001
   l_sflx_solarcst=.TRUE.
   sflx_solarcst=0.0
   nbatm=0
   l_diag=.FALSE.
   l_obc_cycl_x=.FALSE.
   l_obc_cycl_y=.FALSE.

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

   ! geometry (where are the open boundaries ?)

   l_obc_south=.TRUE.
   l_obc_north=.FALSE.
   l_obc_west=.FALSE.
   l_obc_east=.FALSE.

   ! used scheme to manage obc

   l_obc_mars = .TRUE.  
   l_obc_diri = .FALSE. 
   l_obc_char = .FALSE. 
   l_obc_cycl = .FALSE.
   l_obc_cycl_x=.FALSE.
   l_obc_cycl_y=.FALSE.

   ! physics introduced into the simulated area (tide or ogcm solution)

   l_obc_tide=.TRUE.
   l_tide_harmcompo=.FALSE.
   l_obc_ogcm=.FALSE.

   obc_coefrel=0.0_rsh

   ! bathymetry
   !-------------

   mslshift  = 0.0

   dxmarge=80000.0
   dhmarge=1000.0
   hplat=50.0
   hplaine=1000.0
   x0=600000.0
   DO j=jmin+1,jmax
     x=(j-1)*dyval
     hx_g(:,j)=0.5*(hplaine+hplat)-0.5*(hplaine-hplat)*TANH((x-x0)/dxmarge)
     hy_g(:,j)=0.5*(hplaine+hplat)-0.5*(hplaine-hplat)*TANH((x-x0)/dxmarge)
     h0_g(:,j)=0.5*(hplaine+hplat)-0.5*(hplaine-hplat)*TANH((x-x0)/dxmarge)
   END DO

   hy_g(:,jmax-1)=-valmanq-h0fond
   hx_g(imax-1,:)=-valmanq-h0fond
   hx_g(imin+1,:)=-valmanq-h0fond

   h0(:,:) = h0_g(liminm2:limaxp2,ljminm2:ljmaxp2)
   hx(:,:) = hx_g(liminm2:limaxp2,ljminm2:ljmaxp2)
   hy(:,:) = hy_g(liminm2:limaxp2,ljminm2:ljmaxp2)

   x0=0.0
   x1=0.0
   DO k=1,kmax
     i=2
     j=700
!cval revoir ssh non initialise
     x0=x0+((ssh(i,j)+hc_sig_g(i,j)) &
            +(hy_g(i,j)-hc_sig_g(i,j))*(Csw_sig(k)-Csw_sig(k-1))/(sigw(k)-sigw(k-1)))*(sigw(k)-sigw(k-1))
     x1=x1+((ssh(i,j)+hc_sig_g(i,j))+(hy_g(i,j)-hc_sig_g(i,j))*dcusds(k))*dsigu(k)
   END DO
   PRINT*,x0,x1,ssh(i,j)+hy_g(i,j)

   ! grandeurs dynamiques
   !----------------------

   omega=0.0001
   DO j=ljminp1,ljmaxm1
!     x=dy(1,j)*j
     x=dyval*j
     ssh(:,j)  =  0.5*COS(omega*((t-tdeb)-x/SQRT(9.81*hy_g(1,1))) )
!     v(:,j)    = -0.5/SQRT(9.81*hy(1,1))*SIN(omega*((t-tdeb-dt)  -x/SQRT(9.81*hy(1,1))) )
   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))
   u(:,:)    = 0.0
   v(:,:)    = 0.0
   uz(:,:,:) = 0.0
   vz(:,:,:) = 0.0
   wz(:,:,:) = 0.0

   ! Percent of wet surface on each cell
   ! -----------------------------------

   hx_t(:,:)=100000.0_rsh
   hy_t(:,:)=100000.0_rsh
   WHERE (hx_g(:,:) /= -valmanq -h0fond) hx_t(:,:)=hx_g(:,:)
   WHERE (hy_g(:,:) /= -valmanq -h0fond) hy_t(:,:)=hy_g(:,:)
   dhxy(:,:)=1.0_rsh
   DO j=ljminp1,ljmax
      DO i=liminp1,limax
         dhxy(i,j)=MAX(hx(i,j),hx(i-1,j),hy(i,j),hy(i,j-1))- &
                   MIN(hx_t(i,j),hx_t(i-1,j),hy_t(i,j),hy_t(i,j-1))+epsilon
      END DO
   END DO

   fwet(:,:) = 1.0_rsh
   fwetp(:,:) = 1.0_rsh
   fwetmin=1.0_rsh
   IF (fwetmin/=1.0_rsh) THEN
     DO j=ljmin,ljmax
       DO i=limin,limax
         IF (dhxy(i,j) == 0.0_rsh) THEN
            fwet(i,j)=1.0_rsh
           fwetp(i,j)=1.0_rsh
         ELSE
            fwet(i,j)=MIN(MAX(fwetmin,(ssh(i,j)+h0(i,j))/dhxy(i,j)),1.0_rsh)
           fwetp(i,j)=MIN(MAX(fwetmin,(ssh(i,j)+h0(i,j))/dhxy(i,j)),1.0_rsh)
         ENDIF
       END DO
     END DO
   END IF

   ! turbulence
   !------------
   turb_nbeq=0
   turb_0eq_option=1
   turb_2eq_option=2
   z0b(:,:)=1.0e-13
   z0s(:,:)   =0.0_rsh
   hminfrot=0.5_rsh
   hmaxfrot=5.0_rsh
   kzinit=0.0
   nzinit=0.0e-2
   nz(:,:,:) = nzinit
   kz(:,:,:) = kzinit
   ect(:,:,:)=1.0e-6
   ectmin=1.0e-6
   bgdiff=1.0e-6
   vish_phi(:,:,:)= 5.*0.01*dyval**1.15
   vish_xe(:,:,:) = 10.0
   vish_phi(:,:,:) = 10.0
   l_smagor=.FALSE.
   vismin=0.001
   vismax=4.
   cosmag=0.1
   icon=1
   kx=0.0
   ky=0.0
   qmax=8.0
   qmaxz=10.0
   rhoref=1027.34
   saliref=30.0         ! pour la condition a la limite
   tetaref=10.0
   l_stateeq_lin=.TRUE.
   saliref_lin=30.0         ! pour l equation d etat
   tetaref_lin=10.0
   rhoair=1.25
   chp=3986.0
   coext=0.1
   file_river='e'
   phieau(:,:,:)=0.0

   ! initialisation des traceurs
   !----------------------------
   !#if defined key_siggen || defined key_gencoord
   sal(:,:,:) = saliref
   dtemp=10.0
   DO j=ljminm1,ljmaxp2
     DO i=liminm1,limaxp2
       DO k=1,kmax
         !initV7.35      immersion=hc_sig_g(i,j)*(1-sig(k)+Csu_sig(k))-h0(i,j)*csu_sig(k)
         immersion=hc_sig_g(i,j)*(Csu_sig(k)-sig(k))-h0(i,j)*csu_sig(k)
         ! immersion=(1.0-sig(k))*h0(i,j)
         temp(k,i,j) = tetaref+dtemp*(1.0-immersion/hplaine)
       END DO
     END DO
   END DO
   !#endif

   ! initialisation des noms de variables du fichier sortie
   !-------------------------------------------------------
   name_out_h0='h0'
   name_out_hx='hx'
   name_out_hy='hy'
   name_out_u='u'
   name_out_v='v'
   name_out_uz='uz'
   name_out_vz='vz'
   name_out_temp='temp'
   name_out_sal='sal'
   name_out_xe='xe'
   name_out_vis2d='VIS2D'
   name_out_vishxe='VISH_XE'
   name_out_vishphi='VISH_PHI'
   name_out_nz='NZ'
   name_out_kz='KZ'
   name_out_bz='BZ'

   ! initialization of attributes (max and min valid values for each variable)
   ! -------------------------------------------------------------------------

   riog_valid_min_h0=-50.0_riosh
   riog_valid_max_h0=10000.0_riosh
   riog_valid_min_u=-100.0_riosh
   riog_valid_max_u=100.0_riosh
   riog_valid_min_temp=0.0_riosh
   riog_valid_max_temp=50.0_riosh
   riog_valid_min_sal=0.0_riosh
   riog_valid_max_sal=50.0_riosh
   riog_valid_min_xe=-50.0_riosh
   riog_valid_max_xe=50.0_riosh
   riog_valid_min_wstress=0.0_riosh
   riog_valid_max_wstress=2000.0_riosh
   riog_valid_min_visc=0.0_riosh
   riog_valid_max_visc=100000.0_riosh
   riog_valid_min_nz=0.0_riosh
   riog_valid_max_nz=1000.0_riosh
   riog_valid_min_kz=0.0_riosh
   riog_valid_max_kz=1000.0_riosh
   riog_valid_min_bz=-50.0_riosh
   riog_valid_max_bz=50.0_riosh
   value=MINVAL(dx(:,:))
   CALL_MPI MIN_ALL_MPI_RSH(value)
   riog_valid_min_dx=value-1.0_riosh
   value=MAXVAL(dx(:,:))
   CALL_MPI MAX_ALL_MPI_RSH(value)
   riog_valid_max_dx=value+1.0_riosh
   value=MINVAL(dy(:,:))
   CALL_MPI MIN_ALL_MPI_RSH(value)
   riog_valid_min_dy=value-1.0_riosh
   value=MAXVAL(dy(:,:))
   CALL_MPI MAX_ALL_MPI_RSH(value)
   riog_valid_max_dy=value+1.0_riosh

   ! initialization of other attributes
   ! ----------------------------------

   cg_conv_title="name of area"
   cg_conv_comment=" "
   cg_conv_source="MARS CONF"
   cg_conv_area="area"
   cg_conv_institution="IFREMER"
   cg_conv_institution_references="http://www.ifremer.fr"
   cg_conv_data_centre="DYNECO/PHYSED"
   cg_conv_data_centre_references="http://www.ifremer.physed.fr"
   cg_conv_forecast_range=" "
   cg_conv_forecast_type=" "
   cg_conv_field_type="n/a"
   cg_conv_operational_status=" "

   ! initialisation cdate, ijour,imois... pour sorties par dates et utilisation de la maree
   !---------------------------------------------------------------------------------------

   cdate = tool_sectodat(t)
   CALL tool_decompdate(cdate,ijour,imois,ian,iheure,iminu,isec)

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

#if defined key_siggen || defined key_gencoord
#if ! defined key_siggen_g2
!$OMP DO SCHEDULE(STATIC)
   DO j=ljmin,ljmaxp1
     DO i=limin,limaxp1
       DO k=1,kmax-1
         hzdcwsds(k,i,j)=hc_sig(i,j)+(h0(i,j)-hc_sig(i,j))*dcwsds(k)
         hzdcusds(k,i,j)=hc_sig(i,j)+(h0(i,j)-hc_sig(i,j))*dcusds(k)
         hzdcwsds_x(k,i,j)=hcx_sig(i,j)+(hx(i,j)-hcx_sig(i,j))*dcwsds(k)
         hzdcusds_x(k,i,j)=hcx_sig(i,j)+(hx(i,j)-hcx_sig(i,j))*dcusds(k)
         hzdcwsds_y(k,i,j)=hcy_sig(i,j)+(hy(i,j)-hcy_sig(i,j))*dcwsds(k)
         hzdcusds_y(k,i,j)=hcy_sig(i,j)+(hy(i,j)-hcy_sig(i,j))*dcusds(k)
       END DO
       k=kmax
       hzdcusds(k,i,j)=hc_sig(i,j)+(h0(i,j)-hc_sig(i,j))*dcusds(k)
       hzdcusds_x(k,i,j)=hcx_sig(i,j)+(hx(i,j)-hcx_sig(i,j))*dcusds(k)
       hzdcusds_y(k,i,j)=hcy_sig(i,j)+(hy(i,j)-hcy_sig(i,j))*dcusds(k)
     END DO
   END DO
!$OMP END DO
#endif
#endif


  END SUBROUTINE cas_intwave
