SUBROUTINE FPCORDYN(KFIELDS,KGPST,KGPEND,PROW,KFPROMA)

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

USE YOMAFN   , ONLY : TFP_HU     ,TFP_SP     ,TFP_PJET   ,TFP_O3MX,&
 & TFP_L, TFP_I, TFP_CLF, TFP_SN, TFP_RR, TFP_GR, TFP_HL, TFP_TKE
USE YOMFPC   , ONLY : LFPLOSP  ,LFPRH100
USE YOMVPOS  , ONLY : QFPTYPE  ,NFPOSDYN  ,NFPTRDYN


!**** *FPCORDYN*  - FULL-POS corrector for dynamic fields after 
!                   horizontal interpolations

!     PURPOSE.
!     --------
!        To correct dynamic fields after the horizontal interpolations that 
!        may create overshoots, or to recover the requested fields.

!**   INTERFACE.
!     ----------
!       *CALL* *FPCORDYN*

!        EXPLICIT ARGUMENTS
!        --------------------

!        INPUT:
!         KFIELDS  : number of fields in row
!         KGPST    : first output point in row.
!         KGPEND   : last output point in row.
!         KFPROMA  : length of the output row.
!        INPUT/OUTPUT:
!         PROW     : fields rows to correct.

!        IMPLICIT ARGUMENTS
!        --------------------
!          See #include below. 

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

!     EXTERNALS.
!     ----------
!      Calls FPHOR12.

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS
!        See documentation about FULL-POS.

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

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 98-09-02 from FPINTDYN
!    R. El Khatib : 99-10-25 More flexibility for Ps/Log(Ps), LFPRH100
!    R. El Khatib : 99-12-02 Control of O3
!    R. El Khatib : 00-08-18 derived types
!    R. El Khatib : 01-08-07 Pruning options
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!    R. El Khatib : 04-09-29 Fix on o3
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KGPST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KGPEND 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PROW(KFPROMA,KFIELDS) 
REAL(KIND=JPRB) :: ZDUM(KFPROMA), ZMIN(KFPROMA), ZMAX(KFPROMA)

INTEGER(KIND=JPIM) :: ICOR, JFLD, JI, IJ, IPTR, INC, JL, IFLD
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "fphor12.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('FPCORDYN',0,ZHOOK_HANDLE)
IPTR=1
DO JFLD=1,NFPOSDYN
  IJ=NFPTRDYN(JFLD)
  INC=QFPTYPE%ILEV(IJ)
  IF (IJ==TFP_HU%ICOD) THEN
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    IF (LFPRH100) THEN
!     0. < Relative moisture < 100.
      ZMAX(KGPST:KGPEND)=100.0_JPRB
    ELSE
!     0. < Relative moisture < 1.
      ZMAX(KGPST:KGPEND)=1.0_JPRB
    ENDIF
    ICOR=6
  ELSEIF(IJ==TFP_O3MX%ICOD) THEN
!   Ozone > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_L%ICOD) THEN
!   Cloud water > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_I%ICOD) THEN
!   Ice > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_CLF%ICOD) THEN
!   Cloud fraction > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_SN%ICOD) THEN
!   Snow > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_RR%ICOD) THEN
!   Rain > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_GR%ICOD) THEN
!   Graupel > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_HL%ICOD) THEN
!   Haill > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_TKE%ICOD) THEN
!   TKE > 1E-06
    ZMIN(KGPST:KGPEND)=0.000001_JPRB
    ICOR=4
  ELSEIF(IJ==TFP_SP%ICOD) THEN
!   After interpolation of log(Ps) for better consistency with PHIs,
!   Recover Ps or keep Log(Ps) :
    IF (.NOT.LFPLOSP) THEN
      DO JL=IPTR,IPTR+INC-1
        DO JI=KGPST,KGPEND
          PROW(JI,JL)=EXP(PROW(JI,JL))
        ENDDO
      ENDDO
    ENDIF
    ICOR=0
  ELSEIF(IJ==TFP_PJET%ICOD) THEN
!   Jet pressure > 0.
    ZMIN(KGPST:KGPEND)=0.0_JPRB
    ICOR=4
  ELSE
    ICOR=0
  ENDIF
  IF (ICOR /= 0) THEN
    DO JL=IPTR,IPTR+INC-1
      IFLD=JL
      CALL FPHOR12(IFLD,KGPST,KGPEND,PROW,KFIELDS,KFPROMA,ICOR,ZDUM,ZDUM, &
       & ZMIN,ZMAX,ZDUM,ZDUM)  
    ENDDO
  ENDIF
  IPTR=IPTR+INC
ENDDO
IF (IPTR-1 /= KFIELDS) CALL ABOR1('FPCORDYN : INTERNAL ERROR')

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

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