SUBROUTINE CAVTAX(KPROMA,KNBPT,KTASK,PUT,PVT,PTT,PSFLN,&
 & PGNORDL,PGNORDM,PGM,PGELAT,PGELAM,PRCORI,&
 & PCAGUE,PESIG,PSGPHI)  
!****-------------------------------------------------------------------
!****   CAVTAX : ROUTINE GERANT L'EXECUTION DE L'ANALYSE DE TEMPERATURE et VENT
!****-------------------------------------------------------------------
!***  ROUTINES APPELANTES: - CAPOTX -
!***  -------------------
!***  ARGUMENTS:  IN       KPROMA : surdimensionnement horizontal
!***  ---------   IN       KNBPT  : nombre reel de points traites
!***              IN       KTASK  : numero de la tache (multitasking)
!***              IN/OUT   PUT, PVT, PTT :
!***              IN       PSFLN :
!***              IN       PGNORDL, PGNORDM, PGM, PGELAT, PGELAM, PRCORI :
!***              IN       PESIG, PCAGUE :
!***              OUT      PSGPHI : diagnostics

!     COMMONS: - QADOCK - CONTIENT LA TABLE DE CONTINGENCE
!     -------             PREDICTEURS/PREDICTANTS ET DES CONTRAINTES
!                         POUR LA SELECTION DES OBSERVATIONS.
!              - QAPAVU - DIMENSIONS ET INDICE RELATIFS A *QAVARA*.
!              - QADIAG - CHAMPS POINTS DE GRILLE SPECIAUX DE L'ANALYSE.
!              - YOMCST - CONSTANTES FONDAMENTALES D'ARPEGE.
!              - YOMDIM - DIMENSION DES TABLEAUX DE TRAVAIL.

!     EXTERNES: - CANEVA - ROUTINE EFFECTUANT L'ANALYSE.
!     --------
!        R. El Khatib : 01-08-07 Pruning options
!****--------------------------------------------------------------------

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

