SUBROUTINE NSCATIN(KSCATP,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE NSCATIN - PROCESS INPUT NSCAT

!        M. LEIDNER     AER     04/03/99
!        D. VASILJEVIC  ECMWF   12/12/00

!     PURPOSE
!     -------

!        PROCESS INPUT NSCAT

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

!        CALL NSCATIN(KSCATP,
!                     KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,
!                     LDMESAGE,LDTAKEN)

!     WHERE:
!            KSCATP   = SCAT REP POINTER

!        NSCATIN IS CALLED BY SUBROUTINE MKCMARPL

!     METHOD

!     ------

!        THE CMA SCAT. RECORD IS BUILT UP FROM THE EXPANDED BUFR ARRAYS.

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

!        ERS1IF   - SCAT. PROCESSING INTERFACE
!        PRLMCHK  - PRELIMINARY CHECKS
!        GETSETE  - GET/SET BODY ENTRY
!        ERRSTAT  - OBS. ERROR
!        SCATBE   - UPDATE SCAT. 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 PARERSCA
USE YOMDB
USE YOMVNMB  , ONLY : NVNUMB
USE YOMANCS  , ONLY : NMDI     ,RMDI     ,RADIANS,&
 & EPSMAC   ,&
 & RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES    ,RTT      ,RUPMLIM  ,RDWMLIM    
USE YOMNMCOD , ONLY : NSCACVC
USE YOMMKODB , ONLY : LERRPERS, NMKCMVSE ,LTOTREM  
USE YOMNMEV  , ONLY : NR2EVENT
USE YOMCST   , ONLY :        RPI
USE YOMLUN   , ONLY : NULOUT
USE LOCAL_TRAFOS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSCATP 
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 
!-----------------------------------------------------------------------

REAL(KIND=JPRB) ::&
 & ZRETU  (     JPBRSWI)  ,  ZRETV  (JPBRSWI)&
 & ,  ZDU    (JPBRSWI)  ,   ZDV    (JPBRSWI)&
 & ,ZWSPD(       JPBRNSWI) ,  ZWDIR  (JPBRNSWI) ,  ZWMLE  (JPBRNSWI)&
 & ,ZSPDTMP(     JPBRNSWI) ,  ZDIRTMP(JPBRNSWI) ,  ZMLETMP(JPBRNSWI)&
 & ,ZUTMP  (    JPBRNSWI)  ,  ZVTMP (JPBRNSWI)  

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

INTEGER(KIND=JPIM) :: &
 & IDUFLAG  (JPBRSWI),IDVFLAG  (JPBRSWI), IREORDER( JPBRNSWI)  

INTEGER(KIND=JPIM) :: ICELL, JSS, &
 & IEVENTNO, IEVENTVL, &
 & JWIND, IWINSECE,&
 & IOBDATE, IWDIRF (2), IWSPDF (2), &
 & IOBTIME, &
 & ISTID, IREPST, IWQF, JAMB, IAMB, JAMB1, &
 & IVECFL, &
 & IVERCO, IVNM, INDEX, &
 & INORUS, INORVS, INODS,INOFS, &
 & INORUSA, INORVSA, INODSA, INOFSA, &
 & ISATID, ICELLNO, IPROFLAG  

INTEGER(KIND=JPIM) :: IDD, &
 & IDDSTAT, IDDEVEN1, IDDEVEN2, IDDRDBFL, IDDANFLG, &
 & IDDPFLG, IDDFLG, IDDVERCO  
INTEGER(KIND=JPIM) :: IFF, &
 & IFFSTAT, IFFEVEN1, IFFEVEN2, IFFRDBFL, IFFANFLG, &
 & IFFPFLG, IFFFLG, IFFVERCO  
INTEGER(KIND=JPIM) :: IU   , JU   , &
 & IUUSTAT, IUUEVEN1, IUUEVEN2, IUURDBFL, IUUANFLG, &
 & IUUPFLG, IUUFLG, IUUVERCO  
INTEGER(KIND=JPIM) :: IV   , JV   , &
 & IVVSTAT, IVVEVEN1, IVVEVEN2, IVVRDBFL, IVVANFLG, &
 & IVVPFLG, IVVFLG, IVVVERCO  
INTEGER(KIND=JPIM) :: IAUX, JALL, &
 & IAUSTAT, IAUEVEN1, IAUEVEN2, IAURDBFL, IAUANFLG, &
 & IAUPFLG, IAUFLG  , IAUVERCO  

LOGICAL :: LLACCEPT, LLMESAGE, LLFAIL, LLREMOVE
LOGICAL :: LLRU, LLRV, LLD, LLF

CHARACTER (LEN = 8) :: CLSTID

REAL(KIND=JPRB) ::  ZDIRSKI, &
 & ZLAT, ZLON, &
 & ZTIMDIF, &
 & ZWE, ZDIR1, ZDIR2, ZDEL, ZLARGEST, &
 !         &ZWE, ZWSPD (2), ZWMLE (2), ZWDIR (2)
 & ZAZIMUTH, ZINCIDEN, ZKP, ZINVERSF, ZDIRSKIL  

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) :: ZAU, ZAUPRES, ZAUPRL, &
 & ZAUPERR, ZAUOERR, ZAUFERR, ZAURERR, &
 & ZAUFGER, ZAUFGCC1, ZAUFGCC2, &
 & ZAUFGDEP, ZAUANDEP  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "finoerr.intfb.h"
