SUBROUTINE PILOTIN(KPILP,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE PILOTIN - PROCESS INPUT PILOT

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

!     PURPOSE
!     -------

!        PROCESS INPUT PILOT

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

!        CALL PILOTIN(KPILP,
!                     KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,
!                     LDMESAGE,LDTAKEN)
!     WHERE:
!            KPILP   = REPORT POINTER
!            KOBTYP  = OBS. TYPE
!            KCDTYP  = CODE TYPE
!            KOBSQNO = OBS. TYPE SQ. NO.
!            KCDSQNO = CODE TYPE SQ. NO.
!            LDMESAGE= OUTPUT MESSAGE SWITCH  
!            LDTAKEN = REPORT TAKEN ("ODB" REPORT UPDATED) SWITCH  

!        PILOTIN IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

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

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

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

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDB
USE YOMCMBDY , ONLY :    &
 & NPSURBP  ,NPSUROC  
USE YOMANCS  , ONLY : NMDI     ,RMDI     ,RDEGREES ,RADIANS  ,EPSMAC   ,RUPMLIM  ,RDWMLIM  ,&
 & RSANMOMH ,RSBAROMH  
USE YOMVNMB  , ONLY : NVNUMB
USE YOMMKODB , ONLY : LERRPERS, NMKCMVSE ,LTOTREM
USE YOMNMCOD , ONLY : NPRESVC  ,NHEIGVC
USE YOMCST   , ONLY : RG       ,RTT
USE YOETHF   , ONLY : RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES  
USE YOMCOCTP , ONLY : NSHPCD
USE YOMNMEV  , ONLY : NR1EVENT
USE YOMLUN   , ONLY : NULOUT
USE LOCAL_TRAFOS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPILP 
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 
LOGICAL :: LLDD, LLFF, LLU, LLV, LLZ

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

INTEGER(KIND=JPIM) :: IOBDATE, &
 & IOBTIME, &
 & IVNM, JWIND, JLE, &
 & ISTART, IEND, &
 & ISTID, INSTYP, &
 & IREPST, INOEPLE, INOEPL  

INTEGER(KIND=JPIM) :: IDD, &
 & IDDSTAT, IDDEVEN1, IDDEVEN2, IDDRDBFL, IDDANFLG, &
 & IDDPFLG, IDDFLG, IDDVERCO, IDDLEVID, IDDSURF  
INTEGER(KIND=JPIM) :: IFF, &
 & IFFSTAT, IFFEVEN1, IFFEVEN2, IFFRDBFL, IFFANFLG, &
 & IFFPFLG, IFFFLG, IFFVERCO, IFFLEVID, IFFSURF  
INTEGER(KIND=JPIM) :: IU, &
 & IUUSTAT, IUUEVEN1, IUUEVEN2, IUURDBFL, IUUANFLG, &
 & IUUPFLG, IUUFLG, IUUVERCO, IUULEVID  
INTEGER(KIND=JPIM) :: IV, &
 & IVVSTAT, IVVEVEN1, IVVEVEN2, IVVRDBFL, IVVANFLG, &
 & IVVPFLG, IVVFLG, IVVVERCO, IVVLEVID  
INTEGER(KIND=JPIM) :: IZ, JZ, &
 & IZZSTAT, IZZEVEN1, IZZEVEN2, IZZRDBFL, IZZANFLG, &
 & IZZPFLG, IZZFLG, IZZVERCO, IZZLEVID  
INTEGER(KIND=JPIM) :: IAUX, JALL, &
 & IAUSTAT, IAUEVEN1, IAUEVEN2, IAURDBFL, IAUANFLG, &
 & IAUPFLG, IAUFLG  , IAUVERCO, IAULEVID  

LOGICAL :: LLFAIL, LLREMOVE

CHARACTER (LEN = 8) :: CLSTID

REAL(KIND=JPRB) :: ZLAT, ZLON, ZALT, &
 & ZTIMDIF, &
 & ZWEIGHT, &
 & ZPRESAU, ZHEIGOG, ZANMOMH, ZBAROMH  

REAL(KIND=JPRB) :: ZDDPRES, ZDDPRL, ZDIR, &
 & ZDDPERR, ZDDOERR, ZDDFERR, ZDDRERR, &
 & ZDDFGER, ZDDFGCC1, ZDDFGCC2, &
 & ZDDFGDEP, ZDDANDEP  
REAL(KIND=JPRB) :: ZFFPRES, ZFFPRL, ZSPD, &
 & ZFFPERR, ZFFOERR, ZFFFERR, ZFFRERR, &
 & ZFFFGER, ZFFFGCC1, ZFFFGCC2, &
 & ZFFFGDEP, ZFFANDEP  
REAL(KIND=JPRB) :: ZUUPRES, ZUUPRL, ZU, &
 & ZUUOERR, ZUUPERR, ZUUFERR, ZUURERR, &
 & ZUUFGER, ZUUFGCC1, ZUUFGCC2, &
 & ZUUFGDEP, ZUUANDEP  
REAL(KIND=JPRB) :: ZVVPRES, ZVVPRL, ZV, &
 & ZVVOERR, ZVVPERR, ZVVFERR, ZVVRERR, &
 & ZVVFGER, ZVVFGCC1, ZVVFGCC2, &
 & ZVVFGDEP, ZVVANDEP  
REAL(KIND=JPRB) :: ZZZPRES, ZZZPRL, ZZZ  , &
 & ZZZOERR, ZZZPERR, ZZZFERR, ZZZRERR, &
 & ZZZFGER, ZZZFGCC1, ZZZFGCC2, &
 & ZZZFGDEP, ZZZANDEP  
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 "errstat.intfb.h"
#include "getsete.intfb.h"
#include "obinstp.intfb.h"
#include "pilotbe.intfb.h"
#include "ppvafl.intfb.h"
#include "prlmchk.intfb.h"
#include "z2picao.intfb.h"
#include "finoerr.intfb.h"

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

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

!        1.             PRESET
!                       ------
IF (LHOOK) CALL DR_HOOK('PILOTIN',0,ZHOOK_HANDLE)
ZLAT   = ROBHDR(KPILP,MDBLAT) * RDEGREES
ZLON   = ROBHDR(KPILP,MDBLON) * RDEGREES
ZALT   = ROBHDR(KPILP,MDBALT)
IOBDATE= ROBHDR(KPILP,MDBDAT)
IOBTIME= ROBHDR(KPILP,MDBETM)
CLSTID = TRANSFER(ROBHDR(KPILP,MDBSID), CLSTID)
ISTID  = 0
READ(CLSTID(1:8),'(A8)') ISTID
IREPST = ROBHDR(KPILP,MDBRST)
INOEPLE = 5 

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

!        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(KPILP,MDB_INSTTYPE_AT_HDR) = INSTYP

!*          2.3         ANEMOMETER/BAROMETER HEIGHT

ZANMOMH  = RMDI
ZBAROMH  = RMDI
IF(KCDTYP == NSHPCD) then
  ZANMOMH  = RSANMOMH
  ZBAROMH  = RSBAROMH
ENDIF
ROBHDR(KPILP,MDBHOANP) = ZANMOMH
ROBHDR(KPILP,MDBHOBAP) = ZBAROMH

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

!        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       CHECK FOR MISSING ALTITUTE

IF(ZALT == RMDI) THEN
  ROBHDR(KPILP,MDBREV1) = ZCHEV1( ROBHDR(KPILP,MDBREV1),NR1EVENT(13))
ENDIF

!*          3.1.1       UPDATE REPORT STATUS

ROBHDR(KPILP,MDBRST) = IREPST

!*          3.1.2       FAILED REPORT

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

!*          3.1.3       REMOVED REPORT

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

!*          3.2         BODY PARAMETERS

!*          3.2.1       WE ACCEPT: DD, FF, U(10m), V(10m) AND Z

ALL_DATA_LOOP1: DO JALL = MLNKH2B(KPILP),MLNKH2B(KPILP+1)-1,INOEPLE
  INOEPL = 0
  LLDD   = .FALSE.
  LLFF   = .FALSE.
  LLU    = .FALSE.
  LLV    = .FALSE.
  LLZ    = .FALSE.
  321 CONTINUE
  IF(INOEPL == INOEPLE ) THEN
    IF(LLDD .AND. &
     & LLFF .AND. &
     & LLU  .AND. &
     & LLV  .AND. &
     & LLZ         ) CYCLE ALL_DATA_LOOP1  
    WRITE(NULOUT,'('' PILOTIN; NO. OF ENTRIES PER LEVEL EXCEEDED: '')')
    WRITE(NULOUT,'(''          LLDD, LLFF, LLU, LLV, LLZ: '',5(L1,1X))') &
     & LLDD, LLFF, LLU, LLV, LLZ  
    CALL ABOR1(' PILOTIN : TOO MANY ENTRIES ')
  ELSE
    IF(     ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB( 48)) THEN
      LLDD    = .TRUE.
      GO TO 3217
    ELSEIF(ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB( 49)) THEN
      LLFF    = .TRUE.
      GO TO 3217
    ELSEIF(ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB( 56) .OR. &
       & ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB(  1)       ) THEN  
      LLU     = .TRUE.
      GO TO 3217
    ELSEIF(ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB( 57) .OR. &
       & ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB(  2)       ) THEN  
      LLV     = .TRUE.
      GO TO 3217
    ELSEIF(ROBODY(JALL+INOEPL,MDBVNM) == NVNUMB(  3)       ) THEN
      LLZ     = .TRUE.
      GO TO 3217
    ELSE
      IAUX    = JALL + INOEPL
      IVNM    = ROBODY(IAUX  ,MDBVNM)
      WRITE(NULOUT,'('' PILOTIN; UNEXPECTED VARIABLE: '',I10)') IVNM

!*          3.2.1.0     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)
      IAULEVID= ROBODY(IAUX,MDBPILID)

