SUBROUTINE VTRANAD(KSE,KSPEC2,PVORDI,PDIVDI,PTDI,PSPDI,PVOR,PDIV,&
 & PT,KVMOD,KSIGN,KMODE)  

!**** *VTRANAD* - VERTICAL TRANSFORMS FOR NMI.ADJOINT

!     PURPOSE.   TO PERFORM THE VERTICAL TRANSFORMS REQUIRED DURING
!     --------   NORMAL MODE INITIALIZATION FOR A 3-D P. E. MODEL.ADJOINT

!**   INTERFACE.
!     ----------
!        *CALL* *VTRANAD(KSE,KSPEC2,PVORDI,PDIVDI,PTDI,PSPDI,PVOR,PDIV,PT,KSIGN,KMODE)

!        EXPLICIT ARGUMENTS :  KSE    - NUMBER OF COLUMNS PROCESSED
!        ------------------    KSPEC2 - "HORIZONTAL" DIMENSION OF ARRAYS
!                              PVORDI - VORTICITY   ) SPECTRAL COEFFS IN
!                              PDIVDI - DIVERGENCE  ) THE HORIZONTAL,
!                KMODE=1 |.....PTDI   - "P"         ) "PHYSICAL" SPACE IN
!                        |.....PSPDI  - NOT USED    ) THE VERTICAL

!                KMODE=2 |.....PTDI   - TEMPERATURE ) "PHYSICAL" SPACE IN
!                        |.....PSPDI  - LN(PS)      ) THE VERTICAL

!                              PVOR   - VORTICITY   ) SPECTRAL COEFFS IN THE
!                              PDIV   - DIVERGENCE  ) HORIZONTAL, "NORMAL MODE"
!                              PT     - "P"         ) SPACE IN THE VERTICAL

!                              KVMOD  - NUMBER OF VERTICAL MODES
!                              KSIGN  - = +1 FOR TRANSFORMS FROM NORMAL MODE
!                                            SPACE TO PHYSICAL SPACE,
!                                       = -1 FOR TRANSFORMS FROM PHYSICAL SPACE
!                                            TO NORMAL MODE SPACE.

!        IMPLICIT ARGUMENTS : none
!        ------------------

!     METHOD.   MULTIPLICATION OF COLUMN VECTORS BY THE MATRIX OF
!     -------   VERTICAL NORMAL MODES, OR ITS INVERSE.
!               EXTRA MANIPULATIONS BETWEEN T, LN(PS) AND "P",
!               DEPENDING ON KMODE.

!         see ARPEGE documentation vol1 chapter 5

!     EXTERNALS.  MXMAOP  - MATRIX MULTIPLY (CALLS SGEMMX)
!     ----------  SIGAMAD - operator gamma and mu
!                 SITNUAD - operator tau and nu

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     AUTHOR.
!     -------
!        JEAN-NOEL THEPAUT    *METEO FRANCE EERM/CRMD*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 90-03-15
!        MODIFIED : 93-02-02 Bill Heckley - To allow flexibility of
!                                           T, ln(ps) as input or "P".
!        D. Giard : 93-04-05 KSPEC2 as argument
!        L. Isaksen: 95-01-11 Number of columns processed an argument
!        L. Isaksen: 95-06-06 Reordering of spectral arrays
!        L. Isaksen: 96-02-20 Adapted for Message Passing
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NFLEVG
USE YOMDYN   , ONLY : SIMO     ,SIMI     ,SIVP

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSPEC2 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSE 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVORDI(NFLEVG,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIVDI(NFLEVG,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTDI(NFLEVG,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSPDI(KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOR(KSPEC2,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIV(KSPEC2,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT(KSPEC2,NFLEVG) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVMOD 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSIGN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KMODE 
!     ------------------------------------------------------------------

REAL(KIND=JPRB) :: ZP(NFLEVG,KSE), ZT(KSE,NFLEVG)

INTEGER(KIND=JPIM) :: JL, JM
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "mxmaop.h"
END INTERFACE

#include "sigamad.intfb.h"
#include "sitnuad.intfb.h"

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

!*       0.    INITIALIZE LOCAL ARRAYS TO ZERO.
!              --------------------------------

IF (LHOOK) CALL DR_HOOK('VTRANAD',0,ZHOOK_HANDLE)
ZP(:,:)=0.0_JPRB
ZT(:,:)=0.0_JPRB
!     ------------------------------------------------------------------

!*       1.    TRANSFORM TO NORMAL MODE SPACE
!              ------------------------------

IF (KSIGN == -1) THEN

!*       1.2   COMPUTE AND TRANSFORM 'P'

  IF (KMODE == 1) THEN
    CALL MXMAOP(SIMI,NFLEVG,1,PT,KSPEC2,1,PTDI,1,NFLEVG,NFLEVG,KVMOD,KSE)
  ELSE
    CALL MXMAOP(SIMI,NFLEVG,1,PT,KSPEC2,1,ZP,1,NFLEVG,NFLEVG,KVMOD,KSE)
    CALL SIGAMAD(1,NFLEVG,ZP,PTDI,PSPDI,KSE)
  ENDIF

!*       1.1   VORTICITY AND DIVERGENCE

  CALL MXMAOP(SIMI,NFLEVG,1,PDIV,KSPEC2,1,PDIVDI,1,NFLEVG,NFLEVG,KVMOD,KSE)
  CALL MXMAOP(SIMI,NFLEVG,1,PVOR,KSPEC2,1,PVORDI,1,NFLEVG,NFLEVG,KVMOD,KSE)

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

!*       2.   TRANSFORM TO PHYSICAL SPACE
!             ---------------------------

ELSEIF (KSIGN == +1) THEN

!*       2.2   COMPUTE T, PS

  IF (KMODE == 1) THEN
    CALL MXMAOP(SIMO,NFLEVG,1,PTDI,1,NFLEVG,PT,KSPEC2,1,KVMOD,NFLEVG,KSE)
  ELSE

!*        COMPUTE PS and T

    CALL SITNUAD(1,NFLEVG,ZP,PTDI,PSPDI,KSE)

!*        COMPUTE (B)-1 P

    CALL MXMAOP(SIMO,NFLEVG,1,ZP,1,NFLEVG,ZT,KSE,1,KVMOD,NFLEVG,KSE)

    DO JM=1,KVMOD
      DO JL=1,KSE
        PT(JL,JM)=PT(JL,JM)+ZT(JL,JM)/SIVP(JM)
        ZT(JL,JM)=0.0_JPRB
      ENDDO
    ENDDO
  ENDIF

!*       2.1   VORTICITY AND DIVERGENCE

  CALL MXMAOP(SIMO,NFLEVG,1,PDIVDI,1,NFLEVG,PDIV,KSPEC2,1,KVMOD,NFLEVG,KSE)
  CALL MXMAOP(SIMO,NFLEVG,1,PVORDI,1,NFLEVG,PVOR,KSPEC2,1,KVMOD,NFLEVG,KSE)

ENDIF

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

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

