  SUBROUTINE dyn3d_dt(uz,vz)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE dyn3d_dt  ***
!&E
!&E ** Purpose : adaptative computation of the time-step by examining
!&E              by examining the surface current number.
!&E
!&E ** Description : The time-step increase is applied after a lap of tobs,
!&E                  if during this observation period the stability criterion
!&E                  has been exceeded.
!&E
!&E ** Called by : stepu
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays : uz, vz, dy_v, htx, hty
!&E
!&E ** Modified variables : dt, cflumaxmax, cflvmaxmax, tdebobs, tobs
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2004-12-29
!&E       !  2007-09    (V. Garnier) homogeneneity of dx dy...
!&E       !  2015-09    (S. Petton) detection of cfl over all layers
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used
   USE parameters,ONLY : limin,limax,ljmin,kmax,ljminp1,ljmaxm1,rsh,rlg, &
                         liminm2,limaxp2,ljminm2,ljmaxp2,epsilon,        &
                         jshift_obc_south,jshift_obc_north,jmax,valmanq,dxmin
   USE comvars2d, ONLY : ig,id,dx_u,dy_v,htx,hty,h0
   USE comvars2d, ONLY : cflumaxmax,cflvmaxmax,cflcrt ,cdate
   USE comvars2d, ONLY : dt,halfdt,tobs,dtmin,t,tdebobs,dtmax,iscreen

   USE toolmpi, ONLY : FINDMAX_MPI,MASTER



   USE toolmpi, ONLY : FINDMAX_MPI_6

   IMPLICIT NONE

!! * Arguments
   REAL(KIND=rsh),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: uz,vz

!! * Local declarations
   INTEGER        :: i,j,iumax,jumax,ivmax,jvmax,k
   INTEGER        :: liumx,ljumx,livmx,ljvmx
   REAL(KIND=rsh) :: flumx,flvmx
   REAL(KIND=rsh) :: uzmax,htxmax,dxumax,vzmax,htymax,dyvmax
   REAL(KIND=rsh) :: cflumax,cflvmax,cflmax,cflu,cflv
   !REAL(KIND=rlg) :: dtmaxp



   INTEGER        :: kumax,kvmax,lkumx,lkvmx


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

   !PRINT*,'enter DYN3DDT'

! on examine le nb de courant max en surface
!-------------------------------------------

   cflumax=-100.0_rsh
   cflvmax=-100.0_rsh
   iumax=limin
   jumax=ljmin
   ivmax=limin
   jvmax=ljmin


   kumax=1
   kvmax=1



!$OMP PARALLEL DEFAULT(NONE) &
!$OMP& PRIVATE(i,j,liumx,ljumx,livmx,ljvmx,flumx,flvmx,cflu,cflv, &

!$OMP&        lkumx,lkvmx,&


!$OMP&        k) &
!$OMP& SHARED(cflumax,cflvmax,uz,vz,dt,dx_u,iumax,jumax,ivmax,jvmax,ig,id,dy_v, &

!$OMP&        kumax,kvmax, &



!$OMP&        ljminp1,ljmaxm1,ljmin,limin,limax)

   flumx = cflumax
   flvmx = cflvmax




!$OMP do SCHEDULE(runtime)


!DO j=ljminp1,ljmaxm1
   DO j=MAX0(ljminp1,jshift_obc_south+1), MIN0(ljmaxm1,jmax-1-jshift_obc_north)
     DO i=MAX0(limin,ig(j)),MIN0(limax,id(j)-1)
      IF (dx_u(i,j)/=dxmin .and. dy_v(i,j)/=dxmin) THEN


       DO k=1,kmax
         cflu=ABS(uz(k,i,j))*dt/dx_u(i,j)
         IF(cflu > flumx) THEN
           flumx=cflu
           liumx=i
           ljumx=j
           lkumx=k
         ENDIF

         cflv=ABS(vz(k,i,j))*dt/dy_v(i,j)
         IF(cflv > flvmx) THEN
           flvmx=cflv
           livmx=i
           ljvmx=j
           lkvmx=k
         ENDIF
       END DO



      ENDIF
     ENDDO
   ENDDO
!$OMP end do nowait

!$OMP CRITICAL
   IF(flumx.GT.cflumax) THEN
     cflumax = flumx
     iumax = liumx
     jumax = ljumx

     kumax = lkumx

   ENDIF
   IF(flvmx.GT.cflvmax) THEN
     cflvmax = flvmx
     ivmax = livmx
     jvmax = ljvmx

     kvmax = lkvmx

   ENDIF

