SUBROUTINE DRIBUIN(KDRIP,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE DRIBUIN - PROCESS INPUT DRIBU

!        D. VASILJEVIC     ECMWF     13/12/00 (original 04/07/94)

!     PURPOSE
!     -------

!        PROCESS INPUT DRIBU

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

!        CALL DRIBUIN(KDRIP,
!                     KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,
!                     LDMESAGE,LDTAKEN)
!     WHERE:
!            KDRIP   = REPORT POINTER
!            KOBTYP  = CMA OBS. TYPE
!            KCDTYP  = CMA CODE TYPE
!            KOBSQNO = CMA OBS. TYPE SQ. NO.
!            KCDSQNO = CMA CODE TYPE SQ. NO.
!            LDMESAGE= OUTPUT MESSAGE SWITCH  
!            LDTAKEN = REPORT TAKEN ("CMA" REPORT CREATED) SWITCH  

!        DRIBUIN IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

!     EXTERNALS
!     ---------

!        PRLMCHK  - PRELIMINARY CHECKS
!        GETSETE  - GET/SET BODY ENTRY
!        ERRSTAT  - OBS. ERROR
!        DRIBUBE  - UPDATE DRIBU BODY ENTRY
!        PPVAFL   - PRESSURE/VARIABLE FLAG RESET
!        OBINSTP  - INTSTRUMENT TYPE
!        OBSCHAR  - OBS. CHARACTERISTICS

!     MODIFICATIONS
!     -------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMDB
USE YOMCMBDY , ONLY : NCMBPFBP ,NCMBVFBP ,NCMBFLBP ,NCMBFLOC
USE YOMANCS  , ONLY : RMDI     ,RDEGREES ,RADIANS  ,EPSMAC   ,RPAPM    ,RDANMOMH ,RDBAROMH ,RUPMLIM  ,RDWMLIM  
USE YOMCST   , ONLY : RTT
USE YOMVNMB  , ONLY : NVNUMB
USE YOMNMCOD , ONLY : NPRESCD  ,NPRESVC  ,NHEIGVC
USE YOMNMEV  , ONLY : ND1EVENT ,ND2EVENT
USE YOMMKODB , ONLY : LERRPERS ,NMKCMVSE ,LTOTREM  ,L_DRIBU_PS_BIAS
USE YOETHF   , ONLY : RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES  
USE YOMLUN   , ONLY : NULOUT   
USE LOCAL_TRAFOS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KDRIP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBSQNO 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDSQNO 
LOGICAL           ,INTENT(INOUT) :: LDMESAGE 
LOGICAL           ,INTENT(OUT)   :: LDTAKEN 
!-----------------------------------------------------------------------

INTEGER(KIND=JPIM) :: IOBDATE, &
 & IOBTIME, IPFLG, &
 & IVNM, IPCODE, IPSTAFLG, IPMSLFLG, IAUX, &
 & JDDFF, IVECFL, IVERCO, &
 & ISTID, INSTYP, IREPST  

INTEGER(KIND=JPIM) :: IDD, &
 & IDDSTAT, IDDEVEN1, IDDEVEN2, IDDRDBFL, IDDANFLG, &
 & IDDPFLG, IDDFLG  
INTEGER(KIND=JPIM) :: IFF, &
 & IFFSTAT, IFFEVEN1, IFFEVEN2, IFFRDBFL, IFFANFLG, &
 & IFFPFLG, IFFFLG  
INTEGER(KIND=JPIM) :: JU10M, IU10M, &
 & IUUSTAT, IUUEVEN1, IUUEVEN2, IUURDBFL, IUUANFLG, &
 & IUUPFLG, IUUFLG  
INTEGER(KIND=JPIM) :: JV10M, IV10M, &
 & IVVSTAT, IVVEVEN1, IVVEVEN2, IVVRDBFL, IVVANFLG, &
 & IVVPFLG, IVVFLG  
INTEGER(KIND=JPIM) :: JT2M, IT2M, &
 & ITTSTAT, ITTEVEN1, ITTEVEN2, ITTRDBFL, ITTANFLG, &
 & ITTPFLG, ITTFLG  
INTEGER(KIND=JPIM) :: JZ, IZ, &
 & IZZSTAT, IZZEVEN1, IZZEVEN2, IZZRDBFL, IZZANFLG, &
 & IZZPFLG, IZZFLG  
INTEGER(KIND=JPIM) :: JPs, IPs, &
 & IPSSTAT, IPSEVEN1, IPSEVEN2, IPSRDBFL, IPSANFLG, &
 & IPSPFLG, IPSFLG  
INTEGER(KIND=JPIM) :: JALL, &
 & IAUSTAT, IAUEVEN1, IAUEVEN2, IAURDBFL, IAUANFLG, &
 & IAUPFLG, IAUFLG  , IAUVERCO  
INTEGER(KIND=JPIM) :: IDBRDFL

LOGICAL :: LLFAIL, LLREMOVE, LLMSLP, LLSTAP
LOGICAL :: LLPs, LLT2M, LLTd2M, LLDD, LLFF, LLU10m, LLV10m, &
 & LLRH2m, LLPt, LLSDD, LLSFF, LLZ  

CHARACTER (LEN = 8) :: CLSTID

REAL(KIND=JPRB) :: ZLAT, ZLON, &
 & ZPRES, ZTIMDIF, ZANMOMH, ZBAROMH, &
 & ZWEIGHT, ZPSTA, ZPMSL, &
 & ZVERCO, ZPRESAU, ZHEAUX, ZGEOP  

REAL(KIND=JPRB) :: ZDIR, ZDDPRES, ZDDPRL, &
 & ZDDPERR, ZDDOERR, ZDDFERR, ZDDRERR, &
 & ZDDFGER, ZDDFGCC1, ZDDFGCC2, &
 & ZDDFGDEP, ZDDANDEP  
REAL(KIND=JPRB) :: ZSPD, ZFFPRES, ZFFPRL, &
 & ZFFPERR, ZFFOERR, ZFFFERR, ZFFRERR, &
 & ZFFFGER, ZFFFGCC1, ZFFFGCC2, &
 & ZFFFGDEP, ZFFANDEP  
REAL(KIND=JPRB) :: ZU, ZUUPRES, ZUUPRL, &
 & ZUUPERR, ZUUOERR, ZUUFERR, ZUURERR, &
 & ZUUFGER, ZUUFGCC1, ZUUFGCC2, &
 & ZUUFGDEP, ZUUANDEP  
REAL(KIND=JPRB) :: ZV, ZVVPRES, ZVVPRL, &
 & ZVVPERR, ZVVOERR, ZVVFERR, ZVVRERR, &
 & ZVVFGER, ZVVFGCC1, ZVVFGCC2, &
 & ZVVFGDEP, ZVVANDEP  
REAL(KIND=JPRB) :: ZT2M, ZTTPRES, ZTTPRL, &
 & ZTTPERR, ZTTOERR, ZTTFERR, ZTTRERR, &
 & ZTTFGER, ZTTFGCC1, ZTTFGCC2, &
 & ZTTFGDEP, ZTTANDEP  
REAL(KIND=JPRB) :: ZZZ, ZZZPRES, ZZZPRL, &
 & ZZZPERR, ZZZOERR, ZZZFERR, ZZZRERR, &
 & ZZZFGER, ZZZFGCC1, ZZZFGCC2, &
 & ZZZFGDEP, ZZZANDEP  
REAL(KIND=JPRB) :: ZPS, ZPSPRES, ZPSPRL, &
 & ZPSPERR, ZPSOERR, ZPSFERR, ZPSRERR, &
 & ZPSFGER, ZPSFGCC1, ZPSFGCC2, &
 & ZPSFGDEP, ZPSANDEP, ZPSBIAS
REAL(KIND=JPRB) :: ZAU, ZAUPRES, ZAUPRL, &
 & ZAUPERR, ZAUOERR, ZAUFERR, ZAURERR, &
 & ZAUFGER, ZAUFGCC1, ZAUFGCC2, &
 & ZAUFGDEP, ZAUANDEP  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "dribube.intfb.h"
#include "errstat.intfb.h"
#include "finoerr.intfb.h"
#include "getsete.intfb.h"
#include "obinstp.intfb.h"
#include "ppvafl.intfb.h"
#include "prlmchk.intfb.h"
#include "z2picao.intfb.h"

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

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

!        1.             PRESET
!                       ------

IF (LHOOK) CALL DR_HOOK('DRIBUIN',0,ZHOOK_HANDLE)
ZLAT   = ROBHDR(KDRIP,MDBLAT) * RDEGREES
ZLON   = ROBHDR(KDRIP,MDBLON) * RDEGREES
IOBDATE= ROBHDR(KDRIP,MDBDAT)
IOBTIME= ROBHDR(KDRIP,MDBETM)
CLSTID = TRANSFER(ROBHDR(KDRIP,MDBSID), CLSTID)
ISTID  = 0
READ(CLSTID(1:8),'(A8)') ISTID
IREPST = ROBHDR(KDRIP,MDBRST)

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

!        2.             GENERAL OBSERVATION INFO.
!                       -------------------------

!*          2.1         DEFINE INSTRUMENT TYPE

INSTYP  = 0
CALL OBINSTP(ZLAT,ZLON,ISTID,&
 & KOBTYP,KCDTYP,&
 & INSTYP)  

!*          2.2         SET INSTRUMENT TYPE INTO OBSERVATION
!                       CHARACTERISTICS

ROBHDR(KDRIP,MDB_INSTTYPE_AT_HDR) = INSTYP

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

!        3.             PRELIMINARY CHECKS
!                       ------------------

!*          3.1         REPORT PARAMETERS

LLFAIL   = .FALSE.
LLREMOVE = .FALSE.
CALL PRLMCHK(KOBTYP,KCDTYP,ZLAT,ZLON,IOBDATE,IOBTIME,IREPST,ZTIMDIF,&
 & LDMESAGE,LLFAIL,LLREMOVE)  

!*          3.1.0       UPDATE REPORT STATUS

ROBHDR(KDRIP,MDBRST) = IREPST

!*          3.1.1       FAILED REPORT

IF(LLFAIL  ) THEN
  IF(LTOTREM) GO TO 6
ENDIF

!*          3.1.2       REMOVED REPORT

IF(LLREMOVE) THEN
  IF(LTOTREM) GO TO 6
ENDIF

!*          3.1.3       ANEMOMETER/BAROMETER HEIGHT

ZANMOMH  = RDANMOMH
ZBAROMH  = RDBAROMH

!*          3.1.3.2     ASSIGN THE VALUE

IF(ROBHDR(KDRIP,MDBHOAND) /=RMDI ) THEN
  ZANMOMH = ROBHDR(KDRIP,MDBHOAND)
ENDIF

!*          3.1.3.3     REPLACE THE VALUE

ROBHDR(KDRIP,MDBHOAND) = ZANMOMH
ROBHDR(KDRIP,MDBHOBAD) = ZBAROMH

!*          3.2         BODY PARAMETERS

!*          3.2.1       WE ACCEPT: Ps, T2m, DD, FF, U10m, V10m AND Z
!                       (PLATFORM SPEED, DIRECTION, Pt, Rh10m, Td2m
!                        MAY BE PRSENT BUT NOT USED)

LLPs   = .FALSE.
LLT2M  = .FALSE.
LLTd2M = .FALSE.
LLDD   = .FALSE.
LLFF   = .FALSE.
LLU10m = .FALSE.
LLV10m = .FALSE.
LLRH2m = .FALSE.
LLPt   = .FALSE.
LLSDD  = .FALSE.
LLSFF  = .FALSE.
LLZ    = .FALSE.
ALL_DATA_LOOP: DO JALL = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(     ROBODY(JALL  ,MDBVNM) == NVNUMB( 47)) THEN
    LLPs    = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 10)) THEN
    LLT2M   = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 48)) THEN
    LLDD    = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 49)) THEN
    LLFF    = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 56)) THEN
    LLU10m  = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 57)) THEN
    LLV10m  = .TRUE.
    GO TO 322
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB(  3)) THEN
    LLZ     = .TRUE.
    GO TO 322
  ELSE
    IF(     ROBODY(JALL  ,MDBVNM) == NVNUMB( 11)) THEN
      LLTd2M  = .TRUE.
      GO TO 3310
    ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB(  7)) THEN
      LLRH2m  = .TRUE.
      GO TO 3310
    ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 13)) THEN
      LLPt    = .TRUE.
      GO TO 3310
    ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 36)) THEN
      LLSDD   = .TRUE.
      GO TO 3310
    ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 37)) THEN
      LLSFF   = .TRUE.
      GO TO 3310
    ENDIF

