SUBROUTINE PRE_PRSTA(LDFIRST,KLEN)

!**** *PRE_PRSTA*  -  Collect and print ifs observation screening statistics

!     Purpose.
!     --------
!           Collect and print ifs observation screening statistics.

!**   Interface.
!     ----------
!        *CALL* *PRE_PRSTA

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

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

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

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

!        GERSTA - GET REPORT STATUS
!        GEREVE - GET REPORT EVENTS
!        GEDSTA - GET DATUM STATUS
!        GEDEVE - GET DATUM EVENTS

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

!     Author.
!     -------
!        Heikki Jarvinen *ECMWF*

!     Modifications.
!     --------------
!        Original : 95-03-15
!        Modified : 
!        Elias Holm 20001009 : Use maximum variable number NVNUMAX for
!                              dimensioning NDOBVA, NDEPEX, and NDEPNE
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMANCS  , ONLY : RMDI     
USE YOMSCREE , ONLY :    NRDBRR   ,NRDBRD   ,&
 & NREVTOT  ,NDEVTOT  ,NDOBVA   ,NDEPEX   ,&
 & NDEPNE     
USE YOMNMEV  , ONLY : NOREV1   ,NODEV1
USE YOMDB
USE MPL_MODULE
USE OML_MOD, ONLY : OML_SET_LOCK, OML_UNSET_LOCK

IMPLICIT NONE

LOGICAL           ,INTENT(IN)    :: LDFIRST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN 
INTEGER(KIND=JPIM) :: ILIST(32),IRSTA(4),IDSTA(4)

INTEGER(KIND=JPIM) :: ICMVNM, ICOUNT,&
 & IEVENT, ILEV, IOBTYP, JNLV, JO, JOBS

INTEGER(KIND=JPIM) :: &
    &   I_NRDBRR (SIZE(NRDBRR ,DIM=1),SIZE(NRDBRR ,DIM=2)), &
    &   I_NREVTOT(SIZE(NREVTOT,DIM=1),SIZE(NREVTOT,DIM=2)), &
    &   I_NRDBRD (SIZE(NRDBRD ,DIM=1),SIZE(NRDBRD ,DIM=2)), &
    &   I_NDEVTOT(SIZE(NDEVTOT,DIM=1),SIZE(NDEVTOT,DIM=2)), &
    &   I_NDOBVA (SIZE(NDOBVA ,DIM=1),SIZE(NDOBVA ,DIM=2)), &
    &   I_NDEPNE (SIZE(NDEPNE ,DIM=1),SIZE(NDEPNE ,DIM=2)), &
    &   I_NDEPEX (SIZE(NDEPEX ,DIM=1),SIZE(NDEPEX ,DIM=2))

REAL(KIND=JPRB) :: ZCMOMF
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "ngedeve.intfb.h"
#include "ngedsta.intfb.h"
#include "ngereve.intfb.h"
#include "ngersta.intfb.h"

#include "fcobs.h"

#include "openmp_obs.h"

!*
!     ------------------------------------------------------------------

!*       1.      INITIALIZE
!                ----------

!*          1.1  INITIALIZE THE LOCAL ARRAYS

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

!*          1.2  INITIALIZE THE GLOBAL ARRAYS

IF(LDFIRST) THEN
  NRDBRR (:,:) = 0
  NREVTOT(:,:) = 0
  NRDBRD (:,:) = 0
  NDEVTOT(:,:) = 0
  NDOBVA (:,:) = 0
  NDEPNE (:,:) = 0
  NDEPEX (:,:) = 0
  IF (LHOOK) CALL DR_HOOK('PRE_PRSTA',1,ZHOOK_HANDLE)
  RETURN
ENDIF

I_NRDBRR (:,:) = 0
I_NREVTOT(:,:) = 0
I_NRDBRD (:,:) = 0
I_NDEVTOT(:,:) = 0
I_NDOBVA (:,:) = 0
I_NDEPNE (:,:) = 0
I_NDEPEX (:,:) = 0

!*
!     ------------------------------------------------------------------

!*       2.       COLLECT THE SCREENING INFORMATION
!                 ---------------------------------

!*          2.2   LOOP OVER THE OBSERVATIONS

OBS_LOOP: DO JOBS = 1, KLEN

  IOBTYP = ROBHDR(JOBS,MDBOTP)

!*          2.4   GET REPORT STATUS

  CALL NGERSTA(INT(ROBHDR(JOBS,MDBRST)),ICOUNT,IRSTA)

!*          2.5   COUNT FOR ACTIVE/PASSIVE/REJECTED/BLACKLISTED

  IF(IRSTA(1) == 1) I_NRDBRR(IOBTYP,1) = I_NRDBRR(IOBTYP,1) + 1
  IF(IRSTA(2) == 1) I_NRDBRR(IOBTYP,2) = I_NRDBRR(IOBTYP,2) + 1
  IF(IRSTA(3) == 1) I_NRDBRR(IOBTYP,3) = I_NRDBRR(IOBTYP,3) + 1
  IF(IRSTA(4) == 1) I_NRDBRR(IOBTYP,4) = I_NRDBRR(IOBTYP,4) + 1

