SUBROUTINE DYNFPOS(CDCONF,LDCONF)

!**** *DYNFPOS*  - Full Post-Processing of dynamics

!     PURPOSE.
!     --------
!        Performs vertical then horizontal post-processing on dynamical fields

!**   INTERFACE.
!     ----------
!       *CALL* *DYNFPOS*

!        EXPLICIT ARGUMENTS
!        --------------------
!             CDCONF : configuration of STEPO
!             LDCONF : configuration of DYNFPOS
!                      .TRUE. <=> all scans
!                      .FALSE.<=> one single scan on model levels

!        IMPLICIT ARGUMENTS
!        --------------------

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

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

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

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

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-04-08
!        M.Hamrud : 95-10-30 Changed CDCONF def.
!    R. El Khatib : 98-04-06  Tunable spectral packing
!    R. El Khatib 98-08-14 Simplify(?) scan controls
!    R. El Khatib : 00-08-25 remove arrays for packing
!    R. El Khatib : 01-04-10 systematic go to spectral space if any spectral fit
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!    R. El Khatib : 03-08-26 LFPMAT (bugfix)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!    R. El Khatib : 04-08-10 optimisation
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT
USE YOMFPDIM , ONLY : NFPGT1   ,NFPAUXB  ,NFPSPD   ,NFPDYNB  ,NAFPOS   , &
 & NFPSPB  ,NFPGT0B  ,NFPSCA   ,NFPVEC   ,NFPSCAG  ,NFPVECG  , &
 & NFPIVEC ,NFPISCA  ,NFPIVECG ,NFPISCAG ,NFPUVMN  ,NFPBFIN  , &
 & NFPBFOUT  
USE YOMFPC   , ONLY : CFPFMT   ,LTRACEFP
USE YOMCT0   , ONLY : LALLOPR  ,LFPART2
USE YOMFPF   , ONLY : LFPFIL   ,LFPMAT
USE PTRFPB2  , ONLY : NFPVT0
USE YOMPFPB  , ONLY : GFPBUF   ,NSTAGFPB

IMPLICIT NONE

CHARACTER(LEN=9)  ,INTENT(INOUT) :: CDCONF 
LOGICAL           ,INTENT(IN)    :: LDCONF 
CHARACTER (LEN = 1) ::  CLVFP,CLHFP,CLLFP,CLWFP
INTEGER(KIND=JPIM) :: IEN, IST, JSCAN

LOGICAL, SAVE :: LLFILTER=.FALSE.
LOGICAL :: LLSCAN1

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cpvpospr.intfb.h"
#include "fpfilter.intfb.h"
#include "stepo.intfb.h"
#include "sufpconf.intfb.h"
#include "suvfpos.intfb.h"
#include "suvfposl.intfb.h"

!     CLVFP : Configuration for vertical post-processing
!     CLHFP : Configuration for horizontal post-processing
!     CLLFP : Configuration for lagged post-processing
!     CLWFP : Configuration for IOPACK

!*       1. FIND SCANS TO PERFORM
!           ---------------------

IF (LHOOK) CALL DR_HOOK('DYNFPOS',0,ZHOOK_HANDLE)
IF (LDCONF) THEN
  IST=1
  IEN=5
ELSE
  IST=0
  IEN=0
ENDIF
LLSCAN1=.TRUE.

!*       2. LOOP ON SCANS
!           -------------

DO JSCAN=IST,IEN

!*     2.1 PREPARATIONS

!*     2.1.1 INITIALIZE CONFIGURATION

  CALL SUFPCONF(JSCAN,CLVFP,CLHFP,CLLFP,CLWFP,CFPFMT)

!*     2.1.2 INITIALIZE VERTICAL AND HORIZONTAL POST-PROCESSING

  IF (CLLFP == '0') THEN
!   Normal scan
    CALL SUVFPOS(CLVFP,NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPVEC,NFPSCA,&
     & NFPGT0B,NFPDYNB,NFPVECG,NFPSCAG,NFPIVEC,NFPISCA,NFPIVECG,NFPISCAG,&
     & NFPUVMN)  
    NAFPOS=0
  ELSE
!         Find first is lagged vertical pp is effective :
    CALL SUVFPOS(CLLFP,NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPVEC,NFPSCA,&
     & NFPGT0B,NFPDYNB,NFPVECG,NFPSCAG,NFPIVEC,NFPISCA,NFPIVECG,NFPISCAG,&
     & NFPUVMN)  
    IF (NFPDYNB > 0) THEN
!     scan starts on model fields
      CALL SUVFPOS(CLVFP,NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPVEC,NFPSCA,&
       & NFPGT0B,NFPDYNB,NFPVECG,NFPSCAG,NFPIVEC,NFPISCA,NFPIVECG,NFPISCAG,&
       & NFPUVMN)  
      NAFPOS=NFPDYNB
    ELSE
!     No scan on this kind of level
      CYCLE
    ENDIF
  ENDIF

  IF (NFPDYNB > 0) THEN

!   Compute matrixes once :
    IF (LFPMAT.AND.ANY(LFPFIL).AND.(NFPSPD > 0).AND.(.NOT.LLFILTER)) THEN
      CALL FPFILTER
      LLFILTER=.TRUE.
    ENDIF