!*          3.3.1.0     UNEXPECTED VARIABLE

!    WRITE(NULOUT,'('' DRIBUIN; UNEXPECTED VARIABLE: '',I10)') INT(ROBODY(JALL,MDBVNM))
!    LDMESAGE = .TRUE.
    3310 CONTINUE
    IAUX = JALL
    IVNM = ROBODY(IAUX  ,MDBVNM) 

!*          3.2.1.1     SET/GET

    CALL GETSETE(IAUX ,&
     & ZAUPRES,ZAUPRL,ZAU ,&
     & ZAUFGER,ZAUFGCC1,ZAUFGCC2,&
     & ZAUPERR,ZAUOERR,ZAUFERR,ZAURERR,&
     & ZAUFGDEP,ZAUANDEP,&
     & IAUSTAT,IAUEVEN1,IAUEVEN2,IAURDBFL,IAUANFLG,&
     & IAUFLG,IAUPFLG)  

    ZAUPRES = ROBODY(IAUX,MDBPPP)
    IAUVERCO= ROBODY(IAUX,MDBVCO)

!*          3.2.1.2     VARIABLE

    ZAU     = ROBODY(IAUX,MDBVAR)

!*          3.2.1.3     ERROR STATISTICS

!*          3.2.1.4     STATUS, EVENTS, FLAGS

    IAUSTAT = ICHSTAT_REJECT( IAUSTAT )
    IAUEVEN1= ICHEV1(IAUEVEN1,ND1EVENT(17))

