SUBROUTINE UVSPE(PETPSI,PDIKHI,PU,PV,KFLSUR,KFIELD,KOUTP)

!**** *UVSPE - Compute spectral vorticity and divergence from physical
!                space u and v.

!     Purpose.
!     --------
!     Compute vorticity and divergence or psi and khi from u and v.

!**   Interface.
!     ----------
!        *CALL* *UVSPE(PETPSI,PDIKHI,PU,PV,KFLSUR,KFIELD,KOUTP)

!        Explicit arguments :
!        --------------------
!           PETPSI             : vorticity/stream function (output)
!           PDIKHI             : divergence/vel.potential  (output)
!           PU                 : u velocity (input)
!           PV                 : v velocity (input)
!           KFLSUR             : first dimension of spectral arrays (input)
!           KFIELD             : Number of fields (input)
!           KOUTP              : KOUTP=1 output vor/div (input)
!                              : KOUTP=2 output phi/kai

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation about spectral transforms.

!     Externals.  DIR_TRANS - direct transform (TRANS library)
!     ----------

!     Reference.
!     ----------
!        Documentation ARPEGE about spectral transforms.

!     Author.
!     -------
!        MPP Group *ECMWF*

!     Modifications.
!     --------------
!        Original : 97-06-11 Based on UVSPE
!        =99-03-29= Deborah Salmond  JUMP in FTINV changed to 1
!        Modified : 99-05-28  D.Salmond - Optimise copies in trltom/trmtol. 
!        Modified : 04/06/99 D.Salmond : change order of AIA and SIA
!        Modified : 25/10/00 M.Hamrud  : TRANS package
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY :     NSPEC2   
USE YOMLAP   , ONLY : NVALUE   ,RLAPIN
USE YOMCT0   , ONLY :    NPRTRV
USE YOMGEM   , ONLY : NGPTOT
USE YOMMP    , ONLY : MYSETV

IMPLICIT NONE

!     DUMMY:
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLSUR 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELD 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PETPSI(KFLSUR,NSPEC2) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIKHI(KFLSUR,NSPEC2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU(NGPTOT,KFIELD) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV(NGPTOT,KFIELD) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOUTP 

REAL(KIND=JPRB) :: ZUV(NGPTOT,2*KFIELD,1)

INTEGER(KIND=JPIM) :: ILF,ISTG,IFLDPP,IREST,IGF
INTEGER(KIND=JPIM) :: INUMLF(NPRTRV),IVSETUV(KFIELD)
INTEGER(KIND=JPIM) :: JROCB,JLEV,JSP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "dir_trans.h"
END INTERFACE

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

!*    INITIALISATION

IF (LHOOK) CALL DR_HOOK('UVSPE',0,ZHOOK_HANDLE)
ILF = 0
IF( KFIELD == 1 )THEN
  IVSETUV(1) = NPRTRV
  IF(NPRTRV == MYSETV) THEN
    ILF = 1
  ENDIF
  IGF = 1
ELSE
  IFLDPP = KFIELD/NPRTRV
  IREST = KFIELD-IFLDPP*NPRTRV
  DO JROCB=1,NPRTRV
    IF( JROCB <= IREST )THEN
      INUMLF(JROCB) = IFLDPP+1
    ELSE
      INUMLF(JROCB) = IFLDPP
    ENDIF
  ENDDO
  ILF = INUMLF(MYSETV)
  ISTG = 1
  DO JROCB=1,NPRTRV
    IVSETUV(ISTG:ISTG+INUMLF(JROCB)-1) = JROCB
    ISTG = ISTG+INUMLF(JROCB)
  ENDDO
  IGF = ISTG-1
ENDIF

ZUV(:,1:KFIELD,1) = PU(:,:)
ZUV(:,KFIELD+1:2*KFIELD,1) = PV(:,:)
CALL DIR_TRANS(PSPVOR=PETPSI(1:ILF,:),PSPDIV=PDIKHI(1:ILF,:),&
 & KVSETUV=IVSETUV(1:IGF),KPROMA=NGPTOT,PGP=ZUV)  

IF (KOUTP == 2) THEN
  DO JLEV=1,ILF
    DO JSP=1,NSPEC2
      PETPSI(JLEV,JSP) = PETPSI(JLEV,JSP)*RLAPIN(NVALUE(JSP))
      PDIKHI(JLEV,JSP) = PDIKHI(JLEV,JSP)*RLAPIN(NVALUE(JSP))
    ENDDO
  ENDDO
ENDIF

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

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