!*       2.1.3 PRINT OUT DIMENSIONNING

    IF (LTRACEFP) THEN

      WRITE(UNIT=NULOUT,FMT='(&
       & '' DYNFPOS PRINTS OUT FOR CONFIGURATION '',3A1)')&
       & CLVFP,CLHFP,CLWFP  
      WRITE(UNIT=NULOUT,FMT='('' NFPGT1 = '',I4 &
       & ,'' NFPAUXB = '',I4,'' NFPSPD = '',I4 &
       & ,'' NFPSPB = '',I4,'' NFPGT0B = '',I4,'' NFPDYNB = '',I4)')&
       & NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPGT0B,NFPDYNB  
      WRITE(UNIT=NULOUT,FMT='('' NFPVEC = '',I4,'' NFPSCA = '',I4,&
       & '' NFPVECG = '',I4,'' NFPSCAG = '',I4)') NFPVEC,NFPSCA,NFPVECG,NFPSCAG  
      WRITE(UNIT=NULOUT,FMT='('' NFPIVEC = '',I4,'' NFPISCA = '',I4,&
       & '' NFPIVECG = '',I4,'' NFPISCAG = '',I4)') NFPIVEC,NFPISCA,NFPIVECG,&
       & NFPISCAG  
      WRITE(UNIT=NULOUT,FMT='('' NFPUVMN = '',I4)') NFPUVMN

    ENDIF

!*       2.2 POST-PROCESSING STEP

!        2.2.1 VERTICAL POST-PROCESSING


    IF (LDCONF .AND. .NOT.LLSCAN1 .AND. .NOT.LFPART2) THEN
      CDCONF(2:2)='0'
      CDCONF(3:3)='0'
    ELSE
      CDCONF(2:2)='A'
      CDCONF(3:3)='A'
      LLSCAN1=.FALSE.
    ENDIF
    CDCONF(4:4)='0'
    CDCONF(5:5)=CLVFP
    CDCONF(6:6)='0'
    IF (NFPGT1 > 0) THEN
      CDCONF(7:7)='P'
      CDCONF(8:8)='P'
      CDCONF(9:9)='P'
    ELSE
      CDCONF(7:9)='000'
    ENDIF
    CALL CPVPOSPR(CDCONF(5:5))
    NFPBFIN =0
    NFPBFOUT=0
    CALL STEPO(CDCONF)

!        2.2.2 HORIZONTAL POST-PROCESSING & LAGGED VERTICAL PP.

    IF (CLHFP /= '0') THEN
      CDCONF(1:1)='0'
      IF (NFPGT0B > 0) THEN
        CDCONF(2:2)='P'
        CDCONF(3:3)='P'
      ELSE
        CDCONF(2:2)='0'
        CDCONF(3:3)='0'
      ENDIF
      CDCONF(4:4)='0'
      CDCONF(5:5)=CLHFP
      CDCONF(6:9)='0000'
      NFPBFIN =NFPGT0B+NFPAUXB
      NFPBFOUT=NFPDYNB 
      CALL STEPO(CDCONF)
      IF (CLLFP /= '0') THEN
        CALL SUVFPOS(CLLFP,NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPVEC,NFPSCA,&
         & NFPGT0B,NFPDYNB,NFPVECG,NFPSCAG,NFPIVEC,NFPISCA,NFPIVECG,NFPISCAG,&
         & NFPUVMN)  
        IF (LTRACEFP) THEN
          WRITE(UNIT=NULOUT,FMT='('' YOMFPDIM FOR'',&
           & ''  CONFIGURATION '',3A1)') CLHFP,CLLFP,CLWFP  
          WRITE(UNIT=NULOUT,FMT='('' NFPGT1 = '',I4,'' NFPAUXB = '',I4,&
           & '' NFPSPD = '',I4,'' NFPSPB = '',I4,&
           & '' NFPVEC = '',I4,'' NFPSCA = '',I4,'' NFPGT0B = '',I4,&
           & '' NFPDYNB = '',I4)')&
           & NFPGT1,NFPAUXB,NFPSPD,NFPSPB,NFPVEC,NFPSCA,NFPGT0B,NFPDYNB  
          WRITE(UNIT=NULOUT,FMT='('' NAFPOS = '',I4)') NAFPOS
          WRITE(UNIT=NULOUT,FMT='('' NFPVEC = '',I4,'' NFPSCA = '',I4,&
           & '' NFPVECG = '',I4,'' NFPSCAG = '',I4)') NFPVEC,NFPSCA,NFPVECG,&
           & NFPSCAG  
          WRITE(UNIT=NULOUT,FMT='('' NFPIVEC = '',I4,'' NFPISCA = '',I4,&
           & '' NFPIVECG = '',I4,'' NFPISCAG = '',I4)') NFPIVEC,NFPISCA,&
           & NFPIVECG,NFPISCAG  
          WRITE(UNIT=NULOUT,FMT='('' NFPUVMN = '',I4)') NFPUVMN
        ENDIF
        CDCONF(1:4)='0000'
        CDCONF(5:5)=CLLFP
        CDCONF(6:9)='0000'
        CALL CPVPOSPR(CDCONF(5:5))
        NFPBFIN =0
        NFPBFOUT=NFPDYNB
        CALL STEPO(CDCONF)
      ENDIF
    ENDIF

!        2.2.3 WRITE OUT POST-PROCESSED FIELDS

!   "Lagged" variables to write out post-processed fields
    CALL SUVFPOSL
    CDCONF(1:1)=CLWFP

  ENDIF
ENDDO

IF (NFPVT0 > 0) THEN
  DEALLOCATE(GFPBUF,NSTAGFPB)
  IF (LALLOPR) THEN
    WRITE(NULOUT,9991) 'GFPBUF   '
    WRITE(NULOUT,9991) 'NSTAGFPB '
  ENDIF
ENDIF

! ---------------------------------------------------------------------
9990 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',8I8)
9991 FORMAT(1X,'ARRAY ',A10,' DEALLOCATED ')

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