!*          3.2.1.5     DEPARTURES

!*          3.2.1.6     UPDATE BODY ENTRY

    CALL DRIBUBE(IAUX    ,&
     & IVNM    ,IAUVERCO,ZAUPRES ,ZAUPRL  ,ZAU     ,&
     & ZAUOERR ,ZAUPERR ,ZAUFERR ,ZAURERR ,&
     & ZAUFGER ,ZAUFGCC1,ZAUFGCC2,&
     & ZAUFGDEP,ZAUANDEP,&
     & IAUSTAT ,IAUEVEN1,IAUEVEN2,IAURDBFL,IAUANFLG  )  
  ENDIF

!*          3.2.2       CHECK IF BUFR MISSING IND. FOR:
!                       1. VERTICAL COORDINATE
!                       2. VARIABLE
!                       3. ?

  322 CONTINUE

!*          3.2.1.1     VERTICAL COORDINATE

  3211 CONTINUE
  IF(ROBODY(JALL  ,MDBPPP) /= RMDI ) THEN
    IF(.NOT.(ROBODY(JALL ,MDBPPP) >= RDWMLIM.AND.&
     & ROBODY(JALL ,MDBPPP) <= RUPMLIM     )) GO TO 3212  
    ROBODY(JALL ,MDBPPP ) = RMDI
  ENDIF

!*          3.2.1.2     VARIABLE

  3212 CONTINUE
  IF(ROBODY(JALL  ,MDBVAR) /= RMDI ) THEN
    IF(.NOT.(ROBODY(JALL ,MDBVAR) >= RDWMLIM.AND.&
     & ROBODY(JALL ,MDBVAR) <= RUPMLIM     )) GO TO 3213  
    ROBODY(JALL ,MDBVAR ) = RMDI
  ENDIF