!*          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 )

!*          3.2.1.5     DEPARTURES

!*          3.2.1.6     UPDATE BODY ENTRY

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

!*          3.2.1.7     GO CHECK IF LEVEL FINISHED:

  3217 CONTINUE
  INOEPL = INOEPL + 1
  GO TO 321
ENDDO ALL_DATA_LOOP1

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

ALL_DATA_LOOP2: DO JALL = MLNKH2B(KPILP),MLNKH2B(KPILP+1)-1

!*          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.4     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 LOOP2

ENDDO ALL_DATA_LOOP2

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

!        4.             OBSERVATION BODY (GEOPOTENTIAL)
!                       -------------------------------

4 CONTINUE
ZWEIGHT = 1.0_JPRB

!*          4.1         LOOK FOR DATA

HEIGHT_DATA_LOOP: DO JZ = MLNKH2B(KPILP),MLNKH2B(KPILP+1)-1
  IF(ROBODY(JZ   ,MDBVNM) == NVNUMB(  3)) THEN
    IZ   = JZ
    IVNM = NVNUMB(  3)

!*          4.1.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)  

!*          4.1.0       VERTICAL COORDINATE

    IZZVERCO = ROBODY(IZ,MDBVCO  )
    IZZLEVID = ROBODY(IZ,MDBPILID)
    ZZZPRES  = ROBODY(IZ,MDBPPP  )
    ZZZ      = ROBODY(IZ,MDBVAR  )
 
    IF(ZZZPRES == RMDI)  THEN
      IZZSTAT = ICHSTAT_REJECT( IZZSTAT )
      IF(ZZZ /= RMDI) THEN
        IZZVERCO = NPRESVC
        ZHEIGOG = ZZZ / RG
        CALL Z2PICAO(ZHEIGOG,ZZZPRES)
        ZZZPRES = ZZZPRES * 100._JPRB
      ENDIF
    ENDIF

