SUBROUTINE RDPHTRAJT (KSTEP,KSTA,KEND,KJUMP,KFIELDS,&
 & PTENTVD5,PTENQVD5,PTENUVD5,PTENVVD5,&
 & PTENTGW5,PTENUGW5,PTENVGW5,&
 & PTENTCO5,PTENQCO5,PTENUCO5,PTENVCO5,&
 & PTENTCL5,PTENUWM5,PTENVWM5,&
 & PDIFTQ5 ,PDIFTS5 ,PEMTED5 ,PTRSOL5,&
 & PA5 ,PL5 ,PI5,&
 & PLUDE5  , PLU5   ,&
 & PFPLCL5 ,PFPLCN5 ,PUCFL5  ,PVCFL5 ,PTCFL5 ,PQCFL5)  

!**** *RDPHTRAJT* - Read out the trajectory of phys.tendencies 
!                   to work file

!     Purpose.  Read out the trajectory of phys.tendencies 
!               to work file

!**   Interface.
!     ----------
!        *CALL* *RDPHTRAJT(...)*

!        Explicit arguments :  KSTEP   - time step            (input)
!        --------------------  KSTA    - start adress         (input)
!                              KEND    - end adress           (input)
!                              KJUMP   - offset into gp array (input)
!                              KFIELDS - number of fields     (input)
!                              PTENTVD5 to PTRSOL5: the phys.tendencies
!                                        read on file         (output)

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

!     Method.
!     -------

!     -------

!     Externals. EMPTB3 - interface to packing routines
!     ---------- MIOREAD - read routine from MIO packag

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

!     Author.
!     -------
!        Marta Janiskova  *ECMWF*

!     Modifications.
!     --------------
!        Original : 2000-01-21
!        Modified : 
!           P. Lopez  ECMWF   2003-12: Added arguments PLUDE5 and PLU5.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!           M. Janiskova  18-Sep-2009: added  tendencies needed for non-orog.gwd
!           M. Janiskova  18-May-2010: cleaning & adding fields for LEMWAVE
!        --------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOMIOP   , ONLY : NPCKFPH5 ,NEXPBPH5
USE YOMTPHY  , ONLY : TRAJPHTE
USE YOPHNC   , ONLY : LETRAJPT ,LERADSW2 ,LERADN2, LERADLW2, LENCLD2, LEGWWMS2
USE YOEPHY   , ONLY : LEMWAVE

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

IMPLICIT NONE

! * INPUT:
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KJUMP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENTVD5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENQVD5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENUVD5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENVVD5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENTGW5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENUGW5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENVGW5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENTCO5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENQCO5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENUCO5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENVCO5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENTCL5(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENUWM5(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENVWM5(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTQ5(NPROMA,NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTS5(NPROMA,NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PEMTED5(NPROMA,NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTRSOL5(NPROMA,NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PA5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PL5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PI5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLUDE5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLU5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PFPLCL5(NPROMA,NFLEVG+1)
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PFPLCN5(NPROMA,NFLEVG+1)
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PUCFL5(NPROMA)
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PVCFL5(NPROMA)
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTCFL5(NPROMA)
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PQCFL5(NPROMA)
! * OUTPUT:
INTEGER(KIND=JPIM) :: IBLOCKS, IJUMP,  IPCKUNIT, ISTAGPB, IWORDS
REAL(KIND=JPRB) :: ZHOOK_HANDLE


#include "abor1.intfb.h"
#include "emptb3.intfb.h"

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

!*       1.    READ OUT GRIDPOINT ARRAY.
!              --------------------------

!*       1.1       CALCULATE LENGTHS

IF (LHOOK) CALL DR_HOOK('RDPHTRAJT',0,ZHOOK_HANDLE)
IWORDS = KEND-KSTA+1

!*       1.2       WRITE OUT BUFFER

IJUMP=KJUMP-1
IF (NPCKFPH5 == 1) THEN
  ISTAGPB = 1+IJUMP*KFIELDS
ELSE

!  It is assumed that TRAJPHYS data always is read/written in
!  NPROMA chunks (except for the last potentially smaller chunk).
!  Restrictions are necessary because reading/writing of packed
!  data has to start on a word boundary.

  IBLOCKS=IJUMP/NPROMA
  IF (IBLOCKS*NPROMA /= IJUMP)&
   & CALL ABOR1('RDPHTRAJT: INCONSISTENT READING OF PACKED DATA')  
  IPCKUNIT = (NPROMA+NPCKFPH5-1)/NPCKFPH5
  ISTAGPB = 1+IPCKUNIT*IBLOCKS*KFIELDS
ENDIF

!        1.3      UPPER AIR 3D FIELDS

IF (.NOT. LETRAJPT) THEN
  IF (LERADN2.OR.LERADSW2.OR.LERADLW2) THEN
    CALL EMPTB3(NFLEVG+1,IWORDS,PEMTED5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(NFLEVG+1,IWORDS,PTRSOL5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF
  IF (LENCLD2) THEN
    CALL EMPTB3(NFLEVG,IWORDS,PA5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PL5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PI5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5) 
    CALL EMPTB3(NFLEVG,IWORDS,PLUDE5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PLU5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  ENDIF
  IF (LEGWWMS2) THEN
    CALL EMPTB3(NFLEVG,IWORDS,PTENUWM5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(NFLEVG,IWORDS,PTENVWM5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF
ELSE
  CALL EMPTB3(NFLEVG,IWORDS,PTENTVD5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENQVD5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENUVD5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENVVD5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENTGW5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENUGW5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENVGW5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENTCO5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENQCO5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENUCO5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  CALL EMPTB3(NFLEVG,IWORDS,PTENVCO5 (KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)  

  CALL EMPTB3(NFLEVG+1,IWORDS,PDIFTQ5(KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)
  CALL EMPTB3(NFLEVG+1,IWORDS,PDIFTS5(KSTA,1),TRAJPHTE(:,KSTEP),&
   & ISTAGPB,NPCKFPH5,NEXPBPH5)

  IF (LERADN2.OR.LERADSW2.OR.LERADLW2) THEN
    CALL EMPTB3(NFLEVG+1,IWORDS,PEMTED5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(NFLEVG+1,IWORDS,PTRSOL5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF

  IF (LENCLD2) THEN
    CALL EMPTB3(NFLEVG,IWORDS,PA5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PL5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PI5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PLUDE5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
    CALL EMPTB3(NFLEVG,IWORDS,PLU5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)  
  ENDIF

  IF (.NOT. LEMWAVE) THEN
    CALL EMPTB3(NFLEVG,IWORDS,PTENTCL5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF

  IF (LENCLD2 .AND. LEMWAVE) THEN
    CALL EMPTB3(NFLEVG+1,IWORDS,PFPLCL5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(NFLEVG+1,IWORDS,PFPLCN5(KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(1,IWORDS,PUCFL5(KSTA),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(1,IWORDS,PVCFL5(KSTA),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(1,IWORDS,PTCFL5(KSTA),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(1,IWORDS,PQCFL5(KSTA),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF

  IF (LEGWWMS2) THEN
    CALL EMPTB3(NFLEVG,IWORDS,PTENUWM5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
    CALL EMPTB3(NFLEVG,IWORDS,PTENVWM5 (KSTA,1),TRAJPHTE(:,KSTEP),&
     & ISTAGPB,NPCKFPH5,NEXPBPH5)
  ENDIF
ENDIF

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

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

