MODULE TYPE_FPRQPHYS

! Purpose :
! -------
!    To define the internal type "TYPE_FPRQPHY" for the control of 
!    post-processed physical fields :
!      - %ICOD : Internal code number of the field
!      - %IVEC : Information about tensor aspect of the field in spectral space :
!        -j : vector 2nd momentum corresponding to the field nr j
!         0 : "scalar" 
!         1 : vector 1st momentum
!      - %ILEV : the number of vertical levels
!      - %ILVP : the vertical levels pointers
!      - %IDOM : the number of subdomains for each level
!      - %IDMP : the subdomains pointers for each level
!      - %CLBF : originating buffer ("PHY", "CFU", "XFU")
!      - %IMPR : field pointer in originating buffer
!      - %LLXR : .TRUE. if the field is an auxilary one (not to be written out to file)

! Interface :
! ---------
!    Empty.

! External :
! --------
!    None.

! Method :
! ------
!    See Documentation.

! Reference :
! ---------
!    Fullpos technical & users guide.

! Author :
! ------
!    Ryad El Khatib *METEO-FRANCE*

! Modifications :
! -------------
! Original : 2003-03-03
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!-----------------------------------------------------------------------------

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

IMPLICIT NONE
SAVE

PRIVATE
PUBLIC TYPE_FPRQPHY      ,ALLOCATE_FPRQPHY, DEALLOCATE_FPRQPHY, &
 & ALLOCATED_FPRQPHY ,INQUIRE_FPRQPHY  

TYPE TYPE_FPRQPHY
LOGICAL,   POINTER :: LLXR(:)
INTEGER(KIND=JPIM), POINTER :: ICOD(:)
INTEGER(KIND=JPIM), POINTER :: IVEC(:)
INTEGER(KIND=JPIM), POINTER :: ILEV(:)
INTEGER(KIND=JPIM), POINTER :: ILVP(:,:)
INTEGER(KIND=JPIM), POINTER :: IDOM(:,:)
INTEGER(KIND=JPIM), POINTER :: IDMP(:,:,:)
INTEGER(KIND=JPIM), POINTER :: IMPR(:)
CHARACTER(LEN=8), POINTER :: CLBF(:)
END TYPE TYPE_FPRQPHY

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

CONTAINS

SUBROUTINE ALLOCATE_FPRQPHY(YDTYPE,KDOM,KLEV,KDIM)

TYPE(TYPE_FPRQPHY), INTENT(INOUT) :: YDTYPE
INTEGER(KIND=JPIM), INTENT(IN)    :: KLEV, KDOM, KDIM 
REAL(KIND=JPRB) :: ZHOOK_HANDLE

LOGICAL :: LL_FIRST_CALL=.TRUE.

! KDOM : number of subdomains
! KLEV : number of levels
! KDIM : number of fields

IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:ALLOCATE_FPRQPHY',0,ZHOOK_HANDLE)

IF (LL_FIRST_CALL) THEN
  NULLIFY(YDTYPE%LLXR)
  NULLIFY(YDTYPE%ICOD)
  NULLIFY(YDTYPE%IVEC)
  NULLIFY(YDTYPE%ILEV)
  NULLIFY(YDTYPE%ILVP)
  NULLIFY(YDTYPE%IDOM)
  NULLIFY(YDTYPE%IDMP)
  NULLIFY(YDTYPE%CLBF)
  NULLIFY(YDTYPE%IMPR)
  LL_FIRST_CALL=.FALSE.
ENDIF

CALL DEALLOCATE_FPRQPHY(YDTYPE)

ALLOCATE(YDTYPE%LLXR(KDIM))
ALLOCATE(YDTYPE%ICOD(KDIM))
ALLOCATE(YDTYPE%IVEC(KDIM))
ALLOCATE(YDTYPE%ILEV(KDIM))
ALLOCATE(YDTYPE%ILVP(KLEV,KDIM))
ALLOCATE(YDTYPE%IDOM(KLEV,KDIM))
ALLOCATE(YDTYPE%IDMP(KDOM,KLEV,KDIM))
ALLOCATE(YDTYPE%CLBF(KDIM))
ALLOCATE(YDTYPE%IMPR(KDIM))
IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:ALLOCATE_FPRQPHY',1,ZHOOK_HANDLE)

END SUBROUTINE ALLOCATE_FPRQPHY

SUBROUTINE DEALLOCATE_FPRQPHY(YDTYPE)

TYPE(TYPE_FPRQPHY), INTENT(INOUT) :: YDTYPE
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:DEALLOCATE_FPRQPHY',0,ZHOOK_HANDLE)