#include "getsete.intfb.h"
#include "ppvafl.intfb.h"
#include "prlmchk.intfb.h"
#include "scatbe.intfb.h"

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

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

!        0.             PRESET
!                       ------

IF (LHOOK) CALL DR_HOOK('NSCATIN',0,ZHOOK_HANDLE)
ZWE = 2.0_JPRB    ! observational error for scat winds u- and v-components
ZDIRSKI = RMDI ! directional error skill to missing value

!*          1.1         ARRAYS

IWINSECE = 2

!*          1.2         COUNTERS

INORUS  = 2
INORVS  = 2
INODS   = 2
INOFS   = 2

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

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

!*          2.1         GET OBS. PARAMETERS FROM BUFR "HEADER"

ZLAT   = ROBHDR(KSCATP,MDBLAT)
ZLON   = ROBHDR(KSCATP,MDBLON)
IOBDATE= ROBHDR(KSCATP,MDBDAT)
IOBTIME= ROBHDR(KSCATP,MDBETM)
CLSTID = TRANSFER(ROBHDR(KSCATP,MDBSID), CLSTID)
ISTID  = 0
!WRITE(CLSTID,'(3X,I5.5)') ISTID
READ(CLSTID(1:8),'(A8)') ISTID
IREPST = ROBHDR(KSCATP,MDBRST)

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

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

!*          3.1         IS POSITION PRESENT AND WITHIN LIMITS

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

!*          3.1.0       UPDATE REPORT STATUS

ROBHDR(KSCATP,MDBRST) = IREPST

!*          3.1.1       FAILED REPORT

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

!*          3.1.2       REMOVED REPORT

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

!*          3.2         REPORT EXTRA INFO

!*          3.2.1       SATHDR

!*          3.2.1.1     SAT ID

ISATID   = SATHDR(KSCATP,MDB_SATID_AT_SAT)

!*          3.2.1.3     CELL NO.

ICELLNO  = SATHDR(KSCATP,MDBSCCNO)

!*          3.2.1.4     PRODUCT FLAG

IPROFLAG = SATHDR(KSCATP,MDBSCPFL)

!*          3.3         IS OBS. WANTED

!*          3.3.1       BY THINNING CRITERIUM
!     TBD - by some method, LLACCEPT must be set to FALSE, if a datum
!           is to be reject.  If data are to be accepted.
!           See "ersin.F90" for example.

LLACCEPT = .TRUE.

!*          3.4         IS SATELLITE IDENTIFIER PRESENT AND OK

IF(ISTID /= 1.AND.ISTID /= 2.AND.ISTID /= 280)   THEN
  WRITE(NULOUT,'(''0NSCATIN ; SATELLITE IDENTIFIER MISSING;'',&
   & '' HENCE REPORT NOT CREATED'')')  
  WRITE(NULOUT,'('' FOUND SATELLITE IDENTIFIER: '',I10)')  ISTID
  LLMESAGE = .TRUE.
  GO TO 8
ENDIF

!*          3.5         CHECK WIND VECTOR CELL NUMBER

ICELL = ROBHDR(KSCATP,MDBSCCNO)
IF(ICELL < 1.OR.ICELL > 24     )  THEN
  WRITE(NULOUT,'(''0NSCATIN; CELL NO. OUT OF RANGE;'',&
   & '' HENCE REPORT NOT CREATED'')')  
  WRITE(NULOUT,'('' ICELL  : '',I10)') ICELL
  LLMESAGE = .TRUE.
  GO TO 8