!*          4.5.2       VARIABLE

    IF(ZZZ     == RMDI ) THEN
      IZZSTAT = ICHSTAT_REJECT( IZZSTAT )
    ENDIF

!*          4.5.3       ERROR STATISTICS

    IF(ZZZPRES /=RMDI) THEN
      CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
       & IVNM,IZZSTAT,ZZZPRES,ZZZPRL,&
       & ZZZPERR,ZZZOERR,ZZZFERR,ZWEIGHT)  
    ENDIF

!*          4.5.4       STATUS, EVENTS, FLAGS

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

!*          4.5.5       DEPARTURES

!*          4.5.6       UPDATE BODY ENTRY

    CALL PILOTBE(IZ      ,&
     & IVNM    ,IZZVERCO,ZZZPRES ,ZZZPRL  ,ZZZ     ,&
     & ZZZOERR ,ZZZPERR ,ZZZFERR ,ZZZRERR ,&
     & ZZZFGER ,ZZZFGCC1,ZZZFGCC2,&
     & ZZZFGDEP,ZZZANDEP,&
     & IZZSTAT ,IZZEVEN1,IZZEVEN2,IZZRDBFL,IZZANFLG,&
     & IZZLEVID                                      )  
  ENDIF
ENDDO HEIGHT_DATA_LOOP