!$OMP END CRITICAL
!$OMP END PARALLEL

   uzmax=uz(kumax,iumax,jumax)
   htxmax=htx(iumax,jumax)
   dxumax=dx_u(iumax,jumax)
   vzmax=vz(kvmax,ivmax,jvmax)
   htymax=hty(ivmax,jvmax)
   dyvmax=dy_v(ivmax,jvmax)
   CALL FINDMAX_MPI_6(cflumax,iumax,jumax,kumax,uzmax,htxmax,dxumax)
   CALL FINDMAX_MPI_6(cflvmax,ivmax,jvmax,kvmax,vzmax,htymax,dyvmax)

   IF (MASTER) THEN 
   WRITE(iscreen,*) 'DATE : ',cdate
   WRITE(iscreen,*) 'dt=',dt
   WRITE(iscreen,'(a11,f17.8,2x,2(i4,3x),f17.8,2x,i3)') 'max en u = ',uzmax,iumax,jumax,htxmax,kumax
   WRITE(iscreen,'(a11,f17.8,2x,2(i4,3x),f17.8,2x,i3)') 'max en v = ',vzmax,ivmax,jvmax,htymax,kvmax
   ENDIF




   IF(MAX(cflumax,cflvmax) > cflcrt) THEN
     IF (MASTER) THEN 
     WRITE(iscreen,*) 'ALERTE ADS!!!!!!!!!!!!!'
     ENDIF
     !IF (.TRUE.) THEN
       dt=MIN(0.83_rsh*cflcrt*dyvmax/MAX(REAL(ABS(vzmax),rlg),epsilon), &
              0.83_rsh*cflcrt*dxumax/MAX(REAL(ABS(uzmax),rlg),epsilon))
       dt=MAX(dt,dtmin)
       halfdt=dt*0.5_rlg
       IF (MASTER) THEN 
       WRITE(iscreen,*) 'HIIIIIIIII!!!! nouveau dt=',dt
       ENDIF
     !ELSE
       ! dtmaxp=MAX(MIN(0.83_rsh*cflcrt*dyvmax/MAX(REAL(ABS(vzmax),rlg),epsilon), &
       !                0.83_rsh*cflcrt*dxumax/MAX(REAL(ABS(uzmax),rlg),epsilon)),dtmin)
! Set the next time step of the parent grid
       !CALL Agrif_Set_Parent(dtmax, MAX(Agrif_parent(dtmin),dtmaxp*Agrif_Rhot()))
       !IF (cflumax > cflcrt) CALL Agrif_Set_Parent(cflumaxmax, cflumax)
       !IF (cflvmax > cflcrt) CALL Agrif_Set_Parent(cflvmaxmax, cflvmax)
       IF (MASTER) THEN 
       ! WRITE(iscreen,*) 'HIIIIIIIII!!!! desired dt=',dtmaxp
       ENDIF
     !ENDIF
   ENDIF




   cflumaxmax=MAX(cflumaxmax,cflumax)
   cflvmaxmax=MAX(cflvmaxmax,cflvmax)

!***************************************
! fin de la periode d observation
!***************************************

   IF (t > tdebobs+tobs) THEN
     cflmax=MAX(cflumaxmax,cflvmaxmax)
     IF (MASTER) THEN 
     WRITE(iscreen,*) 'ATTENTION  le ',cdate
     WRITE(iscreen,*) 'cflumaxmax=',cflumaxmax
     WRITE(iscreen,*) 'cflvmaxmax=',cflvmaxmax
     ENDIF

! Reinitialisation for the next observation period
     cflumaxmax=0.0_rsh
     cflvmaxmax=0.0_rsh
     tdebobs=t

     !IF (.NOT.Agrif_Root()) RETURN ! Only RootGrid can accelerate

     IF(cflmax < cflcrt*0.84_rsh) THEN
       IF (MASTER) THEN 
       WRITE(iscreen,*) 'TURBOOOOOOOOOOOOOOOOOO.......!!!!!! old dt=',dt
       ENDIF
       dt=MIN(dt*1.2_rlg,dtmax)
       halfdt=dt*0.5_rlg
       IF (MASTER) THEN 
       WRITE(iscreen,*) 'Nouveau dt : ',dt
       ENDIF
     ENDIF
   ENDIF


   !PRINT*,'exit DYN3DDT'

  END SUBROUTINE dyn3d_dt
