SUBROUTINE CDSTA

!**** *CDSTA*  - START THE ADJOINT INTEGRATION

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

!**   Interface.
!     ----------
!        *CALL* *CDSTA

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

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

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

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

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

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

!     Modifications.
!     --------------
!        Original : 87-10-15
!        Modified :  91-08-29 P.Courtier IO scheme
!        Modified :  95-07-25 A. Horanyi for ALADIN
!        D. Dee   :  04-01-23 Variational bias correction
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        P.Burton :  29-09-04 P.Burton LVARBC cleanup
!        Y.Tremolet    23-Jul-2004 Model error
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0   , ONLY : NCONF    ,LELAM
USE YOMVRTL  , ONLY : L801TL
USE YOMTNEWT , ONLY : LIDMODEL
USE YOMGMV   , ONLY : GMV_DEPART,GMVS_DEPART
USE YOMGFL   , ONLY : GFL, GFL_DEPART
USE YOM_YGFL , ONLY : YQ, YO3, YAERO, NAERO, YGHG, NGHG, YGRG, NGRG
USE YOMVAR   , ONLY : LMODERR, LVARBC, LTOVSCV
USE VARBC_SETUP, ONLY : YVARBC, NOBGROUP, NOBPARAM
USE YOMMODEL_ERROR, ONLY : NDIM_MODERR, SPMODERR, GPMODERR, SPMODERR_HYB, GPMODERR_HYB, &
                        & NTYPE_MODERR
USE YOMCVA   , ONLY : NVAPARAM
USE YOMTVRAD , ONLY : TOVSCVX
USE GRIDPOINT_FIELDS_MIX, ONLY: ASSIGNMENT(=)
USE SPECTRAL_FIELDS_MOD, ONLY: ASSIGNMENT(=)

IMPLICIT NONE
INTEGER(KIND=JPIM) :: jj, jg, JAERO, JGHG, JGRG
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "suespe0.intfb.h"
#include "suspe0.intfb.h"

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

!*       1.    SET TO 0  Spectral array.
!              -------------------------

IF (LHOOK) CALL DR_HOOK('CDSTA',0,ZHOOK_HANDLE)
IF (NCONF/100 /= 4 .AND. NCONF/100 /= 6 .AND. .NOT. L801TL) THEN
  CALL SUSPE0
  IF (LELAM) CALL SUESPE0

  IF (YQ%LGP)  GFL(:,:,YQ%MP,:)=0.0_JPRB
  IF (YO3%LGP) GFL(:,:,YO3%MP,:)=0.0_JPRB
   
  DO JAERO=1,NAERO
   IF(YAERO(JAERO)%LGP) GFL(:,:,YAERO(JAERO)%MP,:)=0.0_JPRB
  ENDDO
  DO JGHG=1,NGHG
     IF(YGHG(JGHG)%LGP) GFL(:,:,YGHG(JGHG)%MP,:)=0.0_JPRB
  ENDDO
  DO JGRG=1,NGRG
     IF(YGRG(JGRG)%LGP) GFL(:,:,YGRG(JGRG)%MP,:)=0.0_JPRB
  ENDDO

  IF (LMODERR) THEN
    DO jj=1,NDIM_MODERR
      SPMODERR(jj)=0.0_JPRB
      GPMODERR(jj)=0.0_JPRB
    ENDDO
    IF (NTYPE_MODERR == 5) THEN
      DO jj=1,NDIM_MODERR
        SPMODERR_HYB(jj)=0.0_JPRB
        GPMODERR_HYB(jj)=0.0_JPRB
      ENDDO
    ENDIF
  ENDIF
ENDIF

IF (LIDMODEL) THEN
  IF(.NOT.ALLOCATED(GMV_DEPART)) CALL ABOR1('CDSTA:GMV_DEPART NOT ALLOCATED')
  GMV_DEPART (:,:,:,:) = 0.0_JPRB
  GMVS_DEPART(:,:,:)   = 0.0_JPRB
  GFL_DEPART (:,:,:,:) = 0.0_JPRB
ENDIF
IF (LVARBC) THEN
  IF (NOBPARAM/=NVAPARAM) &
   & CALL ABOR1('CDSTA: Parameter section of control variable wrong length')  
  DO jg = 1, NOBGROUP
    YVARBC(jg)%APARAMS(:) = 0.0_JPRB
  ENDDO
ENDIF
IF (LTOVSCV) TOVSCVX=0.0_JPRB

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

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