ENDIF

!*          3.6         CHECK WIND VECTOR CELL QUALITY FLAG

IWQF  = NMDI
!IWQF  = ROBHDR(KSCATP,        )
IF(IWQF < 0.OR. IWQF > 4     )  THEN
  WRITE(NULOUT,'(''0NSCATIN; WVC QUAL OUT OF RANGE;'',&
   & '' HENCE REPORT NOT CREATED'')')  
  WRITE(NULOUT,'('' IWQF  : '',I10)') IWQF
  LLMESAGE = .TRUE.
ENDIF

!*          3.7         CHECK NUMBER OF RETRIEVED AMBIGUITIESF

IAMB = NMDI
!IAMB = ROBHDR(KSCATP,         )
IF(IAMB < 0.OR.IAMB > 4     )  THEN
  WRITE(NULOUT,'(''0NSCATIN; NUM AMBIGUITIES OUT OF RANGE;'',&
   & '' HENCE REPORT NOT CREATED'')')  
  WRITE(NULOUT,'('' IAMB  : '',I10)') IAMB
  LLMESAGE = .TRUE.
ENDIF

!*          3.8         QC USING THE SUPPLIED WVC QUALITY FLAG

IF (IWQF /= 0) GO TO 8

!*          3.9         BODY PARAMETERS

!*          3.9.1       WE ACCEPT: 4(ret)xU, 4(ret)xV, (3xSigma0),
!                                  4xDD, 4xFF, 4xU, 4xV

LLRU    = .FALSE.
LLRV    = .FALSE.
LLD     = .FALSE.
LLF     = .FALSE.
INORUSA = 0
INORVSA = 0
INODSA  = 0
INOFSA  = 0

ALL_DATA_LOOP: DO JALL = MLNKH2B(KSCATP),MLNKH2B(KSCATP+1)-1
  IF(     ROBODY(JALL  ,MDBVNM) == NVNUMB( 62)) THEN
    INORUSA = INORUSA + 1
    LLRU    = .TRUE.
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 61)) THEN
    INORVSA = INORVSA + 1
    LLRV    = .TRUE.
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 48)) THEN
    INODSA  = INODSA + 1
    LLD     = .TRUE.
  ELSEIF(ROBODY(JALL  ,MDBVNM) == NVNUMB( 49)) THEN
    INOFSA  = INOFSA + 1
    LLF     = .TRUE.
  ELSE
    IAUX = JALL
    IVNM = ROBODY(IAUX  ,MDBVNM)
    WRITE(NULOUT,'('' ERSIN; UNEXPECTED VARIABLE: '',I10)') IVNM

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

    ZAZIMUTH= SATBODY(IAUX,MDBSCBAA)
    ZINCIDEN= SATBODY(IAUX,MDBSCBIA)
    ZKP     = SATBODY(IAUX,MDBSCKP )
    ZINVERSF= SATBODY(IAUX,MDBSCRES)
    ZDIRSKIL= SATBODY(IAUX,MDBSCDIS)

!*          3.9.1.2     VARIABLE

    ZAU     = ROBODY(IAUX,MDBVAR)

!*          3.9.1.3     ERROR STATISTICS

!*          3.9.1.4     STATUS, EVENTS, FLAGS

    IAUSTAT = ICHSTAT_REJECT( IAUSTAT )

!*          3.9.1.5     DEPARTURES

!*          3.9.1.6     UPDATE BODY ENTRY

    CALL SCATBE (IAUX    ,&
     & IVNM    ,IAUVERCO,ZAUPRES ,ZAUPRL  ,ZAU     ,&
     & ZAUOERR ,ZAUPERR ,ZAUFERR ,ZAURERR ,&
     & ZAUFGER ,ZAUFGCC1,ZAUFGCC2,&
     & ZAUFGDEP,ZAUANDEP,&
     & IAUSTAT ,IAUEVEN1,IAUEVEN2,IAURDBFL,IAUANFLG,&
     !               &RMDI    ,RMDI    ,RMDI                        )
     & ZAZIMUTH,ZINCIDEN,ZKP     ,ZINVERSF,ZDIRSKIL)  
  ENDIF

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

