forked from NOAA-EMC/UPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPP_PHYSICS.f
2636 lines (2492 loc) · 96.1 KB
/
UPP_PHYSICS.f
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
!> @file
!>
!> @brief upp_physics is a collection of UPP subroutines for physics variables calculation.
!> @author Jesse Meng @date 2020-05-20
!> calcape() computes CAPE/CINS and other storm related variables.
!>
!> calcape2() computes additional storm related variables.
!>
!> calrh(), calrh_nam(), calrh_gfs(), calrh_gsd() compute RH using various algorithms.
!>
!> The NAM v4.1.18 algorithm (calrh_nam()) is selected as default for
!> NMMB and FV3GFS, FV3GEFS, and FV3R for the UPP 2020 unification.
!>
!> calrh_pw() algorithm use at GSD for RUC and Rapid Refresh.
!>
!> fpvsnew() computes saturation vapor pressure.
!>
!> tvirtual() computes virtual temperature.
!>
!> ### Program history log:
!> Date | Programmer | Comments
!> -----|------------|---------
!> 2020-05-20 | Jesse Meng | Initial
!>
!> @author Jesse Meng @date 2020-05-20
module upp_physics
implicit none
private
public :: CALCAPE, CALCAPE2
public :: CALDIV
public :: CALGRADPS
public :: CALRH
public :: CALRH_GFS, CALRH_GSD, CALRH_NAM
public :: CALRH_PW
public :: CALVOR
public :: FPVSNEW
public :: TVIRTUAL
contains
!
!-------------------------------------------------------------------------------------
!
SUBROUTINE CALRH(P1,T1,Q1,RH)
use ctlblk_mod, only: ista, iend, jsta, jend, MODELNAME
implicit none
REAL,dimension(ista:iend,jsta:jend),intent(in) :: P1,T1
REAL,dimension(ista:iend,jsta:jend),intent(inout) :: Q1
REAL,dimension(ista:iend,jsta:jend),intent(out) :: RH
IF(MODELNAME == 'RAPR')THEN
CALL CALRH_GSD(P1,T1,Q1,RH)
ELSE
CALL CALRH_NAM(P1,T1,Q1,RH)
END IF
END SUBROUTINE CALRH
!
!-------------------------------------------------------------------------------------
!
!> calrh_nam() computes relative humidity.
!>
!> This routine computes relative humidity given pressure,
!> temperature, specific humidity. an upper and lower bound
!> of 100 and 1 percent relative humidity is enforced. When
!> these bounds are applied the passed specific humidity
!> array is adjusted as necessary to produce the set relative
!> humidity.
!>
!> @param[in] P1 Pressure (pa)
!> @param[in] T1 Temperature (K)
!> @param[in] Q1 Specific humidity (kg/kg)
!> @param[out] RH Relative humidity (decimal form)
!> @param[out] Q1 Specific humidity (kg/kg)
!>
!> ### Program History Log
!> Date | Programmer | Comments
!> -----|------------|---------
!> ????-??-?? | DENNIS DEAVEN | Initial
!> 1992-12-22 | Russ Treadon | Modified as described above
!> 1998-06-08 | T Black | Conversion from 1-D to 2-D
!> 1998-08-18 | Mike Baldwin | Modify to compute RH over ice as in model
!> 1998-12-16 | Geoff Manikin | undo RH computation over ice
!> 2000-01-04 | Jim Tuccillo | MPI Version
!> 2002-06-11 | Mike Baldwin | WRF Version
!> 2006-03-19 | Wen Meng | Modify top pressure to 1 pa
!>
!> @author Russ Treadon W/NP2 @date 1992-12-22
SUBROUTINE CALRH_NAM(P1,T1,Q1,RH)
use params_mod, only: PQ0, a2, a3, a4, rhmin
use ctlblk_mod, only: ista, iend, jsta, jend, spval
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
implicit none
!
! SET PARAMETER.
!
! DECLARE VARIABLES.
!
REAL,dimension(ista:iend,jsta:jend),intent(in) :: P1,T1
REAL,dimension(ista:iend,jsta:jend),intent(inout) :: Q1
REAL,dimension(ista:iend,jsta:jend),intent(out) :: RH
REAL QC
integer I,J
!***************************************************************
!
! START CALRH.
!
DO J=JSTA,JEND
DO I=ISTA,IEND
IF (T1(I,J) < spval) THEN
IF (ABS(P1(I,J)) >= 1) THEN
QC = PQ0/P1(I,J)*EXP(A2*(T1(I,J)-A3)/(T1(I,J)-A4))
!
RH(I,J) = Q1(I,J)/QC
!
! BOUNDS CHECK
!
IF (RH(I,J) > 1.0) THEN
RH(I,J) = 1.0
Q1(I,J) = RH(I,J)*QC
ENDIF
IF (RH(I,J) < RHmin) THEN !use smaller RH limit for stratosphere
RH(I,J) = RHmin
Q1(I,J) = RH(I,J)*QC
ENDIF
!
ENDIF
ELSE
RH(I,J) = spval
ENDIF
ENDDO
ENDDO
!
!
! END SUBROUTINE CALRH
END SUBROUTINE CALRH_NAM
!
!-------------------------------------------------------------------------------------
!
!> calrh_gfs() computes relative humidity.
!>
!> This routine computes relative humidity given pressure,
!> temperature, specific humidity. an upper and lower bound
!> of 100 and 1 percent relative humidity is enforced. When
!> these bounds are applied the passed specific humidity
!> array is adjusted as necessary to produce the set relative
!> humidity.
!>
!> @param[in] P1 Pressure (pa)
!> @param[in] T1 Temperature (K)
!> @param[in] Q1 Specific humidity (kg/kg)
!> @param[out] RH Relative humidity (decimal form)
!> @param[out] Q1 Specific humidity (kg/kg)
!>
!> ### Program History Log
!> Date | Programmer | Comments
!> -----|------------|---------
!> ????-??-?? | DENNIS DEAVEN | Initial
!> 1992-12-22 | Russ Treadon | Modified as described above
!> 1998-06-08 | T Black | Conversion from 1-D to 2-D
!> 1998-08-18 | Mike Baldwin | Modify to compute RH over ice as in model
!> 1998-12-16 | Geoff Manikin | undo RH computation over ice
!> 2000-01-04 | Jim Tuccillo | MPI Version
!> 2002-06-11 | Mike Baldwin | WRF Version
!> 2013-08-13 | S. Moorthi | Threading
!> 2006-03-19 | Wen Meng | Modify top pressure to 1 pa
!>
!> @author Russ Treadon W/NP2 @date 1992-12-22
SUBROUTINE CALRH_GFS(P1,T1,Q1,RH)
use params_mod, only: rhmin
use ctlblk_mod, only: ista, iend, jsta, jend, spval
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
implicit none
!
real,parameter:: con_rd =2.8705e+2 ! gas constant air (J/kg/K)
real,parameter:: con_rv =4.6150e+2 ! gas constant H2O
real,parameter:: con_eps =con_rd/con_rv
real,parameter:: con_epsm1 =con_rd/con_rv-1
! real,external::FPVSNEW
! INTERFACE
! ELEMENTAL FUNCTION FPVSNEW (t)
! REAL FPVSNEW
! REAL, INTENT(IN) :: t
! END FUNCTION FPVSNEW
! END INTERFACE
!
REAL,dimension(ista:iend,jsta:jend),intent(in) :: P1,T1
REAL,dimension(ista:iend,jsta:jend),intent(inout):: Q1,RH
REAL ES,QC
integer :: I,J
!***************************************************************
!
! START CALRH.
!
!$omp parallel do private(i,j,es,qc)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF (T1(I,J) < spval .AND. P1(I,J) < spval.AND.Q1(I,J)/=spval) THEN
! IF (ABS(P1(I,J)) > 1.0) THEN
! IF (P1(I,J) > 1.0) THEN
IF (P1(I,J) >= 1.0) THEN
ES = MIN(FPVSNEW(T1(I,J)),P1(I,J))
QC = CON_EPS*ES/(P1(I,J)+CON_EPSM1*ES)
! QC=PQ0/P1(I,J)*EXP(A2*(T1(I,J)-A3)/(T1(I,J)-A4))
RH(I,J) = min(1.0,max(Q1(I,J)/QC,rhmin))
q1(i,j) = rh(i,j)*qc
! BOUNDS CHECK
!
! IF (RH(I,J) > 1.0) THEN
! RH(I,J) = 1.0
! Q1(I,J) = RH(I,J)*QC
! ELSEIF (RH(I,J) < RHmin) THEN !use smaller RH limit for stratosphere
! RH(I,J) = RHmin
! Q1(I,J) = RH(I,J)*QC
! ENDIF
ENDIF
ELSE
RH(I,J) = spval
ENDIF
ENDDO
ENDDO
END SUBROUTINE CALRH_GFS
!
!-------------------------------------------------------------------------------------
!
SUBROUTINE CALRH_GSD(P1,T1,Q1,RHB)
!
! Algorithm use at GSD for RUC and Rapid Refresh
!------------------------------------------------------------------
!
use ctlblk_mod, only: ista, iend, jsta, jend, spval
implicit none
integer :: j, i
real :: tx, pol, esx, es, e
real, dimension(ista:iend,jsta:jend) :: P1, T1, Q1, RHB
DO J=JSTA,JEND
DO I=ISTA,IEND
IF (T1(I,J) < spval .AND. P1(I,J) < spval .AND. Q1(I,J) < spval) THEN
! - compute relative humidity
Tx=T1(I,J)-273.15
POL = 0.99999683 + TX*(-0.90826951E-02 + &
TX*(0.78736169E-04 + TX*(-0.61117958E-06 + &
TX*(0.43884187E-08 + TX*(-0.29883885E-10 + &
TX*(0.21874425E-12 + TX*(-0.17892321E-14 + &
TX*(0.11112018E-16 + TX*(-0.30994571E-19)))))))))
esx = 6.1078/POL**8
ES = esx
E = P1(I,J)/100.*Q1(I,J)/(0.62197+Q1(I,J)*0.37803)
RHB(I,J) = MIN(1.,E/ES)
ELSE
RHB(I,J) = spval
ENDIF
ENDDO
ENDDO
END SUBROUTINE CALRH_GSD
!
!-------------------------------------------------------------------------------------
!
SUBROUTINE CALRH_PW(RHPW)
!
! Algorithm use at GSD for RUC and Rapid Refresh
!------------------------------------------------------------------
!
use vrbls3d, only: q, pmid, t
use params_mod, only: g
use ctlblk_mod, only: lm, ista, iend, jsta, jend, spval
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
implicit none
real,PARAMETER :: svp1=6.1153,svp2=17.67,svp3=29.65
REAL, dimension(ista:iend,jsta:jend):: PW, PW_SAT, RHPW
REAL deltp,sh,qv,temp,es,qs,qv_sat
integer i,j,l,k,ka,kb
pw = 0.
pw_sat = 0.
rhpw = 0.
DO L=1,LM
k=lm-l+1
DO J=JSTA,JEND
DO I=ISTA,IEND
! -- use specific humidity for PW calculation
if(t(i,j,k)<spval.and.q(i,j,k)<spval) then
sh = q(i,j,k)
qv = sh/(1.-sh)
KA = MAX(1,K-1)
KB = MIN(LM,K+1)
! assumes that P is in mb at this point - be careful!
DELTP = 0.5*(PMID(I,J,KB)-PMID(I,J,KA))
PW(I,J) = PW(I,J) + sh *DELTP/G
!Csgb -- Add more for RH w.r.t. PW-sat
temp = T(I,J,K)
! --- use saturation mixing ratio w.r.t. water here
! for this check.
es = svp1*exp(SVP2*(Temp-273.15)/(Temp-SVP3))
! -- get saturation specific humidity (w.r.t. total air)
qs = 0.62198*es/(pmid(i,j,k)*1.e-2-0.37802*es)
! -- get saturation mixing ratio (w.r.t. dry air)
qv_sat = qs/(1.-qs)
pw_sat(i,j) = pw_sat(i,j) + max(sh,Qs)*DELTP/G
if (i==120 .and. j==120 ) &
write (6,*)'pw-sat', temp, sh, qs, pmid(i,j,kb) &
,pmid(i,j,ka),pw(i,j),pw_sat(i,j)
!sgb - This IS RH w.r.t. PW-sat.
RHPW (i,j) = min(1.,PW(i,j) / pw_sat(i,j)) * 100.
else
RHPW (i,j) = spval
endif
ENDDO
ENDDO
ENDDO
END SUBROUTINE CALRH_PW
!
!-------------------------------------------------------------------------------------
!
elemental function fpvsnew(t)
!> fpvsnew() computes saturation vapor pressure.
!>
!> Compute saturation vapor pressure from the temperature.
!> A linear interpolation is done between values in a lookup table
!> computed in gpvs. See documentation for fpvsx for details.
!> Input values outside table range are reset to table extrema.
!> The interpolation accuracy is almost 6 decimal places.
!> On the Cray, fpvs is about 4 times faster than exact calculation.
!> This function should be expanded inline in the calling routine.
!>
!> @param[in] t Real(krealfp) Temperature in Kelvin.
!> @param[out] fpvsnew Real(krealfp) Saturation vapor pressure in Pascals.
!>
!> ### Program history log:
!> Date | Programmer | Comments
!> -----|------------|---------
!> 1991-05-07 | Iredell | Initial. Made into inlinable function
!> 1994-12-30 | Iredell | Expand table
!> 1999-03-01 | Iredell | F90 module
!> 2001-02-26 | Iredell | Ice phase
!>
!> @author N Phillips w/NMC2X2 @date 1982-12-30
implicit none
integer,parameter:: nxpvs=7501
real,parameter:: con_ttp =2.7316e+2 ! temp at H2O 3pt
real,parameter:: con_psat =6.1078e+2 ! pres at H2O 3pt
real,parameter:: con_cvap =1.8460e+3 ! spec heat H2O gas (J/kg/K)
real,parameter:: con_cliq =4.1855e+3 ! spec heat H2O liq
real,parameter:: con_hvap =2.5000e+6 ! lat heat H2O cond
real,parameter:: con_rv =4.6150e+2 ! gas constant H2O
real,parameter:: con_csol =2.1060e+3 ! spec heat H2O ice
real,parameter:: con_hfus =3.3358e+5 ! lat heat H2O fusion
real,parameter:: tliq=con_ttp
real,parameter:: tice=con_ttp-20.0
real,parameter:: dldtl=con_cvap-con_cliq
real,parameter:: heatl=con_hvap
real,parameter:: xponal=-dldtl/con_rv
real,parameter:: xponbl=-dldtl/con_rv+heatl/(con_rv*con_ttp)
real,parameter:: dldti=con_cvap-con_csol
real,parameter:: heati=con_hvap+con_hfus
real,parameter:: xponai=-dldti/con_rv
real,parameter:: xponbi=-dldti/con_rv+heati/(con_rv*con_ttp)
real tr,w,pvl,pvi
real fpvsnew
real,intent(in):: t
integer jx
real xj,x,tbpvs(nxpvs),xp1
real xmin,xmax,xinc,c2xpvs,c1xpvs
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
xmin=180.0
xmax=330.0
xinc=(xmax-xmin)/(nxpvs-1)
! c1xpvs=1.-xmin/xinc
c2xpvs=1./xinc
c1xpvs=1.-xmin*c2xpvs
! xj=min(max(c1xpvs+c2xpvs*t,1.0),real(nxpvs,krealfp))
xj=min(max(c1xpvs+c2xpvs*t,1.0),float(nxpvs))
jx=min(xj,float(nxpvs)-1.0)
x=xmin+(jx-1)*xinc
tr=con_ttp/x
if(x>=tliq) then
tbpvs(jx)=con_psat*(tr**xponal)*exp(xponbl*(1.-tr))
elseif(x<tice) then
tbpvs(jx)=con_psat*(tr**xponai)*exp(xponbi*(1.-tr))
else
w=(t-tice)/(tliq-tice)
pvl=con_psat*(tr**xponal)*exp(xponbl*(1.-tr))
pvi=con_psat*(tr**xponai)*exp(xponbi*(1.-tr))
tbpvs(jx)=w*pvl+(1.-w)*pvi
endif
xp1=xmin+(jx-1+1)*xinc
tr=con_ttp/xp1
if(xp1>=tliq) then
tbpvs(jx+1)=con_psat*(tr**xponal)*exp(xponbl*(1.-tr))
elseif(xp1<tice) then
tbpvs(jx+1)=con_psat*(tr**xponai)*exp(xponbi*(1.-tr))
else
w=(t-tice)/(tliq-tice)
pvl=con_psat*(tr**xponal)*exp(xponbl*(1.-tr))
pvi=con_psat*(tr**xponai)*exp(xponbi*(1.-tr))
tbpvs(jx+1)=w*pvl+(1.-w)*pvi
endif
fpvsnew=tbpvs(jx)+(xj-jx)*(tbpvs(jx+1)-tbpvs(jx))
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
end function fpvsnew
!
!-------------------------------------------------------------------------------------
!> calcape() computes CAPE and CINS.
!>
!> This routine computes CAPE and CINS given temperature,
!> pressure, and specific humidty. In "storm and cloud
!> dynamics" (1989, academic press) cotton and anthes define
!> CAPE (equation 9.16, p501) as
!>
!> @code
!> EL
!> CAPE = SUM G * LN(THETAP/THETAA) DZ
!> LCL
!>
!> Where,
!> EL = Equilibrium level,
!> LCL = Lifting condenstation level,
!> G = Gravitational acceleration,
!> THETAP = Lifted parcel potential temperature,
!> THETAA = Ambient potential temperature.
!> @endcode
!>
!> Note that the integrand ln(THETAP/THETAA) approximately
!> equals (THETAP-THETAA)/THETAA. This ratio is often used
!> in the definition of CAPE/CINS.
!>
!> Two types of CAPE/CINS can be computed by this routine. The
!> summation process is the same For both cases. What differs
!> is the definition of the parcel to lift. FOR ITYPE=1 the
!> parcel with the warmest THETA-E in A DPBND pascal layer above
!> the model surface is lifted. the arrays P1D, T1D, and Q1D
!> are not used. For itype=2 the arrays P1D, T1D, and Q1D
!> define the parcel to lift in each column. Both types of
!> CAPE/CINS may be computed in a single execution of the post
!> processor.
!>
!> This algorithm proceeds as follows.
!> For each column,
!> (1) Initialize running CAPE and CINS SUM TO 0.0
!> (2) Compute temperature and pressure at the LCL using
!> look up table (PTBL). Use either parcel that gives
!> max THETAE in lowest DPBND above ground (ITYPE=1)
!> or given parcel from t1D,Q1D,...(ITYPE=2).
!> (3) Compute the temp of a parcel lifted from the LCL.
!> We know that the parcel's
!> equivalent potential temperature (THESP) remains
!> constant through this process. we can
!> compute tpar using this knowledge using look
!> up table (subroutine TTBLEX).
!> (4) Find the equilibrium level. This is defined as the
!> highest positively buoyant layer.
!> (If there is no positively buoyant layer, CAPE/CINS
!> will be zero)
!> (5) Compute CAPE/CINS.
!> (A) Compute THETAP. We know TPAR and P.
!> (B) Compute THETAA. We know T and P.
!> (6) Add G*(THETAP-THETAA)*DZ to the running CAPE or CINS sum.
!> (A) If THETAP > THETAA, add to the CAPE sum.
!> (B) If THETAP < THETAA, add to the CINS sum.
!> (7) Are we at equilibrium level?
!> (A) If yes, stop the summation.
!> (b) if no, contiunue the summation.
!> (8) Enforce limits on CAPE and CINS (i.e. no negative CAPE)
!>
!> @param[in] ITYPE INTEGER Flag specifying how parcel to lift is identified. See comments above.
!> @param[in] DPBND Depth over which one searches for most unstable parcel.
!> @param[in] P1D Array of pressure of parcels to lift.
!> @param[in] T1D Array of temperature of parcels to lift.
!> @param[in] Q1D Array of specific humidity of parcels to lift.
!> @param[in] L1D Array of model level of parcels to lift.
!> @param[out] CAPE Convective available potential energy (J/kg).
!> @param[out] CINS Convective inhibition (J/kg).
!> @param[out] PPARC Pressure level of parcel lifted when one searches over a particular depth to compute CAPE/CIN.
!>
!> ### Program history log:
!> Date | Programmer | Comments
!> -----|------------|---------
!> 1993-02-10 | Russ Treadon | Initial
!> 1993-06-19 | Russ Treadon | Generalized routine to allow for type 2 CAPE/CINS calculations
!> 1994-09-23 | Mike Baldwin | Modified to use look up tables instead of complicated equations
!> 1994-10-13 | Mike Baldwin | Modified to continue CAPE/CINS calc up to at highest buoyant layer
!> 1998-06-12 | T Black | Conversion from 1-D TO 2-D
!> 1998-08-18 | T Black | Compute APE internally
!> 2000-01-04 | Jim Tuccillo | MPI Version
!> 2002-01-15 | Mike Baldwin | WRF Version
!> 2003-08-24 | G Manikin | Added level of parcel being lifted as output from the routine and added the depth over which one searches for the most unstable parcel as input
!> 2010-09-09 | G Manikin | Changed computation to use virtual temp added eq lvl hght and thunder parameter
!> 2015-??-?? | S Moorthi | Optimization and threading
!> 2021-07-28 | W Meng | Restrict computation from undefined grids
!> 2021-09-01 | E Colon | Equivalent level height index for RTMA
!>
!> @author Russ Treadon W/NP2 @date 1993-02-10
SUBROUTINE CALCAPE(ITYPE,DPBND,P1D,T1D,Q1D,L1D,CAPE, &
CINS,PPARC,ZEQL,THUND)
use vrbls3d, only: pmid, t, q, zint
use vrbls2d, only: teql,ieql
use masks, only: lmh
use params_mod, only: d00, h1m12, h99999, h10e5, capa, elocp, eps, &
oneps, g
use lookup_mod, only: thl, rdth, jtb, qs0, sqs, rdq, itb, ptbl, &
plq, ttbl, pl, rdp, the0, sthe, rdthe, ttblq, &
itbq, jtbq, rdpq, the0q, stheq, rdtheq
use ctlblk_mod, only: jsta_2l, jend_2u, lm, jsta, jend, im, me, spval, &
ista_2l, iend_2u, ista, iend
!
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
implicit none
!
! INCLUDE/SET PARAMETERS. CONSTANTS ARE FROM BOLTON (MWR, 1980).
real,PARAMETER :: ISMTHP=2,ISMTHT=2,ISMTHQ=2
!
! DECLARE VARIABLES.
!
integer,intent(in) :: ITYPE
real,intent(in) :: DPBND
integer, dimension(ista:iend,Jsta:jend),intent(in) :: L1D
real, dimension(ista:iend,Jsta:jend),intent(in) :: P1D,T1D
real, dimension(ista:iend,jsta:jend),intent(inout) :: Q1D,CAPE,CINS,PPARC,ZEQL
!
integer, dimension(ista:iend,jsta:jend) :: IPTB, ITHTB, PARCEL, KLRES, KHRES, LCL, IDX
!
real, dimension(ista:iend,jsta:jend) :: THESP, PSP, CAPE20, QQ, PP, THUND
REAL, ALLOCATABLE :: TPAR(:,:,:)
LOGICAL THUNDER(ista:iend,jsta:jend), NEEDTHUN
real PSFCK,PKL,TBTK,QBTK,APEBTK,TTHBTK,TTHK,APESPK,TPSPK, &
BQS00K,SQS00K,BQS10K,SQS10K,BQK,SQK,TQK,PRESK,GDZKL,THETAP, &
THETAA,P00K,P10K,P01K,P11K,TTHESK,ESATP,QSATP,TVP,TV
! real,external :: fpvsnew
integer I,J,L,KNUML,KNUMH,LBEG,LEND,IQ, KB,ITTBK
! integer I,J,L,KNUML,KNUMH,LBEG,LEND,IQ,IT,LMHK, KB,ITTBK
!
!**************************************************************
! START CALCAPE HERE.
!
ALLOCATE(TPAR(ISTA_2L:IEND_2U,JSTA_2L:JEND_2U,LM))
!
! COMPUTE CAPE/CINS
!
! WHICH IS: THE SUM FROM THE LCL TO THE EQ LEVEL OF
! G * (LN(THETAP) - LN(THETAA)) * DZ
!
! (POSITIVE AREA FOR CAPE, NEGATIVE FOR CINS)
!
! WHERE:
! THETAP IS THE PARCEL THETA
! THETAA IS THE AMBIENT THETA
! DZ IS THE THICKNESS OF THE LAYER
!
! USING LCL AS LEVEL DIRECTLY BELOW SATURATION POINT
! AND EQ LEVEL IS THE HIGHEST POSITIVELY BUOYANT LEVEL.
!
! IEQL = EQ LEVEL
! P_thetaemax - real pressure of theta-e max parcel (Pa)
!
! INITIALIZE CAPE AND CINS ARRAYS
!
!$omp parallel do
DO J=JSTA,JEND
DO I=ISTA,IEND
CAPE(I,J) = D00
CAPE20(I,J) = D00
CINS(I,J) = D00
LCL(I,J) = 0
THESP(I,J) = D00
IEQL(I,J) = LM
PARCEL(I,J) = LM
PSP(I,J) = D00
PPARC(I,J) = D00
THUNDER(I,J) = .TRUE.
ENDDO
ENDDO
!
!$omp parallel do
DO L=1,LM
DO J=JSTA,JEND
DO I=ISTA,IEND
TPAR(I,J,L) = D00
ENDDO
ENDDO
ENDDO
!
! TYPE 2 CAPE/CINS:
! NOTE THAT FOR TYPE 1 CAPE/CINS ARRAYS P1D, T1D, Q1D
! ARE DUMMY ARRAYS.
!
IF (ITYPE == 2) THEN
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
Q1D(I,J) = MIN(MAX(H1M12,Q1D(I,J)),H99999)
ENDDO
ENDDO
ENDIF
!-------FOR ITYPE=1--FIND MAXIMUM THETA E LAYER IN LOWEST DPBND ABOVE GROUND-------
!-------FOR ITYPE=2--FIND THETA E LAYER OF GIVEN T1D, Q1D, P1D---------------------
!--------------TRIAL MAXIMUM BUOYANCY LEVEL VARIABLES-------------------
DO KB=1,LM
!hc IF (ITYPE==2.AND.KB>1) cycle
IF (ITYPE == 1 .OR. (ITYPE == 2 .AND. KB == 1)) THEN
!$omp parallel do private(i,j,apebtk,apespk,bqk,bqs00k,bqs10k,iq,ittbk, &
!$omp & p00k,p01k,p10k,p11k,pkl,psfck,qbtk,sqk,sqs00k, &
!$omp & sqs10k,tbtk,tpspk,tqk,tthbtk,tthesk,tthk)
DO J=JSTA,JEND
DO I=ISTA,IEND
PSFCK = PMID(I,J,NINT(LMH(I,J)))
PKL = PMID(I,J,KB)
IF(PSFCK<spval.and.PKL<spval)THEN
!hc IF (ITYPE==1.AND.(PKL<PSFCK-DPBND.OR.PKL>PSFCK)) cycle
IF (ITYPE ==2 .OR. &
(ITYPE == 1 .AND. (PKL >= PSFCK-DPBND .AND. PKL <= PSFCK)))THEN
IF (ITYPE == 1) THEN
TBTK = T(I,J,KB)
QBTK = max(0.0, Q(I,J,KB))
APEBTK = (H10E5/PKL)**CAPA
ELSE
PKL = P1D(I,J)
TBTK = T1D(I,J)
QBTK = max(0.0, Q1D(I,J))
APEBTK = (H10E5/PKL)**CAPA
ENDIF
!----------Breogan Gomez - 2009-02-06
! To prevent QBTK to be less than 0 which leads to a unrealistic value of PRESK
! and a floating invalid.
! if(QBTK < 0) QBTK = 0
!--------------SCALING POTENTIAL TEMPERATURE & TABLE INDEX--------------
TTHBTK = TBTK*APEBTK
TTHK = (TTHBTK-THL)*RDTH
QQ(I,J) = TTHK - AINT(TTHK)
ITTBK = INT(TTHK) + 1
!--------------KEEPING INDICES WITHIN THE TABLE-------------------------
IF(ITTBK < 1) THEN
ITTBK = 1
QQ(I,J) = D00
ENDIF
IF(ITTBK >= JTB) THEN
ITTBK = JTB-1
QQ(I,J) = D00
ENDIF
!--------------BASE AND SCALING FACTOR FOR SPEC. HUMIDITY---------------
BQS00K = QS0(ITTBK)
SQS00K = SQS(ITTBK)
BQS10K = QS0(ITTBK+1)
SQS10K = SQS(ITTBK+1)
!--------------SCALING SPEC. HUMIDITY & TABLE INDEX---------------------
BQK = (BQS10K-BQS00K)*QQ(I,J) + BQS00K
SQK = (SQS10K-SQS00K)*QQ(I,J) + SQS00K
TQK = (QBTK-BQK)/SQK*RDQ
PP(I,J) = TQK-AINT(TQK)
IQ = INT(TQK)+1
!--------------KEEPING INDICES WITHIN THE TABLE-------------------------
IF(IQ < 1) THEN
IQ = 1
PP(I,J) = D00
ENDIF
IF(IQ >= ITB) THEN
IQ = ITB-1
PP(I,J) = D00
ENDIF
!--------------SATURATION PRESSURE AT FOUR SURROUNDING TABLE PTS.-------
P00K = PTBL(IQ ,ITTBK )
P10K = PTBL(IQ+1,ITTBK )
P01K = PTBL(IQ ,ITTBK+1)
P11K = PTBL(IQ+1,ITTBK+1)
!--------------SATURATION POINT VARIABLES AT THE BOTTOM-----------------
TPSPK = P00K + (P10K-P00K)*PP(I,J) + (P01K-P00K)*QQ(I,J) &
+ (P00K-P10K-P01K+P11K)*PP(I,J)*QQ(I,J)
!!from WPP::tgs APESPK=(H10E5/TPSPK)**CAPA
if (TPSPK > 1.0e-3) then
APESPK = (max(0.,H10E5/ TPSPK))**CAPA
else
APESPK = 0.0
endif
TTHESK = TTHBTK * EXP(ELOCP*QBTK*APESPK/TTHBTK)
!--------------CHECK FOR MAXIMUM THETA E--------------------------------
IF(TTHESK > THESP(I,J)) THEN
PSP (I,J) = TPSPK
THESP(I,J) = TTHESK
PARCEL(I,J) = KB
ENDIF
END IF
ENDIF !end PSFCK<spval.and.PKL<spval
ENDDO ! I loop
ENDDO ! J loop
END IF
ENDDO ! KB loop
!----FIND THE PRESSURE OF THE PARCEL THAT WAS LIFTED
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
PPARC(I,J) = PMID(I,J,PARCEL(I,J))
ENDDO
ENDDO
!
!-----CHOOSE LAYER DIRECTLY BELOW PSP AS LCL AND------------------------
!-----ENSURE THAT THE LCL IS ABOVE GROUND.------------------------------
!-------(IN SOME RARE CASES FOR ITYPE=2, IT IS NOT)---------------------
DO L=1,LM
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF (PMID(I,J,L) < PSP(I,J)) LCL(I,J) = L+1
ENDDO
ENDDO
ENDDO
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF (LCL(I,J) > NINT(LMH(I,J))) LCL(I,J) = NINT(LMH(I,J))
IF (ITYPE > 2) THEN
IF (T(I,J,LCL(I,J)) < 263.15) THEN
THUNDER(I,J) = .FALSE.
ENDIF
ENDIF
ENDDO
ENDDO
!-----------------------------------------------------------------------
!---------FIND TEMP OF PARCEL LIFTED ALONG MOIST ADIABAT (TPAR)---------
!-----------------------------------------------------------------------
DO L=LM,1,-1
!--------------SCALING PRESSURE & TT TABLE INDEX------------------------
KNUML = 0
KNUMH = 0
DO J=JSTA,JEND
DO I=ISTA,IEND
KLRES(I,J) = 0
KHRES(I,J) = 0
IF(L <= LCL(I,J)) THEN
IF(PMID(I,J,L) < PLQ)THEN
KNUML = KNUML + 1
KLRES(I,J) = 1
ELSE
KNUMH = KNUMH + 1
KHRES(I,J) = 1
ENDIF
ENDIF
ENDDO
ENDDO
!***
!*** COMPUTE PARCEL TEMPERATURE ALONG MOIST ADIABAT FOR PRESSURE<PLQ
!**
IF(KNUML > 0) THEN
CALL TTBLEX(TPAR(ISTA_2L,JSTA_2L,L),TTBL,ITB,JTB,KLRES &
, PMID(ISTA_2L,JSTA_2L,L),PL,QQ,PP,RDP,THE0,STHE &
, RDTHE,THESP,IPTB,ITHTB)
ENDIF
!***
!*** COMPUTE PARCEL TEMPERATURE ALONG MOIST ADIABAT FOR PRESSURE>PLQ
!**
IF(KNUMH > 0) THEN
CALL TTBLEX(TPAR(ISTA_2L,JSTA_2L,L),TTBLQ,ITBQ,JTBQ,KHRES &
, PMID(ISTA_2L,JSTA_2L,L),PLQ,QQ,PP,RDPQ &
,THE0Q,STHEQ,RDTHEQ,THESP,IPTB,ITHTB)
ENDIF
!------------SEARCH FOR EQ LEVEL----------------------------------------
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF(KHRES(I,J) > 0) THEN
IF(TPAR(I,J,L) > T(I,J,L)) IEQL(I,J) = L
ENDIF
ENDDO
ENDDO
!
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF(KLRES(I,J) > 0) THEN
IF(TPAR(I,J,L) > T(I,J,L) .AND. &
PMID(I,J,L)>100.) IEQL(I,J) = L
ENDIF
ENDDO
ENDDO
!-----------------------------------------------------------------------
ENDDO ! end of do l=lm,1,-1 loop
!------------COMPUTE CAPE AND CINS--------------------------------------
LBEG = 1000
LEND = 0
DO J=JSTA,JEND
DO I=ISTA,IEND
LBEG = MIN(IEQL(I,J),LBEG)
LEND = MAX(LCL(I,J),LEND)
ENDDO
ENDDO
!
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF(T(I,J,IEQL(I,J)) > 255.65) THEN
THUNDER(I,J) = .FALSE.
ENDIF
ENDDO
ENDDO
!
DO L=LBEG,LEND
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
IDX(I,J) = 0
IF(L >= IEQL(I,J).AND.L <= LCL(I,J)) THEN
IDX(I,J) = 1
ENDIF
ENDDO
ENDDO
!
!$omp parallel do private(i,j,gdzkl,presk,thetaa,thetap,esatp,qsatp,tvp,tv)
DO J=JSTA,JEND
DO I=ISTA,IEND
IF(IDX(I,J) > 0) THEN
PRESK = PMID(I,J,L)
GDZKL = (ZINT(I,J,L)-ZINT(I,J,L+1)) * G
ESATP = min(FPVSNEW(TPAR(I,J,L)),PRESK)
QSATP = EPS*ESATP/(PRESK-ESATP*ONEPS)
! TVP = TPAR(I,J,L)*(1+0.608*QSATP)
TVP = TVIRTUAL(TPAR(I,J,L),QSATP)
THETAP = TVP*(H10E5/PRESK)**CAPA
! TV = T(I,J,L)*(1+0.608*Q(I,J,L))
TV = TVIRTUAL(T(I,J,L),Q(I,J,L))
THETAA = TV*(H10E5/PRESK)**CAPA
IF(THETAP < THETAA) THEN
CINS(I,J) = CINS(I,J) + (LOG(THETAP)-LOG(THETAA))*GDZKL
ELSEIF(THETAP > THETAA) THEN
CAPE(I,J) = CAPE(I,J) + (LOG(THETAP)-LOG(THETAA))*GDZKL
IF (THUNDER(I,J) .AND. T(I,J,L) < 273.15 &
.AND. T(I,J,L) > 253.15) THEN
CAPE20(I,J) = CAPE20(I,J) + (LOG(THETAP)-LOG(THETAA))*GDZKL
ENDIF
ENDIF
ENDIF
ENDDO
ENDDO
ENDDO
!
! ENFORCE LOWER LIMIT OF 0.0 ON CAPE AND UPPER
! LIMIT OF 0.0 ON CINS.
!
!$omp parallel do private(i,j)
DO J=JSTA,JEND
DO I=ISTA,IEND
CAPE(I,J) = MAX(D00,CAPE(I,J))
CINS(I,J) = MIN(CINS(I,J),D00)
! add equillibrium height
ZEQL(I,J) = ZINT(I,J,IEQL(I,J))
TEQL(I,J) = T(I,J,IEQL(I,J))
IF (CAPE20(I,J) < 75.) THEN
THUNDER(I,J) = .FALSE.
ENDIF
IF (THUNDER(I,J)) THEN
THUND(I,J) = 1.0
ELSE
THUND(I,J) = 0.0
ENDIF
ENDDO
ENDDO
!
DEALLOCATE(TPAR)
!
END SUBROUTINE CALCAPE
!
!
!-------------------------------------------------------------------------------------
!> calcape2() computes CAPE and CINS.
!>
!> This routine computes CAPE and CINS given temperature,
!> pressure, and specific humidty. In "storm and cloud
!> dynamics" (1989, academic press) cotton and anthes define
!> CAPE (equation 9.16, p501) as
!>
!> @code
!> EL
!> CAPE = SUM G * ln(THETAP/THETAA) DZ
!> LCL
!>
!> Where,
!> EL = Equilibrium level,
!> LCL = Lifting condenstation level,
!> G = Gravitational acceleration,
!> THETAP = Lifted parcel potential temperature,
!> THETAA = Ambient potential temperature.
!> @endcode
!>
!> Note that the integrand ln(THETAP/THETAA) approximately
!> equals (THETAP-THETAA)/THETAA. This ratio is often used
!> in the definition of CAPE/CINS.
!>
!> Two types of CAPE/CINS can be computed by this routine. The
!> summation process is the same For both cases. What differs
!> is the definition of the parcel to lift. FOR ITYPE=1 the
!> parcel with the warmest THETA-E in A DPBND pascal layer above
!> the model surface is lifted. the arrays P1D, T1D, and Q1D
!> are not used. For itype=2 the arrays P1D, T1D, and Q1D
!> define the parcel to lift in each column. Both types of
!> CAPE/CINS may be computed in a single execution of the post
!> processor.
!>
!> This algorithm proceeds as follows.
!> For each column,
!> (1) Initialize running CAPE and CINS SUM TO 0.0
!> (2) Compute temperature and pressure at the LCL using
!> look up table (PTBL). Use either parcel that gives
!> max THETAE in lowest DPBND above ground (ITYPE=1)
!> or given parcel from t1D,Q1D,...(ITYPE=2).
!> (3) Compute the temp of a parcel lifted from the LCL.
!> We know that the parcel's
!> equivalent potential temperature (THESP) remains
!> constant through this process. we can
!> compute tpar using this knowledge using look
!> up table (subroutine TTBLEX).
!> (4) Find the equilibrium level. This is defined as the
!> highest positively buoyant layer.
!> (If there is no positively buoyant layer, CAPE/CINS
!> will be zero)
!> (5) Compute CAPE/CINS.
!> (A) Compute THETAP. We know TPAR and P.
!> (B) Compute THETAA. We know T and P.
!> (6) Add G*(THETAP-THETAA)*DZ to the running CAPE or CINS sum.
!> (A) If THETAP > THETAA, add to the CAPE sum.
!> (B) If THETAP < THETAA, add to the CINS sum.
!> (7) Are we at equilibrium level?
!> (A) If yes, stop the summation.
!> (b) if no, contiunue the summation.
!> (8) Enforce limits on CAPE and CINS (i.e. no negative CAPE)
!>
!> @param[in] ITYPE INTEGER Flag specifying how parcel to lift is identified. See comments above.
!> @param[in] DPBND Depth over which one searches for most unstable parcel.
!> @param[in] P1D Array of pressure of parcels to lift.
!> @param[in] T1D Array of temperature of parcels to lift.
!> @param[in] Q1D Array of specific humidity of parcels to lift.
!> @param[in] L1D Array of model level of parcels to lift.
!> @param[out] CAPE Convective available potential energy (J/kg).
!> @param[out] CINS Convective inhibition (J/kg).
!> @param[out] LFC level of free convection (m).
!> @param[out] ESRHL Lower bound to account for effective helicity calculation.
!> @param[out] ESRHH Upper bound to account for effective helicity calculation.
!> @param[out] DCAPE downdraft CAPE (J/KG).
!> @param[out] DGLD Dendritic growth layer depth (m).
!> @param[out] ESP Enhanced stretching potential.
!>
!> ### Program history log:
!> Date | Programmer | Comments
!> -----|------------|---------
!> 1993-02-10 | Russ Treadon | Initial
!> 1993-06-19 | Russ Treadon | Generalized routine to allow for type 2 CAPE/CINS calculations
!> 1994-09-23 | Mike Baldwin | Modified to use look up tables instead of complicated equations
!> 1994-10-13 | Mike Baldwin | Modified to continue CAPE/CINS calc up to at highest buoyant layer
!> 1998-06-12 | T Black | Conversion from 1-D TO 2-D
!> 1998-08-18 | T Black | Compute APE internally
!> 2000-01-04 | Jim Tuccillo | MPI Version
!> 2002-01-15 | Mike Baldwin | WRF Version
!> 2003-08-24 | G Manikin | Added level of parcel being lifted as output from the routine and added the depth over which one searches for the most unstable parcel as input
!> 2010-09-09 | G Manikin | Changed computation to use virtual temp added eq lvl hght and thunder parameter
!> 2015-??-?? | S Moorthi | Optimization and threading