!*          3.2.1.3     ERROR STATISTICS

  3213 CONTINUE
  ROBODY(JALL  ,MDBOER) = RMDI
  ROBODY(JALL  ,MDBRER) = RMDI
  ROBODY(JALL  ,MDBPER) = RMDI
  ROBODY(JALL  ,MDBFGE) = RMDI
  ROBODY(JALL  ,MDBFOE) = RMDI

!*          3.2.3       CLOSE ALL DATA LOOP

ENDDO ALL_DATA_LOOP

!*          3.2.3       CHECK IF ALL PRESENT

IF(LLPs   .AND. &
 & LLT2M  .AND. &
 & LLTd2M .AND. &
 & LLDD   .AND. &
 & LLFF   .AND. &
 & LLU10m .AND. &
 & LLV10m .AND. &
 & LLRH2m .AND. &
 & LLPt   .AND. &
 & LLSDD  .AND. &
 & LLSFF  .AND. &
 & LLZ           ) GO TO 324  
WRITE(NULOUT,'('' DRIBUIN; LAT, LON: '',2(F9.2,1X))') ZLAT,ZLON
WRITE(NULOUT,'(''          LLPs   :'',L1)') LLPs
WRITE(NULOUT,'(''          LLT2M  :'',L1)') LLT2M
WRITE(NULOUT,'(''          LLTd2M :'',L1)') LLTd2M
WRITE(NULOUT,'(''          LLDD   :'',L1)') LLDD
WRITE(NULOUT,'(''          LLFF   :'',L1)') LLFF
WRITE(NULOUT,'(''          LLU10m :'',L1)') LLU10m
WRITE(NULOUT,'(''          LLV10m :'',L1)') LLV10m
WRITE(NULOUT,'(''          LLRH2m :'',L1)') LLRH2m
WRITE(NULOUT,'(''          LLPt   :'',L1)') LLPt
WRITE(NULOUT,'(''          LLSDD  :'',L1)') LLSDD
WRITE(NULOUT,'(''          LLSFF  :'',L1)') LLSFF
WRITE(NULOUT,'(''          LLZ    :'',L1)') LLZ

!*          3.2.4       ?

324 CONTINUE

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

!        4.             OBSERVATION BODY (OUTPUT TO ODB)
!                       --------------------------------

ZWEIGHT = 1.0_JPRB

!*          4.0         DECIDE ON VERTICAL COORDINATE; 
!                       CHECK IF PRESSURE PRESENT; FOR WIND AND
!                       TEMPERATURE BODIES ARE DONE ANYWAY BUT ASSIGN
!                       VERTICAL COOORDINATE AS HEIGHT SET TO 0.

ZPRES    = RMDI
IPFLG    = 0
ZPSTA    = RMDI
ZPMSL    = RMDI
LLMSLP   = .FALSE.
LLSTAP   = .FALSE.
IPSTAFLG = 0
IPMSLFLG = 0
Ps0_LOOP: DO JPs = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JPs  ,MDBVNM) == NVNUMB( 47)) THEN
    IPS    = JPs
    IPCODE = ROBODY(JPs  ,MDBSYPC)
    IF(IPCODE == NPRESCD( 1)) THEN
      IF(LLMSLP)                 THEN
        WRITE(NULOUT,'('' DRIBUIN; MSL PRESSURE ALREADY FOUND '')')
        CALL ABOR1(' DRIBUIN : ')
      ELSE
        LLMSLP   = .TRUE.
        ZPMSL    = ROBODY(IPS,MDBVAR)
        IPMSLFLG = IBITS(INT(ROBODY(IPS,MDBRDFL)),NCMBVFBP+NCMBFLBP,NCMBFLOC)
        IDBRDFL  = ROBODY(IPS,MDBRDFL)
        CALL PPVAFL(IPMSLFLG,IPMSLFLG,IDBRDFL)
        ROBODY(IPS,MDBRDFL) = IDBRDFL
      ENDIF
    ELSEIF(IPCODE == NPRESCD( 2)) THEN
      IF(LLSTAP)                 THEN
        WRITE(NULOUT,'('' DRIBUIN; STATION LEVEL PRESSURE ALREADY FOUND '')')
        CALL ABOR1(' SHIPIN : ')
      ELSE
        LLSTAP   = .TRUE.
        ZPSTA    = ROBODY(IPS,MDBVAR)
        IPSTAFLG = IBITS(INT(ROBODY(IPS,MDBRDFL)),NCMBVFBP+NCMBFLBP,NCMBFLOC)
        IDBRDFL  = ROBODY(IPS,MDBRDFL)
        CALL PPVAFL(IPSTAFLG,IPSTAFLG,IDBRDFL)
        ROBODY(IPS,MDBRDFL) = IDBRDFL
      ENDIF
    ELSE
      WRITE(NULOUT,'('' DRIBUIN; UNKNOWN PRESSURE CODE: '',I10)') IPCODE
      CALL ABOR1(' DRIBUIN : ')
    ENDIF
  ENDIF
ENDDO Ps0_LOOP

!*          4.0.1       ASSIGN PRESSURE

