SUBROUTINE AIREPIN(KAIRP,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE AIREPIN - PROCESS INPUT AIREP

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

!     PURPOSE
!     -------

!        PROCESS INPUT AIREP

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

!        CALL AIREPIN(KAIRP,
!                     KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,
!                     LDMESAGE,LDTAKEN)
!     WHERE:
!            KAIRP   = 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  

!        AIREPIN IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

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

!        PRLMCHK  - PRELIMINARY CHECKS
!        GETSETE  - GET/SET BODY ENTRY
!        ERRSTAT  - OBS. ERROR
!        AIREPBE  - UPDATE AIREP 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 : NCMBPFBP ,NCMBVFBP ,NCMBFLBP ,NCMBFLOC
USE YOMANCS  , ONLY : RMDI     ,RDEGREES ,RADIANS  ,EPSMAC   ,RUPMLIM  ,RDWMLIM  
USE YOMCST   , ONLY : RTT
USE YOMVNMB  , ONLY : NVNUMB
USE YOMNMCOD , ONLY : NPRESVC  ,NHEIGVC
USE YOMMKODB , ONLY :  LERRPERS, NMKCMVSE ,LTOTREM
USE YOETHF   , ONLY : RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES  
USE YOMLUN   , ONLY : NULOUT   
USE LOCAL_TRAFOS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KAIRP 
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, &
 & IVNM, JDDFF, &
 & 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) :: IU, JU, &
 & IUUSTAT, IUUEVEN1, IUUEVEN2, IUURDBFL, IUUANFLG, &
 & IUUPFLG, IUUFLG  
INTEGER(KIND=JPIM) :: IV, JV, &
 & IVVSTAT, IVVEVEN1, IVVEVEN2, IVVRDBFL, IVVANFLG, &
 & IVVPFLG, IVVFLG  
INTEGER(KIND=JPIM) :: ITEMP, JT, &
 & ITTSTAT, ITTEVEN1, ITTEVEN2, ITTRDBFL, ITTANFLG, &
 & ITTPFLG, ITTFLG  
INTEGER(KIND=JPIM) :: IAUX, JALL, &
 & IAUSTAT, IAUEVEN1, IAUEVEN2, IAURDBFL, IAUANFLG, &
 & IAUPFLG, IAUFLG  , IAUVERCO  

LOGICAL :: LLFAIL, LLREMOVE

CHARACTER (LEN = 8) :: CLSTID

REAL(KIND=JPRB) :: ZLAT, ZLON, &
 & ZTIMDIF, &
 & ZWEIGHT, ZHT  
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) :: ZTTPRES, ZTTPRL, ZT   , &
 & ZTTOERR, ZTTPERR, ZTTFERR, ZTTRERR, &
 & ZTTFGER, ZTTFGCC1, ZTTFGCC2, &
 & ZTTFGDEP, ZTTANDEP  
REAL(KIND=JPRB) :: ZAU, ZAUPRES, ZAUPRL, &
 & ZAUPERR, ZAUOERR, ZAUFERR, ZAURERR, &
 & ZAUFGER, ZAUFGCC1, ZAUFGCC2, &
 & ZAUFGDEP, ZAUANDEP  

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "airepbe.intfb.h"
#include "errstat.intfb.h"
#include "getsete.intfb.h"
#include "obinstp.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
!                       ------

!*          1.1         GET OBS. PARAMETERS FROM "HEADER"

