SUBROUTINE STEPOTL(CDCONF)

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YEMDIM   , ONLY : LECOBI
USE YEMCT0   , ONLY : NECRIPL  ,NECOTL
USE YOMCT0   , ONLY : NCONF    ,NSTART   ,NSTOP    ,LOPDIS   ,LELAM    ,LRUBC
USE YOMCT3   , ONLY : NSTEP
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK
USE YOMLUN   , ONLY : NULOUT
USE YOMTIM   , ONLY : RSTART   ,RVSTART  ,RTIMEF

!**** *STEPOTL*  - Controls tangent linear integration at lowest level

!     Purpose.
!     --------
!     Controls the tangent linear integration.

!**   Interface.
!     ----------
!        *CALL* *STEPOTL(CDCONF)

!        Explicit arguments :
!        --------------------
!     CHARACTER*9 CDCONF
!   1 : configuration of IOPACK      write the trajectory
!   2 : configuration of TRANSINVH  inverse Legendre transform
!   2 : configuration of ETRANSINVH inverse North/South Fourier tr. (LAM)
!   3 : configuration of TRANSINVH  inverse Fourier transform
!   3 : configuration of ETRANSINVH inverse East/West Fourier tr. (LAM)
!   4 : configuration of CPCTL      lin. tan. grid point computations
!   5 : configuration of POSTL      post processing
!   6 : configuration of OBSVTL     comparison to observations
!   7 : configuration of TRANSDIRH  direct Fourier transform
!   7 : configuration of ETRANSDIRH direct East/West Fourier tr. (LAM)
!   8 : configuration of TRANSDIRH  direct Legendre transform
!   8 : configuration of ETRANSDIRH direct North/South Fourier tr. (LAM)
!   9 : configuration of SPC        spectral space computations
!   9 : configuration of ESPCH      spectral space computations (LAM)
!   9 : configuration of ESPAY/ESPAWR spectral space computations (LAM/NHS)

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

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

!     Externals.
!     ----------
!        Called by CNT4TL and NNMI2TL,NNMI3TL.

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

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

! Modifications
! -------------
!   Original    : 87-10-15
!   J N Thepaut : allow for obs in TL configuration.
!   M.Hamrud    : 95-10-30 New form for CDCONF
!   F. Bouttier : 95-12-28 Clean calls to OBSV using config string
!   M.Hamrud    : 97-02-24 Use OBSVTL instead of OBSV
!   D.Dent      : 97-10-02 Replace SECOND by USER_CLOCK 
!   M.Hamrud    : 00-10-25 implementation of transform package
!   C. Fischer  : 01-03-14 merge Aladin with Arp/IFS version
!   G. Radnoti  : 01-03-15 Aladin spectral transform package
!   J.Vivoda    : 03-2002  PC schemes for NH dynamics (LPC_XXXX keys)
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   10-Jun-2004 J. Masek   NH cleaning (LPC_NOTR)
!   01-Jul-2008 :B. Chapnik LL_CPL consistent between stepotl and ad
!   F. Vana     : 13-Jan-2009 Remove useless LSPC_FROM_DI key
!  EndModifications
!------------------------------------------------------------------------------

IMPLICIT NONE

CHARACTER(LEN=9)  ,INTENT(IN)    :: CDCONF 

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

INTEGER(KIND=JPIM) :: IJUM

LOGICAL :: LLCPL

REAL(KIND=JPRB) :: ZCT, ZJUM, ZVT, ZWT
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

INTERFACE
#include "user_clock.h"
END INTERFACE

#include "ecoupl1.intfb.h"
#include "espch.intfb.h"
#include "etransdirh.intfb.h"
#include "etransinvh.intfb.h"
#include "iopack.intfb.h"
#include "opdis.intfb.h"
#include "scan2htl.intfb.h"
#include "spch.intfb.h"
#include "transdirh.intfb.h"
#include "transinvh.intfb.h"

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

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

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

!*       0.    VARIOUS INITIALIZATIONS
!              -----------------------

call gstats(1889,0)
CALL USER_CLOCK(PELAPSED_TIME=ZWT,PVECTOR_CP=ZVT,PTOTAL_CP=ZCT)
ZCT=ZCT-RSTART
ZVT=ZVT-RVSTART
ZWT=ZWT-RTIMEF
RSTART=RSTART+ZCT
RVSTART=RVSTART+ZVT
RTIMEF=RTIMEF+ZWT
ZJUM=10._JPRB**(INT(LOG10(REAL(MAX(NSTOP-NSTART,1),JPRB)))+1)
IJUM=NINT(MAX(ZJUM/100._JPRB,1.0_JPRB))
IF(NSTEP-NSTART <= 10.OR.MOD(NSTEP,IJUM) == 0)THEN
  WRITE(NULOUT,'('' NSTEP ='',I6,'' STEPOTL '',A9)')&
   & NSTEP,CDCONF
ENDIF
call gstats(1889,1)

IF (LOPDIS) THEN
  CALL OPDIS(CDCONF,'STEPOTL ',ZCT,ZVT,ZWT,RSTART,RVSTART,RTIMEF)
ENDIF

!        0.1   Define configurations for coupling LBC in LAM

LLCPL = (LELAM.AND.LECOBI.AND.NECRIPL == 1.AND.&
 & .NOT.(LRUBC.AND.CDCONF(9:9) /= 'T')) &
 &.AND.&
 & ((CDCONF(3:3) == 'A'.OR.CDCONF(3:3) == 'B'.OR.&
 & CDCONF(6:6) == '1').AND.CDCONF(5:5) == '0')&
 & .AND.&
 & (NCONF/100 == 0.OR.NCONF/100 == 2.OR.&
 & NCONF/100 == 7.OR.NCONF == 801.OR. NECOTL < 0)


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

!*       1.    READ TRAJECTORY (NOT NORMALLY DONE HERE ANY LONGER).
!              ----------------------------------------------------

IF(CDCONF(1:1) /= '0')THEN

  CALL IOPACK(CDCONF(1:1))

ENDIF
!     ------------------------------------------------------------------

!*       2.    INVERSE TRANSFORMS.
!              -------------------

IF(CDCONF(2:2) /= '0')THEN

  IF (LELAM) THEN
    CALL ETRANSINVH(CDCONF)
  ELSE
    CALL TRANSINVH(CDCONF)
  ENDIF

ENDIF
!     ------------------------------------------------------------------

!*       3.    GRIDPOINT COMPUTATIONS.
!             ------------------------

IF(CDCONF(3:7) /= '00000') THEN

  CALL SCAN2HTL(CDCONF)

ENDIF
!     ------------------------------------------------------------------

!*       4.    JO COMPUTATION
!              --------------

!Moved to CNT4TL
!     ------------------------------------------------------------------

!*       8.    DIRECT TRANSFORMS.
!              ------------------

IF(CDCONF(8:8) /= '0')THEN

  IF (LELAM) THEN
    IF (LLCPL) CALL ECOUPL1
    CALL ETRANSDIRH(CDCONF)
  ELSE
    CALL TRANSDIRH(CDCONF)
  ENDIF

ENDIF
!     ------------------------------------------------------------------

!*       9.    SPECTRAL COMPUTATIONS.
!              ----------------------

IF(CDCONF(9:9) /= '0')THEN

  IF(.NOT.LELAM) THEN
    CALL SPCH(CDCONF(9:9))
  ELSE
    CALL ESPCH(CDCONF(9:9))
  ENDIF

ENDIF
!     ------------------------------------------------------------------

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

