SUBROUTINE SPEHOU3AD(PVORI,PDIVI,PHGTI,PFO,KVMO,KM)

!**** *SPEHOU3AD* - ROUTINE TO TRANSFORM FROM "STANDARD SPACE" TO
!                 HOUGH SPACE.ADJOINT

!     PURPOSE.   A SET OF "STANDARD SPACE" COEFFICIENTS IS USED
!     --------   TO CREATE THE CORRESPONDING SET OF FIELDS IN HOUGH
!                SPACE.

!**   INTERFACE.
!     ----------
!        *CALL* *SPEHOU3AD(PVORI,PDIVI,PHGTI,PFO,KVMO,KM)

!        EXPLICIT ARGUMENTS :  PVORI- VORTICITY         \
!        --------------------  PDIVI- DIVERGENCE        \    - OUTPUT
!                              PHGTI- GEOPOTENTIAL      \
!                              PFO  - HOUGH COMPONENTS       - INPUT
!                              KVMO - VERTICAL MODE INDEX    - INPUT
!                              KM   - ZONAL WAVENUMBER INDEX - INPUT
!        IMPLICIT ARGUMENTS
!        --------------------

!     METHOD.   THE FIELDS ARE REORDERED AND NORMALIZED. WE GO INTO NORMAL
!     -------   MODE SPACE BY MULTIPLYING BY THE MATRIX CONSISTING OF THE
!               "STANDARD SPACE" EXPRESSED IN NORMAL MODES.THIS MATRIX HAS
!               BEEN PREVIOUSLY COMPUTED BY SUMODE.

!     EXTERNALS.  REORDO3AD - REORDER AND NORMALIZE FIELDS
!     ----------  MXMAOP    - MATRIX MULTIPLY ( using SGEMMX )
!                 MIO..     - IO ROUTINES

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

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

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 90-03-15
!        MATS  HAMRUD    : 93-02-08 : MULTITASKING, IO-SCHEME
!        LARS  ISAKSEN   : 95-01-11 : MULTITASKING MOVED TO HIGHER LEVEL
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NCMAX    ,NXMAX
USE YOMSPHB  , ONLY : SPHBUF   ,NSTASPHB

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KM 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVORI(2*(NCMAX+1-KM)) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIVI(2*(NCMAX+1-KM)) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PHGTI(2*(NCMAX+1-KM)) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFO(6*(NXMAX+1-KM)) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVMO 
REAL(KIND=JPRB) :: ZVORI(2*(NCMAX+1-KM)),ZDIVI(2*(NCMAX+1-KM)),ZHGTI(2*(NCMAX+1-KM))
REAL(KIND=JPRB) :: ZF1(6*(NXMAX+1-KM))   
INTEGER(KIND=JPIM) ::  IJ, IORDR, IREC, IXP, JJ
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "mxmaop.h"
END INTERFACE

#include "reordo3ad.intfb.h"

IF (LHOOK) CALL DR_HOOK('SPEHOU3AD',0,ZHOOK_HANDLE)
IXP=(MIN(1,KM)+1)*(NCMAX+1-KM)
DO JJ=1,IXP
  ZHGTI(JJ) = 0.0_JPRB
  ZDIVI(JJ) = 0.0_JPRB
  ZVORI(JJ) = 0.0_JPRB
ENDDO
!     ------------------------------------------------------------------

!        2.    GO INTO NORMAL MODE SPACE.

IREC=(KVMO-1)*(NXMAX+1)+KM+1
IORDR=3*(NXMAX-KM+1)
CALL MXMAOP(SPHBUF(NSTASPHB(IREC)+1),IORDR,1,PFO,2,1,ZF1,2,1,IORDR,IORDR,2)

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

!*       1.    RE-ORDER AND NORMALIZE FIELDS.
!              ------------------------------

CALL REORDO3AD(ZF1,ZVORI,ZDIVI,ZHGTI,KVMO,KM,1)

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

!*       0.    EXTRACTION OF T"NXMAX" FIELDS.
!              ------------------------------

IF (KM == 0) THEN
  DO JJ=1,NXMAX+1
    PHGTI(JJ) = PHGTI(JJ) + ZHGTI(JJ)
    ZHGTI(JJ) = 0.0_JPRB
    PDIVI(JJ) = PDIVI(JJ) + ZDIVI(JJ)
    ZDIVI(JJ) = 0.0_JPRB
    PVORI(JJ) = PVORI(JJ) + ZVORI(JJ)
    ZVORI(JJ) = 0.0_JPRB
  ENDDO
ELSE
  DO JJ=1,NXMAX+1-KM
    IJ=JJ+NCMAX+1-KM
    PHGTI(JJ) = PHGTI(JJ) + ZHGTI(JJ)
    ZHGTI(JJ) = 0.0_JPRB
    PHGTI(IJ) = PHGTI(IJ) + ZHGTI(IJ)
    ZHGTI(IJ) = 0.0_JPRB
    PDIVI(JJ) = PDIVI(JJ) + ZDIVI(JJ)
    ZDIVI(JJ) = 0.0_JPRB
    PDIVI(IJ) = PDIVI(IJ) + ZDIVI(IJ)
    ZDIVI(IJ) = 0.0_JPRB
    PVORI(JJ) = PVORI(JJ) + ZVORI(JJ)
    ZVORI(JJ) = 0.0_JPRB
    PVORI(IJ) = PVORI(IJ) + ZVORI(IJ)
    ZVORI(IJ) = 0.0_JPRB
  ENDDO
ENDIF

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

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