!*
!-----------------------------------------------------------------------
!        5.             OBSERVATION BODY (WIND)
!                       -----------------------

5 CONTINUE
ZWEIGHT = 1.0_JPRB

!*          5.1         LOOK FOR WIND DATA

!if(0 < 1 ) go to 6
WIND_DATA_LOOP: DO JWIND   = MLNKH2B(KPILP) , MLNKH2B(KPILP+1)-1 , INOEPLE

!*          5.1.1       PRESET

  ZDIR    = RMDI
  ZDDPRES = RMDI
  IDDFLG  = 0
  IDDPFLG = 0
  IDDVERCO= NMDI
  IDDLEVID= NMDI

  ZSPD    = RMDI
  ZFFPRES = RMDI
  IFFFLG  = 0
  IFFPFLG = 0
  IFFVERCO= NMDI
  IFFLEVID= NMDI

!*          5.1.2       GET POINTERS

  IDD     = NMDI
  IFF     = NMDI
  IU      = NMDI
  IV      = NMDI
  IZ      = NMDI
  ISTART  = JWIND
  IEND    = ISTART + INOEPLE - 1
  DO JLE = ISTART , IEND
    IF(ROBODY(JLE,MDBVNM) == NVNUMB( 48))        IDD = JLE
    IF(ROBODY(JLE,MDBVNM) == NVNUMB( 49))        IFF = JLE
    IF(ROBODY(JLE,MDBVNM) == NVNUMB(  1) .OR. &
     & ROBODY(JLE,MDBVNM) == NVNUMB( 56)       ) IU  = JLE  
    IF(ROBODY(JLE,MDBVNM) == NVNUMB(  2) .OR. &
     & ROBODY(JLE,MDBVNM) == NVNUMB( 57)       ) IV  = JLE  
    IF(ROBODY(JLE,MDBVNM) == NVNUMB(  3))        IZ  = JLE
  ENDDO

!*          5.2         WIND DIRECTION

  IF(IDD == NMDI )    THEN
    WRITE(NULOUT,'('' PILOTIN; DD ENTRY MISSING '')')
    CALL ABOR1(' PILOTIN : DD MISSING ')
  ENDIF
  IVNM    = NVNUMB( 48)

!*          5.2.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 = ROBODY(IDD,MDBPPP  )
  IDDVERCO= ROBODY(IDD,MDBVCO  )
  IDDLEVID= ROBODY(IDD,MDBPILID)
  IDDSURF = IBITS(IDDLEVID,NPSURBP,NPSUROC)
  IF(ZDDPRES == RMDI) THEN
    ZZZ = ROBODY(IZ ,MDBVAR)
    IF(ZZZ /= RMDI) THEN
      ZDDPRES  = ZZZ
      IDDVERCO = NHEIGVC
    ELSE
      IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
    ENDIF
  ENDIF
  
