SUBROUTINE REORD(PVOR,PDIV,PHGT,PVORA,PDIVA,PHGTA,KM,KDIR)

!**** *REORD* - RE-ORDER FIELDS FOR NMI

!     PURPOSE.   TO REORDER THE FIELDS FOR NMI, EXTRACTING OR INCLUDING
!     --------         SPECTRAL COMPONENTS FOR ZONAL WAVENUMBER M

!**   INTERFACE.
!     ----------
!        *CALL* *REORD(PVOR,PDIV,PHGT,PVORA,PDIVA,PHGTA,KM,KDIR)*

!        EXPLICIT ARGUMENTS :
!        --------------------  PVOR  - VORTICITY    |
!                              PDIV  - DIVERGENCE   | all spectral comp.
!                              PHGT  - GEOPOTENTIAL |
!                              PVORA - VORTICITY    |
!                              PDIVA - DIVERGENCE    |
!                              PHGTA - GEOPOTENTIAL  |
!                              KM    - ZONAL WAVENUMBER      for m=
!                              KDIR - SWITCH
!       IF KDIR=+1 THEN PXXXA IS OUTPUT AND PXXX INPUT (EXTRACTION)
!       IF KDIR=-1 THEN PXXXA IS INPUT AND PXXX OUTPUT (FILLING)

!     METHOD.
!     -------

!     EXTERNALS.   NONE.
!     ----------

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

!     AUTHOR.
!     -------
!        LARS ISAKSEN  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-08-27
!        Modified : 95-06-06 L.Isaksen   Reordering of spectral arrays
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NCMAX    ,NCPEC2
USE YOMLAP   , ONLY : NACM0

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KM 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOR(NCPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIV(NCPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PHGT(NCPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVORA(2*(NCMAX+1-KM)) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIVA(2*(NCMAX+1-KM)) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PHGTA(2*(NCMAX+1-KM)) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDIR 
INTEGER(KIND=JPIM) :: IMPVAL, INM, IREVAL, JN
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    COPY FROM SPECTRAL ARRAYS TO "NMI" ARRAY.
!              -----------------------------------------

IF (LHOOK) CALL DR_HOOK('REORD',0,ZHOOK_HANDLE)
IF (KDIR == 1) THEN
  IF(KM /= 0) THEN
    DO JN=KM,NCMAX
      IREVAL=JN+1-KM
      IMPVAL=NCMAX+1-KM+IREVAL
      INM=NACM0(KM)+(JN-KM)*2
      PVORA(IREVAL) = PVOR(INM)
      PVORA(IMPVAL) = PVOR(INM+1)
      PDIVA(IREVAL) = PDIV(INM)
      PDIVA(IMPVAL) = PDIV(INM+1)
      PHGTA(IREVAL) = PHGT(INM)
      PHGTA(IMPVAL) = PHGT(INM+1)
    ENDDO
  ELSE
    DO JN=KM,NCMAX
      IREVAL=JN+1-KM
      INM=NACM0(KM)+(JN-KM)*2
      PVORA(IREVAL) = PVOR(INM)
      PDIVA(IREVAL) = PDIV(INM)
      PHGTA(IREVAL) = PHGT(INM)
    ENDDO
  ENDIF

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

!*          2.    COPY FROM "NMI" ARRAY TO SPECTRAL ARRAY.
!                 ----------------------------------------

ELSEIF (KDIR == -1) THEN
  IF(KM /= 0) THEN
!DIR$ IVDEP
!OCL NOVREC
    DO JN=KM,NCMAX
      IREVAL=JN+1-KM
      IMPVAL=NCMAX+1-KM+IREVAL
      INM=NACM0(KM)+(JN-KM)*2
      PVOR(INM)   = PVORA(IREVAL)
      PVOR(INM+1) = PVORA(IMPVAL)
      PDIV(INM)   = PDIVA(IREVAL)
      PDIV(INM+1) = PDIVA(IMPVAL)
      PHGT(INM)   = PHGTA(IREVAL)
      PHGT(INM+1) = PHGTA(IMPVAL)
    ENDDO
  ELSE
    DO JN=KM,NCMAX
      IREVAL=JN+1-KM
      INM=NACM0(KM)+(JN-KM)*2
      PVOR(INM) = PVORA(IREVAL)
      PDIV(INM) = PDIVA(IREVAL)
      PHGT(INM) = PHGTA(IREVAL)
    ENDDO
  ENDIF
ENDIF

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

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

