SUBROUTINE MERTSIN(KTSP ,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE MERTSIN - PROCESS INPUT TOVS/SATEM

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

!     PURPOSE
!     -------

!        PROCESS INPUT TOVS/SATEM

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

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

!        MERTSIN IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

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

!        PRLMCHK  - PRELIMINARY CHECKS
!        GETSETE  - GET/SET BODY ENTRY
!        ERRSTAT  - OBS. ERROR
!        TOSABE   - UPDATE TOVS/SATEM 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 
!        A.Fouilloux   31-Mar-2010 CY36R4 Cleaning remove NSSMI

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDB
USE YOMCOCTP , ONLY : NSTMCD   ,NSTOVS   
USE YOMSTRE  , ONLY : NCLEAR   ,NPCLOU   ,NCLOUD
USE YOMCMHDR , ONLY : NSMI4BP  ,NSMI4OC  ,NTOAABP  ,NTOAAOC  ,&
 & NTOBBBP  ,NTOBBOC  
USE YOMCMBDY , ONLY : NCMBPFBP ,NCMBVFBP ,NCMBFLBP ,NCMBFLOC
USE YOMANCS  , ONLY : NMDI     ,RMDI     ,RDEGREES ,RADIANS  ,EPSMAC   ,&
 & RRD      ,NBITS    ,RUPMLIM  ,RDWMLIM    
USE YOMCST   , ONLY : RTT      ,RG
USE YOMVNMB  , ONLY : NVNUMB
USE YOMNMCOD , ONLY : NPRESVC
USE YOMMKODB , ONLY :  NMKCMVSE ,LTOTREM
USE YOETHF   , ONLY : RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES  
USE YOMLUN   , ONLY : NULOUT

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KTSP 
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, &
 & ISTID, INSTYP, ISCHAN, ISINS, ISDPT, IREPST  
INTEGER(KIND=JPIM) :: IRETYP, INSSPEC, IAIRFP, II4BP, II4BO, II4, &
 & IBPARM, IABP, IABO, IBBP, IBBO, IAPARM  

INTEGER(KIND=JPIM) :: ITEMP, JT, &
 & ITTSTAT, ITTEVEN1, ITTEVEN2, ITTRDBFL, ITTANFLG, &
 & ITTPFLG, ITTFLG  
INTEGER(KIND=JPIM) :: IPWC, JPWC, &
 & IPWCSTAT, IPWCEVEN1, IPWCEVEN2, IPWCRDBFL, IPWCANFLG, &
 & IPWCPFLG, IPWCFLG  
INTEGER(KIND=JPIM) :: IDZ, JDZ, &
 & IDZSTAT, IDZEVEN1, IDZEVEN2, IDZRDBFL, IDZANFLG, &
 & IDZPFLG, IDZFLG  
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  
REAL(KIND=JPRB) :: ZGORD

REAL(KIND=JPRB) :: ZTTPRES, ZTTPRL, ZT, &
 & ZTTOERR, ZTTPERR, ZTTFERR, ZTTRERR, &
 & ZTTFGER, ZTTFGCC1, ZTTFGCC2, &
 & ZTTFGDEP, ZTTANDEP  
REAL(KIND=JPRB) :: ZPWCPRES, ZPWCPRL, ZPWC, &
 & ZPWCOERR, ZPWCPERR, ZPWCFERR, ZPWCRERR, &
 & ZPWCFGER, ZPWCFGCC1, ZPWCFGCC2, &
 & ZPWCFGDEP, ZPWCANDEP  
REAL(KIND=JPRB) :: ZDZPRES, ZDZPRL, ZDZ  , &
 & ZDZOERR, ZDZPERR, ZDZFERR, ZDZRERR, &
 & ZDZFGER, ZDZFGCC1, ZDZFGCC2, &
 & ZDZFGDEP, ZDZANDEP  
REAL(KIND=JPRB) :: ZAU, ZAUPRES, ZAUPRL, &
 & ZAUPERR, ZAUOERR, ZAUFERR, ZAURERR, &
 & ZAUFGER, ZAUFGCC1, ZAUFGCC2, &
 & ZAUFGDEP, ZAUANDEP  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "getsete.intfb.h"
#include "obinssp.intfb.h"
#include "ppvafl.intfb.h"
#include "prlmchk.intfb.h"
#include "tosabe.intfb.h"

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

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

!        1.             PRESET
!                       ------

!*          1.1         GET OBS. PARAMETERS FROM "HEADER"

!*          1.1.0       BASIC ONES

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

!*          1.1.1       SATEM INSTRUMENT

ISINS  = SATHDR(KTSP ,MDB_SATEM_INSTDATA_AT_HDR)

!*          1.1.2       DATA PROCESSING TECHNIQUE

ISDPT  = SATHDR(KTSP ,MDB_SATEM_DATAPROC_AT_HDR)

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

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

!*          2.1         DEFINE INSTRUMENT TYPE

INSSPEC = 0
INSTYP  = 0
IAIRFP  = NMDI
ISCHAN  = NMDI
!ISINS   = NMDI
!ISDPT   = NMDI
!CALL OBINSTP(ZLAT,ZLON,ISTID,&
!            &KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
!            &ISCHAN,ISINS,ISDPT,&
!            &INSTYP)
CALL OBINSSP(ZLAT,ZLON,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & ISTID,IAIRFP,ISCHAN,ISINS,ISDPT,&
 & INSSPEC,INSTYP)  

!*          2.2         SET INSTRUMENT TYPE INTO OBSERVATION
!                       CHARACTERISTICS

ROBHDR(KTSP ,MDB_INSTTYPE_AT_HDR) = INSTYP

!*          2.3         FIND/SET RETRIVEL TYPE

IRETYP = NMDI
IF(KCDTYP == NSTMCD)                  THEN
  IF(INSTYP == 84.OR.INSTYP == 85.OR.INSTYP == 88.OR.INSTYP == 89    )   THEN
    II4BP = NSMI4BP(KCDSQNO)
    II4BO = NSMI4OC(KCDSQNO)
    II4   = IBITS (INSSPEC,II4BP,II4BO)
    IF(II4 /= NBITS (II4BO))               THEN
      IRETYP = II4
    ENDIF
  ENDIF
ELSEIF(KCDTYP == NSTOVS) THEN
  IABP   = NTOAABP(KCDSQNO)
  IABO   = NTOAAOC(KCDSQNO)
  IAPARM = IBITS (INSSPEC,IABP ,IABO )
  IBBP   = NTOBBBP(KCDSQNO)
  IBBO   = NTOBBOC(KCDSQNO)
  IBPARM = IBITS (INSSPEC,IBBP ,IBBO )
  IF(IAPARM == 1) THEN
    IRETYP = NCLEAR
  ELSEIF(IAPARM == 2.AND.IBPARM == 1)  THEN
    IRETYP = NPCLOU
  ELSE
    IRETYP = NCLOUD
  ENDIF
ENDIF
ROBHDR(KTSP ,MDB_RETRTYPE_AT_HDR) = IRETYP
!*
!-----------------------------------------------------------------------

!        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(KTSP ,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

!*          3.2.1       WE ACCEPT: T, DZ and PWC

ALL_DATA_LOOP: DO JALL = MLNKH2B(KTSP),MLNKH2B(KTSP+1)-1
  IF(ROBODY(JALL  ,MDBVNM) == NVNUMB(  8) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB(  4) .OR. &
   & ROBODY(JALL  ,MDBVNM) == NVNUMB(  6)       ) GO TO 322  

!*          3.3.1.0     UNEXPECTED VARIABLE

  IAUX = JALL
  IVNM = ROBODY(IAUX  ,MDBVNM)
  WRITE(NULOUT,'('' MERTSIN; 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     VARIBLE

  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 TOSABE (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 32111  
    ROBODY(JALL ,MDBPPP ) = RMDI
  ENDIF
  32111 CONTINUE
  IF(ROBODY(JALL  ,MDBPRL) /= RMDI ) THEN
    IF(.NOT.(ROBODY(JALL ,MDBPRL) >= RDWMLIM.AND.&
     & ROBODY(JALL ,MDBPRL) <= RUPMLIM     )) GO TO 3212  
    ROBODY(JALL ,MDBPRL ) = RMDI
  ENDIF

!*          3.2.1.2     VARIABLE

  3212 CONTINUE
! swap pressures round for now
! ZTOPP = ROBODY(JALL ,MDBPPP )
! ZRELP = ROBODY(JALL ,MDBPRL )
! ROBODY(JALL ,MDBPPP ) = ZRELP
! ROBODY(JALL ,MDBPRL ) = ZTOPP
  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

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

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

4 CONTINUE
ZWEIGHT = 1.0_JPRB

!*          4.1         LOOK FOR DATA

!*          4.2         VIRTUAL TEMPERATURE

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

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

    ZTTPRES= ROBODY(ITEMP,MDBPPP)
    ZTTPRL = ROBODY(ITEMP,MDBPRL)
    ITTPFLG= IBITS(INT(ROBODY(ITEMP,MDBRDFL)),NCMBPFBP+NCMBFLBP,NCMBFLOC)
    IF(ZTTPRES == RMDI .OR. ZTTPRL == RMDI ) THEN
      ITTSTAT = ICHSTAT_REJECT( ITTSTAT )
    ENDIF

!*          4.2.2       VARIABLE

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

!*          4.2.3       ERROR STATISTICS

!*          4.2.4       STATUS, EVENTS, FLAGS

    IF(NMKCMVSE(  8,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 TOSABE (ITEMP   ,&
     & IVNM    ,NPRESVC ,ZTTPRES ,ZTTPRL  ,ZT      ,&
     & ZTTOERR ,ZTTPERR ,ZTTFERR ,ZTTRERR ,&
     & ZTTFGER ,ZTTFGCC1,ZTTFGCC2,&
     & ZTTFGDEP,ZTTANDEP,&
     & ITTSTAT ,ITTEVEN1,ITTEVEN2,ITTRDBFL,ITTANFLG  )  
  ENDIF
ENDDO T_LOOP

!*          4.3         PWC

430 CONTINUE
PWC_LOOP: DO JPWC  = MLNKH2B(KTSP ),MLNKH2B(KTSP +1)-1
  IF(ROBODY(JPWC ,MDBVNM) == NVNUMB(  6)) THEN
    IPWC  = JPWC
    IVNM  = NVNUMB(  6)

!*          4.3.0       SET/GET

    CALL GETSETE(IPWC,&
     & ZPWCPRES,ZPWCPRL,ZPWC,&
     & ZPWCFGER,ZPWCFGCC1,ZPWCFGCC2,&
     & ZPWCPERR,ZPWCOERR,ZPWCFERR,ZPWCRERR,&
     & ZPWCFGDEP,ZPWCANDEP,&
     & IPWCSTAT,IPWCEVEN1,IPWCEVEN2,IPWCRDBFL,IPWCANFLG,&
     & IPWCFLG,IPWCPFLG)  

    ZPWCPRES= ROBODY(IPWC,MDBPPP)
    ZPWCPRL = ROBODY(IPWC,MDBPRL)
    IPWCPFLG= IBITS(INT(ROBODY(IPWC ,MDBRDFL)),NCMBPFBP+NCMBFLBP,NCMBFLOC)
    IF(ZPWCPRES == RMDI .OR. ZPWCPRL == RMDI ) THEN
      IPWCSTAT = ICHSTAT_REJECT( IPWCSTAT )
    ENDIF

!*          4.3.2       VARIABLE

    ZPWC    = ROBODY(IPWC ,MDBVAR  )
    IPWCFLG = IBITS(IPWCRDBFL,NCMBVFBP+NCMBFLBP,NCMBFLOC)
    IF(ROBODY(IPWC ,MDBVAR) /= RMDI ) THEN
      IPWCSTAT = ICHSTAT_REJECT( IPWCSTAT )
    ENDIF

!*          4.3.3       ERROR STATISTICS

!*          4.3.4       STATUS, EVENTS, FLAGS

    IF(NMKCMVSE(  6,KCDSQNO,KOBSQNO) == 0)  THEN
      IPWCSTAT = ICHSTAT_REJECT( IPWCSTAT )
    ENDIF
    CALL PPVAFL(IPWCPFLG,IPWCFLG,IPWCRDBFL)

!*          4.3.5       DEPARTURES

!*          4.3.6       UPDATE BODY ENTRY

    CALL TOSABE (IPWC    ,&
     & IVNM    ,NPRESVC ,ZPWCPRES ,ZPWCPRL  ,ZPWC    ,&
     & ZPWCOERR ,ZPWCPERR ,ZPWCFERR ,ZPWCRERR ,&
     & ZPWCFGER ,ZPWCFGCC1,ZPWCFGCC2,&
     & ZPWCFGDEP,ZPWCANDEP,&
     & IPWCSTAT ,IPWCEVEN1,IPWCEVEN2,IPWCRDBFL,IPWCANFLG  )  
  ENDIF
ENDDO PWC_LOOP

!*          4.4         THICKNESS

440 CONTINUE
DZ_LOOP: DO JDZ   = MLNKH2B(KTSP ),MLNKH2B(KTSP +1)-1
  IF(ROBODY(JDZ  ,MDBVNM) == NVNUMB(  4)) THEN
    IDZ   = JDZ
    IVNM  = NVNUMB(  4)

!*          4.4.0       SET/GET

    CALL GETSETE(IDZ,&
     & ZDZPRES,ZDZPRL,ZDZ ,&
     & ZDZFGER,ZDZFGCC1,ZDZFGCC2,&
     & ZDZPERR,ZDZOERR,ZDZFERR,ZDZRERR,&
     & ZDZFGDEP,ZDZANDEP,&
     & IDZSTAT,IDZEVEN1,IDZEVEN2,IDZRDBFL,IDZANFLG,&
     & IDZFLG,IDZPFLG)  

!*          4.4.0.1     FIND VIRTUAL TEMPERATURE

    ITEMP = IDZ - 1
    IF(ROBODY(ITEMP,MDBVNM) /= NVNUMB(  8)) THEN
      WRITE(NULOUT,'(''0MERTSIN; DZ IS NOT AFTER Vt ENTRY'')')
      CALL FLUSH(NULOUT)
      IDZSTAT = ICHSTAT_REJECT( IDZSTAT )
    ELSE
      ZTTPRES= ROBODY(ITEMP,MDBPPP)
      ZTTPRL = ROBODY(ITEMP,MDBPRL)
      ITTPFLG= IBITS(INT(ROBODY(ITEMP,MDBRDFL)),NCMBPFBP+NCMBFLBP,NCMBFLOC)
      ZT     = ROBODY(ITEMP,MDBVAR  )
      ITTFLG = IBITS(INT(ROBODY(ITEMP,MDBRDFL)),NCMBVFBP+NCMBFLBP,NCMBFLOC)
      IF(ZTTPRES == RMDI .OR. ZTTPRL == RMDI .OR. ZT == RMDI ) THEN
        IDZSTAT = ICHSTAT_REJECT( IDZSTAT )
      ELSE
  
!*          4.4.2       VARIABLE

        ZDZPRES = ZTTPRES
        ZDZPRL  = ZTTPRL
        IDZPFLG =  ITTPFLG
        ZGORD = RG/RRD
        IF(ZDZPRES > 0.0_JPRB .AND. ZDZPRL > 0.0_JPRB     )        THEN
          ZDZ   = (ZT*(LOG(ZDZPRL)-LOG(ZDZPRES))/ZGORD) * RG
          IDZFLG= MAX(ITTFLG,ITTPFLG)
        ELSE
          IDZSTAT = ICHSTAT_REJECT( IDZSTAT )
        ENDIF

!*          4.4.3       ERROR STATISTICS

      ENDIF
    ENDIF

!*          4.4.4       STATUS, EVENTS, FLAGS

    IF(NMKCMVSE(  4,KCDSQNO,KOBSQNO) == 0)  THEN
      IDZSTAT = ICHSTAT_REJECT( IDZSTAT )
    ENDIF
    CALL PPVAFL(IDZPFLG,IDZFLG,IDZRDBFL)

!*          4.4.5       DEPARTURES

!*          4.4.6       UPDATE BODY ENTRY

    CALL TOSABE (IDZ     ,&
     & IVNM    ,NPRESVC ,ZDZPRES ,ZDZPRL  ,ZDZ     ,&
     & ZDZOERR ,ZTTPERR ,ZDZFERR ,ZDZRERR ,&
     & ZDZFGER ,ZTTFGCC1,ZDZFGCC2,&
     & ZDZFGDEP,ZTTANDEP,&
     & IDZSTAT ,ITTEVEN1,IDZEVEN2,IDZRDBFL,IDZANFLG  )  
  ENDIF
ENDDO DZ_LOOP
!*
!-----------------------------------------------------------------------

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

5 CONTINUE

!*          5.1         UPDATE HEADER PARAMETERS (IF ANY)

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