SUBROUTINE MWAVE_PUT_TL( PMWAVE_TB_BUF_TL,KMWAVE_BUF)

!*****MWAVE_PUT_TL* - 4D-VAR RAIN ASSIMILATION 

!     P. Bauer, P. Lopez, D. Salmond   ECMWF    Aug 2006

!     PURPOSE.
!     -------
!     PUT BRIGHTNESS TEMPERATURE_TL FROM GRID-POINT SPACE TO ODB

!**   INTERFACE.
!**   ---------
!**   *CALL* * MWAVE_PUT_TL()*  (FROM GP_MODEL_TL)

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

!     AUTHOR: Deborah Salmond
!     DATE  : 01-08-2006

!     Modifications :
!     23 April 2008   A. Geer   Extension to multiple sensors
!     01 Oct   2008   A. Geer   Initialise some more variables here
!     10 Jun   2009   A. Geer   Allow passive monitoring; per-channel errors

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDB
USE YOMVNMB  , ONLY : NVNUMB
USE YOMNMEV  , ONLY : NR1EVENT
USE YOMECTAB , ONLY : NTSLOTNOS,NTSLOTNOE
USE YOMGEM   , ONLY : NGPTOT
USE YOMMWAVE , ONLY : N_SATSENS, MWAVE_ID, MWAVE_OK, &
                    & MWAVE_REJECTOBS
USE PARMWAVE , ONLY : NMAX_CHAN

IMPLICIT NONE

REAL(KIND=JPRB)   ,INTENT(IN)   :: PMWAVE_TB_BUF_TL(NGPTOT,NMAX_CHAN,N_SATSENS)
INTEGER(KIND=JPIM),INTENT(IN)   :: KMWAVE_BUF(NGPTOT,NMAX_CHAN,N_SATSENS)

INTEGER(KIND=JPIM) :: JOBS
INTEGER(KIND=JPIM) :: IRET, IVNM, ICHAN, ILEN, JBODY, IBODY, ICH
INTEGER(KIND=JPIM) :: INFO(2)
INTEGER(KIND=JPIM) :: ICMBDY
INTEGER(KIND=JPIM) :: ISAT, JROF
LOGICAL :: LLMWAVE_OK
REAL(KIND=JPRB) :: ZINFO(0)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "getdb.h"
#include "putdb.h"
END INTERFACE

#include "fcobs.h"
#include "openmp_obs.h"

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

IF ( N_SATSENS == 0 ) THEN
  IF (LHOOK) CALL DR_HOOK('MWAVE_PUT_TL',1,ZHOOK_HANDLE)
  RETURN
ENDIF

INFO(1)=NTSLOTNOS
INFO(2)=NTSLOTNOE

CALL GETDB('MWAVE_PUT_SSMI',0, ILEN, INFO, 2, ZINFO, 0, &
 & -1, -1, -1, -1, -1, -1)  

DO JOBS = 1,ILEN

  ! Find satellite index in our local table
  DO ISAT=1,N_SATSENS
    IF(ROBHDR(JOBS,MDB_SATID_AT_SAT) == MWAVE_ID(ISAT) % BUFR_ID ) EXIT
  ENDDO

  ! Grid point index
  JROF = ROBHDR(JOBS,MDB_GP_NUMBER)

  ! Certain error conditions will reject whole observation
  LLMWAVE_OK=.TRUE.
  DO ICH=1,MWAVE_ID (ISAT) % NCHANNELS
    IF( MWAVE_REJECTOBS(KMWAVE_BUF (JROF,ICH,ISAT)) ) THEN

      LLMWAVE_OK=.FALSE.
      ROBHDR (JOBS,MDBRST)  = ZCHSTAT_REJECT (ROBHDR (JOBS,MDBRST))
      ROBHDR (JOBS,MDBREV1) = ZCHEV1 (ROBHDR (JOBS,MDBREV1),NR1EVENT(16))
      EXIT
    ENDIF 
  ENDDO

  ICMBDY=MLNKH2B(JOBS+1)-MLNKH2B(JOBS)
  DO JBODY=1,ICMBDY
    IBODY = MLNKH2B(JOBS)+(JBODY-1)
    IVNM  = ROBODY(IBODY,MDBVNM)
    IF (IVNM == NVNUMB(50)) THEN          
      ICHAN = ROBODY(IBODY,MDBPPP)

      DO ICH=1,NMAX_CHAN
        IF(ICHAN == ICH)THEN

          ! Status flag
          ROBODY(IBODY,MDB_DATUM_TBFLAG) = KMWAVE_BUF(JROF,ICH,ISAT)  

          IF ( MWAVE_OK(KMWAVE_BUF(JROF,ICH,ISAT)) ) THEN
            ROBODY (IBODY,MDB_TBVALUETL) = PMWAVE_TB_BUF_TL  (JROF,ICH,ISAT)    ! TB FG
          ENDIF

          IF (.NOT. LLMWAVE_OK .OR. (.NOT. MWAVE_OK(KMWAVE_BUF(JROF,ICH,ISAT)))) THEN 
            ! Reject body entry
            ROBODY(IBODY,MDBDSTA) = ZCHSTAT_REJECT( ROBODY(IBODY,MDBDSTA))
          ENDIF

          EXIT
        ENDIF
      ENDDO
    ENDIF
  ENDDO
ENDDO
  
CALL PUTDB('MWAVE_PUT_SSMI', 0, IRET, INFO, 0, ZINFO, 0)

IF (LHOOK) CALL DR_HOOK('MWAVE_PUT_TL',1,ZHOOK_HANDLE)

END SUBROUTINE MWAVE_PUT_TL
