Skip to content
Snippets Groups Projects
vdfhghtnhl.F90 47.5 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
!OPTIONS XOPT(HSFUN)
SUBROUTINE VDFHGHTNHL (YDVDF,YDEPHLI,YDECUMF,YDEPHY,YDPARAR,KIDIA   , KFDIA   , KLON    , KLEV   , KDRAFT, PTMST, KSTEP, &
                   & PUM1    , PVM1    , PTM1    , PQM1    , PLM1    , PIM1   , PAM1,&
                   & PAPHM1  , PAPM1   , PGEOM1  , PGEOH  , &
                   & PKMFL   , PKHFL   , PKQFL   , PMFLX  , PEXNF , PEXNH, &
! DIAGNOSTIC OUTPUT

                   & PUUH    , PVUH    , PSLGUH  , PQTUH  , PTHTVUH, PFRACB, &
                   & PZPTOP  , KPTOP   , PZPLCL  , KPLCL  , KPLZB   , &
                   &  PRICUI  , &
                   & PFPLVL  , PFPLVN  , PCLFR, &
                   & PBIR    , LDNODECP, LDRUNDRY, KPBLTYPE, &
                   & YSPP_CLDDPTH,YSPP_CLDDPTHDP, &
                   & YSPP_RFAC_TWOC,YSPP_RZC_H,YSPP_RZL_INF, &
                   & ZLENGTH_M, ZLENGTH_H,  PTKE)

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

!**   * VDFHGHTNHL* - DETERMINES THE PBL-HEIGHT AND STRONG UPDRAFT FIELDS
!                  USING A ENTRAINING PARCEL ASCENT METHOD.

!     A.P. SIEBESMA                30/06/1999  Original (dry)
!     M. Ko"hler                    3/12/2004  Moist Version
!     Roel Neggers                 12/04/2005  Multiple updraft extension
!     Wim de Rooy/Geert Lenderink  13/06/2008 and 21/09/2010 Updates to combine TKE turbulence
!                                              with dual updraft EDMF. Lateral mixing according
!                                              to de Rooy & Siebesma MWR 2008 and QJRMS 2010
!     Wim de Rooy                  July /2015  Implementation LHARATU in Harmonie
!     Lisa Bengtsson               Feb /2017   Introduce LTOTPREC option
!     Karl-Ivar Ivarsson           Feb /2018   Code optimation
!     R. El Khatib 30-Apr-2019                 bugfix
!     Wim de Rooy                  June / 2019 Modifications among which energy
!                                              energy cascade term
!     R. El Khatib 27-Aug-2019                 Cleaning
!     Karl-Ivar Ivarsson           April /2020 Introduce LTOTPRECL option
!     U. Andrae Dec 2020                       Introduce SPP for HARMONIE-AROME
!      R. El Khatib 08-Jul-2022 Contribution to the encapsulation of YOMCST and YOETHF


!     PURPOSE
!     -------

!     DETERMINE PBL HEIGHT AND UPDRAFT FIELDS

!     INTERFACE
!     ---------

!     * VDFHGHTNHL* IS CALLED BY *VDFHGHTHL*

!     PARAMETER     DESCRIPTION                                   UNITS
!     ---------     -----------                                   -----
!     INPUT PARAMETERS (INTEGER):

!     *KIDIA*        START POINT
!     *KFDIA*        END POINT
!     *KLEV*         NUMBER OF LEVELS
!     *KLON*         NUMBER OF GRID POINTS PER PACKET
!     *KDRAFT*       NUMBER OF EXPLICITLY MODELED DRAFTS - CURRENTLY 3:
!                    1: test parcel
!                    2: rising dry thermals which stop at cloud base or inversion
!                    3: rising dry thermals which become cloudy
!                    (4: downdrafts .. to be done?)

!     INPUT PARAMETERS (REAL):

!     *PTMST*        DOUBLE TIME STEP (SINGLE AT 1TH STEP)        S
!     *PUM1*         X-VELOCITY COMPONENT AT T-1                  M/S
!     *PVM1*         Y-VELOCITY COMPONENT AT T-1                  M/S
!     *PTM1*         TEMPERATURE AT T-1                           K
!     *PQM1*         SPECIFIC HUMUDITY AT T-1                     KG/KG
!     *PLM1*         SPECIFIC CLOUD LIQUID WATER AT T-1           KG/KG
!     *PIM1*         SPECIFIC CLOUD ICE AT T-1                    KG/KG
!     *PAM1*         CLOUD FRACTION AT T-1                        KG/KG
!     *PAPHM1*       PRESSURE AT HALF LEVEL AT T-1                PA
!     *PAPM1*        PRESSURE AT FULL LEVEL AT T-1                PA
!     *PGEOM1*       GEOPOTENTIAL AT T-1                          M2/S2
!     *PGEOH*        GEOPOTENTIAL AT HALF LEVEL                   M2/S2
!     *PKMFL*        SURFACE KINEMATIC MOMENTUM FLUX              M2/S2
!     *PKHFL*        SURFACE KINEMATIC HEAT FLUX                  K*M/S
!     *PKQFL*        SURFACE KINEMATIC MOISTURE FLUX              M/S
!     *PBIR*         BUOYANCY-FLUX INTEGRAL RATIO (-N/P)
!                    USED FOR DECOUPLING CRITERIA
!     *PEXNF*        ENXNER FUNCTION FOR FULL PRESSURE LEVELS (FOR OPTIMATION OF CODE)
!     *PEXNH*        ENXNER FUNCTION FOR HALF PRESSURE LEVELS (FOR OPTIMATION OF CODE),
!                    =  (P / PREF) ** R/CP

!     INPUT PARAMETERS (LOGICAL):

!     *LDNODECP*     TRUE:  NEVER DECOUPLE
!                    FALSE: MAYBE DECOUPLE
!     *LDRUNDRY*     TRUE:  RUN PARCEL WITHOUT CONDENSATION
!                    FALSE: RUN PARCEL WITH CONDENSATION

!     OUTPUT PARAMETERS (REAL):

!     *PFPLVL*       PBL PRECIPITATION FLUX AS RAIN                KG/(M**2*S)
!     *PFPLVN*       PBL PRECIPITATION FLUX AS SNOW                KG/(M**2*S)

