SUBROUTINE ADDBGS

!**** *ADDBGS* - Routine to ADD FIRST GUESS IN SPECTRAL SPACE TO
!****             SP3 ARRAYS

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

!**   Interface.
!     ----------
!        *CALL* *ADDBGS*

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

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

!     Method.
!     -------

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

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

!     Author.
!     -------
!        W. HECKLEY  *ECMWF*

!     Modifications.
!     --------------
!        Original : 91-02-20
!        Modified 95-06-06 by L.Isaksen : Reordering of spectral arrays
!        Modified 95-12-12 by M.Hamrud  : Array syntax for vect. 
!        Modified 98-07-06 E.Andersson  : TOVS control variable
!        Modified 99-06-23 A. Horanyi   : ALADIN - mean wind 
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!-----------------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPRB, JPIM
USE YOMHOOK   ,ONLY : LHOOK, DR_HOOK
USE YOMMP    , ONLY : NPSP
USE YOMCT0   , ONLY : LELAM
USE YOMSP    , ONLY : SPA3, SPA2, SPA1, SPGFL
USE YOMSPJB  , ONLY : SP7A3, SP7A2, SP7A1, GP7A3
USE YOMVAR   , ONLY : LTOVSCV  ,LTSCV
USE YOMTVRAD , ONLY : TOVSCVX  ,TOVSCVBG
USE YOMANCS  , ONLY : RMDI
USE YOMGFL   , ONLY : GFL
USE YOMJG    , ONLY : N_SPJB_VARS, SPJB_VARS_INFO
USE YOMLUN   , ONLY : NULOUT
USE YOMDIM   , ONLY : NPROMA
USE YOMGEM   , ONLY : NGPTOT
USE YOMTS    , ONLY : TSCVX    ,TSCVBG

IMPLICIT NONE

INTEGER(KIND=JPIM) :: JFIELD, IPTBG, IPT, IBL, ICEND, JKGLO
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

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

!*    1.    ADD THE BACKGROUND TO THE SP3 ARRAYS
!           ------------------------------------

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

DO JFIELD=1,N_SPJB_VARS
  IPTBG = SPJB_VARS_INFO(JFIELD)%IPTBG
  IPT   = SPJB_VARS_INFO(JFIELD)%IPT

  IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA2) THEN

    IF (NPSP>0) THEN
      SPA2(:,IPT) = SPA2(:,IPT) + SP7A2(:,IPTBG)
    ENDIF

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA3) THEN

    SPA3(:,:,IPT) = SPA3(:,:,IPT) + SP7A3(:,:,IPTBG)

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPGFL) THEN

    SPGFL(:,:,IPT) = SPGFL(:,:,IPT) + SP7A3(:,:,IPTBG)

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_GPGFL) THEN

  DO JKGLO=1,NGPTOT,NPROMA
    ICEND=MIN(NPROMA,NGPTOT-JKGLO+1)
    IBL=(JKGLO-1)/NPROMA+1
    GFL(1:ICEND,:,IPT,IBL) = GFL(1:ICEND,:,IPT,IBL) + GP7A3(1:ICEND,:,IPTBG,IBL)
  ENDDO
!   GFL(:,:,IPT,:) = GFL(:,:,IPT,:) + GP7A3(:,:,IPTBG,:)

  ELSE

    WRITE (NULOUT,*) 'CAN''T MATCH BG FIELD WITH MODEL FIELD : ',JFIELD
    CALL ABOR1 ('CAN''T MATCH BG FIELD WITH MODEL FIELD')

  ENDIF
ENDDO

!     1-D FIELDS
IF (LELAM) THEN
  SPA1(:,:) = SPA1(:,:) + SP7A1(:,:)
ENDIF

!     TOVS CONTROL VARIABLE
IF (LTOVSCV) THEN
  WHERE(TOVSCVBG(:,:) /= RMDI)TOVSCVX(:,:)=TOVSCVX(:,:)+TOVSCVBG(:,:)
ENDIF

!     TS CONTROL VARIABLE
IF(LTSCV) THEN
  WHERE(TSCVBG(:) /= RMDI) TSCVX(:)=TSCVX(:)+TSCVBG(:)
ENDIF

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

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