!*          2.6   COUNT FOR THE REPORTS

  I_NRDBRR(IOBTYP,5) = I_NRDBRR(IOBTYP,5) + 1

!*          2.7   GET REPORT EVENTS

  CALL NGEREVE(INT(ROBHDR(JOBS,MDBREV1)),IEVENT,ILIST)

!*          2.8   COLLECT REPORT EVENTS OF DIFFERENT OBS.TYPES

  IF(IEVENT /= 0) THEN
    DO JO = 1, NOREV1
      IF(ILIST(JO) == 1) THEN
        I_NREVTOT(IOBTYP,JO) = I_NREVTOT(IOBTYP,JO) + 1
      ENDIF
    ENDDO
  ENDIF
!*
!     ------------------------------------------------------------------

!*       3.       LOOP OVER THE REPORT, DATUM BY DATUM
!                 ------------------------------------

  REP_LOOP: DO JNLV = MLNKH2B(JOBS),MLNKH2B(JOBS+1)-1
    ILEV = JNLV

!*          3.1   GET DATUM STATUS

    CALL NGEDSTA(INT(ROBODY(JNLV,MDBDSTA)),ICOUNT,IDSTA)

!*          3.2   COUNT FOR ACTIVE/PASSIVE/REJECTED/BLACKLISTED

    IF(IDSTA(1) == 1) I_NRDBRD(IOBTYP,1) = I_NRDBRD(IOBTYP,1) + 1
    IF(IDSTA(2) == 1) I_NRDBRD(IOBTYP,2) = I_NRDBRD(IOBTYP,2) + 1
    IF(IDSTA(3) == 1) I_NRDBRD(IOBTYP,3) = I_NRDBRD(IOBTYP,3) + 1
    IF(IDSTA(4) == 1) I_NRDBRD(IOBTYP,4) = I_NRDBRD(IOBTYP,4) + 1

!*          3.3   COUNT FOR THE DATA IN OBSERVATION TYPES

    I_NRDBRD(IOBTYP,5) = I_NRDBRD(IOBTYP,5) + 1

!*          3.4   COUNT FOR DATA IN OBSERVATION TYPES/VARIABLES

    ICMVNM = ROBODY(ILEV,MDBVNM)
        
    I_NDOBVA(IOBTYP,ICMVNM) = I_NDOBVA(IOBTYP,ICMVNM) + 1

!*             3.4.1   FIRST GUESS DEPARTURE EXIST/NONEXIST

    ZCMOMF = ROBODY(ILEV,MDBOMF)
    IF(ZCMOMF == RMDI) THEN
      I_NDEPNE(IOBTYP,ICMVNM) = I_NDEPNE(IOBTYP,ICMVNM) + 1
    ELSE
      I_NDEPEX(IOBTYP,ICMVNM) = I_NDEPEX(IOBTYP,ICMVNM) + 1
    ENDIF

!*          3.5   GET DATUM EVENTS

    CALL NGEDEVE(INT(ROBODY(ILEV,MDBDEV1)),IEVENT,ILIST)

!*          3.6   COUNT FOR DATUM EVENTS IN DIFFERENT OBS.TYPES

    IF(IEVENT /= 0) THEN
      DO JO = 1, NODEV1
        IF(ILIST(JO) == 1) THEN
          I_NDEVTOT(IOBTYP,JO) = I_NDEVTOT(IOBTYP,JO) + 1
        ENDIF
      ENDDO
    ENDIF
!*
!     ------------------------------------------------------------------

!*       4.        CLOSE THE LOOP OVER ...
!                  -----------------------
!*
!           4.1    ... THE REPORT

  ENDDO REP_LOOP
!*
!           4.2    ... THE OBSERVATION

ENDDO OBS_LOOP

!$ CALL OML_SET_LOCK()
NRDBRR (:,:) = NRDBRR (:,:) + I_NRDBRR (:,:)
NREVTOT(:,:) = NREVTOT(:,:) + I_NREVTOT(:,:)
NRDBRD (:,:) = NRDBRD (:,:) + I_NRDBRD (:,:)
NDEVTOT(:,:) = NDEVTOT(:,:) + I_NDEVTOT(:,:)
NDOBVA (:,:) = NDOBVA (:,:) + I_NDOBVA (:,:)
NDEPNE (:,:) = NDEPNE (:,:) + I_NDEPNE (:,:)
NDEPEX (:,:) = NDEPEX (:,:) + I_NDEPEX (:,:)
!$ CALL OML_UNSET_LOCK()

!*
!     ------------------------------------------------------------------

!*       6.    RETURN
!              ------

6 CONTINUE
IF (LHOOK) CALL DR_HOOK('PRE_PRSTA',1,ZHOOK_HANDLE)
END SUBROUTINE PRE_PRSTA
