  SUBROUTINE cas_profile

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE cas_profile  ***
!&E
!&E ** Purpose : definition of a test case for a vertical profile
!&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
!&E---------------------------------------------------------------------





























































!! * 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 comturb
   USE comdiag, ONLY : l_diag,file_diag,tdebdiag,tfindiag
   USE cometemp
   USE comfluxdo

   USE comaree
   USE obccombine
   USE 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)    :: L,zeta0,dyval,dxval
   REAL(KIND=rlg)    :: tool_datosec
   REAL(KIND=rsh)    :: value

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

!--------------------------------------------
!--------------------------------------------
!! premier  cas_test : profile             !!
!--------------------------------------------
!--------------------------------------------
   
! parametres de forme
!---------------------

   fisac=0.0_riolg
   gwac= 0.0_riolg
   dfiac=0.01_riolg
   dgac= 0.01_riolg
   L=200.0_rsh
   dyval=L/(REAL(imax,rsh)-1.5_rsh)
   dxval=L/(REAL(imax,rsh)-1.5_rsh)
   DO j = ljmin,ljmax
     DO i = limin,limax
       dx(i,j)=L/(REAL(imax,rsh)-1.5_rsh)
       dx_u(i,j)=L/(REAL(imax,rsh)-1.5_rsh)
       dx_v(i,j)=L/(REAL(imax,rsh)-1.5_rsh)
       dx_f(i,j)=L/(REAL(imax,rsh)-1.5_rsh)
       dy(i,j) =L/(REAL(imax,rsh)-1.5_rsh)
       dy_u(i,j) =L/(REAL(imax,rsh)-1.5_rsh)
       dy_v(i,j) =L/(REAL(imax,rsh)-1.5_rsh)
       dy_f(i,j) =L/(REAL(imax,rsh)-1.5_rsh)
       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

!$OMP barrier
!$OMP master
   CALL ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,govlet(limin:limax,ljminm1:ljmax))
   CALL ex_i_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dx(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL ex_j_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dx(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL ex_i_rsh(0,1,1,limin,limaxp1,ljminm1,ljmax,dx_v(limin:limaxp1,ljminm1:ljmax))
   CALL ex_j_rsh(-1,0,1,limin,limaxp1,ljminm1,ljmax,dx_v(limin:limaxp1,ljminm1:ljmax))
   CALL ex_i_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dy(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL ex_j_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,dy(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmaxp1,dy_u(liminm1:limax,ljmin:ljmaxp1))
   CALL ex_j_rsh(0,1,1,liminm1,limax,ljmin,ljmaxp1,dy_u(liminm1:limax,ljmin:ljmaxp1))
   CALL ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmax,dx_u(liminm1:limax,ljmin:ljmax))
   CALL ex_i_rsh(-1,0,1,liminm1,limax,ljmin,ljmax,dx_f(liminm1:limax,ljmin:ljmax))
   CALL ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,dy_v(limin:limax,ljminm1:ljmax))
   CALL ex_j_rsh(-1,0,1,limin,limax,ljminm1,ljmax,dy_f(limin:limax,ljminm1:ljmax))
!$OMP end master
!$OMP barrier
!$OMP 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
   l_obc_cycl_x=.FALSE.
   l_obc_cycl_y=.FALSE.

   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



! 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/1901 00:00:00'
   date_end=   '02/01/1901 00:00:00'
   date_start3d=  '01/01/1901 00:00:00'
   date_startagrif='01/01/1901 00:00:00'
   date_ref = '01-Jan-1900 00:00:00'
   tdeb     = tool_datosec(date_start) 
   t        = tdeb-1.0_rlg*86400.0_rlg
   tfin     = tool_datosec(date_end)
   tdeb3d   = t-1.0_rlg*86400.0_rlg
   tdebagrif = tool_datosec(date_startagrif)
   date_startshift= 0.0_rlg
   dtmin=4.0_rlg
   dtmax=4.0_rlg
   dt=4.0_rlg
   l_modele2d=.FALSE.
   cflcrt=0.6_rsh
   tobs=12.0_rlg*3600.0_rlg
   tdebobs=t
   cflumaxmax=0.0_rsh
   cflvmaxmax=0.0_rsh

   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='profile'
   date_startout='01/01/1901 00:00:00'
   date_endout='20/01/1901 00:00:00'
   l_maxu=.FALSE.
   tdebsor(1)=tool_datosec(date_startout)
   tfinsor(1)=tool_datosec(date_endout)
   pasor(1)=3600.0_rlg
   l_filebydate(1)=.FALSE.
   IF ( .NOT. l_filebydate(1) ) THEN
     CALL ADD_TAG(fileresu(1),len(fileresu(1)))
   ENDIF
   l_out_off(1)=.FALSE.
   l_out_precdate(1)=.TRUE.
   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)=.FALSE.
   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_rsh
   l_stability=.FALSE.
   iscreen=12
   iscreenlog=18
   ierrorlog=17
   iwarnlog=16
   l_out_nc4par = .false.
   l_out_pack = .false.

   IF (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

! diagnostics
!------------

   file_diag='diagnostic.nc'
   tdebdiag=tool_datosec(date_start)
   tfindiag=tool_datosec(date_end)

! meteo
!-------

   l_meteo_stat=.TRUE.
   l_meteo_hom=.TRUE.
   wind_veloc=0.0_rsh
   wind_dir=0.0_rsh
   l_sflx_solarcst=.TRUE.
   sflx_solarcst=0.0_rsh
   nbatm=0
   l_diag=.TRUE.
   zeta0=0.001_rsh

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

! geometry (where are the open boundaries ?)

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

! 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_rsh
   DO i=imin+1,imax
     hx_g(i,:)=10.0_rsh
     hy_g(i,:)=10.0_rsh
     h0_g(i,:)=10.0_rsh
   END DO
   h0fond=1.01_rsh
   hy_g(:,jmin+1)=-valmanq-h0fond
   hy_g(:,jmax-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)

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

   ssh(:,:)   = zeta0*COS(2.0_rlg*pi*(t-tdeb)/44712.0_rlg)
   u(:,:) = 0.0_rsh
   uz(:,:,:) = 0.0_rsh
   DO i=liminp2,limax
!     u(i,2)    = zeta0*2.0_rlg*pi/44712.0_rlg/hx(i,2)*i*dx(i,2) &
!                 *SIN(2.0_rlg*pi*(t-tdeb)/44712.0_rlg)
     u(i,2)    = zeta0*2.0_rlg*pi/44712.0_rlg/hx_g(i,2)*i*dxval &
                 *SIN(2.0_rlg*pi*(t-tdeb)/44712.0_rlg)
     uz(:,i,2) = u(i,2)
   END DO
   v(:,:)    = 0.0_rsh
   vz(:,:,:) = 0.0_rsh
   wz(:,:,:) = 0.0_rsh

   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))
   CALL ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,u(liminm2:limaxp2,ljminm2:ljmaxp2))

