SUBROUTINE ASCATIF &
 & (K_ISATID,KCELL,KS0QF,KKPQF,PAZI,PINC,PSIGM,PKP,PLFR,KNWIND,&
 & KFLAG,PURET,PVRET,PRES,PSKILL,PKPM,PWE,&
 & LDNOSOL)  

!*****************************************************************************

!        H. HERSBACH     ECMWF     2006-06-12

!     PURPOSE
!     -------

!     COMPUTE THE 2 AMBIGOUS WINDS FOR ASCAT (FROM BUFR/ODB),
!     KEEP THE RESIDUAL AND DIRECTIONNAL SKILL ASSOCIATED,
!     CHECK ON MISSING DATA AND COLLECT QC INFO IN KFLAG 

!     INPUT:  K_ISATID       SATELLITE IDENTIFIER (3 FOR METOP-A) 
!             KCELL          ACROSS NODE NUMBER INSIDE THE SWATH ( 1 TO 42 )
!             KS0QF(JPANT)   SIGMA0 USABILITY FLAG:  0=GOOD, 1=USABLE, 2=BAD, 3=MISSING
!             KKPQF(JPANT)   KP ESTIMATE QUALITY FLAG: 0=OK, 1=NOK, 3=MISSING
!             PAZI (JPANT)   AZIMUTH ANGLE  (DEGRES)
!             PINC (JPANT)   INCIDENCE ANGLE 
!             PSIGM(JPANT)   SIGMA0 VALUES
!             PLFR (JPANT)   LAND FRACTION
!             PKP  (JPANT)   INSTRUMENT NOISE

!    OUTPUT:  KFLAG         FLAG ( 32 BIT; USE BIT 0-12 )                             
!                           BIT 0        : NO WIND SOLUTION
!                           BIT I+  JPANT: MISSING VALUE FOR ANTENNA I 
!                           BIT I+1*JPANT: LAND FRACTION EXCEEDED 
!                           BIT I+2*JPANT: KP QUALITY LOW
!                           BIT I+3*JPANT: SIGMA0  UNUSABLE
!             PURET(KNWIND) 2 U SOLUTIONS
!             PVRET(KNWIND) 2 V SOLUTIONS                 
!             PRES(KNWIND)  2 INVERSION RESIDUAL (SQUARED NORMALISED DISTANCE)        
!             PSKILL        1 DIRECTIONAL SKILL  (NATURAL NORMALISED VALUE)        
!             PKPM(JPANT)   MODELLED NOISE FOR THE 3 ANTENNAE    
!             PWE           WIND ERROR IN U AND V (1.5 M/S)
!             LDNOSOL       NO SOLUTION SWITCH

!****************************************************************************** 

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

USE PARERSCA , ONLY : JPANT    ,JPSOLMAX , JPSPEEDMAX,  JPASCAT,&
 & JPAMETOPA,  JPAMETOPB,  JPAMETOPC