!*          3.9.2.1     PRESSURE

  IF(ROBODY(JALL  ,MDBPPP) /= RMDI ) THEN
    IF((ROBODY(JALL ,MDBPPP) >= RDWMLIM.AND.&
     & ROBODY(JALL ,MDBPPP) <= RUPMLIM     )) &
     & ROBODY(JALL ,MDBPPP ) = RMDI  
  ENDIF

!*          3.9.2.2     VARIABLE

  IF(ROBODY(JALL  ,MDBVAR) /= RMDI ) THEN
    IF((ROBODY(JALL ,MDBVAR) >= RDWMLIM.AND.&
     & ROBODY(JALL ,MDBVAR) <= RUPMLIM     )) &
     & ROBODY(JALL ,MDBVAR ) = RMDI  
  ENDIF

!*          3.9.2.3     ERROR STATISTICS

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

!*          3.9.3       CLOSE ALL DATA LOOP

ENDDO ALL_DATA_LOOP

!*          3.9.4       CHECK IF ALL PRESENT

IF(.NOT.((LLRU .AND. INORUS == INORUSA ) .AND. &
   & (LLRV .AND. INORVS == INORVSA ) .AND. &
   & (LLD  .AND. INODS  == INODSA  ) .AND. &
   & (LLF  .AND. INOFS  == INOFSA  )        )) THEN  
  WRITE(NULOUT,'('' ERROR IN SUBROUTINE ERSIN; '')')
  WRITE(NULOUT,'(''        LLRU, INORUSA, INORUS: '',L1,1X,2(I1,1X))') &
   & LLRU,INORUSA,INORUS  
  WRITE(NULOUT,'(''        LLRV, INORVSA, INORVS: '',L1,1X,2(I1,1X))') &
   & LLRV,INORVSA,INORVS  
  WRITE(NULOUT,'(''        LLD, INODSA, INODS   : '',L1,1X,2(I1,1X))') &
   & LLD,INODSA,INODS  
  WRITE(NULOUT,'(''        LLF, INOFSA, INOFS: '',L1,1X,2(I1,1X))') &
   & LLF,INOFSA,INOFS  
  CALL ABOR1(' ERSIN : ')
ENDIF

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

!        4.             OBSERVATION VARIABLES AND THEIR FLAGS
!                       -------------------------------------

!*          4.1         FIRST PASS

INODSA = 0
INOFSA = 0
WIND_SECTION_1: DO JWIND = MLNKH2B(KSCATP),MLNKH2B(KSCATP+1)-1

!*          4.1.1       WIND DIRECTION

  IF(ROBHDR(JWIND,MDBVNM) == NVNUMB( 48))      THEN
    INODSA  = INODSA + 1
    IDD     = JWIND
    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  = 1.0_JPRB
    IDDVERCO = NSCACVC

    ZAZIMUTH= SATBODY(IDD ,MDBSCBAA)
    ZINCIDEN= SATBODY(IDD ,MDBSCBIA)
    ZKP     = SATBODY(IDD ,MDBSCKP )
    ZINVERSF= SATBODY(IDD ,MDBSCRES)
    ZDIRSKIL= SATBODY(IDD ,MDBSCDIS)

!*          4.1.1.2     VARIABLE

    ZDIR    = ROBODY(IDD ,MDBVAR)
    IF(ZDIR /= RMDI) THEN
      ZDIR = MOD(ZDIR + 180._JPRB, 360._JPRB)
    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 SCATBE (IDD      ,&
     & IVNM     ,IDDVERCO ,ZDDPRES ,ZDDPRL  ,ZDIR    ,&
     & ZDDOERR  ,ZDDPERR  ,ZDDOERR ,ZDDRERR ,&
     & ZDDFGER  ,ZDDFGCC1 ,ZDDFGCC2,&
     & ZDDFGDEP ,ZDDANDEP ,&
     & IDDSTAT  ,IDDEVEN1 ,IDDEVEN2,IDDRDBFL,IDDANFLG,&
     !               &RMDI     ,RMDI     ,RMDI                        )
     & ZAZIMUTH ,ZINCIDEN ,ZKP     ,ZINVERSF,ZDIRSKIL)  

!*          4.1.1.7     GET/STORE SOME EXTREA VALUES FOR LATER USE

    IWDIRF(INODSA) = IDDFLG
    ZWMLE (INODSA) = ROBODY(IDD  ,MDBSCRES)

