!--------------------------------------------------------------------------------------------------
!  DOXYGEN
!> @brief CONVECTIVE PRECIPITATION PARAMETERIZATION
!> @details FEEDS TENDENCIES GENERATED BY THE KAIN-FRITSCH CONVECTIVE SCHEME INTO THE GRIDSCALE 
!! TEMPERATURE AND MOISTURE FIELDS
!> @author ORIGINATOR - KAIN 
!> @date ??-??-?? \n
!> @author LUCCI 
!> @date 18-03-20 \n
!> @version V1.1.0
!> @details MODERNIZATION OF THE CODE, INCLUDING:
!!                      * F77 TO F90/F95
!!                      * INDENTATION & UNIFORMIZATION CODE
!!                      * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!!                      * DOCUMENTATION WITH DOXYGEN
!!                      * OPENMP FUNCTIONALITY 
!<
!> @details <b>Use Module:</b>
!! @arg @c ACMCLH
!! @arg @c ACMPRE
!! @arg @c CLDWTR
!! @arg @c CNVCLD
!! @arg @c CTLBLK
!! @arg @c DYNAM
!! @arg @c F77KINDS
!! @arg @c KFFDBK
!! @arg @c LOOPS
!! @arg @c MASKS
!! @arg @c MPPSTAFF
!! @arg @c PARMETA
!! @arg @c PVRBLS
!! @arg @c VRBLS
!> @details <b>Driver:</b> 
!! @arg @c GEF
!> @details <b>Calls:</b>
!--------------------------------------------------------------------------------------------------
    SUBROUTINE KFTEND
!--------------------------------------------------------------------------------------------------
! SUBROUTINE KFTEND
!
! SUBPROGRAM: KFTEND - CONVECTIVE PRECIPITATION PARAMETERIZATION
! PROGRAMMER: KAIN
! ORG: ?????
! DATE: ??-??-??
!
! ABSTRACT:
! KFTEND FEEDS TENDENCIES GENERATED BY THE KAIN-FRITSCH CONVECTIVE SCHEME INTO THE GRIDSCALE 
! TEMPERATURE AND MOISTURE FIELDS
! 
! PROGRAM HISTORY LOG:
! ??-??-??  KAIN       - ORIGINATOR
! 18-01-15  LUCCI      - MODERNIZATION OF THE CODE, INCLUDING:
!                        * F77 TO F90/F95
!                        * INDENTATION & UNIFORMIZATION CODE
!                        * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!                        * DOCUMENTATION WITH DOXYGEN
!                        * OPENMP FUNCTIONALITY
!
! INPUT ARGUMENT LIST:
! NONE 
!
! OUTPUT ARGUMENT LIST:
! NONE
!
! INPUT/OUTPUT ARGUMENT LIST:
! NONE   
!
! USE MODULES: ACMCLH
!              ACMPRE
!              CLDWTR
!              CNVCLD
!              CTLBLK
!              DYNAM
!              F77KINDS
!              KFFDBK
!              LOOPS
!              MASKS
!              MPPSTAFF
!              PARMETA
!              PVRBLS
!              VRBLS
!  
! DRIVER     : GEF
!
! CALLS      : -----
!--------------------------------------------------------------------------------------------------
    USE ACMCLH
    USE ACMPRE
    USE CLDWTR
    USE CNVCLD
    USE CTLBLK
    USE DYNAM
    USE F77KINDS
    USE KFFDBK
    USE LOOPS
    USE MASKS
    USE MPPSTAFF
    USE PARMETA
    USE PVRBLS
    USE VRBLS
!----------------------------------------------
! FEEDBACK CONVECTIVE TENDENCIES EACH TIME STEP
!----------------------------------------------
!
!$omp parallel do private(I, L, LG)
!
    DO 30 J=1,JM
        DO 30 I=1,IM
 
            IF (NCA(I,J) <= 0) GOTO 30
            LG = LMH(I,J)
 !
            DO L=1,LG
                   T(I,J,L) =     T(I,J,L) +  DTDT(I,J,L) * DT
                   Q(I,J,L) =     Q(I,J,L) +  DQDT(I,J,L) * DT
                 CWM(I,J,L) =   CWM(I,J,L) + DQCDT(I,J,L) * DT 
               TCUCN(I,J,L) = TCUCN(I,J,L) +  DTDT(I,J,L) * DT 
            END DO
!
            CUPREC(I,J) = CUPREC(I,J) + RAINCV(I,J) * 0.01
            ACPREC(I,J) = ACPREC(I,J) + RAINCV(I,J) * 0.01
             CUPPT(I,J) =  CUPPT(I,J) + RAINCV(I,J) * 0.01
              PREC(I,J) =   PREC(I,J) + RAINCV(I,J) * 0.01
             PPTKF(I,J) =  PPTKF(I,J) + RAINCV(I,J) * 0.01
! 
            IF (NCAD(I,J) > 0) THEN
                 TNCA(I,J) =  TNCA(I,J) + 1.
                SPCLB(I,J) = SPCLB(I,J) + PCLB(I,J)
!--------------------------------------------- 
! USE PEAK VALUE OF UMF INSTEAD OF 1 HOUR AVG.
! DO THE SAME FOR PSRC.
!---------------------------------------------
                SPSRC(I,J) = AMAX1(PSRC(I,J) ,SPSRC(I,J))
                SUMFB(I,J) = AMAX1(UMFB(I,J) ,SUMFB(I,J))
                 NCAD(I,J) =       NCAD(I,J) - 1
            END IF
!---------------------------------------- 
! UPDATE THE CONVECTIVE TIME SCALE ARRAY.
!----------------------------------------
            NCA(I,J) = NCA(I,J) - 1
! 
            IF (NCA(I,J) == 0) THEN
                RAINCV(I,J) = 0.
! 
                DO L=1,LM
                     DTDT(I,J,L) = 0.
                     DQDT(I,J,L) = 0.
                    DQCDT(I,J,L) = 0.  
                END DO
! 
                PSRC(I,J) = 0.
                PCLB(I,J) = 0.
                UMFB(I,J) = 0.
            END IF
!
30  CONTINUE
!
    RETURN
!
    END SUBROUTINE KFTEND
