SUBROUTINE FLGDMX(KOBS,KMXVAL,KFLAGS)

!**** SUBROUTINE FLGDMX - GET THE MAXIMUM DATUM FLAG VALUE

!          HEIKKI JARVINEN   ECMWF          15/NOV/1995
!          DRASKO VASILJEVIC ECMWF          21/10/1999

!*    PURPOSE
!     -------

!        GET THE MAXIMUM DATUM FLAG VALUE

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

!     ***CALL FLGDMX(KOBS,KMXVAL,KFLAGS)
!     WHERE KOBS    = CURRENT OBSERVATION
!           KMXVAL  = VALUE OF THE MAXIMUM FLAG
!           KFLAGS  = VALUES OF ALL (5) FLAGS

!        FLGDMX IS CALLED BY VARIOUS SUBROUTINES

!*    METHOD
!     ------

!        CRACK THE PACKED DATUM FLAG WORD

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

!**   MODIFICATIONS
!     -------------
!        H. Jarvinen 12 Dec 1996 : A modified subroutine call
!        F. Taillefer   Oct 2007 : possible different use of this BL flag between MF and ECMWF
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0B  , ONLY : LECMWF
USE YOMCMBDY , ONLY : NCMAFFBP ,NCMAFFOC ,NCMAGFBP ,NCMAGFOC ,&
 & NCMADFBP ,NCMADFOC ,NCMAVFBP ,NCMAVFOC ,&
 & NCMABFBP ,NCMABFOC  
USE YOMLUN   , ONLY : NULERR
USE YOMSCREE , ONLY : NDFLMX   ,NDFLMN
USE YOMDB

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBS 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KMXVAL 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KFLAGS(5) 

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

INTEGER(KIND=JPIM) :: ICMFLG
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"

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

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('FLGDMX',0,ZHOOK_HANDLE)
!     ------------------------------------------------------------------

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

!*          1.1   VALUES

KMXVAL = NDFLMN
KFLAGS(:) = 0

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

!        2.       FIND THE MAXIMUM DATUM FLAGS
!                 ----------------------------

!*          2.0   RETRIEVE FLAG WORD

2 CONTINUE
ICMFLG  = ROBODY(KOBS,MDBFLG)

!*          2.1   FINAL FLAG

KFLAGS(1) = IBITS(ICMFLG, NCMAFFBP, NCMAFFOC)

!*          2.2   FIRST GUESS FLAG

KFLAGS(2) = IBITS(ICMFLG, NCMAGFBP, NCMAGFOC)

!*          2.3   DEPARTURE FLAG

KFLAGS(3) = IBITS(ICMFLG, NCMADFBP, NCMADFOC)

!*          2.4   VARIATIONAL QUALITY CONTROL FLAG

KFLAGS(4) = IBITS(ICMFLG, NCMAVFBP, NCMAVFOC)

!*          2.5   BLACKLIST FLAG

KFLAGS(5) = IBITS(ICMFLG, NCMABFBP, NCMABFOC)
! this flag is really used as blacklist flag by CANARI
IF (.NOT.LECMWF) KFLAGS(5) = 0

!*          2.6   MAXIMUM FLAG VALUE

!KMXVAL = MAXVAL(KFLAGS)
IF(.NOT.LECMWF) KFLAGS(5) = 0
KMXVAL = MAX(KFLAGS(1),KFLAGS(2),KFLAGS(3),KFLAGS(4),KFLAGS(5))

!*          2.7   SOMETHING IS NOT QUITE RIGHT

IF(KMXVAL < NDFLMN .OR. KMXVAL > NDFLMX) THEN
  WRITE(NULERR,'('' *** MAXIMUM FLAG OUT OF RANGE '')')
  CALL ABOR1('FLGDMX: MAXIMUM FLAG OUT OF RANGE')
ENDIF

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

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