IF (ASSOCIATED(YDTYPE%LLXR)) DEALLOCATE(YDTYPE%LLXR)
IF (ASSOCIATED(YDTYPE%ICOD)) DEALLOCATE(YDTYPE%ICOD)
IF (ASSOCIATED(YDTYPE%IVEC)) DEALLOCATE(YDTYPE%IVEC)
IF (ASSOCIATED(YDTYPE%ILEV)) DEALLOCATE(YDTYPE%ILEV)
IF (ASSOCIATED(YDTYPE%ILVP)) DEALLOCATE(YDTYPE%ILVP)
IF (ASSOCIATED(YDTYPE%IDOM)) DEALLOCATE(YDTYPE%IDOM)
IF (ASSOCIATED(YDTYPE%IDMP)) DEALLOCATE(YDTYPE%IDMP)
IF (ASSOCIATED(YDTYPE%CLBF)) DEALLOCATE(YDTYPE%CLBF)
IF (ASSOCIATED(YDTYPE%IMPR)) DEALLOCATE(YDTYPE%IMPR)

IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:DEALLOCATE_FPRQPHY',1,ZHOOK_HANDLE)

END SUBROUTINE DEALLOCATE_FPRQPHY

LOGICAL FUNCTION ALLOCATED_FPRQPHY(YDTYPE)

TYPE(TYPE_FPRQPHY), INTENT(IN) :: YDTYPE 

ALLOCATED_FPRQPHY = &
 & ASSOCIATED(YDTYPE%LLXR).AND. &
 & ASSOCIATED(YDTYPE%ICOD).AND. &
 & ASSOCIATED(YDTYPE%IVEC).AND. &
 & ASSOCIATED(YDTYPE%ILEV).AND. &
 & ASSOCIATED(YDTYPE%ILVP).AND. &
 & ASSOCIATED(YDTYPE%IDOM).AND. &
 & ASSOCIATED(YDTYPE%IDMP).AND. &
 & ASSOCIATED(YDTYPE%CLBF).AND. &
 & ASSOCIATED(YDTYPE%IMPR)  

END FUNCTION ALLOCATED_FPRQPHY

SUBROUTINE INQUIRE_FPRQPHY(YDTYPE,CDNAME,KULOUT)

! YDTYPE  : structure variable
! CDNAME : structure name
! KULOUT : output logical unit number

TYPE(TYPE_FPRQPHY), INTENT(IN) :: YDTYPE
CHARACTER(LEN=*),   INTENT(IN) :: CDNAME
INTEGER(KIND=JPIM),          INTENT(IN) :: KULOUT

! CLFMT  : format for output

CHARACTER(LEN=36) :: CLFMT
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:INQUIRE_FPRQPHY',0,ZHOOK_HANDLE)
CLFMT='(1X,''ARRAY '',A,A7,'' ALLOCATED '',8I8)'

WRITE(KULOUT,CLFMT) CDNAME,'%LLXR  ',SIZE(YDTYPE%LLXR),SHAPE(YDTYPE%LLXR)
WRITE(KULOUT,CLFMT) CDNAME,'%ICOD  ',SIZE(YDTYPE%ICOD),SHAPE(YDTYPE%ICOD)
WRITE(KULOUT,CLFMT) CDNAME,'%IVEC  ',SIZE(YDTYPE%IVEC),SHAPE(YDTYPE%IVEC)
WRITE(KULOUT,CLFMT) CDNAME,'%ILEV  ',SIZE(YDTYPE%ILEV),SHAPE(YDTYPE%ILEV)
WRITE(KULOUT,CLFMT) CDNAME,'%ILVP  ',SIZE(YDTYPE%ILVP),SHAPE(YDTYPE%ILVP)
WRITE(KULOUT,CLFMT) CDNAME,'%IDOM  ',SIZE(YDTYPE%IDOM),SHAPE(YDTYPE%IDOM)
WRITE(KULOUT,CLFMT) CDNAME,'%IDMP  ',SIZE(YDTYPE%IDMP),SHAPE(YDTYPE%IDMP)
WRITE(KULOUT,CLFMT) CDNAME,'%CLBF  ',SIZE(YDTYPE%CLBF),SHAPE(YDTYPE%CLBF)
WRITE(KULOUT,CLFMT) CDNAME,'%IMPR  ',SIZE(YDTYPE%IMPR),SHAPE(YDTYPE%IMPR)
IF (LHOOK) CALL DR_HOOK('TYPE_FPRQPHYS:INQUIRE_FPRQPHY',1,ZHOOK_HANDLE)

END SUBROUTINE INQUIRE_FPRQPHY

END MODULE TYPE_FPRQPHYS