IF(ZPMSL /= RMDI ) THEN
  ZPRES = ZPMSL
  IPFLG = IPMSLFLG
ELSEIF(ZPSTA /= RMDI ) THEN
  ZPRES = ZPSTA
  IPFLG = IPMSLFLG
ENDIF

!*          4.0.2       VERTICAL COORDINATE

ZVERCO = RMDI
IVECFL = 0
IVERCO = 0
IF(ZPRES  == RMDI)            THEN
  ZVERCO = 0.0_JPRB
  IVECFL = 0
  IVERCO = NHEIGVC
ELSE
  ZVERCO = ZPRES
  IVECFL = IPFLG
  IVERCO = NPRESVC
ENDIF

!*          4.0.2       IF HEIGHT VERTICAL COORDINATE GET A PRESSURE
!                       FOR OBS. ERROR CALCULATIONS

ZPRESAU = ZPRES
IF(IVERCO == NHEIGVC)         THEN
  ZHEAUX  = 0.0_JPRB
  CALL Z2PICAO(ZHEAUX,ZPRESAU)
  ZPRESAU = ZPRESAU * 100._JPRB
ENDIF

!*          4.1         LOOK FOR WIND DIRECTION/FORCE DATA

ZDIR   = RMDI
ZSPD   = RMDI
IDDFLG = 0
IFFFLG = 0
ZDDPRES= RMDI
ZFFPRES= RMDI
IDDPFLG= 0
IFFPFLG= 0
DDFF_LOOP: DO JDDFF = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1

!*          4.1.1       WIND DIRECTION

  IF(ROBODY(JDDFF,MDBVNM) == NVNUMB( 48)) THEN
    IDD     = JDDFF
    IVNM    = NVNUMB( 48)

!*          4.1.1.0     SET/GET

    CALL GETSETE(IDD,&
     & ZDDPRES,ZDDPRL,ZDIR,&
     & ZDDFGER,ZDDFGCC1,ZDDFGCC2,&
     & ZDDPERR,ZDDOERR,ZDDFERR,ZDDRERR,&
     & ZDDFGDEP,ZDDANDEP,&
     & IDDSTAT,IDDEVEN1,IDDEVEN2,IDDRDBFL,IDDANFLG,&
     & IDDFLG,IDDPFLG)  

    ZDDPRES  = ZVERCO
    IDDPFLG  = IVECFL

!*          4.1.1.2     VARIABLE

    IF(ROBODY(IDD  ,MDBVAR) /= RMDI )       THEN
      ZDIR   = ROBODY(IDD  ,MDBVAR)
      IDDFLG = IBITS(IDDRDBFL,NCMBVFBP+NCMBFLBP,NCMBFLOC)
    ELSE
      IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
    ENDIF

!*          4.1.1.3     ERROR STATISTICS

!*          4.1.1.4     STATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 48,KCDSQNO,KOBSQNO) == 0)  THEN
      IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
    ENDIF
    CALL PPVAFL(IDDPFLG,IDDFLG,IDDRDBFL)

!*          4.1.1.5     DEPARTURES

!*          4.1.1.6     UPDATE BODY ENTRY

    CALL DRIBUBE(IDD     ,&
     & IVNM    ,IVERCO  ,ZVERCO  ,ZDDPRL  ,ZDIR    ,&
     & ZDDOERR ,ZDDPERR ,ZDDFERR ,ZDDRERR ,&
     & ZDDFGER ,ZDDFGCC1,ZDDFGCC2,&
     & ZDDFGDEP,ZDDANDEP,&
     & IDDSTAT ,IDDEVEN1,IDDEVEN2,IDDRDBFL,IDDANFLG  )  

!*          4.1.2       WIND SPEED

  ELSEIF(ROBODY(JDDFF,MDBVNM) == NVNUMB( 49)) THEN
    IFF     = JDDFF
    IVNM    = NVNUMB( 49)

!*          4.2.2.0     SET/GET

    CALL GETSETE(IFF,&
     & ZFFPRES,ZFFPRL,ZSPD,&
     & ZFFFGER,ZFFFGCC1,ZFFFGCC2,&
     & ZFFPERR,ZFFOERR,ZFFFERR,ZFFRERR,&
     & ZFFFGDEP,ZFFANDEP,&
     & IFFSTAT,IFFEVEN1,IFFEVEN2,IFFRDBFL,IFFANFLG,&
     & IFFFLG,IFFPFLG)  

    ZFFPRES  = ZVERCO
    IFFPFLG  = IVECFL

!*          4.1.2.2     VARIABLE

    IF(ROBODY(IFF  ,MDBVAR) /= RMDI )        THEN
      ZSPD   = ROBODY(IFF  ,MDBVAR)
      IFFFLG = IBITS(IFFRDBFL,NCMBVFBP+NCMBFLBP,NCMBFLOC)
    ELSE
      IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
    ENDIF

!*          4.1.2.3     ERROR STATISTICS

    IF(LERRPERS)           THEN
      ZFFPERR  = 1.0_JPRB ! Rough first estimate of persistence error
    ENDIF
    ZFFOERR  = 2.0_JPRB ! Rough first estimate of obs. error =2 m/s
    CALL FINOERR(ZFFPERR,ZFFOERR,ZFFFERR)

