SUBROUTINE CNT3AD

!**** *CNT3AD*  - LEVEL 3 CONTROL ROUTINE OF THE ADJOINT MODEL

!     Purpose.
!     --------

!**   Interface.
!     ----------
!        *CALL* *CNT3AD

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

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

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

!     Externals.
!     ----------
!      Called by SU3YOM, CDSTA, CNT4AD, COSJG, SPNORM, FLTRGAD,
!                CNMIAD, FLTMODE, COSJC.
!      Called by TESADJ, ...

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

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

!     Modifications.
!     --------------
!        Original : 87-10-15
!        Modified : 91-09-25 p.c and J.N.T - remove useless logic and prepare
!                                            filter initial state adjoint
!                                          - Jg and Jc are now called at this
!                                            level.
!        Modified : 91-10-10 W Heckley     - option to restict contributions
!                                            to gradient to ROSSBY modes only
!        Modified : 92-10-19 Bill Heckley - reduce gravity mode amplitude
!                                           to aid 3/4DVAR conditioning
!        Modified : 95-01-20 Mike Fisher  - don't call COSJG if LBEQLTL
!        D. Giard : 99-07-19 LINITER moved from YOMNMIA to YOMINI
!        Bouttier : 00-06-12 protect against clouds
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        G. Radnoti:   12-08-2004: fix Eulerian COSJC
!     ------------------------------------------------------------------

USE PARKIND1 , ONLY : JPRB
USE YOMHOOK  , ONLY : LHOOK,   DR_HOOK
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0   , ONLY : NCONF    ,LSLAG    ,LTWOTL
USE YOMNMIA  , ONLY : LNMIRQ   
USE YOMVAR   , ONLY : LJC
USE YOMLCZ   , ONLY : LFORCE   ,YSPFORCE
USE YOEPHY   , ONLY : LEPHYS
USE YOEPHLI  , ONLY : LPHYLIN
USE TESTVAR_MIX  , ONLY : LTESTADJM
USE GFL_SUBS_MOD , ONLY : DEACT_CLOUD_GFL
USE GMV_SUBS_MOD , ONLY : SETUP_GMV

IMPLICIT NONE

LOGICAL :: LLSLAG, LLTWOTL
LOGICAL :: LLPHYLIN
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cain.intfb.h"
#include "cdsta.intfb.h"
#include "cnmiad.intfb.h"
#include "cnt4ad.intfb.h"
#include "cosjc.intfb.h"
#include "su3yom.intfb.h"

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

!*       1.    Initialize LEVEL 3 COMMONS, check and start.
!              --------------------------------------------

IF (LHOOK) CALL DR_HOOK('CNT3AD',0,ZHOOK_HANDLE)
WRITE(UNIT=NULOUT,FMT='('' START CNT3AD'')')
CALL SU3YOM
IF (LEPHYS) THEN
  CALL DEACT_CLOUD_GFL
ENDIF

!IF (YGFL%NUMGPFLDS>0) &
! & CALL ABOR1('ERROR CNT3AD - upper air GP fields not yet supported by AD model')  

IF (.NOT.LTESTADJM) CALL CDSTA
LLPHYLIN=LPHYLIN
LPHYLIN=.TRUE.
!      -----------------------------------------------------------

!*       5.    ADJOINT INTEGRATION.
!              --------------------

CALL CNT4AD

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

!*       4.    FILTERING
!              ---------

!*       4.4   Forcing.
!              ---------------

IF(LFORCE) THEN
  CALL CAIN(YSPFORCE)
ENDIF

!*       4.3   Jc COMPUTATION.
!              ---------------

IF (NCONF == 131) THEN
  IF (LJC) THEN

!   Force eulerian
    LLSLAG =LSLAG
    LLTWOTL=LTWOTL

    LSLAG  =.FALSE.
    LTWOTL =.FALSE.
    CALL SETUP_GMV

    CALL COSJC

!   Reset
    LSLAG  =LLSLAG
    LTWOTL =LLTWOTL
    CALL SETUP_GMV
  ENDIF
ENDIF

!*       4.2   HOUGH FILTERING OF INCREMENTS
!              -----------------------------

!        moved up to SIM4D.

!*       4.1   NORMAL MODE INITIALIZATION ADJOINT.
!              -----------------------------------

IF(LNMIRQ) THEN
  CALL CNMIAD
ENDIF

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

LPHYLIN=LLPHYLIN
WRITE(UNIT=NULOUT,FMT='('' END CNT3AD'')')
IF (LHOOK) CALL DR_HOOK('CNT3AD',1,ZHOOK_HANDLE)

END SUBROUTINE CNT3AD
