SUBROUTINE SUFPPHY(LDNOFIL,CDYFILE)

!**** *SUFPPHY*  - INITIALIZE REQUESTS ON PHYSICS

!     PURPOSE.
!     --------
!        INITIALIZE PHYSIC REQUESTS OF POST-PROCESSING AT A GIVEN TIME STEP.

!**   INTERFACE.
!     ----------
!       *CALL* *SUFPPHY*

!        EXPLICIT ARGUMENTS
!        --------------------
!           LDNOFIL : .TRUE. if no selection namelists
!           CDYFILE : filename containing the selection namelists

!        IMPLICIT ARGUMENTS
!        ------------------
!        NONE.

!     METHOD.
!     -------
!        SEE DOCUMENTATION

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

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

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-04-08
!        Modified : 99-05-06 D.Dent   : more flexible PP options
!        Modified : 00-05-25 R. El Khatib : abort if unknown fields
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvments
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULNAM   ,NULOUT
USE PARFPOS  , ONLY : JPOSDOM  ,JPOSLEN  ,JPOSPHY  ,JPOSCFU  ,JPOSXFU  , &
 & JPOSSGP  
USE YOMFPC   , ONLY : CFPDOM   ,NFPDOM   ,CFPPHY   ,CFPCFU   ,CFPXFU   , &
 & NFPPHY   ,NFPCFU   ,NFPXFU   ,LTRACEFP ,NFPDPHY  
USE YOM4FPOS , ONLY : NCPHYFP  ,NAPHYFP  ,NIPHYFP  ,NFPOSPHY
USE YOMAFN   , ONLY : GFP_PHYDS
USE YOMCT0   , ONLY : NPRINTLEV

IMPLICIT NONE

LOGICAL           ,INTENT(IN)    :: LDNOFIL 
CHARACTER(LEN=*)  ,INTENT(IN)    :: CDYFILE 
INTEGER(KIND=JPIM), PARAMETER :: JPSIZE=JPOSDOM*(JPOSLEN+1)-1

CHARACTER(LEN=1)      :: CLBLANK=' '
CHARACTER(LEN=16)     :: CLPHY(JPOSSGP),  CLCFU(JPOSCFU),  CLXFU(JPOSXFU)
CHARACTER(LEN=JPSIZE) :: CLDPHY(JPOSSGP), CLDCFU(JPOSCFU), CLDXFU(JPOSXFU)

INTEGER(KIND=JPIM) :: I, ICFU, IERR, IPHY, IXD, IXFU, J, JD, JF, IN
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "posnam.intfb.h"
#include "sufpdom.intfb.h"

#include "namfpphy.h"

IF (LHOOK) CALL DR_HOOK('SUFPPHY',0,ZHOOK_HANDLE)
IXD=JPOSDOM

IERR=0
I=0

SELECT CASE (LDNOFIL)

CASE (.TRUE.)

!   Select all fields :
  NFPOSPHY=NFPDPHY