!*          4.1.2.4     STATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 49,KCDSQNO,KOBSQNO) == 0)          THEN
      IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
    ENDIF
    CALL PPVAFL(IFFPFLG,IFFFLG,IFFRDBFL)

!*          4.1.2.5     DEPARTURES

!*          4.1.2.6     UPDATE BODY ENTRY

    CALL DRIBUBE(IFF     ,&
     & IVNM    ,IVERCO  ,ZVERCO  ,ZFFPRL  ,ZSPD    ,&
     & ZFFOERR ,ZFFPERR ,ZFFFERR ,ZFFRERR ,&
     & ZFFFGER ,ZFFFGCC1,ZFFFGCC2,&
     & ZFFFGDEP,ZFFANDEP,&
     & IFFSTAT ,IFFEVEN1,IFFEVEN2,IFFRDBFL,IFFANFLG  )  
  ENDIF
ENDDO DDFF_LOOP

!*          4.1.3       CHECK IF DD, FF AND PRESSURE PRESENT

!*          4.1.4       10m (?) U COMPONENT

U10M_LOOP: DO JU10M = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JU10M,MDBVNM) == NVNUMB( 56)) THEN
    IU10M = JU10M
    IVNM  = NVNUMB( 56)

!*          4.1.4.0     SET/GET

    CALL GETSETE(IU10M,&
     & ZUUPRES,ZUUPRL,ZU  ,&
     & ZUUFGER,ZUUFGCC1,ZUUFGCC2,&
     & ZUUPERR,ZUUOERR,ZUUFERR,ZUURERR,&
     & ZUUFGDEP,ZUUANDEP,&
     & IUUSTAT,IUUEVEN1,IUUEVEN2,IUURDBFL,IUUANFLG,&
     & IUUFLG,IUUPFLG)  

    ZUUPRES  = ZVERCO
    IUUPFLG  = IVECFL

!*          4.1.4.2     VARIABLE

    IF(ZDIR /= RMDI.AND.ZSPD /= RMDI    )        THEN
      ZU     = UCOM(ZDIR,ZSPD)
      IUUFLG = MAX(IDDFLG,IFFFLG)
    ELSE
      IUUSTAT = ICHSTAT_REJECT( IUUSTAT )
    ENDIF

!*          4.1.4.3     ERROR STATISTICS

    CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
     & IVNM,IUUSTAT,ZUUPRES,ZUUPRL,&
     & ZUUPERR,ZUUOERR,ZUUFERR,ZWEIGHT)  

!*          4.1.4.4     STATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 56,KCDSQNO,KOBSQNO) == 0)          THEN
      IUUSTAT = ICHSTAT_REJECT( IUUSTAT )
    ENDIF
    CALL PPVAFL(IUUPFLG,IUUFLG,IUURDBFL)

!*          4.1.4.5     DEPARTURES

!*          4.1.4.6     UPDATE BODY ENTRY

    CALL DRIBUBE(IU10M   ,&
     & IVNM    ,IVERCO  ,ZVERCO  ,ZUUPRL  ,ZU      ,&
     & ZUUOERR ,ZUUPERR ,ZUUFERR ,ZUURERR ,&
     & ZUUFGER ,ZUUFGCC1,ZUUFGCC2,&
     & ZUUFGDEP,ZUUANDEP,&
     & IUUSTAT ,IUUEVEN1,IUUEVEN2,IUURDBFL,IUUANFLG  )  

  ENDIF
ENDDO U10M_LOOP

!*          4.1.5       10m(?) V COMPONENT

V10M_LOOP: DO JV10M    = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JV10M,MDBVNM) == NVNUMB( 57)) THEN
    IV10M = JV10M
    IVNM  = NVNUMB( 57)

!*          4.1.5.0     SET/GET

    CALL GETSETE(IV10M,&
     & ZVVPRES,ZVVPRL,ZV  ,&
     & ZVVFGER,ZVVFGCC1,ZVVFGCC2,&
     & ZVVPERR,ZVVOERR,ZVVFERR,ZVVRERR,&
     & ZVVFGDEP,ZVVANDEP,&
     & IVVSTAT,IVVEVEN1,IVVEVEN2,IVVRDBFL,IVVANFLG,&
     & IVVFLG,IVVPFLG)  

    ZVVPRES  = ZVERCO
    IVVPFLG  = IVECFL

!*          4.1.5.2     VARIABLE

    IF(ZDIR /= RMDI.AND.ZSPD /= RMDI    )        THEN
      ZV      = VCOM(ZDIR,ZSPD)
      IVVFLG  = MAX(IDDFLG,IFFFLG)
    ELSE
      IVVSTAT = ICHSTAT_REJECT( IVVSTAT )
    ENDIF

!*          4.1.5.3     ERROR STATISTICS

    CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
     & IVNM,IVVSTAT,ZVVPRES,ZVVPRL,&
     & ZVVPERR,ZVVOERR,ZVVFERR,ZWEIGHT)  

!*          4.1.5.4     STATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 57,KCDSQNO,KOBSQNO) == 0)          THEN
      IVVSTAT = ICHSTAT_REJECT( IVVSTAT )
    ENDIF
    CALL PPVAFL(IVVPFLG,IVVFLG,IVVRDBFL)

!*          4.1.5.5     DEPARTURES

