SUBROUTINE NGENADA(KOBS,KNADA)

!**** SUBROUTINE GENADA - GET NUMBER OF ACTIVE DATA IN THE REPORT

!          HEIKKI JARVINEN   ECMWF          15/MAR/1995

!*    PURPOSE
!     -------

!        GET NUMBER OF ACTIVE DATA IN THE REPORT

!**   INTERFACE
!     ---------

!     ***CALL NGENADA(KOBS,KNADA)
!     WHERE KOBS  = CURRENT REP POINTER
!           KNADA   = NUMBER OF ACTIVE DATA IN THE REPORT

!        NGENADA IS CALLED BY SUBROUTINE REDUN

!*    METHOD
!     ------

!        LOOP OVER THE REPORT AND COLLECT THE NUMBER OF ACTIVE DATA

!*    EXTERNALS
!     ---------

!        NGEDSTA  - GET DATUM STATUS

!**   MODIFICATIONS
!     -------------
!        H. Jarvinen  12-12-1996 : Renew the subroutine call
!        E. Andersson 16-11-2000 : Vectorize with ODB
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMDB
USE YOMCMBDY , ONLY : NCMDACBP ,NCMDACOC ,NCMDPABP ,NCMDPAOC ,&
 & NCMDREBP ,NCMDREOC ,NCMDBLBP ,NCMDBLOC   
USE YOMCMHDR , ONLY : NCMRPABP, NCMRPAOC

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBS 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KNADA 
INTEGER(KIND=JPIM) :: JL

LOGICAL :: LLACT, LLPAS, LLREJ, LLBLA, LLREPPAS
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "fcobs.h"
#include "openmp_obs.h"
!*
!     ------------------------------------------------------------------

!        1.       LOOP OVER THE REPORT, DATUM BY DATUM
!                 ------------------------------------
IF (LHOOK) CALL DR_HOOK('NGENADA',0,ZHOOK_HANDLE)
KNADA = 0

LLREPPAS = IBITS(INT(ROBHDR(KOBS,MDBRST)), NCMRPABP, NCMRPAOC) == 1
IF(ASSOCIATED(MLNKH2B))THEN
  DO JL=MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1
    LLACT = IBITS(INT(ROBODY(JL,MDBDSTA)), NCMDACBP, NCMDACOC) == 1
    LLPAS = IBITS(INT(ROBODY(JL,MDBDSTA)), NCMDPABP, NCMDPAOC) == 1
    LLREJ = IBITS(INT(ROBODY(JL,MDBDSTA)), NCMDREBP, NCMDREOC) == 1
    LLBLA = IBITS(INT(ROBODY(JL,MDBDSTA)), NCMDBLBP, NCMDBLOC) == 1

    IF((LLREJ .OR. LLBLA) .AND. .NOT.LLACT) CYCLE

    KNADA=KNADA+1
  ENDDO
ENDIF

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