!   Physical fields :
  DO JF=1,NFPPHY
    I=I+1
    DO J=1, JPOSPHY
      IF (CFPPHY(JF) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN REQUESTED FIELD ',CFPPHY(JF)
      IERR=IERR+1
    ELSE
      NCPHYFP(I)=J
    ENDIF
  ENDDO
!   Cumulated fluxes :
  DO JF=1,NFPCFU
    I=I+1
    DO J=1, JPOSPHY
      IF (CFPCFU(JF) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN REQUESTED FIELD ',CFPCFU(JF)
      IERR=IERR+1
    ELSE
      NCPHYFP(I)=J
    ENDIF
  ENDDO
!   Instantaneous fluxes/diagnostics :
  DO JF=1,NFPXFU
    I=I+1
    DO J=1, JPOSPHY
      IF (CFPXFU(JF) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN REQUESTED FIELD ',CFPXFU(JF)
      IERR=IERR+1
    ELSE
      NCPHYFP(I)=J
    ENDIF
  ENDDO
!   Select all domains for all fields :
  NAPHYFP(1:NFPOSPHY)=NFPDOM
  DO JD=1,NFPDOM
    NIPHYFP(JD,1:NFPOSPHY)=JD
  ENDDO

CASE (.FALSE.)

!   Read selection namelists

  CLPHY (:)=CLBLANK
  CLDPHY(:)=CLBLANK
  CLCFU (:)=CLBLANK
  CLDCFU(:)=CLBLANK
  CLXFU (:)=CLBLANK
  CLDXFU(:)=CLBLANK

  CLOSE(NULNAM)
  OPEN(NULNAM,FILE=CDYFILE)
  CALL POSNAM(NULNAM,'NAMFPPHY')
  READ(NULNAM,NAMFPPHY)

  IPHY=COUNT(CLPHY /= CLBLANK)
  ICFU=COUNT(CLCFU /= CLBLANK)
  IXFU=COUNT(CLXFU /= CLBLANK)

!   Count selected fields :
  IF (IPHY > NFPPHY) THEN
    WRITE(NULOUT,*) 'TOO MUCH PHYSICAL FIELDS IN SELECTION NAMELIST'
    CALL ABOR1('SUFPPHY: ABOR1 CALLED')
  ELSEIF (ICFU > NFPCFU) THEN
    WRITE(NULOUT,*) 'TOO MUCH CUMULATED FLUXES IN SELECTION NAMELIST'
    CALL ABOR1('SUFPPHY: ABOR1 CALLED')
  ELSEIF (IXFU > NFPXFU) THEN
    WRITE(NULOUT,*) 'TOO MUCH INSTANTANEOUS FLUXES IN SELECTION NAMELIST'
    CALL ABOR1('SUFPPHY: ABOR1 CALLED')
  ELSE
    NFPOSPHY=IPHY+ICFU+IXFU
  ENDIF
!   Physical fields :
  IN=0
  DO JF=1,IPHY
    IN=IN+1
    DO WHILE (CLPHY(IN) == CLBLANK)
      IN=IN+1
    ENDDO
    I=I+1
    DO J=1, JPOSPHY
      IF (CLPHY(IN) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN SELECTED FIELD ',CLPHY(IN)
      IERR=IERR+1
    ELSEIF (ANY(CFPPHY(1:NFPPHY)==CLPHY(IN))) THEN
      NCPHYFP(I)=J
      CALL SUFPDOM(CFPDOM,IXD,NFPDOM,CLDPHY(IN),1,1,NAPHYFP(I),NIPHYFP(1,I))
    ELSE
      WRITE(NULOUT,*) ' MISSING FIELD IN MAIN NAMELIST ', CLPHY(IN)
    ENDIF
  ENDDO
!   Cumulated fluxes :
  IN=0
  DO JF=1,ICFU
    IN=IN+1
    DO WHILE (CLCFU(IN) == CLBLANK)
      IN=IN+1
    ENDDO
    I=I+1
    DO J=1, JPOSPHY
      IF (CLCFU(IN) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN SELECTED FIELD ',CLCFU(IN)
      IERR=IERR+1
    ELSEIF (ANY(CFPCFU(1:NFPCFU)==CLCFU(IN))) THEN
      NCPHYFP(I)=J
      CALL SUFPDOM(CFPDOM,IXD,NFPDOM,CLDCFU(IN),1,1,NAPHYFP(I),NIPHYFP(1,I))
    ELSE
      WRITE(NULOUT,*) ' MISSING FIELD IN MAIN NAMELIST ', CLCFU(IN)
    ENDIF
  ENDDO
!   Instantaneous fluxes/diagnostics :
  IN=0
  DO JF=1,IXFU
    IN=IN+1
    DO WHILE (CLXFU(IN) == CLBLANK)
      IN=IN+1
    ENDDO
    I=I+1
    DO J=1, JPOSPHY
      IF (CLXFU(IN) == GFP_PHYDS(J)%CLNAME) EXIT
    ENDDO
    IF (J > JPOSPHY) THEN
      WRITE(NULOUT,*) ' UNKNOWN SELECTED FIELD ',CLXFU(IN)
      IERR=IERR+1
    ELSEIF (ANY(CFPXFU(1:NFPXFU)==CLXFU(IN))) THEN
      NCPHYFP(I)=J
      CALL SUFPDOM(CFPDOM,IXD,NFPDOM,CLDXFU(IN),1,1,NAPHYFP(I),NIPHYFP(1,I))
    ELSE
      WRITE(NULOUT,*) ' MISSING FIELD IN MAIN NAMELIST ', CLXFU(IN)
    ENDIF
  ENDDO

END SELECT

IF (IERR > 0) CALL ABOR1('SUFPPHY: ABOR1 CALLED')

IF (LTRACEFP .AND. NPRINTLEV >= 2 .AND. NFPOSPHY > 0) THEN
  WRITE(NULOUT,*) ' THE FOLLOWING PHYSICAL FIELDS WILL BE POST_PROCESSED AT ', &
   & 'THE CURRENT TIME STEP :'  
  DO JF=1,NFPOSPHY
    WRITE(NULOUT,FMT='(A16,''/'',15(1X,A7))') GFP_PHYDS(NCPHYFP(JF))%CLNAME, &
     & (CFPDOM(NIPHYFP(JD,JF)), JD=1,NAPHYFP(JF))  
  ENDDO
ENDIF

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