!*          4.1.5.5     UPDATE BODY ENTRY

    CALL DRIBUBE(IV10M   ,&
     & IVNM    ,IVERCO  ,ZVERCO  ,ZVVPRL  ,ZV      ,&
     & ZVVOERR ,ZVVPERR ,ZVVFERR ,ZVVRERR ,&
     & ZVVFGER ,ZVVFGCC1,ZVVFGCC2,&
     & ZVVFGDEP,ZVVANDEP,&
     & IVVSTAT ,IVVEVEN1,IVVEVEN2,IVVRDBFL,IVVANFLG  )  
  ENDIF
ENDDO V10M_LOOP

!*          4.2         2m (?) TEMPERATURE

420 CONTINUE
T2M_LOOP: DO JT2M = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JT2M ,MDBVNM) == NVNUMB( 10)) THEN
    IT2M = JT2M
    IVNM = NVNUMB( 10)

!*          4.2.0       SET/GET

    CALL GETSETE(IT2M,&
     & ZTTPRES,ZTTPRL,ZT2M,&
     & ZTTFGER,ZTTFGCC1,ZTTFGCC2,&
     & ZTTPERR,ZTTOERR,ZTTFERR,ZTTRERR,&
     & ZTTFGDEP,ZTTANDEP,&
     & ITTSTAT,ITTEVEN1,ITTEVEN2,ITTRDBFL,ITTANFLG,&
     & ITTFLG,ITTPFLG)  

    ZTTPRES  = ZVERCO
    ITTPFLG  = IVECFL

!*          4.2.2       VARIABLE

    IF(ROBODY(IT2M ,MDBVAR) /= RMDI ) THEN
      ZT2M  = ROBODY(JT2M ,MDBVAR)
      ITTFLG= IBITS(ITTRDBFL,NCMBVFBP+NCMBFLBP,NCMBFLOC)
    ELSE
      ITTSTAT = ICHSTAT_REJECT( ITTSTAT )
    ENDIF

!*          4.2.3       ERROR STATISTICS

    CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
     & IVNM,ITTSTAT,ZTTPRES,ZTTPRL,&
     & ZTTPERR,ZTTOERR,ZTTFERR,ZWEIGHT)  

!*          4.2.4       STATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 10,KCDSQNO,KOBSQNO) == 0)          THEN
      ITTSTAT = ICHSTAT_REJECT( ITTSTAT )
    ENDIF
    CALL PPVAFL(ITTPFLG,ITTFLG,ITTRDBFL)

!*          4.2.5       DEPARTURES

!*          4.2.6       UPDATE BODY ENTRY

    CALL DRIBUBE(IT2M    ,&
     & IVNM    ,IVERCO  ,ZVERCO  ,ZTTPRL  ,ZT2M    ,&
     & ZTTOERR ,ZTTPERR ,ZTTFERR ,ZTTRERR ,&
     & ZTTFGER ,ZTTFGCC1,ZTTFGCC2,&
     & ZTTFGDEP,ZTTANDEP,&
     & ITTSTAT ,ITTEVEN1,ITTEVEN2,ITTRDBFL,ITTANFLG  )  
  ENDIF
ENDDO T2M_LOOP

!*          4.3         GEOPOTENTIAL

430 CONTINUE
ZGEOP  = 0.0_JPRB
Z_LOOP: DO JZ = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JZ   ,MDBVNM) == NVNUMB(  3)) THEN
    IZ   = JZ
    IVNM = NVNUMB(  3)

!*          4.3.0       SET/GET

    CALL GETSETE(IZ ,&
     & ZZZPRES,ZZZPRL,ZZZ ,&
     & ZZZFGER,ZZZFGCC1,ZZZFGCC2,&
     & ZZZPERR,ZZZOERR,ZZZFERR,ZZZRERR,&
     & ZZZFGDEP,ZZZANDEP,&
     & IZZSTAT,IZZEVEN1,IZZEVEN2,IZZRDBFL,IZZANFLG,&
     & IZZFLG,IZZPFLG)  

    ZZZPRES  = ZPRES
    ZZZ      = ZGEOP

    IZZFLG   = 0
    IZZPFLG  = IPFLG

!*          4.3.2       VARIABLE

    IF(ZZZ     == RMDI .OR. ZZZPRES == RMDI ) THEN
      IZZSTAT = ICHSTAT_REJECT( IZZSTAT )
    ENDIF

!*          4.3.3       ERROR STATISTICS

    CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
     & IVNM,IZZSTAT,ZZZPRES,ZZZPRL,&
     & ZZZPERR,ZZZOERR,ZZZFERR,ZWEIGHT)  

!*          4.3.4       STATUS, EVENTS, FLAGS

    IF(NMKCMVSE(  3,KCDSQNO,KOBSQNO) == 0)          THEN
      IZZSTAT = ICHSTAT_REJECT( IZZSTAT )
    ENDIF
    CALL PPVAFL(IZZPFLG,IZZFLG,IZZRDBFL)

!*          4.3.5       DEPARTURES

!*          4.3.6       UPDATE BODY ENTRY

    CALL DRIBUBE(IZ      ,&
     & IVNM    ,NPRESVC ,ZZZPRES ,ZZZPRL  ,ZZZ     ,&
     & ZZZOERR ,ZZZPERR ,ZZZFERR ,ZZZRERR ,&
     & ZZZFGER ,ZZZFGCC1,ZZZFGCC2,&
     & ZZZFGDEP,ZZZANDEP,&
     & IZZSTAT ,IZZEVEN1,IZZEVEN2,IZZRDBFL,IZZANFLG  )  
  ENDIF