!     *PUUH*         UPDRAFT X-MOMENTUM
!     *PVUH*         UPDRAFT Y-MOMENTUM
!     *PSLGUH*       UPDRAFT GENERALIZED LIQUID STATIC ENERGY (SLG)
!                    AT HALF LEVEL                                M2/S2
!     *PQTUH*        UPDRAFT SPECIFIC TOTAL WATER AT HALF LEVEL   KG/KG
!     *PTHTVUH*      UPDRAFT virt potential temp at HALF LEVEL (for TKE)  K
!     *PMFLX*        PBL MASS FLUX                                M/S
!     *PZPLCL*       HEIGHT OF LIFTING CONDENSATION LEVEL OF UPDRAFT          M
!     *PZPTOP*       HEIGHT OF LEVEL OF ZERO KINETIC ENERGY (W=0) OF UPDRAFT  M
!cstep/GL
!     *PBUOY_COR*    STABILITY CORRECTION PARAMETER TO BE USED FOR TKE SCHEME
!
!     *PWU*          VERTICAL VELOCITY OF SECOND UPDRAFT
!cstep/GL
!
!     OUTPUT PARAMETERS (INTEGER):

!     *KPLCL*         FIRST HALF LEVEL ABOVE REAL HEIGHT OF UPRAFT LCL
!     *KPTOP*         HIGHEST HALF LEVEL BELOW PZTOP, AND
!                       UPDRAFT TOP FULL LEVEL (PZTOP IS WITHIN THAT LAYER)
!     *KPLZB*         LEVEL OF UPRAFT ZERO BUOYANCY (LAST FULL LEVEL THAT IS POS. BUOYANT)
!     *KPBLTYPE*    -1: not defined yet
!                    0: stable PBL
!                    1: dry convective PBL (no cloud below parcel top)
!                    2: stratocumulus
!                    3: shallow cumulus
!                    4: deep cumulus

!     METHOD
!     ------

!     SEE DOCUMENTATION

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

USE YOEPHLI  , ONLY : TEPHLI
USE PARKIND1  ,ONLY : JPIM     ,JPRB

USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMCST   , ONLY : YDCST=>YRCST ! allows use of included functions. REK.
USE YOETHF   , ONLY : YDTHF=>YRTHF ! allows use of included functions. REK.
USE PARPHY   , ONLY : RKAP
USE YOECUMF  , ONLY : TECUMF
USE YOMPARAR , ONLY : TPARAR
USE YOEPHY   , ONLY : TEPHY
USE YOEVDF   , ONLY : TVDF

!for optimation
USE MODD_CST
USE MODD_RAIN_ICE_DESCR
USE MODD_RAIN_ICE_PARAM
USE MODE_TIWMX_TAB
USE MODE_TIWMX

USE SPP_MOD_TYPE, ONLY : TSPP_CONFIG_TYPE, APPLY_SPP 

IMPLICIT NONE


!*         0.1    GLOBAL VARIABLES

