SUBROUTINE SUTSTEP(PTSTEP_OPT)

!**** *SUTSTEP*   - Computes the "optimal" timestep according to the resolution.

!     Purpose.
!     --------
!        Computes the "optimal" timestep according to the resolution.
!        This "optimal" timestep depends on the horizontal and vertical
!        resolution, and also on the type of advection scheme used.

!**   Interface.
!     ----------
!        *CALL* *SUTSTEP

!        Explicit arguments :
!        --------------------

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        K. YESSAD (METEO-FRANCE/CNRM/GMAP)

!     Modifications.
!     --------------
!        Original : Dec 2003.
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE PARDIM   , ONLY : JPMXLE
USE YOMDIM   , ONLY : NFLEVG   ,NSMAX
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0   , ONLY : LSLAG    ,LTWOTL
USE YOMGEM   , ONLY : RSTRET   ,RNLGINC

IMPLICIT NONE

REAL(KIND=JPRB),INTENT(OUT) :: PTSTEP_OPT

!     ------------------------------------------------------------------

REAL(KIND=JPRB) :: ZCOVE0(1:JPMXLE)
REAL(KIND=JPRB) :: ZCOHO,ZCOVE,ZCOADV
REAL(KIND=JPRB) :: ZTSTEP_OPT,ZNSTEP003
INTEGER(KIND=JPIM) :: IMXLE,INSTEP003

REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SUTSTEP',0,ZHOOK_HANDLE)

!     ------------------------------------------------------------------

!*       1.    Set default values.
!              -------------------

! * coefficient depending on the vertical resolution.
ZCOVE0(1:31)=22._JPRB
ZCOVE0(32:40)=21._JPRB
IMXLE=MAX(41,JPMXLE)
ZCOVE0(41:IMXLE)=20._JPRB
ZCOVE=ZCOVE0(NFLEVG)/22._JPRB

! * coefficient depending on the horizontal resolution.
ZCOHO=3._JPRB/(REAL(NSMAX,JPRB)*RSTRET*(2._JPRB+RNLGINC))

! * coefficient depending on the advection scheme.
IF (LSLAG) THEN
  IF (LTWOTL) THEN
    ZCOADV=600000.
  ELSE
    ZCOADV=300000.
  ENDIF
ELSE
  ZCOADV=75000.
ENDIF

! * first guess for optimal timestep.
ZTSTEP_OPT=ZCOHO*ZCOVE*ZCOADV
ZTSTEP_OPT=REAL(INT(ZTSTEP_OPT),JPRB)

! * the timestep is modified in order to match the following criteria:
!   a/ 10800/timestep is (as close as possible to) an integer.
!   b/ the timestep is <= 1 hour.
!   c/ the timestep has no more than 2 decimals.
ZNSTEP003=10800._JPRB/ZTSTEP_OPT
INSTEP003=MAX(3,INT(ZNSTEP003))
ZTSTEP_OPT=10800._JPRB/REAL(INSTEP003,JPRB)
PTSTEP_OPT=0.01_JPRB*REAL(NINT(100._JPRB*ZTSTEP_OPT),JPRB)

! * print this optimal timestep.
WRITE(UNIT=NULOUT, &
 & FMT='('' PTSTEP_OPT (optimal timestep computed in SUTSTEP) '',E14.8)') &
 & PTSTEP_OPT

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SUTSTEP',1,ZHOOK_HANDLE)
END SUBROUTINE SUTSTEP