ENDDO Z_LOOP

!*          4.4         PRESSURE

440 CONTINUE
Ps_LOOP: DO JPs = MLNKH2B(KDRIP),MLNKH2B(KDRIP+1)-1
  IF(ROBODY(JPs  ,MDBVNM) == NVNUMB( 47)) THEN
    IPS    = JPs
    IVNM   = NVNUMB( 47)
    IPCODE = ROBODY(IPS  ,MDBSYPC)

!*          4.4.0       SET/GET

    CALL GETSETE(IPS,&
     & ZPSPRES,ZPSPRL,ZPS ,&
     & ZPSFGER,ZPSFGCC1,ZPSFGCC2,&
     & ZPSPERR,ZPSOERR,ZPSFERR,ZPSRERR,&
     & ZPSFGDEP,ZPSANDEP,&
     & IPSSTAT,IPSEVEN1,IPSEVEN2,IPSRDBFL,IPSANFLG,&
     & IPSFLG,IPSPFLG)  

    ZPSPRES  = ZGEOP

    IPSFLG   = IBITS(IPSRDBFL,NCMBVFBP+NCMBFLBP,NCMBFLOC)
    IPSPFLG  = IBITS(IPSRDBFL,NCMBPFBP+NCMBFLBP,NCMBFLOC)

!*          4.4.2       VARIABLE

    IF(ROBODY(IPS,MDBVAR) == RMDI )  THEN
      IPSSTAT = ICHSTAT_REJECT( IPSSTAT )
    ELSE
      ZPS = ROBODY(IPS  ,MDBVAR)
!     IF(LLMSLP .AND. LLSTAP .AND. IPCODE == NPRESCD( 2))   THEN
      IF(LLSTAP .AND. IPCODE == NPRESCD( 2))   THEN
        IF(LLMSLP .AND. ZPMSL /= RMDI)   THEN
          IPSSTAT = ICHSTAT_REJECT( IPSSTAT )
          IPSEVEN1= ICHEV1( IPSEVEN1,ND1EVENT(17))
        ENDIF
      ENDIF

!*          4.4.2.0    BIAS CORRECTION

      IF(L_DRIBU_PS_BIAS) THEN
        ZPSBIAS = ROBODY(IPS,MDBTORB)
        IF(ZPSBIAS /= RMDI) THEN
          ZPS = ZPS - ZPSBIAS
          IPSEVEN2 = ICHEV2( IPSEVEN2,ND2EVENT( 1,KOBTYP))
        ENDIF
      ENDIF
    ENDIF

!*          4.4.3       ERROR STATISTICS

    443 CONTINUE

!*          4.4.3.1     PERSISTENCE ERROR

    IF(LERRPERS)           THEN
      IF(ZZZPERR /= RMDI )   THEN
        ZPSPERR  = ZZZPERR*RPAPM
      ENDIF
    ENDIF

!*          4.4.3.2     OBSERVATION ERROR

    IF(ZZZOERR /= RMDI )   THEN
      ZPSOERR  = ZZZOERR*RPAPM
    ENDIF

!*          4.4.3.3     PRESSURE FINAL OBSERVATION ERROR

    CALL FINOERR(ZPSPERR,ZPSOERR,ZPSFERR)

!*          4.4.4       STATUS, EVENTS, FLAGSTATUS, EVENTS, FLAGS

    IF(NMKCMVSE( 47,KCDSQNO,KOBSQNO) == 0)          THEN
      IPSSTAT = ICHSTAT_REJECT( IPSSTAT )
    ENDIF
    CALL PPVAFL(IPSPFLG,IPSFLG,IPSRDBFL)

!*          4.4.5       DEPARTURES

!*          4.4.6       UPDATE BODY ENTRY

    CALL DRIBUBE(IPS     ,&
     & IVNM    ,NHEIGVC ,ZGEOP   ,ZPSPRL  ,ZPS     ,&
     & ZPSOERR ,ZPSPERR ,ZPSFERR ,ZPSRERR ,&
     & ZPSFGER ,ZPSFGCC1,ZPSFGCC2,&
     & ZPSFGDEP,ZPSANDEP,&
     & IPSSTAT ,IPSEVEN1,IPSEVEN2,IPSRDBFL,IPSANFLG  )  
  ENDIF
ENDDO Ps_LOOP
!*
!-----------------------------------------------------------------------

!        5.             FINISH OFF REPORT (re)CREATION
!                       ------------------------------

5 CONTINUE

!*          5.1         UPDATE HEADER PARAMETERS (IF ANY)

!*          5.2         CMA REPORT CREATED SWITCH

LDTAKEN = .TRUE.
!*
!-----------------------------------------------------------------------

!        6.             IF OUTPUT MESSAGE; PRINT LAT/LON
!                       --------------------------------

6 CONTINUE
IF(LDMESAGE)           THEN
  WRITE(NULOUT,'('' LAT.     :'',F10.2)') ZLAT
  WRITE(NULOUT,'('' LON.     :'',F10.2)') ZLON
  CALL FLUSH(NULOUT)
ENDIF
!*
!-----------------------------------------------------------------------

!        7.             RETURN
!                       ------

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