USE YOMERSCA , ONLY : A180     ,A270     ,A360     ,ARAD     ,&
 & SIGMAX   ,XLFRMAX, &
 & BIASABM   ,RSCAMDI  ,ASCAT_SPEED_BIASCOR
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KNWIND 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_ISATID 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCELL 
INTEGER(KIND=JPIM),INTENT(IN)    :: KS0QF(JPANT)
INTEGER(KIND=JPIM),INTENT(IN)    :: KKPQF(JPANT)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAZI(JPANT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PINC(JPANT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSIGM(JPANT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKP(JPANT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLFR(JPANT) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KFLAG 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PURET(KNWIND) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVRET(KNWIND) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRES(KNWIND) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSKILL 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PKPM(JPANT) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PWE 
LOGICAL           ,INTENT(OUT)   :: LDNOSOL 
!     LOCAL VARIABLES
REAL(KIND=JPRB) ::        ZVS(JPSOLMAX), ZPHI(JPSOLMAX),ZDRES(JPSOLMAX)
REAL(KIND=JPRB) ::        ZBA(JPANT),ZSIGM(JPANT)

INTEGER(KIND=JPIM) :: ISOL, ISOLMAX, J, ISAT, ISPEEDBIN

REAL(KIND=JPRB) :: ZPH
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "s0towind.intfb.h"

!***  1.INITIALIZE


IF (LHOOK) CALL DR_HOOK('ASCATIF',0,ZHOOK_HANDLE)
LDNOSOL = .FALSE.

DO J=1,KNWIND
  PURET(J)=RSCAMDI
  PVRET(J)=RSCAMDI
  PRES(J) =RSCAMDI
ENDDO
PSKILL=RSCAMDI
DO J=1,JPANT
  PKPM(J)=RSCAMDI
ENDDO

PWE=1.5_JPRB


!**2.CHECK ON NON-MISSING VALUES AND DATA-SUPPLIED LAND FLAG
!    POSTPONE CHECK ON SIGMA0 USABILITY AND KP QUALITY TO END

KFLAG=0 !A PRIORI OK
DO J=1,JPANT
  IF(PSIGM(J) == RSCAMDI.OR.&
  &  PKP(J)   == RSCAMDI.OR.&
  &  PSIGM(J)  > SIGMAX .OR.&
  &  PINC(J)  == RSCAMDI.OR.&
  &  PAZI(J)  == RSCAMDI.OR.&
  &  PLFR(J)   > XLFRMAX   ) KFLAG=IBSET(KFLAG,J-1)
ENDDO

!***3. FIND AMBIGUOUS WINDS (ONLY IF KFLAG=0)

IF (KFLAG == 0) THEN
  DO J=1,JPANT
    ZBA(J)=MOD(PAZI(J)-PAZI(2),A360)
    ZSIGM(J)= 10._JPRB**(PSIGM(J)/10._JPRB)
  ENDDO

!   3.0 BEAM-DEPENDENT SIGMA0 BIAS CORRECTION

  ISAT=0
  IF(K_ISATID == JPAMETOPA) ISAT=1
  IF(K_ISATID == JPAMETOPB) ISAT=2
  IF(K_ISATID == JPAMETOPC) ISAT=3
  IF(ISAT < 1 .OR. ISAT > JPASCAT) THEN
    WRITE(NULOUT,'(''0WARNING IN SUBROUTINE ASCATIF; UNEXPECTED SATID: '',I10)') K_ISATID
    IF (LHOOK) CALL DR_HOOK('ASCATIF',1,ZHOOK_HANDLE)
    RETURN
  ENDIF

  DO J=1,JPANT
    ZSIGM(J)=ZSIGM(J)/BIASABM(J,KCELL,ISAT)
  ENDDO

!   3.1 INVERSION

  ISOLMAX=JPSOLMAX
  CALL S0TOWIND(ZSIGM,PINC,ZBA,ISOLMAX,ISOL,ZVS,ZPHI,ZDRES,PSKILL,PKPM)

! NO SOLUTION -> (WIND EITHER TOO LOW OR TOO HIGH)
  IF(ISOL == 0) LDNOSOL = .TRUE.

  DO J=1,MIN(KNWIND,ISOL)

!   3.2 PUT DIRECTION IN METEOROLOGICAL CONVENTION

! BY HABIT WIND DIRECTION IS RELATIVE TO THE MID BEAM (ZBA=PAZI-PAZI(2))
! THE ANGLE USED TO COMPUTE THE TABLE IS THE ANGLE BETWEEN THE BACKSCATERRED VECTOR
! AND THE VECTOR WIND (NO METEOROLOGICAL CONVENTION)
! THE AZIMUTH ANGLE GIVEN BY ESA IS THE ANGLE OF THE BACKSCATERRED VECTOR RELATIVE
! TO THE NORTH, THE DIRECTION FROM WHERE THE ANTENNA BLOWS!!

    ZPHI(J)=MOD(PAZI(2)-ZPHI(J) +A180,A360)

!   3.3 APPLY SPEED BIAS CORRECTION

    ISPEEDBIN=NINT(ZVS(J)-0.25)
    ISPEEDBIN=MAX(ISPEEDBIN,0)
    ISPEEDBIN=MIN(ISPEEDBIN,JPSPEEDMAX)

    ZVS(J)=ZVS(J)+ASCAT_SPEED_BIASCOR(ISPEEDBIN,KCELL,ISAT)

!   3.4 COMPUTE U AND V

    ZPH = (A270 - ZPHI(J))*ARAD
    PURET(J) = ZVS(J)*COS(ZPH)
    PVRET(J) = ZVS(J)*SIN(ZPH)
    PRES(J)  = ZDRES(J)
  ENDDO
ENDIF

!   3.5 PUT UPPER LIMITS ON PRES AND PSKILL
DO J=1,KNWIND
  PRES(J)  =MIN(PRES(J),409.5_JPRB)
ENDDO
PSKILL=MIN(PSKILL,409.5_JPRB)

!   4.0 UPDATE QUALITY FLAG

!   4.1 UNSUCCESSFUL  WIND INVERSION
IF(LDNOSOL)KFLAG=IBSET(KFLAG,3)

!   4.2 CHECK THAT SIGMA0 USABILITY AND KP QUALITY
!       HAVE ACCEPTABLE VALUES
DO J=1,JPANT
  IF(KS0QF(J) /= 0 .AND. &
  &  KS0QF(J) /= 1)        KFLAG=IBSET(KFLAG,J-1)
  IF(KKPQF(J) /= 0)        KFLAG=IBSET(KFLAG,J-1)
ENDDO


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