TYPE(TVDF)        ,INTENT(IN)    :: YDVDF
TYPE(TECUMF)      ,INTENT(IN)    :: YDECUMF
TYPE(TEPHLI)      ,INTENT(IN)    :: YDEPHLI
TYPE(TEPHY)       ,INTENT(IN)    :: YDEPHY
TYPE(TPARAR)      ,INTENT(IN)    :: YDPARAR
INTEGER(KIND=JPIM),INTENT(IN)    :: KLON
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV
INTEGER(KIND=JPIM),INTENT(IN)    :: KDRAFT
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP
INTEGER(KIND=JPIM),INTENT(INOUT) :: KPLCL(KLON,KDRAFT)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KPTOP(KLON,KDRAFT)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KPLZB(KLON,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTMST
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PIM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHM1(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM1(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKMFL(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKHFL(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKQFL(KLON)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFLX(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSLGUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQTUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTHTVUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFRACB(KLON,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZPLCL(KLON,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZPTOP(KLON,KDRAFT)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLVL(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLVN(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCLFR(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBIR(KLON)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRICUI(KLON)
TYPE(TSPP_CONFIG_TYPE),INTENT(INOUT) :: YSPP_CLDDPTH,  YSPP_CLDDPTHDP, &
                                      & YSPP_RFAC_TWOC,YSPP_RZC_H,YSPP_RZL_INF
! variables RACMO turbulence scheme
REAL(KIND=JPRB)   ,INTENT(OUT) :: ZLENGTH_M(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(OUT) :: ZLENGTH_H(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTKE(KLON,KLEV)

! variables for optimation of code
REAL(KIND=JPRB)   ,INTENT(IN) :: PEXNF(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN) :: PEXNH(KLON,0:KLEV)

LOGICAL           ,INTENT(IN)    :: LDNODECP(KLON)
!ldrundry not used now
LOGICAL           ,INTENT(IN)    :: LDRUNDRY(KLON)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KPBLTYPE(KLON)
REAL(KIND=JPRB)    :: ZENCASC(KLON,0:KLEV)

! --- variables associated with Lgeert
REAL(KIND=JPRB)    ::  PBUOY_COR (KLON,0:KLEV)
REAL(KIND=JPRB)    :: ZQCUH(KLON,0:KLEV,KDRAFT)
REAL(KIND=JPRB)    :: ZWU2H(KLON,0:KLEV,KDRAFT)

REAL(KIND=JPRB)    ::  PWU       (KLON,0:KLEV)
REAL(KIND=JPRB)    :: ZQSVAR(KLON,KLEV)
REAL(KIND=JPRB)    :: ZDQSDTEMP(KLON,KLEV)
REAL(KIND=JPRB)    :: ZFACW,ZFACI,ZESDP,ZCOR,ZLAT2CP,&
                      & ZESW,ZESI,ZES,ZQSAT,ZEL2R,ZEI2R,ZE2R
!cstep/GL ---------------------------------------------



!*         0.2    LOCAL VARIABLES

!--- mean & environmental properties ---
REAL(KIND=JPRB) ::    ZUSTAR (KLON)      , ZWSTAR(KLON)       , ZKHVFL(KLON)       , &
                    & ZWSIGMA(KLON)      , &
                    & ZSLGENH(KLON,0:KLEV),ZQLENH(KLON,0:KLEV), ZQIENH(KLON,0:KLEV), &
                    & ZQTENH(KLON,0:KLEV), ZUENH(KLON,0:KLEV) , ZVENH(KLON,0:KLEV) , &
                    & ZTVEN(KLON,KLEV),    ZQTM1 (KLON,KLEV)  , &
                    & ZSLGM1(KLON,KLEV)  ,&
                    & ZTENH(KLON,0:KLEV) , ZRHOH (KLON,0:KLEV), ZTHVEN(KLON,KLEV)

REAL(KIND=JPRB) ::    ZSTAR(KLON), ZCHICRIT(KLON,0:KLEV),ZMEANCHICRIT(KLON)   , &
                    & ZDETRSHALLOW(KLON), ZVARQ(KLON,0:KLEV), &
                    & ZFRACMB(KLON) , ZMINIMUM(KLON) ,  &
                    & ZQVENH(KLON,0:KLEV), &
                    & ZTHTVENH(KLON,0:KLEV), ZTHTLUH(KLON,0:KLEV), &
                    & ZTHTENH(KLON,0:KLEV), &
                    & ZTHTVUH(KLON,0:KLEV), ZTHTLEH(KLON,0:KLEV),&
                    & ZDQSDTU(KLON,0:KLEV),ZGAMMA(KLON,0:KLEV),ZDUMFUNC(KLON,0:KLEV), &
                    & ZPRES_0, ZKAPPA,ZQSATU(KLON,0:KLEV)


!--- updraft parameters ---
REAL(KIND=JPRB) ::    ZWUH,DZH, &
                    & ZQUH  (KLON,0:KLEV,KDRAFT), &
    & ZTUH  (KLON,0:KLEV,KDRAFT), ZEPS  (KLON,0:KLEV,KDRAFT), &
    & ZDETR (KLON,0:KLEV,KDRAFT), ZFRAC (KLON,0:KLEV,KDRAFT), &
    & ZBUOF (KLON,KLEV,KDRAFT)  , &
                    & ZDELTAMINEPS(KLON,0:KLEV), ZCAPE1(KLON)

REAL(KIND=JPRB) ::    ZQSATM, ZSATDEF, &
                    & ZUPFLXL(KLON,0:KLEV,KDRAFT), ZUPFLXN(KLON,0:KLEV,KDRAFT), &
                    & ZUPGENL(KLON,KLEV,KDRAFT), ZUPGENN(KLON,KLEV,KDRAFT), &
                    & ZDZRHO, ZPFLXTOT, ZPEVAPUP, ZFAC, ZUPMELT, ZUPMELTTEND

REAL(KIND=JPRB) ::    ZFRACB(KLON,KDRAFT), ZMFLXB(KLON,KDRAFT), ZTVEXCSURF(KLON,KDRAFT)


REAL(KIND=JPRB) ::    ZFRACMAX , ZFACMAXEXC , ZFRACTEST , ZFACTESTEXC , &
                    & ZFACEXC(KLON,KDRAFT), ZDUMFRAC, ZDUMR, TVEXCSURF, ZWT, ZWL, ZEL, ZET, &
                    & ZFACCASC(KLON,0:KLEV)

LOGICAL ::            LLDONE(KLON,KDRAFT)


INTEGER(KIND=JPIM) :: IS, JK, JL, JD, JKM

INTEGER(KIND=JPIM) :: IKSTAR(KLON)

REAL(KIND=JPRB) ::    ZQEXC   , ZTEXC   , ZDZ     , &
                    & ZCONS10 , ZTVMEAN     , &
                    & ZRG     , ZMFMAX  , ZMFS(KLON,KDRAFT)

!          REMAINING MODEL PARAMETERS

REAL(KIND=JPRB) ::    ZTAUEPS(KLON) , ZCLDDEPTH  , &
                    & ZW2THRESH         , ZSTABTHRESH       , ZBIRTHRESH , &
                    & ZCLDDEPTHDP       , ZDZCLOUD(KLON), &
                    & ZREPUST, ZGHM1
    
REAL(KIND=JPRB) ::    ZZI(KLON),ZB1(KLON),ZCOUNT(KLON), &
                    & ZFRMIN(KLON,2),ZMU,ZVAL
INTEGER(KIND=JPIM) :: ITOP, IBASE, JKO,JKE

!cstep 30082007: introduce two parcel time scales, one for the test parcel (ZTAUEPS_TEST) and one
!              : for the actual parcels (ZTAUEPS)
REAL(KIND=JPRB) :: ZTAUEPS_TEST

INTEGER(KIND=JPIM) :: IZI(KLON,KDRAFT)


REAL(KIND=JPRB) ::    ZHOOK_HANDLE


#include "surf_inq.h"

#include "vdfparcelhl.intfb.h"
#include "vdfpdftablehl.intfb.h"
#include "vdfexcuhl.intfb.h"
#include "fcttre.func.h"



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

!*         1.     INITIALIZATION
!                 --------------

IF (LHOOK) CALL DR_HOOK('VDFHGHTNHL',0,ZHOOK_HANDLE)
ASSOCIATE(RTAUMEL=>YDECUMF%RTAUMEL, &
 & RG=>YDCST%RG, RCPD=>YDCST%RCPD, RETV=>YDCST%RETV, RLVTT=>YDCST%RLVTT, RLSTT=>YDCST%RLSTT, &
 & RATM=>YDCST%RATM, RTT=>YDCST%RTT, RLMLT=>YDCST%RLMLT, RD=>YDCST%RD, &
 & R2ES=>YDTHF%R2ES, R3LES=>YDTHF%R3LES, R3IES=>YDTHF%R3IES, R4LES=>YDTHF%R4LES, &
 & R4IES=>YDTHF%R4IES, R5LES=>YDTHF%R5LES, R5IES=>YDTHF%R5IES, R5ALVCP=>YDTHF%R5ALVCP, &
 & R5ALSCP=>YDTHF%R5ALSCP, RALVDCP=>YDTHF%RALVDCP, RALSDCP=>YDTHF%RALSDCP, &
 & RTWAT=>YDTHF%RTWAT, RTICE=>YDTHF%RTICE, RTICECU=>YDTHF%RTICECU, RTWAT_RTICE_R=>YDTHF%RTWAT_RTICE_R, &
 & RTWAT_RTICECU_R=>YDTHF%RTWAT_RTICECU_R, &
 & YSURF=>YDEPHY%YSURF,LHARATU=>YDPARAR%LHARATU, &
 & LTOTPREC=>YDPARAR%LTOTPREC,LTOTPRECL=>YDPARAR%LTOTPRECL)

ZWL= 200._JPRB
ZWT= 400._JPRB
! typical entrainment values at LCL (L) and TOP (T)
ZEL= 0.002_JPRB
ZET= 0.002_JPRB
ZPRES_0 = 100000._JPRB     ! standard pressure consistent with vdfexcu
ZKAPPA = RD / RCPD         ! consistent with vdfexcu

ZFRACTEST   = 0.002_JPRB   ! top % of the PDF associated with the test parcel
CALL VDFPDFTABLEHL (ZFRACTEST, ZFACTESTEXC, ZDUMR, ZDUMR, 0) ! associated PDF scaling factor


ZFRACMAX    = 0.1_JPRB     ! total convective area fraction that is done with mass flux

CALL VDFPDFTABLEHL (ZFRACMAX, ZFACMAXEXC, ZDUMR, ZDUMR, 0) ! associated PDF scaling factor

! eddy turnover time scale used in parcel entrainment [s]  (Neggers, Siebesma & Jonker, JAS 2002)
ZTAUEPS_TEST = 400._JPRB   ! Roel's original ZTAUEPS value


!ZW2THRESH  = -1._JPRB     ! threshold parcel vertical velocity squared [m2/s2]
!CGL
ZW2THRESH   = 0.0_JPRB

ZCLDDEPTH   = 2000._JPRB   ! threshold cloud thickness for stcu/cu transition [m]

ZCLDDEPTHDP = 4000._JPRB ! threshold cloud thickness used in shallow/deep decision [m]

TVEXCSURF = 0.0_JPRB ! initialisation

IF(XFRMIN(19)>0.)ZCLDDEPTH = XFRMIN(19)
IF(XFRMIN(20)>0.)ZCLDDEPTHDP = XFRMIN(20)


ZSTABTHRESH = 20._JPRB     ! threshold stability (Klein & Hartmann criteria) [K]
ZBIRTHRESH  = 0.1_JPRB     ! threshold BIR (TKE decoupling criteria) [1]

CALL SURF_INQ(YSURF,PREPUST=ZREPUST)

! optimization
ZRG         = 1.0_JPRB/RG
ZLAT2CP     = RLVTT/RCPD
ZEL2R       = 0.62198_JPRB*RLVTT/RD
ZEI2R       = 0.62198_JPRB*RLSTT/RD

DO JL=KIDIA,KFDIA
   KPBLTYPE(JL)     = -1           ! -1 means: yet unknown

   ZZI(JL)          = 0._JPRB      ! mixed layer scalings
   ZWSTAR(JL)       = 0._JPRB

   PRICUI(JL)       = 1._JPRB      ! 1 / cumulus inversion Richardson number
   ZCAPE1(JL)       = 0._JPRB

ENDDO

DO JD=1,KDRAFT
   DO JL=KIDIA,KFDIA
      PZPLCL(JL,JD)  = -100._JPRB  ! default value: -100 (no LCL)
      PZPTOP(JL,JD)  = 0._JPRB
      KPLCL(JL,JD)   = 0           ! default value: 0 (no PBL cloud)
      KPTOP(JL,JD)   = 0
      KPLZB(JL,JD)   = 0
      LLDONE(JL,JD)  = .TRUE.      ! default: TRUE (don't launch the parcel)
      ZFRACB(JL,JD)  = 0._JPRB
      PFRACB(JL,JD)  = 0._JPRB
      ZFACEXC(JL,JD) = 0._JPRB
      ZMFLXB(JL,JD)  = 0._JPRB
      ZTVEXCSURF(JL,JD) = 0._JPRB
   ENDDO
ENDDO

DO JK=0,KLEV
   DO JL=KIDIA,KFDIA
      ZCHICRIT(JL,JK) = 0._JPRB
   ENDDO
ENDDO

!--- parcel half level parameters ---
DO JD=1,KDRAFT
   DO JK=0,KLEV
      DO JL=KIDIA,KFDIA
         PUUH(JL,JK,JD)    = 0.0_JPRB
         PVUH(JL,JK,JD)    = 0.0_JPRB
         PSLGUH(JL,JK,JD)  = 0.0_JPRB
         PQTUH(JL,JK,JD)   = 0.0_JPRB
         PTHTVUH(JL,JK,JD) = 0.0_JPRB
         PMFLX(JL,JK,JD)   = 0.0_JPRB
         ZTUH(JL,JK,JD)    = 0.0_JPRB
         ZQUH(JL,JK,JD)    = 0.0_JPRB
         ZQCUH(JL,JK,JD)   = 0.0_JPRB
         ZEPS(JL,JK,JD)    = 0.0_JPRB
         ZDETR(JL,JK,JD)   = 0.0_JPRB
         ZWU2H(JL,JK,JD)   = 0.0_JPRB
         ZFRAC(JL,JK,JD)   = 0.0_JPRB
         ZUPFLXL(JL,JK,JD) = 0.0_JPRB
         ZUPFLXN(JL,JK,JD) = 0.0_JPRB
         ZVARQ(JL,JK)       = 0.0_JPRB
         ZENCASC(JL,JK) =0.0_JPRB
      ENDDO
   ENDDO
ENDDO

!--- parcel full level parameters ---
DO JD=1,KDRAFT
   DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
         ZBUOF(JL,JK,JD)    = 0.0_JPRB
         ZUPGENL(JL,JK,JD)  = 0.0_JPRB
         ZUPGENN(JL,JK,JD)  = 0.0_JPRB
      ENDDO
   ENDDO
ENDDO

! Setup SPP patterns 
IF (YSPP_CLDDPTH%LPERT) THEN
  CALL APPLY_SPP(YSPP_CLDDPTH, &
               & KLON,KIDIA,KFDIA, &
               & ZCLDDEPTH,ZFRMIN(:,1))
ELSE
  DO JL=KIDIA,KFDIA
    ZFRMIN(JL,1) = ZCLDDEPTH
  ENDDO
ENDIF

IF (YSPP_CLDDPTHDP%LPERT) THEN
  CALL APPLY_SPP(YSPP_CLDDPTHDP, &
               & KLON,KIDIA,KFDIA, &
               & ZCLDDEPTHDP,ZFRMIN(:,2))
ELSE
  DO JL=KIDIA,KFDIA
    ZFRMIN(JL,2) = ZCLDDEPTHDP
  ENDDO
ENDIF



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

!*         2.     PREPARE FIELDS ON HALF LEVELS BY LINEAR INTERPOLATION
!*                OF CONSERVED VARIABLES
!                 -----------------------------------------------------

!*         2.1  full level cpm, slg, qt and Tv
!*

DO JK=1,KLEV
   DO JL=KIDIA,KFDIA
      ZSLGM1(JL,JK) = RCPD * PTM1(JL,JK) + PGEOM1(JL,JK)&
           & - RLVTT * PLM1(JL,JK) - RLSTT * PIM1(JL,JK)
      ZQTM1 (JL,JK) = PQM1(JL,JK) + PLM1(JL,JK) + PIM1(JL,JK)

      !          parcel goes through cloud portion of environment
      !          (added ql loading; ql,cld=ql,mean/fc; qv = qsat)
      !          safety: fc>0.1; linear interpolation between overcast
      !                  and cloudy portion for 0<fc<0.1
      !                  guaranteed to be < tv from mean conditions

      !          grid box mean virtual effect
      ZTVMEAN       = PTM1(JL,JK) * ( 1.0_JPRB + RETV * PQM1(JL,JK)&
           & - PLM1(JL,JK) - PIM1(JL,JK) )       !qli loading
      ZTVEN(JL,JK) = ZTVMEAN
      ZTHVEN(JL,JK) = ZTVEN(JL,JK) / PEXNF(JL,JK) !( PAPM1(JL,JK)/RATM )**(-RD/RCPD) * ZTVEN(JL,JK)
   ENDDO
ENDDO


!*         2.2  half-level environment interpolation (qt, ql, qi, slg)
!*              attention:  not good to interpolate everything independently
!*              better:     interpolate conserved variables and derive rest!!!
!*

DO JK=1,KLEV-1
   DO JL=KIDIA,KFDIA

      IF (JK==1) THEN
         ZGHM1 = PGEOH(JL,JK) + 50000._JPRB*RG   !avoid using top half level (=inf)
      ELSE
         ZGHM1 = PGEOH(JL,JK-1)
      ENDIF

      ZQTENH(JL,JK) = ( ZQTM1(JL,JK+1) *(ZGHM1-PGEOH(JL,JK  )) &
                  & +   ZQTM1(JL,JK)   *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))
      ZQLENH(JL,JK) = ( PLM1(JL,JK+1)  *(ZGHM1-PGEOH(JL,JK  )) &
                  & +   PLM1(JL,JK)    *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))
      ZQIENH(JL,JK) = ( PIM1(JL,JK+1)  *(ZGHM1-PGEOH(JL,JK  )) &
                  & +   PIM1(JL,JK)    *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))

      ZQVENH(JL,JK) = ZQTENH(JL,JK) - ZQLENH(JL,JK) - ZQIENH(JL,JK)

      ZSLGENH(JL,JK)= ( ZSLGM1(JL,JK+1)*(ZGHM1-PGEOH(JL,JK  )) &
                  & +   ZSLGM1(JL,JK)  *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))
      ZUENH(JL,JK)  = ( PUM1(JL,JK+1)  *(ZGHM1-PGEOH(JL,JK  )) &
                  & +   PUM1(JL,JK)    *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))
      ZVENH(JL,JK)  = ( PVM1(JL,JK+1)  *(ZGHM1-PGEOH(JL,JK  )) &
                  & +   PVM1(JL,JK)    *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                  &   )                /(ZGHM1-PGEOH(JL,JK+1))


      !  Calculate T at half levels from sl, for later use in density calculations
      ZTENH(JL,JK)  =  ( PTM1(JL,JK+1)  *(ZGHM1-PGEOH(JL,JK  )) &
                   & +   PTM1(JL,JK)    *(PGEOH(JL,JK  )-PGEOH(JL,JK+1)) &
                   &   )                /(ZGHM1-PGEOH(JL,JK+1))

      !  Determine thetav environment
      ZTHTENH(JL,JK) = (ZTENH(JL,JK)/PEXNH(JL,JK))
      ZTHTVENH(JL,JK) = (ZTENH(JL,JK)/PEXNH(JL,JK)) &
           &     * (1._JPRB + RETV * ZQVENH(JL,JK) &
                                ! add qice correction
           &      - ZQLENH(JL,JK) )

      ! initialize updraft thetav for dry and moist updraft with environment values
      PTHTVUH(JL,JK,1)=ZTHTVENH(JL,JK)
      PTHTVUH(JL,JK,2)=ZTHTVENH(JL,JK)
      PTHTVUH(JL,JK,3)=ZTHTVENH(JL,JK)
      PQTUH(JL,JK,1)=ZQTENH(JL,JK)
      PQTUH(JL,JK,2)=ZQTENH(JL,JK)
      PQTUH(JL,JK,3)=ZQTENH(JL,JK)
      PUUH(JL,JK,1)=ZUENH(JL,JK)
      PUUH(JL,JK,2)=ZUENH(JL,JK)
      PUUH(JL,JK,3)=ZUENH(JL,JK)
      PVUH(JL,JK,1)=ZVENH(JL,JK)
      PVUH(JL,JK,2)=ZVENH(JL,JK)
      PVUH(JL,JK,3)=ZVENH(JL,JK)
      PSLGUH(JL,JK,1)=ZSLGENH(JL,JK)
      PSLGUH(JL,JK,2)=ZSLGENH(JL,JK)
      PSLGUH(JL,JK,3)=ZSLGENH(JL,JK)


      ZRHOH(JL,JK) = PAPHM1(JL,JK)/(RD*ZTENH(JL,JK))

   ENDDO
ENDDO

!
!  Initialization lowest updraft level with environment like values
!
DO JD=1,KDRAFT
   DO JL=KIDIA,KFDIA
      PUUH(JL,KLEV,JD)  = PUUH(JL,KLEV-1,JD)
      PVUH(JL,KLEV,JD)  =  PVUH(JL,KLEV-1,JD)
      PSLGUH(JL,KLEV,JD)= PSLGUH(JL,KLEV-1,JD)
      PQTUH(JL,KLEV,JD) = PQTUH(JL,KLEV-1,JD)
      PTHTVUH(JL,KLEV,JD) =  PTHTVUH(JL,KLEV-1,JD)
   ENDDO
ENDDO



!     -----------------------------------------------------------------
!*         3.     RELEASE THE FIRST (TEST) UPDRAFT TO GET PBL HEIGHTS


!* set updraft index to 1
JD = 1

DO JL=KIDIA,KFDIA

   ZFRACB(JL,JD) = ZFRACMAX  !CGL replaced ZFRACB(JL,JD) = ZFRACTEST

   !* 3.1    Determine stability of BL using the surface buoyancy flux

   ZKHVFL(JL)  = ( 1.0_JPRB + RETV *  ZQTM1(JL,KLEV) ) * PKHFL(JL) +&
        & ( RETV * ZSLGM1(JL,KLEV) / RCPD )     * PKQFL(JL)

   IF ( ZKHVFL(JL) >= 0.0_JPRB ) THEN

      ! stable BL (no updrafts expected/needed)
      KPBLTYPE(JL)  = 0

   ELSE

      LLDONE(JL,JD) = .FALSE.  !confirm launch

      !* 3.2    Sigma-w-L60 (ignore 1-z/zi term)

      ZUSTAR  (JL)  = MAX( SQRT(PKMFL(JL)), ZREPUST )      ! u* (repust=10e-4

      ZWSIGMA(JL)      = 1.2_JPRB&
           & * ( ZUSTAR(JL)**3&
           & - 1.5_JPRB * RKAP * ZKHVFL(JL) * (PGEOH(JL,KLEV-1)-PGEOH(JL,KLEV))&
           &  / PTM1(JL,KLEV-1)&
           & ) ** ( 1.0_JPRB/3._JPRB )                         ! Kolmogorov 1/3-power



      !* 3.3    Initialize updraft

      !get the constant associated with the top ZFRACTEST % of the PDF
      ZFACEXC(JL,1) = ZFACTESTEXC
      !calculate the initial excess values
      ZWU2H(JL,KLEV-1,JD) = ( ZFACEXC(JL,1) * ZWSIGMA(JL) )**2
      ZTEXC               = - ZFACEXC(JL,1) * PKHFL(JL) / ZWSIGMA(JL)
      ZQEXC               = - ZFACEXC(JL,1) * PKQFL(JL) / ZWSIGMA(JL)
      ZTEXC            = MAX(ZTEXC, 0.0_JPRB)
      ZQEXC            = MAX(ZQEXC, 0.0_JPRB)
      PQTUH(JL,KLEV-1,JD) = ZQTENH(JL,KLEV-1) + ZQEXC
      ZQCUH(JL,KLEV-1,JD) = ZQLENH(JL,KLEV-1) + ZQIENH(JL,KLEV-1)
      ZQUH (JL,KLEV-1,JD) = PQTUH(JL,KLEV-1,JD)  - ZQCUH(JL,KLEV-1,JD)
      PSLGUH(JL,KLEV-1,JD)= ZSLGENH(JL,KLEV-1) + RCPD * ZTEXC
      ZTUH (JL,KLEV-1,JD) = ( PSLGUH (JL,KLEV-1,JD) - PGEOH(JL,KLEV-1)&
           & + RLVTT*ZQLENH(JL,KLEV-1) + RLSTT*ZQIENH(JL,KLEV-1)&
           & ) / RCPD
      PUUH(JL,KLEV-1,JD)= ZUENH(JL,KLEV-1)
      PVUH(JL,KLEV-1,JD)= ZVENH(JL,KLEV-1)

   ENDIF
ENDDO !JL


!* 3.4   Release the test updraft #1
!*          - Mainly used to get a first guess of the heights of cloud base & inversion,
!*            and to determine PBL type accordingly.

ZTAUEPS(:) = 400._JPRB

!
CALL VDFPARCELHL(YDEPHLI,YDPARAR,KIDIA,KFDIA,KLON,KLEV,KDRAFT,PGEOH,PGEOM1,PAPHM1,PUM1,PVM1,ZQTM1,ZSLGM1,ZTVEN,PUUH,PVUH,&
 & PSLGUH,PQTUH,ZWU2H,ZQCUH,ZBUOF,ZQUH,ZTUH,ZEPS,PZPLCL,KPLCL,PZPTOP,KPTOP,KPLZB,JD,ZUPGENL,ZUPGENN,ZTAUEPS,ZW2THRESH,LLDONE,KPBLTYPE)



!     -----------------------------------------------------------------
!*         4.     CLASSIFICATION OF THE CONVECTIVE PBL
!                 ------------------------------------


!* 4.1    Classify the convective PBL
!*


DO JL=KIDIA,KFDIA
   IF ( KPBLTYPE(JL)/=0 ) THEN

      !CGL loose criterium by 100 m
      IF ( PZPLCL(JL,1) > (PZPTOP(JL,1)+100._JPRB) .OR. KPLCL(JL,1) == 0 ) THEN

         !dry convective PBL
         KPBLTYPE(JL)  = 1                   !dry convective PBL
         ZDZCLOUD(JL)  = 0.0_JPRB            !cloud thickness

      ELSE

         !moist convective PBL
         ZDZCLOUD(JL)  = PZPTOP(JL,1) - PZPLCL(JL,1) !cloud thickness
         IF (ZDZCLOUD(JL)>ZFRMIN(JL,2)) THEN

            !deep convection
            KPBLTYPE(JL) = 4
         ELSE

!wc no special stratocumulus regime anymore
!           KPBLTYPE(JL) = 2   !set the type to stratocumulus for the moment
            KPBLTYPE(JL) = 3   !set the type to stratocumulus for the moment

         ENDIF

      ENDIF

   ENDIF !KPBLTYPE /=0
ENDDO !JL



!wc no special stratocumulus regime anymore
!* 4.2    Check the stratocumulus/shallow cumulus criterion (trigger function)
!*        If shallow cumulus is diagnosed, KPBLTYPE will be set to 3
!*
!CALL VDFSTCUCRITHL ( KIDIA , KFDIA  , KLON  , KLEV , KDRAFT ,&
!     &    PTM1  , ZSLGM1 , ZQTM1 , PAPM1 ,&
!     &    ZSTABTHRESH, ZCLDDEPTH, ZBIRTHRESH, ZDZCLOUD,&
!     &    KPTOP , KPBLTYPE, LDNODECP)


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

!*         5.     CLOSURE FOR ORGANIZED UPDRAFTS (JD=2,3)
!                 ---------------------------------------


!* 5.1    Determine some mixed layer scalings
!*

DO JL=KIDIA,KFDIA

   IF ( KPBLTYPE(JL)/=0 ) THEN    !don't do this for stable PBL

      SELECT CASE (KPBLTYPE(JL))

      CASE(1)
         !Dry convective PBL - Inversion height
         ZZI(JL)   = PZPTOP(JL,1)

      CASE(2)
         !Stratocumulus - Inversion height
         !CAUTION: During decoupling in the intermediate regime (e.g. ASTEX/ATEX) the
         !   relevant ML scaling height changes from PBL inversion to level of minimum
         !   buoyancy flux. In the current setup this is not modelled yet!
         ZZI(JL)   = PZPTOP(JL,1)

      CASE(3)
         !Shallow cumulus - Level of minimum buoyancy flux
         !Assume that the moist updraft LCL is very close to this level
         ZZI(JL)   = PZPLCL(JL,1)

      CASE(4)
         !Deep cumulus - Only do a dry parcel up to cloud base
         ZZI(JL)   = PZPLCL(JL,1)

      END SELECT

      !--- Mixed layer convective velocity scale ---
      ZWSTAR(JL) = ( -ZKHVFL(JL) * RG * ZZI(JL) / ZTHVEN(JL,KLEV)  ) ** (1._JPRB/3._JPRB)
      ! CGL for the moment revert back to old constant time scale
      ZTAUEPS(JL) = 400._JPRB


   ENDIF

ENDDO


!* 5.3    Closure of updraft area fractions (JD=2,3)
!*

DO JL=KIDIA,KFDIA

   IF ( KPBLTYPE(JL)/=0 ) THEN    !don't do this for stable PBL

      SELECT CASE (KPBLTYPE(JL))

        CASE(1)
          !Dry convective PBL
          ZFRACB(JL,3) = 0._JPRB
          ZFRACB(JL,2) = ZFRACMAX - ZFRACB(JL,3)
          !          ZFRACB(JL,2) = ZFRACB(JL,2)*(1.-EXP(-ZZI(JL)/400._JPRB))
        CASE(2)
          !Stratocumulus
          ZFRACB(JL,3) = 0.1_JPRB
          ZFRACB(JL,2) = ZFRACMAX - ZFRACB(JL,3)
        CASE(3)
          !Shallow cumulus
          ZFRACB(JL,3)  = 0.03_JPRB
          ZFRACB(JL,2) = ZFRACMAX - ZFRACB(JL,3)
          !          ZFRACB(JL,2) = ZFRACB(JL,2)*(1.-EXP(-ZZI(JL)/400._JPRB) )

        CASE(4)
          !Deep cumulus
          ZFRACB(JL,3) = 0._JPRB
          ZFRACB(JL,2) = ZFRACMAX - ZFRACB(JL,3)

      END SELECT !KPBLTYPE


   ENDIF !KPBLTYPE /=0

ENDDO !JL



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

!*         6.     CALCULATE VERTICAL PROFILES OF ALL UPDRAFTS (JD=2,3)
!                 ----------------------------------------------------


!*       6.1    Calculate the scaling factors of the updraft excess with the surface joint PDFs
!*
DO JD = 2,KDRAFT
   DO JL=KIDIA,KFDIA

      IF ( KPBLTYPE(JL)/=0 .AND. ZFRACB(JL,JD)>0._JPRB ) THEN

         !-- Get the PDF scaling factor --
         SELECT CASE (JD)

         CASE(2)
            !lower part of top ZFRACMAX %
            ZDUMFRAC = ZFRACMAX - ZFRACB(JL,2)
            CALL VDFPDFTABLEHL(ZDUMFRAC , ZFACEXC(JL,2), ZDUMR, ZDUMR, 0)
            ZFACEXC(JL,2) = ( ZFRACMAX * ZFACMAXEXC - ZDUMFRAC * ZFACEXC(JL,2) ) / ZFRACB(JL,2)
         CASE(3)
            !upper part of top ZFRACMAX %
            ZDUMFRAC = ZFRACB(JL,JD)
            CALL VDFPDFTABLEHL(ZDUMFRAC , ZFACEXC(JL,3), ZDUMR, ZDUMR, 0)

         END SELECT

      ENDIF !KPBLTYPE & ZFRACB

   ENDDO !JL
ENDDO !JD


!*       6.2    Vertical integration of dry & moist updraft budgets (JD=2,3)
!*
DO JD = 2,KDRAFT

   !-- Initialize updraft --
   DO JL=KIDIA,KFDIA

      IF ( KPBLTYPE(JL)/=0 .AND. ZFRACB(JL,JD)>0._JPRB ) THEN

         LLDONE(JL,JD) = .FALSE. !confirm launch

         ZTEXC            = - ZFACEXC(JL,JD) * PKHFL(JL) / ZWSIGMA(JL)
         ZQEXC            = - ZFACEXC(JL,JD) * PKQFL(JL) / ZWSIGMA(JL)

         ZWU2H(JL,KLEV-1,JD) = (ZWSIGMA(JL))**2

         ZTEXC            = MAX(ZTEXC, 0.0_JPRB)
         ZQEXC            = MAX(ZQEXC, 0.0_JPRB)

         ! cgl thv excess surface ; used to correct buoyancy flux
         ZTVEXCSURF(JL,JD) = ZTEXC + RETV*(ZQEXC*PTM1(JL,KLEV-1) + ZQTENH(JL,KLEV-1)*ZTEXC)

         PQTUH(JL,KLEV-1,JD) = ZQTENH(JL,KLEV-1) + ZQEXC
         ZQCUH(JL,KLEV-1,JD) = ZQLENH(JL,KLEV-1) + ZQIENH(JL,KLEV-1)
         ZQUH (JL,KLEV-1,JD) = PQTUH(JL,KLEV-1,JD)  - ZQCUH(JL,KLEV-1,JD)
         PSLGUH(JL,KLEV-1,JD)= ZSLGENH(JL,KLEV-1) + RCPD * ZTEXC
         ZTUH (JL,KLEV-1,JD) = ( PSLGUH (JL,KLEV-1,JD) - PGEOH(JL,KLEV-1) &
              & + RLVTT*ZQLENH(JL,KLEV-1) + RLSTT*ZQIENH(JL,KLEV-1) &
              & ) / RCPD
         PUUH(JL,KLEV-1,JD)= ZUENH(JL,KLEV-1)
         PVUH(JL,KLEV-1,JD)= ZVENH(JL,KLEV-1)

      ENDIF !KPBLTYPE & ZFRACB

   ENDDO !JL


   !-- Release the updraft --

   CALL VDFPARCELHL(YDEPHLI,YDPARAR,KIDIA,KFDIA,KLON,KLEV,KDRAFT,PGEOH,PGEOM1,PAPHM1,PUM1,PVM1,ZQTM1,ZSLGM1,ZTVEN,PUUH, &
     & PVUH,PSLGUH,PQTUH,ZWU2H,ZQCUH,ZBUOF,ZQUH,ZTUH,ZEPS,PZPLCL,KPLCL,PZPTOP,KPTOP,KPLZB,JD,ZUPGENL,ZUPGENN,ZTAUEPS,ZW2THRESH, &
     & LLDONE,KPBLTYPE)


ENDDO !JD


!*        6.3. In case no lcl is found in final updraft calculation, do some resque
!*
!*
! CGL      made an adjustment in vdfparcel to initialize pzplcl = -100

DO JL=KIDIA,KFDIA

   IF ( KPBLTYPE(JL)==2 .OR. KPBLTYPE(JL)==3) THEN

      IF (  PZPLCL(JL,3) < 0._JPRB .OR. KPLCL(JL,3)<KPTOP(JL,3) &
      &.OR. KPLCL(JL,3)==0  .OR. KPTOP(JL,3)==0 ) THEN
         KPBLTYPE(JL) = 1
         ZFRACB(JL,2) = ZFRACMAX
         ZFRACB(JL,3) = 0._JPRB
         KPLCL(JL,3) = 0
      ENDIF

   ENDIF

ENDDO !JL

!*        6.5  Updraft precipitation fluxes (rain and snow)
!*
DO JD = 3,KDRAFT  !moist updrafts only

   DO JK=2,KLEV
      DO JL=KIDIA,KFDIA

         ZDZRHO = ZRG * ( PAPHM1(JL,JK)-PAPHM1(JL,JK-1) )

         !-- Add precip generation to flux [kg /m2 /s: tendency * layer depth * air density] --
         ZUPFLXL(JL,JK,JD) = ZUPFLXL(JL,JK-1,JD) + ZUPGENL(JL,JK,JD) * ZDZRHO
         ZUPFLXN(JL,JK,JD) = ZUPFLXN(JL,JK-1,JD) + ZUPGENN(JL,JK,JD) * ZDZRHO

         !-- Do some melting at freezing level (snow->rain) --
         IF (ZUPFLXN(JL,JK,JD)>0._JPRB .AND. PTM1(JL,JK) > RTT) THEN
!wc
!   No melting and evaporation in the convection scheme in case LTOTPREC=TRUE
!   because this will be done inside microphysics
          IF (LTOTPREC) THEN
            ZUPMELT = 0._JPRB
          ELSE
            ZUPMELT = (1.0_JPRB+0.5_JPRB*(PTM1(JL,JK)-RTT)) * &
                 & (PTM1(JL,JK)-RTT) * RCPD/(RLMLT*RTAUMEL) * ZDZRHO
            ZUPMELT = MIN(ZUPFLXN(JL,JK,JD),ZUPMELT)
          ENDIF
            ZUPFLXL(JL,JK,JD) = ZUPFLXL(JL,JK,JD) + ZUPMELT
            ZUPFLXN(JL,JK,JD) = ZUPFLXN(JL,JK,JD) - ZUPMELT
         ENDIF

         ZPFLXTOT = ZUPFLXL(JL,JK,JD) + ZUPFLXN(JL,JK,JD)

         IF (ZPFLXTOT>0._JPRB) THEN

            !-- Saturation deficit of mean state T -
            ZESW=ESATW(PTM1(JL,JK))
            ZESI=ESATI(PTM1(JL,JK))
            ZFAC = ZUPFLXL(JL,JK,JD) / ZPFLXTOT
            ZES = ZESW*ZFAC + ZESI*(1._JPRB-ZFAC) ! Weigting according to precititation type
            ZQSAT = 0.62198_JPRB*ZES/(MAX(ZES,PAPM1(JL,JK))-0.37802_JPRB*ZES)
          ! Above boiling point for PAPHM1(JL,JK) < ZES --> no condensation. May happen in stratosphere, ZQSATU becomes 1.
            ZSATDEF=ZQSAT-PQM1(JL,JK) ! Also allow deposition = negative values

            !-- Precip evaporation tendency [kg/kg /s] (Kessler 1969, Tiedtke 1993) --
!wc
!   No melting and evaporation in the convection scheme in case LTOTPREC=TRUE
!   because this will be done inside microphysics
            IF (LTOTPREC) THEN
               ZPEVAPUP = 0._JPRB
            ELSE
               ZPEVAPUP = 0.001_JPRB * ZSATDEF * ( &             !cy32r1
                    & ( ZPFLXTOT / 0.00509_JPRB ) * &
                    & ( PAPM1(JL,JK)/PAPHM1(JL,KLEV) )**0.5_JPRB &
                    & )**0.5777_JPRB

            ENDIF
            !-- Back-partition evaporation and substract from fluxes --
            ZUPFLXL(JL,JK,JD) = ZUPFLXL(JL,JK,JD) - ZPEVAPUP * ZDZRHO * ZFAC
            ZUPFLXN(JL,JK,JD) = ZUPFLXN(JL,JK,JD) - ZPEVAPUP * ZDZRHO * (1._JPRB - ZFAC)
            ZUPFLXL(JL,JK,JD) = MAX(0._JPRB,ZUPFLXL(JL,JK,JD))
            ZUPFLXN(JL,JK,JD) = MAX(0._JPRB,ZUPFLXN(JL,JK,JD))
         ENDIF

      ENDDO
   ENDDO

   !Add contribution to total flux - weight by updraft area fraction
   !(or weighted by the mean cloud fraction in the cloud layer
   ! in case of LTOTPREC = TRUE.)

   IF (LTOTPREC .AND. (.NOT.LTOTPRECL)) THEN

      !compute mean cloud-fraction in the convective cloud layer:

      DO JL=KIDIA,KFDIA
         PCLFR(JL)=0._JPRB
      ENDDO

      DO JL=KIDIA,KFDIA
         ZB1(JL)=0.0_JPRB
         ZCOUNT(JL)=0.0_JPRB
         ITOP=KPTOP(JL,3)
         IBASE=KPLCL(JL,3)
         IF (ITOP > 0 .AND. IBASE > 0) THEN
            DO JK=ITOP,IBASE
               ZB1(JL)=ZB1(JL)+PAM1(JL,JK)
               ZCOUNT(JL)=ZCOUNT(JL)+1._JPRB
            ENDDO
         ENDIF
      ENDDO

      DO JL=KIDIA,KFDIA
         IF (ZB1(JL) > 0.0_JPRB .AND. ZCOUNT(JL) > 0.0_JPRB) THEN
            PCLFR(JL)=ZB1(JL)/ZCOUNT(JL)
         ENDIF
      ENDDO

      DO JK=0,KLEV
         DO JL=KIDIA,KFDIA
            PFPLVL(JL,JK) = PFPLVL(JL,JK) + PCLFR(JL) * ZUPFLXL(JL,JK,JD)