!*          4.1.2       WIND SPEED

  ELSEIF(ROBHDR(JWIND,MDBVNM) == NVNUMB( 49)) THEN
    INOFSA  = INOFSA + 1
    IFF     = JWIND
    IVNM    = NVNUMB( 49)

!*          4.1.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  = 1.0_JPRB
    IFFVERCO = NSCACVC

    ZAZIMUTH= SATBODY(IFF ,MDBSCBAA)
    ZINCIDEN= SATBODY(IFF ,MDBSCBIA)
    ZKP     = SATBODY(IFF ,MDBSCKP )
    ZINVERSF= SATBODY(IFF ,MDBSCRES)
    ZDIRSKIL= SATBODY(IFF ,MDBSCDIS)

!*          4.1.2.2     VARIABLE

    ZSPD    = ROBODY(IFF ,MDBVAR)
    IF(ZDIR == RMDI) THEN
      IDDSTAT = ICHSTAT_REJECT( IDDSTAT )
    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 SCATBE (IFF      ,&
     & IVNM     ,IFFVERCO ,ZFFPRES ,ZFFPRL  ,ZSPD    ,&
     & ZFFOERR  ,ZFFPERR  ,ZFFOERR ,ZFFRERR ,&
     & ZFFFGER  ,ZFFFGCC1 ,ZFFFGCC2,&
     & ZFFFGDEP ,ZFFANDEP ,&
     & IFFSTAT  ,IFFEVEN1 ,IFFEVEN2,IFFRDBFL,IFFANFLG,&
     !               &RMDI     ,RMDI     ,RMDI                        )
     & ZAZIMUTH ,ZINCIDEN ,ZKP     ,ZINVERSF,ZDIRSKIL)  

!*          4.1.2.7     GET/STORE SOME EXTREA VALUES FOR LATER USE

    ZWSPD (INOFSA) = ROBODY(IFF  ,MDBVAR  )
    IWSPDF(INOFSA) = IFFFLG
  ENDIF

!*          4.1.3       CLOSE THE LOOP

ENDDO WIND_SECTION_1

!*          4.1.4       CONVERT SCAT(DIR,SPD) TO (U,V)

DO JSS = 1 , 4
  IDUFLAG(JSS) = MAX(IWSPDF(JSS),IWDIRF(JSS))
  IDVFLAG(JSS) = MAX(IWSPDF(JSS),IWDIRF(JSS))
  IF(ZWDIR(JSS) /= RMDI.AND.ZWSPD(JSS) /= RMDI    )        THEN
    ZDU  (JSS) = UCOM(ZWDIR(JSS),ZWSPD(JSS))
    ZDV  (JSS) = VCOM(ZWDIR(JSS),ZWSPD(JSS))
  ENDIF
ENDDO

!*          4.2         REORDER WINDS BY MLE AND DIRECTION
!                       To make better use of the ambiguities in IFS
!                       order by MLE

!*          4.2.0       INITIALIZE THE REORDERING ARRAYS

DO JSS = 1 , 4
  IREORDER(JSS) = NMDI
  ZSPDTMP (JSS) = RMDI
  ZDIRTMP (JSS) = RMDI
  ZMLETMP (JSS) = RMDI
  ZUTMP   (JSS) = RMDI
  ZVTMP   (JSS) = RMDI
ENDDO

!*          4.2.1       FIND AMBIGUITY WITH HIGHEST MLE

IF(IAMB >= 1) THEN
  ZLARGEST = RMDI
  INDEX    = NMDI
  DO JAMB = 1 , IAMB
    IF(ZWMLE(JAMB) > ZLARGEST) THEN
      ZLARGEST = ZWMLE(JAMB)
      INDEX    = JAMB
    ENDIF
  ENDDO

!*          4.2.2       ERROR CHECK RESULT AND STORE

  IF(INDEX == NMDI) THEN
    WRITE(NULOUT,'('' ERROR: HIGHEST MLE VALUE NOT '',&
     & ''FOUND IN NSCAT DATA'')')  
    CALL ABOR1('NSCATIN: HIGHEST MLE VALUE NOT FOUND IN NSCAT DATA')
  ELSE
    IREORDER(1) = INDEX
  ENDIF
ENDIF

!*          4.2.3       FIND AMBIGUITY WHICH OPPOSES THE FIRST
!*                      BY 180 DEGRESS (OR CLOSEST TO 180)

