SUBROUTINE CANAMI
!****------------------------------------------------------------------------
!****  DK CANAMI : choix des predictands et des predicteurs
!****------------------------------------------------------------------------
!**   ROUTINES APPELANTES: - CANARI -  APPEL: CALL CANAMI
!**   ENTREES:
!**            - EXPLICITE - NAMELIST NAMPRE
!**            - IMPLICITE - COMMONS  QAPAVU YOMVNMB QACTEX YOMPHY QAPADO
!**   SORTIES: - QAPREX -
!**                        NPREK  : CODE VARIABLES PREDICTEURS par "checking"
!**                        NBPREK : NOMBRE DE PREDICTEURS par "checking"
!**                        NPREA  : CODE VARIABLES PREDICTEURS par analyse
!**                        NBPREA : NOMBRE DE PREDICTEURS par analyse
!**   COMMONS:
!**            - QAPADO - VALEURS DONNEES MANQUANTES.
!**            - QAPAVU - DIMENSIONS ET INDICES RELATIFS A *QAVARA*.
!**            - QAPREX - PREDICTEURS RETENUS
!**            - PARDIMO - dimension pour *YOMNUMBR*
!**            - YOMVNMB - liste des codes de predicteurs.
!**            - QACTEX - cles de controle de l'analyse
!**            - YOMPHY - cles de controle de la physique
!**   NAMELIST:
!**            - NAMPRE liste des predicteurs demandes pour les controles
!**                     et les differentes analyses
!**   EXTERNES:
!**            - ABORT  - ROUTINE SYSTEME D'ARRET D'EXECUTION.
!**            - CAVEGI - INITIALISATION DE QACVEG/NACVEG (ISBA)
!**   METHODE:
!**            - definition des predicteurs possibles
!**            - lecture de *NAMPRE* definissant les predicteurs souhaites
!**            - definition par *CAPREDI* des predicteurs utilisables.
!     modif  :   LG 03/00  redefinition of snow predictors - temporary solution
!****------------------------------------------------------------------------

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

USE YOMVNMB  , ONLY : NVNUMB
USE YOMANCS  , ONLY : NMDI
USE YOMLUN   , ONLY : NULNAM
USE QAPAVU   , ONLY : JPNINV   ,JPPXZ    ,JPPXT    ,JPPXDZ   ,&
 & JPPXHU  ,JPPXST   ,JPPXSN   ,JPPXT2   ,JPPXH2   ,&
 & JPPXU1  ,JPPXQ    ,JPPXU    ,JPPXV    ,JPPXV1   
USE QAPREX   , ONLY : JPNPRE   ,JPCHEK   ,JPANAL   ,NJPP     ,&
 & NPREK   ,NBPREK   ,NPREA    ,NBPREA   ,NKZV     ,&
 & NKTT    ,NKHU     ,NKDZ     ,NKST     ,NKSN     ,&
 & NAPS    ,NATT     ,NAUV     ,NAHU     ,NAT2     ,&
 & NAH2    ,NAV1     ,NASN     ,NAST     ,NKQQ     ,&
 & NAQQ    ,NKT2     ,NKH2     ,NKV1    
USE QAVARA  , ONLY : NVXINV

IMPLICIT NONE

!     IPRPx : CODE PREDICTEURS POSSIBLES.
!     IPRSx : CODE PREDICTEURS SOUHAITES PUIS SELECTIONNES.

INTEGER(KIND=JPIM) :: IPRPK(JPNPRE,JPCHEK), INBK(JPCHEK)
INTEGER(KIND=JPIM) :: IPRPA(JPNPRE,JPANAL), INBA(JPANAL)

INTEGER(KIND=JPIM) :: JJ
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "capredi.intfb.h"
#include "posnam.intfb.h"

#include "nampre.h"

!**--------------------------------------------------------------------------
!**       1. DEFINITION DES PREDICTANDS ET DES PREDICTEURS ENVISAGEABLES.
!**          -----------------------------------------------------------
!**