!*          5.2.2       VARIABLE

  IF(ROBODY(IDD  ,MDBVAR) /= RMDI ) THEN
    ZDIR    = ROBODY(IDD  ,MDBVAR  )
  ELSE
    IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
  ENDIF

!*          5.2.3       ERROR STATISTICS

!*          5.2.4       STATUS, EVENTS, FLAGS

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

!*          5.2.5       DEPARTURES

!*          5.2.6       UPDATE BODY ENTRY

  CALL PILOTBE(IDD     ,&
   & IVNM    ,IDDVERCO,ZDDPRES ,ZDDPRL  ,ZDIR    ,&
   & ZDDOERR ,ZDDPERR ,ZDDFERR ,ZDDRERR ,&
   & ZDDFGER ,ZDDFGCC1,ZDDFGCC2,&
   & ZDDFGDEP,ZDDANDEP,&
   & IDDSTAT ,IDDEVEN1,IDDEVEN2,IDDRDBFL,IDDANFLG,&
   & IDDLEVID                                      )  

!*          5.3         WIND SPEED (ASSUMPTION THAT IS NEXT)

  IF(IFF == NMDI )    THEN
    WRITE(NULOUT,'('' PILOTIN; FF ENTRY MISSING '')')
    CALL ABOR1(' PILOTIN : FF MISSING ')
  ENDIF
  IVNM    = NVNUMB( 49)

!*          5.3.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 = ROBODY(IFF,MDBPPP)
  IFFVERCO= ROBODY(IFF  ,MDBVCO  )
  IFFLEVID= ROBODY(IFF  ,MDBPILID)
  IFFSURF = IBITS(IFFLEVID,NPSURBP,NPSUROC)
  IF(ZFFPRES == RMDI) THEN
    ZZZ = ROBODY(IZ ,MDBVAR)
    IF(ZZZ /= RMDI) THEN
      ZFFPRES  = ZZZ
      IFFVERCO = NHEIGVC
    ELSE
      IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
    ENDIF
  ENDIF

!*          5.3.2       VARIABLE

  IF(ROBODY(IFF  ,MDBVAR) /= RMDI ) THEN
    ZSPD    = ROBODY(IFF  ,MDBVAR  )
  ELSE
    IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
  ENDIF

!*          5.3.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)

!*          5.3.4       STATUS, EVENTS, FLAGS

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

!*          5.3.5       DEPARTURES

!*          5.3.6       UPDATE BODY ENTRY

  CALL PILOTBE(IFF     ,&
   & IVNM    ,IFFVERCO,ZFFPRES ,ZFFPRL  ,ZSPD    ,&
   & ZFFOERR ,ZFFPERR ,ZFFFERR ,ZFFRERR ,&
   & ZFFFGER ,ZFFFGCC1,ZFFFGCC2,&
   & ZFFFGDEP,ZFFANDEP,&
   & IFFSTAT ,IFFEVEN1,IFFEVEN2,IFFRDBFL,IFFANFLG,&
   & IFFLEVID                                      )  

!*          5.4         CROSSCHECK DD AND FF

  IF(IFFVERCO /= IDDVERCO .OR. &
     & ZFFPRES  /= ZDDPRES  .OR. &
     & IFFSURF  /= IDDSURF        ) THEN  
    WRITE(NULOUT,'('' PILOTIN; DD AND FF MIXUP '')')
    CALL ABOR1(' PILOTIN : DD/FF MIXUP ')
  ENDIF

!*          5.5         U COMPONENT

  IF(IU  == NMDI )    THEN
    WRITE(NULOUT,'('' PILOTIN; U ENTRY MISSING '')')
    CALL ABOR1(' PILOTIN : U MISSING ')
  ENDIF

  IVNM    = NVNUMB(   1)
  IF(IDDSURF == 1 ) IVNM    = NVNUMB(  56)

!*          5.5.0       SET/GET

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

  ZUUPRES  = ZDDPRES
  IUUPFLG  = IDDPFLG
  IUULEVID = IDDLEVID
  IUUVERCO = IDDVERCO
   