IF(IAMB >= 2) THEN
  ZLARGEST = RMDI
  INDEX    = NMDI
  ZDIR1    = ZWDIR(IREORDER(1))*RADIANS
  DO JAMB = 1 , IAMB
    ZDIR2 = ZWDIR(JAMB)*RADIANS
    ZDEL  = RPI - ABS(RPI - ABS(ZDIR1 - ZDIR2))
    IF(ZDEL > ZLARGEST)  THEN
      ZLARGEST = ZDEL
      INDEX    = JAMB
    ENDIF
  ENDDO

!*          4.2.4       ERROR CHECK RESULT AND STORE

  IF(INDEX == NMDI) THEN
    WRITE(NULOUT,'('' ERROR: HIGHEST MLE VALUE NOT '',&
     & ''FOUND IN NSCAT DATA'')')  
    CALL ABOR1('NSCATIN:HIGHEST MLE VALUE NOT FOUND IN NSCAT DATA')
  ELSE
    IREORDER(1) = INDEX
  ENDIF
ENDIF

!*          4.2.5       FIND AMBIGUITY WHICH OPPOSES THE FIRST
!*                      BY 180 DEGRESS (OR CLOSEST TO 180)

IF(IAMB >= 2) THEN
  ZLARGEST = RMDI
  INDEX    = NMDI
  ZDIR1    = ZWDIR(IREORDER(1))*RADIANS
  DO JAMB = 1 , IAMB
    ZDIR2 = ZWDIR(JAMB)*RADIANS
    ZDEL  = RPI - ABS(RPI - ABS(ZDIR1 - ZDIR2))
    IF(ZDEL > ZLARGEST)  THEN
      ZLARGEST = ZDEL
      INDEX    = JAMB
    ENDIF
  ENDDO

!*          4.2.6       ERROR CHECK RESULT AND STORE

  IF(INDEX == NMDI) THEN
    WRITE(NULOUT,'('' ERROR: 180-DEGREE AMBIGUITY NOT '',&
     & ''FOUND IN NSCAT DATA'')')  
    CALL ABOR1('NSCATIN:180-DEGREE AMBIGUITY NOT FOUND IN NSCAT DATA')
  ELSE
    IREORDER(2) = INDEX

!*          4.2.7       PRINT WARNING IF DIRECTIONAL DIFFERENCE IS
!*                      SMALLER THAN EXPECTED

    IF(ZLARGEST <  3._JPRB*RPI/4._JPRB) THEN
      WRITE(NULOUT,'('' WARNING: 180-DEGREE AMBIGUITY WITHIN '',&
       & ''3*PI/4 OF HIGHEST MLE AMBIGUITY'')')  
      WRITE(NULOUT,'('' IAMB      ='',I4)') IAMB
      WRITE(NULOUT,'('' IREORDER  ='',4I4)') IREORDER
      DO JAMB = 1 , IAMB
        WRITE(NULOUT,'('' JAMB    :'',I10)')   JAMB
        WRITE(NULOUT,'(''   ZWSPD :'',F8.3)')  ZWSPD(JAMB)
        WRITE(NULOUT,'(''   ZWDIR :'',F8.3)')  ZWDIR(JAMB)
        WRITE(NULOUT,'(''   ZWMLE :'',F8.3)')  ZWMLE(JAMB)
        WRITE(NULOUT,'(''   ZDU   :'',F8.3)')  ZDU  (JAMB)
        WRITE(NULOUT,'(''   ZDV   :'',F8.3)')  ZDV  (JAMB)
      ENDDO
    ENDIF
  ENDIF
ENDIF

!*         4.2.8        DETERMINE HIGHEST MLE OF REMAINING AMBIGUITIES

IF(IAMB >= 3) THEN
  ZLARGEST = RMDI
  INDEX   = NMDI
  DO JAMB = 1 , IAMB

!*        4.2.9         IF THIS AMBIGUITY IS ALREADY REORDERED, SKIP
 
    DO JAMB1 = 1 , IAMB
      IF(JAMB == IREORDER(JAMB1)) CYCLE
    ENDDO

    IF(ZWMLE(JAMB) > ZLARGEST) THEN
      ZLARGEST = ZWMLE(JAMB)
      INDEX = JAMB
    ENDIF
  ENDDO