! 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(:,:)   =0.005_rsh
   z0s(:,:)   =0.0_rsh
   nzinit=0.0028_rsh
   kzinit=0.0_rsh
   frot_lin=0.0008_rsh
   nz(:,:,:) = nzinit
   kz(:,:,:) = kzinit
   ect(:,:,:)=1.0e-6
   ectmin=1.0e-6
   bgdiff=1.0e-6
   vish_xe(:,:,:) = 0.0_rsh*0.01_rsh*dyval**1.15_rsh
   vish_phi(:,:,:)= 0.0_rsh*0.01_rsh*dyval**1.15_rsh
   vis2d(:,:)= 0.0_rsh*0.01_rsh*dyval**1.15_rsh
   l_smagor=.FALSE.
   vismin=0.001_rsh
   vismax=4.0_rsh
   cosmag=0.1_rsh
   icon=0
   kx=1.0_rsh
   ky=1.0_rsh
   qmax=8.0_rsh
   qmaxz=10.0_rsh
   rhoref=1027.34_rsh
   saliref=30.0_rsh       ! pour la condition a la limite
   tetaref=10.0_rsh
   l_stateeq_lin=.FALSE.
   saliref_lin=saliref
   tetaref_lin=tetaref
   rhoair=1.25_rsh
   chp=3986.0_rsh
   coext=0.1_rsh
   file_river='e'
   phieau(:,:,:)=0.0_rlg

! initialisation des traceurs
!----------------------------

   temp(:,:,:) = tetaref
   sal(:,:,:) = saliref

   OPEN(12,file='listing',form='formatted')

! 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 MIN_ALL_MPI_RSH(value)
   riog_valid_min_dx=value-1.0_riosh
   value=MAXVAL(dx(:,:))
   CALL MAX_ALL_MPI_RSH(value)
   riog_valid_max_dx=value+1.0_riosh
   value=MINVAL(dy(:,:))
   CALL MIN_ALL_MPI_RSH(value)
   riog_valid_min_dy=value-1.0_riosh
   value=MAXVAL(dy(:,:))
   CALL 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)

  END SUBROUTINE cas_profile