IF (LHOOK) CALL DR_HOOK('AIREPIN',0,ZHOOK_HANDLE)
ZLAT   = ROBHDR(KAIRP,MDBLAT) * RDEGREES
ZLON   = ROBHDR(KAIRP,MDBLON) * RDEGREES
IOBDATE= ROBHDR(KAIRP,MDBDAT)
IOBTIME= ROBHDR(KAIRP,MDBETM)
CLSTID = TRANSFER(ROBHDR(KAIRP,MDBSID), CLSTID)
ISTID  = 0
!READ(CLSTID(1:8),'(A8)') ISTID
IREPST = ROBHDR(KAIRP,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(KAIRP,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(KAIRP,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.2         BODY PARAMETERS

ALL_DATA_LOOP: DO JALL = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1

!*          3.2.1       WE ACCEPT: T, DD, FF, U, V

  IF(ROBODY(JALL  ,MDBVNM) == NVNUMB(  8) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB( 48) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB( 49) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB(  1) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB(  2)       ) GO TO 322  

!*          3.2.1.1     UNEXPECTED VARIABLE

  IAUX = JALL
  IVNM = ROBODY(IAUX  ,MDBVNM)
  WRITE(NULOUT,'('' AIREPIN; UNEXPECTED VARIABLE: '',I10)') IVNM
  LDMESAGE = .TRUE.

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

!*          3.2.1.5     DEPARTURES

!*          3.2.1.6     UPDATE BODY ENTRY

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

!*          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  ,MDBRER) = RMDI
  ROBODY(JALL  ,MDBPER) = RMDI
  ROBODY(JALL  ,MDBFGE) = RMDI
  ROBODY(JALL  ,MDBFOE) = RMDI
  ROBODY(JALL  ,MDBOER) = RMDI

!*          3.2.3       CLOSE ALL DATA LOOP

ENDDO ALL_DATA_LOOP

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

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

ZWEIGHT = 1.0_JPRB

!*          4.1         LOOK FOR WIND DATA

ZDIR     = RMDI
ZSPD     = RMDI
DDFF_LOOP: DO JDDFF = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1

!*          4.2         WIND DIRECTION (DD)

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

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

!*          4.2.1       VERTICAL COORDINATE

    IF(ROBODY(IDD  ,MDBPPP) /= RMDI )       THEN
      IDDPFLG = IBITS(IDDRDBFL,NCMBPFBP+NCMBFLBP,NCMBFLOC)
      IF(ROBODY(IDD  ,MDBVCO) /= NPRESVC) THEN
        IF(ROBODY(IDD  ,MDBVCO) == NHEIGVC )       THEN
          ZHT    = ROBODY(IDD  ,MDBPPP)
          CALL Z2PICAO(ZHT,ZDDPRES)
          ZDDPRES = ZDDPRES*100._JPRB
        ELSE
          WRITE(NULOUT,'('' AIREPIN; UNKNOWN DD VERTICAL COORDINATE:'',I10)') &
           & ROBODY(IDD  ,MDBVCO)  
          IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
          GO TO 426
        ENDIF
      ELSE
        ZDDPRES = ROBODY(IDD  ,MDBPPP)
      ENDIF
    ELSE
      IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
      GO TO 426
    ENDIF

!*          4.2.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.2.3       ERROR STATISTICS

!*          4.2.4       STATUS, EVENTS, FLAGS

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

!*          4.2.5       DEPARTURES

!*          4.2.6       UPDATE BODY ENTRY
    426 CONTINUE
    CALL AIREPBE(IDD     ,&
     & IVNM    ,NPRESVC ,ZDDPRES ,ZDDPRL  ,ZDIR    ,&
     & ZDDOERR ,ZDDPERR ,ZDDFERR ,ZDDRERR ,&
     & ZDDFGER ,ZDDFGCC1,ZDDFGCC2 ,&
     & ZDDFGDEP,ZDDANDEP,&
     & IDDSTAT ,IDDEVEN1,IDDEVEN2,IDDRDBFL,IDDANFLG  )  

!*          4.3         WIND SPEED

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

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

!*          4.3.1       VERTICAL COORDINATE

    IF(ROBODY(IFF  ,MDBPPP) /= RMDI )       THEN
      IFFPFLG = IBITS(IFFRDBFL,NCMBPFBP+NCMBFLBP,NCMBFLOC)
      IF(ROBODY(IFF  ,MDBVCO) /= NPRESVC) THEN
        IF(ROBODY(IFF  ,MDBVCO) == NHEIGVC )       THEN
          ZHT    = ROBODY(IFF  ,MDBPPP)
          CALL Z2PICAO(ZHT,ZFFPRES)
          ZFFPRES = ZFFPRES*100._JPRB
        ELSE
          WRITE(NULOUT,'('' AIREPIN; UNKNOWN FF VERTICAL COORDINATE:'',I10)') &
           & ROBODY(IFF  ,MDBVCO)  
          IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
          GO TO 436
        ENDIF
      ELSE
        ZFFPRES = ROBODY(IFF  ,MDBPPP)
      ENDIF
    ELSE
      IFFSTAT = ICHSTAT_REJECT( IFFSTAT )
      GO TO 436
    ENDIF

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

!*          4.3.4       STATUS, EVENTS, FLAGS

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

!*          4.3.5       DEPARTURES

!*          4.3.6       UPDATE BODY ENTRY

    436 CONTINUE
    CALL AIREPBE(IFF     ,&
     & IVNM    ,NPRESVC ,ZFFPRES,ZFFPRL  ,ZSPD    ,&
     & ZFFOERR ,ZFFPERR ,ZFFFERR ,ZFFRERR ,&
     & ZFFFGER ,ZFFFGCC1,ZFFFGCC2,&
     & ZFFFGDEP,ZFFANDEP,&
     & IFFSTAT ,IFFEVEN1,IFFEVEN2,IFFRDBFL,IFFANFLG  )  
  ENDIF
ENDDO DDFF_LOOP

!*          4.4         CHECK IF DD, FF AND PRESSURE PRESENT

!*          4.5         U COMPONENT

450 CONTINUE
U_LOOP: DO JU    = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1
  IF(ROBODY(JU   ,MDBVNM) ==  NVNUMB(  1))     THEN
    IU = JU
    IVNM   = NVNUMB(  1)

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

    IF(ZDDPRES /= ZFFPRES)   THEN
      ROBODY(IU   ,MDBDSTA) = ZCHSTAT_REJECT( ROBODY(IU   ,MDBDSTA) )
    ENDIF
    ZUUPRES  = ZDDPRES

    IF(IDDPFLG /= IFFPFLG)                   THEN
      IUUSTAT = ICHSTAT_REJECT( IUUSTAT )
    ENDIF
    IUUPFLG  = IDDPFLG
    
!*          4.5.1       VERTICAL COORDINATE

!*          4.5.2       VARIABLE

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

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

!*          4.5.4       STATUS

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

!*          4.5.5       DEPARTURES

!*          4.5.6       UPDATE/CREATE BODY ENTRY

    456 CONTINUE
    CALL AIREPBE(IU      ,&
     & IVNM    ,NPRESVC ,ZUUPRES ,ZUUPRL  ,ZU      ,&
     & ZUUOERR ,ZUUPERR ,ZUUFERR ,ZUURERR ,&
     & ZUUFGER ,ZUUFGCC1,ZUUFGCC2,&
     & ZUUFGDEP,ZUUANDEP,&
     & IUUSTAT ,IUUEVEN1,IUUEVEN2,IUURDBFL,IUUANFLG  )  
  ENDIF
ENDDO U_LOOP

!*          4.6         V COMPONENT

460 CONTINUE
V_LOOP: DO JV    = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1
  IF(ROBODY(JV   ,MDBVNM) ==  NVNUMB(  2))     THEN
    IV = JV
    IVNM   = NVNUMB(  2)

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

    IF(ZDDPRES /= ZFFPRES)   THEN
      ROBODY(IV   ,MDBDSTA) = ZCHSTAT_REJECT( ROBODY(IV   ,MDBDSTA) )
    ENDIF

    ZVVPRES  = ZDDPRES

    IF(IDDPFLG /= IFFPFLG)                   THEN
      IVVSTAT = ICHSTAT_REJECT( IVVSTAT )
    ENDIF
    IVVPFLG  = IDDPFLG
    
!*          4.6.1       VERTICAL COORDINATE

!*          4.6.2       VARIABLE

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

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

!*          4.6.4       STATUS

    IF(NMKCMVSE(  2,KCDSQNO,KOBSQNO) == 0)          THEN
      ROBODY(IV   ,MDBDSTA) = ZCHSTAT_REJECT( ROBODY(IV   ,MDBDSTA))
    ENDIF
    CALL PPVAFL(IVVPFLG,IVVFLG,IVVRDBFL)

!*          4.6.5       DEPARTURES

!*          4.6.6       UPDATE/CREATE BODY ENTRY

    466 CONTINUE
    CALL AIREPBE(IV      ,&
     & IVNM    ,NPRESVC ,ZVVPRES ,ZVVPRL  ,ZV      ,&
     & ZVVOERR ,ZVVPERR ,ZVVFERR ,ZVVRERR ,&
     & ZVVFGER ,ZVVFGCC1,ZVVFGCC2,&
     & ZVVFGDEP,ZVVANDEP,&
     & IVVSTAT ,IVVEVEN1,IVVEVEN2,IVVRDBFL,IVVANFLG  )  
  ENDIF
ENDDO V_LOOP

!*          4.7         TEMPERATURE

470 CONTINUE
T_LOOP: DO JT    = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1
  IF(ROBODY(JT   ,MDBVNM) == NVNUMB(  8)) THEN
    ITEMP= JT
    IVNM = NVNUMB(  8)

!*          4.7.0       SET/GET

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

!*          4.7.1       VERTICAL COORDINATE

    IF(ROBODY(ITEMP,MDBPPP) /= RMDI )       THEN
      ITTPFLG = IBITS(ITTRDBFL,NCMBPFBP+NCMBFLBP,NCMBFLOC)
      IF(ROBODY(ITEMP,MDBVCO) /= NPRESVC) THEN
        IF(ROBODY(ITEMP,MDBVCO) == NHEIGVC )       THEN
          ZHT    = ROBODY(ITEMP,MDBPPP)
          CALL Z2PICAO(ZHT,ZTTPRES )
          ZTTPRES  = ZTTPRES *100._JPRB
        ELSE
          WRITE(NULOUT,'('' AIREPIN; UNKNOWN T VERTICAL COORDINATE:'',I10)') &
           & ROBODY(ITEMP,MDBVCO)  
          ITTSTAT = ICHSTAT_REJECT( ITTSTAT )
          GO TO 476
        ENDIF
      ELSE
        ZTTPRES  = ROBODY(ITEMP,MDBPPP)
      ENDIF
    ELSE

      ITTSTAT = ICHSTAT_REJECT( ITTSTAT )
      GO TO 476
    ENDIF

!*          4.7.2       VARIABLE

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

!*          4.7.3       ERROR STATISTICS

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

!*          4.7.4       STATUS

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

!*          4.7.5       DEPARTURES

!*          4.7.6       UPDATE/CREATE BODY ENTRY

    476 CONTINUE
    CALL AIREPBE(ITEMP   ,&
     & IVNM    ,NPRESVC ,ZTTPRES ,ZTTPRL  ,ZT      ,&
     & ZTTOERR ,ZTTPERR ,ZTTFERR ,ZTTRERR ,&
     & ZTTFGER ,ZTTFGCC1,ZTTFGCC2,&
     & ZTTFGDEP,ZTTANDEP,&
     & ITTSTAT ,ITTEVEN1,ITTEVEN2,ITTRDBFL,ITTANFLG  )  
  ENDIF
ENDDO T_LOOP
!*
!-----------------------------------------------------------------------

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

5 CONTINUE
!iev1R(:)=0
!iev2R(:)=0
!IDATE = ROBHDR(KAIRP,MDBDAT)
!ITIME = ROBHDR(KAIRP,MDBETM)
!IREV1 = ROBHDR(KAIRP,MDBREV1)
!IREV2 = ROBHDR(KAIRP,MDBREV2)
!IRSTA = ROBHDR(KAIRP,MDBRST )
!DO JEV = 1, 32
!  iev1R(jev) = IBITS(IREV1, Jev-1, 1)
!  iev2R(jev) = IBITS(IREV2, Jev-1, 1)
!ENDDO
!iactR = IBITS(IRSTA, 0, 1)
!ipasR = IBITS(IRSTA, 1, 1)
!irejR = IBITS(IRSTA, 2, 1)
!iblkR = IBITS(IRSTA, 3, 1)

!print *,'********************* ODB REPORT ********************'
!print 9988,IDATE,ITIME,zlat,zlon,CLSTID
!9988 FORMAT(1X,2(i10,1x),2(F10.2,1X),A8)
!print 9987,IRSTA,IREV1,IREV2
!9987 FORMAT(1X,3(O22,1X))
!print 9986,iactR,ipasR,irejR,iblkR
!9986 FORMAT(1X,4(I1,1X))
!print 9985,iev1R
!9985 FORMAT(1X,32(I1,1X))
!print 9985,iev2R

!iev1D(:) = 0
!iev2D(:) = 0

!DO JB = MLNKH2B(KAIRP),MLNKH2B(KAIRP+1)-1
!  ieno  = ROBODY(JB,MDBESQN)
!  ivno  = ROBODY(JB,MDBVNM)
!  ivco  = ROBODY(JB,MDBVCO)
!  idsta = ROBODY(JB,MDBDSTA)
!  idev1 = ROBODY(JB,MDBDEV1)
!  idev2 = ROBODY(JB,MDBDEV2)
!  iflg  = ROBODY(JB,MDBFLG)
!  idRDB = ROBODY(JB,MDBRDFL)
!  iactD = IBITS(idsta, 0, 1)
!  ipasD = IBITS(idsta, 1, 1)
!  irejD = IBITS(idsta, 2, 1)
!  iblkD = IBITS(idsta, 3, 1)
!  DO JEV = 1, 32
!    iev1D(jev) = IBITS(idev1, Jev-1, 1)
!    iev2D(jev) = IBITS(idev2, Jev-1, 1)
!  ENDDO

!  zval  = ROBODY(JB,MDBVAR)
!  zp    = ROBODY(JB,MDBPPP)
!  zprl  = ROBODY(JB,MDBPRL)
!  zomf  = ROBODY(JB,MDBOMF)
!  zfge  = ROBODY(JB,MDBOMN)
!  zpe   = ROBODY(JB,MDBPER)
!  zoe   = ROBODY(JB,MDBOER)
!  zre   = ROBODY(JB,MDBRER)
!  zfe   = ROBODY(JB,MDBFOE)

!print 8899,ieno,ivno,ivco,zp,zprl,zval,zomf,zfge
!8899 format(1x,3(i5,1x),5(f10.2,1x))
!print 8895,zpe,zoe,zre,zfe,zf1,zf2
!8895 format(1x,6(f10.2,1x))
!print 8898,idsta,idev1,idev2
!8898 format(1x,3(o22,1x))
!print 8897,iactD,ipasD,irejD,iblkD
!8897 format(1x,4(I1,1x))
!print 8896,idev1
!8896 format(1x,32(I1,1x))
!print 8896,idev2
!ENDDO

!*          5.1         UPDATE HEADER PARAMETERS (IF ANY)

!*          5.2         ODB REPORT UPDATED 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('AIREPIN',1,ZHOOK_HANDLE)
END SUBROUTINE AIREPIN