!*        4.2.10        ERROR CHECK RESULT AND STORE

  IF(INDEX == NMDI) THEN
    WRITE(NULOUT,'('' ERROR: 3rd RANKED AMBIGUITY NOT '',&
     & ''FOUND IN NSCAT DATA'')')  
    CALL ABOR1('NSCATIN:3rd RANKED AMBIGUITY NOT FOUND IN NSCAT DATA')
  ELSE
    IREORDER(3) = INDEX
  ENDIF
ENDIF

!*        4.2.11       FILL IN LAST AMBIGUITY, IF PRESENT

IF(IAMB == 4) THEN
  INDEX = NMDI
  DO  JAMB = 1 , IAMB

!*        4.2.12       IF THIS AMBIGUITY IS ALREADY REORDERED, SKIP

    DO JAMB1 = 1 , IAMB
      IF(JAMB == IREORDER(JAMB1)) CYCLE
    ENDDO

    INDEX = JAMB
  ENDDO

!*        4.2.13        ERROR CHECK RESULT AND STORE

  IF(INDEX == NMDI) THEN
    WRITE(NULOUT,'('' ERROR: 4th RANKED AMBIGUITY NOT '',&
     & ''FOUND IN NSCAT DATA'')')  
    CALL ABOR1('NSCATIN:4th RANKED AMBIGUITY NOT FOUND IN NSCAT DATA')
  ELSE
    IREORDER(4) = INDEX
  ENDIF
ENDIF

!*        4.2.14       REORDER WIND COMPONENTS AND MLE

DO JAMB = 1 , IAMB
  ZSPDTMP(JAMB) = ZWSPD(IREORDER(JAMB))
  ZDIRTMP(JAMB) = ZWDIR(IREORDER(JAMB))
  ZMLETMP(JAMB) = ZWMLE(IREORDER(JAMB))
  ZUTMP  (JAMB) = ZDU  (IREORDER(JAMB))
  ZVTMP  (JAMB) = ZDV  (IREORDER(JAMB))
ENDDO

DO JAMB = 1 , IAMB
  ZWSPD(JAMB) = ZSPDTMP(JAMB)
  ZWDIR(JAMB) = ZDIRTMP(JAMB)
  ZWMLE(JAMB) = ZMLETMP(JAMB)
  ZRETU(JAMB) = ZUTMP  (JAMB)
  ZRETV(JAMB) = ZVTMP  (JAMB)
ENDDO

WRITE(NULOUT,'('' AFTER REORDERING: '')')
DO JSS = 1 , 4
  WRITE(NULOUT,'('' JSS     :'',I10)')   JSS
  WRITE(NULOUT,'(''   ZWSPD :'',F8.3)')  ZWSPD(JSS)
  WRITE(NULOUT,'(''   ZWDIR :'',F8.3)')  ZWDIR(JSS)
  WRITE(NULOUT,'(''   ZWMLE :'',F8.3)')  ZWMLE(JSS)
  WRITE(NULOUT,'(''   ZRETU :'',F8.3)')  ZRETU(JSS)
  WRITE(NULOUT,'(''   ZRETV :'',F8.3)')  ZRETV(JSS)
ENDDO
!*
!-----------------------------------------------------------------------

!        5.             MODIFY OBSERVATION HEADER
!                       -------------------------

!*          5.4.1       THINNING EVENT

IF(.NOT.LLACCEPT)   THEN
  IEVENTNO = NR2EVENT( 1,KOBSQNO)
  IEVENTVL = 1
! CALL SRGEVENT('SET E',KHEADER(IEV2WR ),IEVENTNO,IEVENTVL)
ENDIF
!*
!-----------------------------------------------------------------------

!        6.             MODIFY OBSERVATION BODY
!                       -----------------------

!*          6.0         VERTICAL COORDINATE

IVERCO = NSCACVC
IVECFL = 0

!*          6.1         RETREIVED U COMPONENTS

INORUSA  = 0
U_COMPONENTS: DO JU = MLNKH2B(KSCATP),MLNKH2B(KSCATP+1)-1
  IF(ROBODY(JU,MDBVNM) == NVNUMB( 62)) THEN
    INORUSA  = INORUSA + 1
    IU       = JU
    IVNM     = NVNUMB( 62)
 