IF (LHOOK) CALL DR_HOOK('CANAMI',0,ZHOOK_HANDLE)
INBK = 0
INBA = 0

NKZV = 1
NKTT = 2
NKHU = 3
NKDZ = 4
NKST = 5
NKSN = 6
NKQQ = 7
NKT2 = 8
NKH2 = 9
NKV1 = 10

NAPS = 1
NATT = 2
NAUV = 3
NAHU = 4
NAT2 = 5
NAH2 = 6
NAV1 = 7
NASN = 8
NAST = 9
NAQQ =10

IPRPK(1,NKZV) = NVNUMB( 1) !u
IPRPK(2,NKZV) = NVNUMB( 2) !v
IPRPK(3,NKZV) = NVNUMB( 3) !phi
IPRPK(4,NKZV) = NVNUMB(56) !u10
IPRPK(5,NKZV) = NVNUMB(57) !v10
INBK(NKZV) = 5

IPRPK(1,NKTT) = NVNUMB( 8) !T
IPRPK(2,NKTT) = NVNUMB(10) !T2
INBK(NKTT) = 2

IPRPK(1,NKHU) = NVNUMB( 5) !Hu
IPRPK(2,NKHU) = NVNUMB( 6) !PWC
IPRPK(3,NKHU) = NVNUMB( 7) !Hu2
INBK(NKHU) = 3

IPRPK(1,NKDZ) = NVNUMB( 3) !phi
IPRPK(2,NKDZ) = NVNUMB( 4) !E
IPRPK(3,NKDZ) = NVNUMB( 8) !T
INBK(NKDZ) = 3

IPRPK(1,NKST) = NVNUMB(12) !SST
INBK(NKST) = 1

!IPRPK(1,NKSN) = NVNUMB(25) !snow depth
!IPRPK(2,NKSN) = NVNUMB(34) !RRliqu
!IPRPK(3,NKSN) = NVNUMB(46) !RRsnow
!INBK(NKSN) = 3
!temporary solution
IPRPK(1,NKSN) = NVNUMB(46)  !quantity of snow
INBK(NKSN) = 1

IPRPK(1,NKQQ) = NVNUMB(63) !QQ
INBK(NKQQ) = 1
 
IPRPK(1,NKT2) = NVNUMB(10) !T2
INBK(NKT2) = 1

IPRPK(1,NKH2) = NVNUMB( 7) !Hu2
INBK(NKH2) = 1

IPRPK(1,NKV1) = NVNUMB(56) !u10
IPRPK(2,NKV1) = NVNUMB(57) !v10
INBK(NKV1) = 2

IPRPA(1,NAPS) = NVNUMB( 1) !u
IPRPA(2,NAPS) = NVNUMB( 2) !v
IPRPA(3,NAPS) = NVNUMB( 3) !phi
IPRPA(4,NAPS) = NVNUMB( 4) !E
IPRPA(5,NAPS) = NVNUMB( 8) !T
IPRPA(6,NAPS) = NVNUMB(56) !u10
IPRPA(7,NAPS) = NVNUMB(57) !v10
INBA(NAPS) = 7

IPRPA(1,NATT) = NVNUMB( 1) !u
IPRPA(2,NATT) = NVNUMB( 2) !v
IPRPA(3,NATT) = NVNUMB( 3) !phi
IPRPA(4,NATT) = NVNUMB( 4) !E
IPRPA(5,NATT) = NVNUMB( 8) !T
IPRPA(6,NATT) = NVNUMB(10) !T2
IPRPA(7,NATT) = NVNUMB(56) !u10
IPRPA(8,NATT) = NVNUMB(57) !v10
INBA(NATT) = 8

INBA(NAUV) = 0

IPRPA(1,NAHU) = NVNUMB( 5) !Hu
IPRPA(2,NAHU) = NVNUMB( 6) !PWC
IPRPA(3,NAHU) = NVNUMB( 7) !Hu2
INBA(NAHU) = 3

IPRPA(1,NAT2) = NVNUMB(10) !T2
INBA(NAT2) = 1