!*          5.5.1       VERTICAL COORDINATE

  IF(ZUUPRES  == RMDI .OR. &
     & IUULEVID == NMDI .OR. &
     & IUUVERCO == NMDI       ) THEN  
    IUUSTAT = ICHSTAT_REJECT( IUUSTAT )
  ENDIF

!*          5.5.2       VARIABLE

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

!*          5.5.3       ERROR STATISTICS

  ZPRESAU = ZUUPRES
  IF(IUUVERCO == NHEIGVC) THEN
    ZPRESAU = ROBODY(IZ,MDBPPP)
  ENDIF
  CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
   & IVNM,IUUSTAT,ZPRESAU,ZUUPRL,&
   & ZUUPERR,ZUUOERR,ZUUFERR,ZWEIGHT)  

!*          5.5.4       STATUS

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

!*          5.5.5       DEPARTURES

!*          5.5.6       UPDATE/CREATE BODY ENTRY

  CALL PILOTBE(IU      ,&
   & IVNM    ,IUUVERCO,ZUUPRES ,ZUUPRL  ,ZU      ,&
   & ZUUOERR ,ZUUPERR ,ZUUFERR ,ZUURERR ,&
   & ZUUFGER ,ZUUFGCC1,ZUUFGCC2,&
   & ZUUFGDEP,ZUUANDEP,&
   & IUUSTAT ,IUUEVEN1,IUUEVEN2,IUURDBFL,IUUANFLG,&
   & IUULEVID                                      )  

!*          5.6         V COMPONENT

  IF(IV  == NMDI )    THEN
    WRITE(NULOUT,'('' PILOTIN; V ENTRY MISSING '')')
    CALL ABOR1(' PILOTIN : V MISSING ')
  ENDIF

  IVNM    = NVNUMB(   2)
  IF(IDDSURF == 1 ) IVNM    = NVNUMB(  57)

!*          5.5.0       SET/GET

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

  ZVVPRES  = ZDDPRES
  IVVPFLG  = IDDPFLG
  IVVLEVID = IDDLEVID
  IVVVERCO = IDDVERCO
   
!*          5.5.1       VERTICAL COORDINATE

  IF(ZVVPRES  == RMDI .OR. &
     & IVVLEVID == NMDI .OR. &
     & IVVVERCO == NMDI       ) THEN  
    IVVSTAT = ICHSTAT_REJECT( IVVSTAT )
  ENDIF

!*          5.5.2       VARIABLE

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

!*          5.5.3       ERROR STATISTICS

  ZPRESAU = ZVVPRES
  IF(IVVVERCO == NHEIGVC) THEN
    ZPRESAU = ROBODY(IZ,MDBPPP)
  ENDIF
  CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,ZLAT,ZLON,ZTIMDIF,&
   & IVNM,IVVSTAT,ZPRESAU,ZVVPRL,&
   & ZVVPERR,ZVVOERR,ZVVFERR,ZWEIGHT)  

!*          5.5.4       STATUS

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

!*          5.5.5       DEPARTURES

!*          5.5.6       UPDATE/CREATE BODY ENTRY

  CALL PILOTBE(IV      ,&
   & IVNM    ,IVVVERCO,ZVVPRES ,ZVVPRL  ,ZV      ,&
   & ZVVOERR ,ZVVPERR ,ZVVFERR ,ZVVRERR ,&
   & ZVVFGER ,ZVVFGCC1,ZVVFGCC2,&
   & ZVVFGDEP,ZVVANDEP,&
   & IVVSTAT ,IVVEVEN1,IVVEVEN2,IVVRDBFL,IVVANFLG,&
   & IVVLEVID                                      )  
ENDDO WIND_DATA_LOOP

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

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

6 CONTINUE

!*          6.1         UPDATE HEADER PARAMETERS (IF ANY)

!*          6.2         ODB REPORT UPDATED SWITCH

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

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

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

!        8.             RETURN
!                       ------

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