!*          6.1.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  = 1.0_JPRB
    IUUVERCO = NSCACVC
    IUUFLG   = IDUFLAG(INORUSA)
    IUUPFLG  = 0

    ZAZIMUTH= SATBODY(IU  ,MDBSCBAA)
    ZINCIDEN= SATBODY(IU  ,MDBSCBIA)
    ZKP     = SATBODY(IU  ,MDBSCKP )
    ZINVERSF= SATBODY(IU  ,MDBSCRES)
    ZDIRSKIL= SATBODY(IU  ,MDBSCDIS)

!*          6.1.2       VARIABLE

    ZU      = ZRETU(INORUSA)
    IF(ZU == RMDI) THEN
      IUUSTAT = ICHSTAT_REJECT( IUUSTAT )
    ENDIF

!*          6.1.3       ERROR STATISTICS

    ZUUOERR = ZWE
    CALL FINOERR(ZUUPERR,ZUUOERR,ZUUFERR)

!*          6.1.4       STATUS, EVENTS, FLAGS

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

!*          6.1.5       DEPARTURES

!*          6.1.6       UPDATE BODY ENTRY

    CALL SCATBE (IU            ,&
     & IVNM          ,IUUVERCO ,ZUUPRES ,ZUUPRL  ,ZU      ,&
     & ZUUOERR       ,ZUUPERR  ,ZUUOERR ,ZUURERR ,&
     & ZUUFGER       ,ZUUFGCC1 ,ZUUFGCC2,&
     & ZUUFGDEP      ,ZUUANDEP ,&
     & IUUSTAT       ,IUUEVEN1 ,IUUEVEN2,IUURDBFL,IUUANFLG,&
     !               &ZWMLE(INORUSA),RMDI     ,ZDIRSKI                     )
     & ZAZIMUTH      ,ZINCIDEN ,ZKP     ,ZINVERSF,ZDIRSKIL)  

  ENDIF
ENDDO U_COMPONENTS

!*          6.2         RETREIVED V COMPONENTS

INORVSA  = 0
V_COMPONENTS: DO JV = MLNKH2B(KSCATP),MLNKH2B(KSCATP+1)-1
  IF(ROBODY(JV,MDBVNM) == NVNUMB( 61)) THEN
    INORVSA  = INORVSA + 1
    IV       = JV
    IVNM     = NVNUMB( 61)

!*          6.2.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  = 1.0_JPRB
    IVVVERCO = NSCACVC
    IVVFLG   = IDVFLAG(INORVSA)
    IVVPFLG  = 0

    ZAZIMUTH= SATBODY(IV  ,MDBSCBAA)
    ZINCIDEN= SATBODY(IV  ,MDBSCBIA)
    ZKP     = SATBODY(IV  ,MDBSCKP )
    ZINVERSF= SATBODY(IV  ,MDBSCRES)
    ZDIRSKIL= SATBODY(IV  ,MDBSCDIS)

!*          6.2.2       VARIABLE

    ZV      = ZRETV(INORVSA)
    IF(ZV == RMDI) THEN
      IVVSTAT = ICHSTAT_REJECT( IVVSTAT )
    ENDIF

!*          6.2.3       ERROR STATISTICS

    ZVVOERR = ZWE
    CALL FINOERR(ZVVPERR,ZVVOERR,ZVVFERR)

!*          6.2.4       STATUS, EVENTS, FLAGS

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

!*          6.2.5       DEPARTURES

!*          6.2.6       UPDATE BODY ENTRY

    CALL SCATBE (IV            ,&
     & IVNM          ,IVVVERCO ,ZVVPRES ,ZVVPRL  ,ZV      ,&
     & ZVVOERR       ,ZVVPERR  ,ZVVOERR ,ZVVRERR ,&
     & ZVVFGER       ,ZVVFGCC1 ,ZVVFGCC2,&
     & ZVVFGDEP      ,ZVVANDEP ,&
     & IVVSTAT       ,IVVEVEN1 ,IVVEVEN2,IVVRDBFL,IVVANFLG,&
     !               &ZWMLE(INORVSA),RMDI     ,ZDIRSKI                     )
     & ZAZIMUTH      ,ZINCIDEN ,ZKP     ,ZINVERSF,ZDIRSKIL)  
 
  ENDIF
ENDDO V_COMPONENTS
!*
!-----------------------------------------------------------------------

!        7.             FINISH OFF REPORT CREATION
!                       --------------------------

!*          7.1         UPDATE A FEW HEADER PARAMETERS (FIXED PART)

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

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

8 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('NSCATIN',1,ZHOOK_HANDLE)
END SUBROUTINE NSCATIN