IPRPA(1,NAH2) = NVNUMB( 7) !Hu2
INBA(NAH2) = 1

IPRPA(1,NAV1) = NVNUMB(56) !u10
IPRPA(2,NAV1) = NVNUMB(57) !v10
INBA(NAV1) = 2

!IPRPA(1,NASN) = NVNUMB(25) !snow depth
!IPRPA(2,NASN) = NVNUMB(34) !RRliqu
!IPRPA(3,NASN) = NVNUMB(46) !RRsnow
!INBA(NASN) = 3
!temporary solution
IPRPA(1,NASN) = NVNUMB(46) !quantity of snow
INBA(NASN) = 1

IPRPA(1,NAST) = NVNUMB(12) !SST
INBA(NAST) = 1

IPRPA(1,NAQQ) = NVNUMB(63)
INBA(NAQQ) = 1

!**--------------------------------------------------------------------------
!**       2. SELECTION DES PREDICTEURS.
!**          -------------------------
!**

DO JJ = 1 , JPCHEK
  IF (INBK(JJ)  >  JPNPRE) CALL ABOR1('CANAMI inconsistant')
ENDDO

IPRSK = NMDI

DO JJ = 1 , JPANAL
  IF (INBA(JJ)  >  JPNPRE) CALL ABOR1('CANAMI inconsistant')
ENDDO

IPRSA = NMDI

CALL POSNAM(NULNAM,'NAMPRE')
READ (NULNAM,NAMPRE, ERR=310, END=390)

NJPP(NKZV) = JPPXZ
NJPP(NKTT) = JPPXT
NJPP(NKHU) = JPPXHU
NJPP(NKDZ) = JPPXDZ
NJPP(NKST) = JPPXST
NJPP(NKSN) = JPPXSN
NJPP(NKQQ) = JPPXQ
NJPP(NKT2) = JPPXT2
NJPP(NKH2) = JPPXH2
NJPP(NKV1) = JPPXU1

NVXINV(:)  = 0
NVXINV( NVNUMB( 1) ) = JPPXU
NVXINV( NVNUMB( 2) ) = JPPXV
NVXINV( NVNUMB( 3) ) = JPPXZ
NVXINV( NVNUMB( 4) ) = JPPXDZ
NVXINV( NVNUMB( 5) ) = JPPXHU
NVXINV( NVNUMB( 6) ) = 0     ! there is no statistical model for PWC in CANARI
NVXINV( NVNUMB( 7) ) = JPPXH2
NVXINV( NVNUMB( 8) ) = JPPXT
NVXINV( NVNUMB(10) ) = JPPXT2
NVXINV( NVNUMB(12) ) = JPPXST
NVXINV( NVNUMB(25) ) = JPPXSN
NVXINV( NVNUMB(34) ) = JPPXSN
NVXINV( NVNUMB(46) ) = JPPXSN
NVXINV( NVNUMB(56) ) = JPPXU1
NVXINV( NVNUMB(57) ) = JPPXV1
NVXINV( NVNUMB(63) ) = JPPXQ

DO JJ = 1 , JPCHEK
  CALL CAPREDI(IPRSK(1,JJ), IPRPK(1,JJ), INBK(JJ),NBPREK(JJ), NPREK(1,JJ))
ENDDO

DO JJ = 1 , JPANAL
  CALL CAPREDI(IPRSA(1,JJ), IPRPA(1,JJ), INBA(JJ),NBPREA(JJ), NPREA(1,JJ))
ENDDO

!**--------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('CANAMI',1,ZHOOK_HANDLE)
RETURN
310 CONTINUE
CALL ABOR1 (' ROUTINE *CANAMI*: ERREUR LECTURE ')
390 CONTINUE
CALL ABOR1 (' ROUTINE *CANAMI*: FIN DE FICHIER SUR *NULNAM* ')
IF (LHOOK) CALL DR_HOOK('CANAMI',1,ZHOOK_HANDLE)
END SUBROUTINE CANAMI