USE PARDIMO  , ONLY : JPNOTP
USE YOMDIM   , ONLY : NFLEVG
USE QAPAVU   , ONLY : JPPXT    ,JPPXU    ,JPPXV
USE YOMCST   , ONLY : ROMEGA   ,RD
USE QAPREX   , ONLY : NPREA    ,NBPREA   ,NATT     ,NAUV
USE QADOCK   , ONLY : QDSTVA   ,MINMA    ,QCORMIN  ,QDELPI
USE QADIAG   , ONLY : RCATSL   ,RCARES   ,RCASIG
USE QACOST   , ONLY : QPOHZ    ,QRNU
USE QACTAN   , ONLY : NCANLI   ,NCANLS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNBPT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTASK 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSFLN(KNBPT,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDL(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDM(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAT(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAM(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCORI(KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCAGUE(2,KNBPT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESIG(2*NFLEVG+5,KNBPT) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSGPHI(NFLEVG,KNBPT) 
!     INM    : NOMBRE DE PREDICTEURS PAR TYPE DE PREDICTANT.
!     INBPR  : NOMBRE DE PREDICTEURS PAR VERTICALES D'ANALYSE.

INTEGER(KIND=JPIM) :: INM(3), INBPR(KNBPT*NFLEVG), INXI(3), INLXI

!     ZSIGPS : EN GENERAL, ECART TYPES D'ERREUR DE L'EBAUCHE
!     ZRESIA : RESIDUS ANALYSES.
!     ZSIGA  : ECART TYPES D'ERREUR D'ANALYSE.

REAL(KIND=JPRB) :: ZSIGPS(KNBPT*3*NFLEVG)
REAL(KIND=JPRB) :: ZRESIA(KNBPT*3*NFLEVG), ZSIGA(KNBPT*3*NFLEVG)
REAL(KIND=JPRB) :: ZPRESF(KNBPT*NFLEVG)
REAL(KIND=JPRB) :: ZGNORDL(KNBPT), ZGNORDM(KNBPT)

INTEGER(KIND=JPIM) :: IANTY, ILEVI, INBPV, IQUOI, Ixx(JPNOTP), JLEV, JROF

REAL(KIND=JPRB) :: ZCORI, ZCORMIN, ZDELPI, Zxxx(JPNOTP)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "caneva.intfb.h"

#include "qavari.h"

!**----------------------------------------------------------------------
!**       1. Initialisations.
!**          ---------------

IF (LHOOK) CALL DR_HOOK('CAVTAX',0,ZHOOK_HANDLE)
RCATSL(:,:,KTASK) = 0.0_JPRB
RCARES(:,:,KTASK) = 0.0_JPRB
RCASIG(:,:,KTASK) = 1.0_JPRB

ILEVI = NCANLI - 1
INBPV = NCANLS-ILEVI

ZGNORDM(:) = PGNORDM(:)/PGM(:)
ZGNORDL(:) = PGNORDL(:)/PGM(:)

!**---------------------------------------------------------------------
!**       2. Initialisation des statistiques de l'ebauche.
!**          --------------------------------------------

! TUV ordre fixe desormais !
DO JLEV = 1 , INBPV
  DO JROF = 1 , KNBPT
    ZPRESF(JROF+KNBPT*(JLEV-1))=PSFLN(JROF,ILEVI+JLEV)
    ZSIGPS(JROF+KNBPT*3*(JLEV-1))=PESIG(ILEVI+JLEV,JROF)/RD
    ZSIGPS(JROF+KNBPT+KNBPT*3*(JLEV-1))=&
     & PESIG(ILEVI+JLEV,JROF)/MAX(ROMEGA,ABS(PRCORI(JROF)))  
    ZSIGPS(JROF+KNBPT*2+KNBPT*3*(JLEV-1))=&
     & PESIG(ILEVI+JLEV,JROF)/MAX(ROMEGA,ABS(PRCORI(JROF)))  
  ENDDO
ENDDO

!**---------------------------------------------------------------------
!**       3. Initialisation des parametres.
!**          -----------------------------

IQUOI = -2
IANTY = 0

INLXI = 3
INXI(1) = JPPXT
INXI(2) = JPPXU
INXI(3) = JPPXV

ZDELPI = QDELPI(NATT)
ZCORMIN = QCORMIN(NATT)

INM(1) = MINMA(NATT)
INM(2) = MINMA(NAUV)
INM(3) = MINMA(NAUV)

!**---------------------------------------------------------------------
!**       4. Lancement de l'analyse par interpolation optimale.
!**          -------------------------------------------------

CALL CANEVA(IQUOI, IANTY, KTASK,&
 & Zxxx, QDSTVA(1), ZDELPI, Ixx, Ixx,&
 & ZCORMIN, PCAGUE, PGELAT, PGELAM,&
 & ZPRESF(1), KNBPT, INBPV,&
 & INXI(1), INLXI, NPREA(1,NATT), NBPREA(NATT), INM(1),&
 & ZSIGPS(1), ZRESIA(1), ZSIGA(1), INBPR(1))  

!**---------------------------------------------------------------------
!**       5. Calcul des champs analyses.
!**          --------------------------

DO JLEV = 1 , INBPV
  DO JROF = 1 , KNBPT
    PTT(JROF,ILEVI+JLEV) = PTT(JROF,ILEVI+JLEV)&
     & + ZRESIA(JROF+        KNBPT*3*(JLEV-1))  
    PUT(JROF,ILEVI+JLEV) = PUT(JROF,ILEVI+JLEV)&
     & + ZGNORDM(JROF) * ZRESIA(JROF+KNBPT  +KNBPT*3*(JLEV-1))&
     & + ZGNORDL(JROF) * ZRESIA(JROF+KNBPT*2+KNBPT*3*(JLEV-1))  
    PVT(JROF,ILEVI+JLEV) = PVT(JROF,ILEVI+JLEV)&
     & - ZGNORDL(JROF) * ZRESIA(JROF+KNBPT  +KNBPT*3*(JLEV-1))&
     & + ZGNORDM(JROF) * ZRESIA(JROF+KNBPT*2+KNBPT*3*(JLEV-1))  
    RCATSL(JROF,ILEVI+JLEV,KTASK) =REAL(INBPR(JROF+KNBPT*(JLEV-1)),JPRB)
    RCARES(JROF,ILEVI+JLEV,KTASK) = ZRESIA(JROF+KNBPT*3*(JLEV-1))
    RCASIG(JROF,ILEVI+JLEV,KTASK) =&
     & ZSIGA (JROF+KNBPT*3*(JLEV-1)) / ZSIGPS(JROF+KNBPT*3*(JLEV-1))  
    PSGPHI(ILEVI+JLEV,JROF) = SQRT( 0.5_JPRB * (&
     & ZSIGA(JROF+KNBPT  +KNBPT*3*(JLEV-1))**2 &
     & + ZSIGA(JROF+KNBPT*2+KNBPT*3*(JLEV-1))**2 ) )  
    ZCORI = MAX(ROMEGA,ABS(PRCORI(JROF)))
    PSGPHI(ILEVI+JLEV,JROF) =&
     & FSIGPSI(PSGPHI(ILEVI+JLEV,JROF)*ZCORI,&
     & QPOHZ(ILEVI+JLEV,KTASK)**2, QRNU(ILEVI+JLEV,KTASK)**2)  
  ENDDO
ENDDO

!**---------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('CAVTAX',1,ZHOOK_HANDLE)
END SUBROUTINE CAVTAX
