-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_diag_functions.f90
1748 lines (1460 loc) · 70.6 KB
/
mod_diag_functions.f90
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
! https://github.com/wrf-model/WRF/blob/master/phys/module_diag_functions.F
!WRF:MEDIATION_LAYER:PHYSICS
! #if (NMM_CORE == 1)
! MODULE diag_functions
! CONTAINS
! SUBROUTINE diag_functions_stub
! END SUBROUTINE diag_functions_stub
! END MODULE diag_functions
! #else
MODULE diag_functions
CONTAINS
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ calc_rh
!~
!~ Description:
!~ This function calculates relative humidity given pressure,
!~ temperature, and water vapor mixing ratio.
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION calc_rh ( p, t, qv ) result ( rh )
IMPLICIT NONE
REAL, INTENT(IN) :: p, t, qv
REAL :: rh
! Local
! -----
REAL, PARAMETER :: pq0=379.90516
REAL, PARAMETER :: a2=17.2693882
REAL, PARAMETER :: a3=273.16
REAL, PARAMETER :: a4=35.86
REAL, PARAMETER :: rhmin=1.
REAL :: q, qs
INTEGER :: i,j,k
! Following algorithms adapted from WRFPOST
! May want to substitute with another later
! -----------------------------------------
q=qv/(1.0+qv)
qs=pq0/p*exp(a2*(t-a3)/(t-a4))
rh=100.*q/qs
IF (rh .gt. 100.) THEN
rh=100.
ELSE IF (rh .lt. rhmin) THEN
rh=rhmin
ENDIF
END FUNCTION calc_rh
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ uv_wind
!~
!~ Description:
!~ This function calculates the wind speed given U and V wind
!~ components.
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION uv_wind ( u, v ) result ( wind_speed )
IMPLICIT NONE
REAL, INTENT(IN) :: u, v
REAL :: wind_speed
wind_speed = sqrt( u*u + v*v )
END FUNCTION uv_wind
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ Theta
!~
!~ Description:
!~ This function calculates potential temperature as defined by
!~ Poisson's equation, given temperature and pressure ( hPa ).
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION Theta ( t, p )
IMPLICIT NONE
!~ Variable declaration
! --------------------
REAL, INTENT ( IN ) :: t
REAL, INTENT ( IN ) :: p
REAL :: theta
REAL :: Rd ! Dry gas constant
REAL :: Cp ! Specific heat of dry air at constant pressure
REAL :: p0 ! Standard pressure ( 1000 hPa )
Rd = 287.04
Cp = 1004.67
p0 = 1000.00
!~ Poisson's equation
! ------------------
theta = t * ( (p0/p)**(Rd/Cp) )
END FUNCTION Theta
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ Thetae
!~
!~ Description:
!~ This function returns equivalent potential temperature using the
!~ method described in Bolton 1980, Monthly Weather Review, equation 43.
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION Thetae ( tK, p, rh, mixr )
IMPLICIT NONE
!~ Variable Declarations
! ---------------------
REAL :: tK ! Temperature ( K )
REAL :: p ! Pressure ( hPa )
REAL :: rh ! Relative humidity
REAL :: mixr ! Mixing Ratio ( kg kg^-1)
REAL :: te ! Equivalent temperature ( K )
REAL :: thetae ! Equivalent potential temperature
REAL, PARAMETER :: R = 287.04 ! Universal gas constant (J/deg kg)
REAL, PARAMETER :: P0 = 1000.0 ! Standard pressure at surface (hPa)
REAL, PARAMETER :: lv = 2.54*(10**6) ! Latent heat of vaporization
! (J kg^-1)
REAL, PARAMETER :: cp = 1004.67 ! Specific heat of dry air constant
! at pressure (J/deg kg)
REAL :: tlc ! LCL temperature
!~ Calculate the temperature of the LCL
! ------------------------------------
tlc = TLCL ( tK, rh )
!~ Calculate theta-e
! -----------------
thetae = (tK * (p0/p)**( (R/Cp)*(1.- ( (.28E-3)*mixr*1000.) ) ) )* &
exp( (((3.376/tlc)-.00254))*&
(mixr*1000.*(1.+(.81E-3)*mixr*1000.)) )
END FUNCTION Thetae
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ The2T.f90
!~
!~ Description:
!~ This function returns the temperature at any pressure level along a
!~ saturation adiabat by iteratively solving for it from the parcel
!~ thetae.
!~
!~ Dependencies:
!~ function thetae.f90
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION The2T ( thetaeK, pres, flag ) result ( tparcel )
IMPLICIT NONE
!~ Variable Declaration
! --------------------
REAL, INTENT ( IN ) :: thetaeK
REAL, INTENT ( IN ) :: pres
LOGICAL, INTENT ( INOUT ) :: flag
REAL :: tparcel
REAL :: thetaK
REAL :: tovtheta
REAL :: tcheck
REAL :: svpr, svpr2
REAL :: smixr, smixr2
REAL :: thetae_check, thetae_check2
REAL :: tguess_2, correction
LOGICAL :: found
INTEGER :: iter
REAL :: R ! Dry gas constant
REAL :: Cp ! Specific heat for dry air
REAL :: kappa ! Rd / Cp
REAL :: Lv ! Latent heat of vaporization at 0 deg. C
R = 287.04
Cp = 1004.67
Kappa = R/Cp
Lv = 2.500E+6
!~ Make initial guess for temperature of the parcel
! ------------------------------------------------
tovtheta = (pres/100000.0)**(r/cp)
tparcel = thetaeK/exp(lv*.012/(cp*295.))*tovtheta
iter = 1
found = .false.
flag = .false.
DO
IF ( iter > 105 ) EXIT
tguess_2 = tparcel + REAL ( 1 )
svpr = 6.122 * exp ( (17.67*(tparcel-273.15)) / (tparcel-29.66) )
smixr = ( 0.622*svpr ) / ( (pres/100.0)-svpr )
svpr2 = 6.122 * exp ( (17.67*(tguess_2-273.15)) / (tguess_2-29.66) )
smixr2 = ( 0.622*svpr2 ) / ( (pres/100.0)-svpr2 )
! ------------------------------------------------------------------ ~!
!~ When this function was orinially written, the final parcel ~!
!~ temperature check was based off of the parcel temperature and ~!
!~ not the theta-e it produced. As there are multiple temperature- ~!
!~ mixing ratio combinations that can produce a single theta-e value, ~!
!~ we change the check to be based off of the resultant theta-e ~!
!~ value. This seems to be the most accurate way of backing out ~!
!~ temperature from theta-e. ~!
!~ ~!
!~ Rentschler, April 2010 ~!
! ------------------------------------------------------------------ !
!~ Old way...
!thetaK = thetaeK / EXP (lv * smixr /(cp*tparcel) )
!tcheck = thetaK * tovtheta
!~ New way
thetae_check = Thetae ( tparcel, pres/100., 100., smixr )
thetae_check2 = Thetae ( tguess_2, pres/100., 100., smixr2 )
!~ Whew doggies - that there is some accuracy...
!IF ( ABS (tparcel-tcheck) < .05) THEN
IF ( ABS (thetaeK-thetae_check) < .001) THEN
found = .true.
flag = .true.
EXIT
END IF
!~ Old
!tparcel = tparcel + (tcheck - tparcel)*.3
!~ New
correction = ( thetaeK-thetae_check ) / ( thetae_check2-thetae_check )
tparcel = tparcel + correction
iter = iter + 1
END DO
!IF ( .not. found ) THEN
! print*, "Warning! Thetae to temperature calculation did not converge!"
! print*, "Thetae ", thetaeK, "Pressure ", pres
!END IF
END FUNCTION The2T
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ VirtualTemperature
!~
!~ Description:
!~ This function returns virtual temperature given temperature ( K )
!~ and mixing ratio.
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION VirtualTemperature ( tK, w ) result ( Tv )
IMPLICIT NONE
!~ Variable declaration
real, intent ( in ) :: tK !~ Temperature
real, intent ( in ) :: w !~ Mixing ratio ( kg kg^-1 )
real :: Tv !~ Virtual temperature
Tv = tK * ( 1.0 + (w/0.622) ) / ( 1.0 + w )
END FUNCTION VirtualTemperature
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ SaturationMixingRatio
!~
!~ Description:
!~ This function calculates saturation mixing ratio given the
!~ temperature ( K ) and the ambient pressure ( Pa ). Uses
!~ approximation of saturation vapor pressure.
!~
!~ References:
!~ Bolton (1980), Monthly Weather Review, pg. 1047, Eq. 10
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION SaturationMixingRatio ( tK, p ) result ( ws )
IMPLICIT NONE
REAL, INTENT ( IN ) :: tK
REAL, INTENT ( IN ) :: p
REAL :: ws
REAL :: es
es = 6.122 * exp ( (17.67*(tK-273.15))/ (tK-29.66) )
ws = ( 0.622*es ) / ( (p/100.0)-es )
END FUNCTION SaturationMixingRatio
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ tlcl
!~
!~ Description:
!~ This function calculates the temperature of a parcel of air would have
!~ if lifed dry adiabatically to it's lifting condensation level (lcl).
!~
!~ References:
!~ Bolton (1980), Monthly Weather Review, pg. 1048, Eq. 22
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION TLCL ( tk, rh )
IMPLICIT NONE
REAL, INTENT ( IN ) :: tK !~ Temperature ( K )
REAL, INTENT ( IN ) :: rh !~ Relative Humidity ( % )
REAL :: tlcl
REAL :: denom, term1, term2
term1 = 1.0 / ( tK - 55.0 )
IF ( rh > REAL (0) ) THEN
term2 = ( LOG (rh/100.0) / 2840.0 )
ELSE
term2 = ( LOG (0.001/1.0) / 2840.0 )
END IF
denom = term1 - term2
tlcl = ( 1.0 / denom ) + REAL ( 55 )
END FUNCTION TLCL
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~
!~ Name:
!~ PWat
!~
!~ Description:
!~ This function calculates precipitable water by summing up the
!~ water vapor in a column from the first eta layer to model top
!~
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION Pwat ( nz, qv, qc, dz8w, rho )
IMPLICIT NONE
!~ Variable declaration
! --------------------
INTEGER, INTENT ( IN ) :: nz !~ Number of vertical levels
REAL, INTENT ( IN ) :: qv ( nz ) !~ Specific humidity in layer (kg/kg)
REAL, INTENT ( IN ) :: qc ( nz ) !~ Cloud water in layer (kg/kg)
REAL, INTENT ( IN ) :: dz8w ( nz ) !~ Dist between vertical levels (m)
REAL, INTENT ( IN ) :: rho ( nz ) !~ Air density (kg/m^3)
REAL :: Pwat !~ Precipitable water (kg/m^2)
INTEGER :: k !~ Vertical index
!~ Precipitable water (kg/m^2)
! ---------------------------
Pwat=0
DO k = 1, nz
!Based on AMS PWAT defination (https://glossary.ametsoc.org/wiki/Precipitable_water)
!PWAT is corrected as the column accumulated water vapor rather than water vapor + cloud water.
!Modified by Zhixiao
Pwat = Pwat + qv(k) * dz8w(k) * rho(k)
ENDDO
END FUNCTION Pwat
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~ ~!
!~ Name: ~!
!~ Buoyancy ~!
!~ ~!
!~ Description: ~!
!~ This function computes Convective Available Potential Energy (CAPE) ~!
!~ with inhibition as a result of water loading given the data required ~!
!~ to run up a sounding. ~!
!~ ~!
!~ Additionally, since we are running up a sounding anyways, this ~!
!~ function returns the height of the Level of Free Convection (LFC) and ~!
!~ the pressure at the LFC. That-a-ways, we don't have to run up a ~!
!~ sounding later, saving a relatively computationally expensive ~!
!~ routine. ~!
!~ ~!
!~ Usage: ~!
!~ ostat = Buoyancy ( tK, rh, p, hgt, sfc, CAPE, ZLFC, PLFC, parcel ) ~!
!~ ~!
!~ Where: ~!
!~ ~!
!~ IN ~!
!~ -- ~!
!~ tK = Temperature ( K ) ~!
!~ rh = Relative Humidity ( % ) ~!
!~ p = Pressure ( Pa ) ~!
!~ hgt = Geopotential heights ( m ) ~!
!~ sfc = integer rank within submitted arrays that represents the ~!
!~ surface ~!
!~ ~!
!~ OUT ~!
!~ --- ~!
!~ ostat INTEGER return status. Nonzero is bad. ~!
!~ CAPE ( J/kg ) Convective Available Potential Energy ~!
!~ ZLFC ( gpm ) Height at the LFC ~!
!~ PLFC ( Pa ) Pressure at the LFC ~!
!~ ~!
!~ tK, rh, p, and hgt are all REAL arrays, arranged from lower levels ~!
!~ to higher levels. ~!
!~ ~!
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
FUNCTION Buoyancy ( nz, tk, rh, p, hgt, sfc, cape, cin, zlfc, plfc, lidx, &
parcel ) result (ostat)
IMPLICIT NONE
INTEGER, INTENT ( IN ) :: nz !~ Number of vertical levels
INTEGER, INTENT ( IN ) :: sfc !~ Surface level in the profile
REAL, INTENT ( IN ) :: tk ( nz ) !~ Temperature profile ( K )
REAL, INTENT ( IN ) :: rh ( nz ) !~ Relative Humidity profile ( % )
REAL, INTENT ( IN ) :: p ( nz ) !~ Pressure profile ( Pa )
REAL, INTENT ( IN ) :: hgt ( nz ) !~ Height profile ( gpm )
REAL, INTENT ( OUT ) :: cape !~ CAPE ( J kg^-1 )
REAL, INTENT ( OUT ) :: cin !~ CIN ( J kg^-1 )
REAL, INTENT ( OUT ) :: zlfc !~ LFC Height ( gpm )
REAL, INTENT ( OUT ) :: plfc !~ LFC Pressure ( Pa )
REAL, INTENT ( OUT ) :: lidx !~ Lifted index
INTEGER :: ostat !~ Function return status
!~ Nonzero is bad.
INTEGER, INTENT ( IN ) :: parcel !~ Most Unstable = 1 (default)
!~ Mean layer = 2
!~ Surface based = 3
!~ Derived profile variables
! -------------------------
REAL :: ws ( nz ) !~ Saturation mixing ratio
REAL :: w ( nz ) !~ Mixing ratio
REAL :: etheta( nz )!~ Equivalent potential temperature. Modified by Zhixiao.
REAL :: dTvK ( nz ) !~ Parcel / ambient Tv difference
REAL :: buoy ( nz ) !~ Buoyancy
REAL :: tlclK !~ LCL temperature ( K )
REAL :: plcl !~ LCL pressure ( Pa )
REAL :: pel !~ Equilibrium pressure ( Pa ). Modified by Zhixiao.
REAL :: nbuoy !~ Negative buoyancy
REAL :: pbuoy !~ Positive buoyancy
!~ Source parcel information
! -------------------------
REAL :: srctK !~ Source parcel temperature ( K )
REAL :: srcrh !~ Source parcel rh ( % )
REAL :: srcws !~ Source parcel sat. mixing ratio
REAL :: srcw !~ Source parcel mixing ratio
REAL :: srcp !~ Source parcel pressure ( Pa )
REAL :: srctheta !~ Source parcel theta ( K )
REAL :: srcthetaeK !~ Source parcel theta-e ( K )
INTEGER :: srclev !~ Level of the source parcel
REAL :: spdiff !~ Pressure difference
REAL :: srce !~ Equivalent potential temperature ( K ). Modified by Zhixiao.
!~ Parcel variables
! ----------------
REAL :: ptK !~ Parcel temperature ( K )
REAL :: ptvK !~ Parcel virtual temperature ( K )
REAL :: tvK !~ Ambient virtual temperature ( K )
REAL :: pw !~ Parcel mixing ratio
!~ Other utility variables
! -----------------------
INTEGER :: i, j, k !~ Dummy iterator
INTEGER :: lfclev !~ Level of LFC
INTEGER :: ellev !~ Level of EL. Modified by Zhixiao.
INTEGER :: prcl !~ Internal parcel type indicator
INTEGER :: mlev !~ Level for ML calculation
INTEGER :: lyrcnt !~ Number of layers in mean layer
LOGICAL :: flag !~ Dummy flag
LOGICAL :: wflag !~ Saturation flag
REAL :: freeze !~ Water loading multiplier
REAL :: pdiff !~ Pressure difference between levs
REAL :: pm, pu, pd !~ Middle, upper, lower pressures
REAL :: lidxu !~ Lifted index at upper level
REAL :: lidxd !~ Lifted index at lower level
!~ Thermo / dynamical constants
! ----------------------------
REAL :: Rd !~ Dry gas constant
PARAMETER ( Rd = 287.058 ) !~ J deg^-1 kg^-1
REAL :: Cp !~ Specific heat constant pressure
PARAMETER ( Cp = 1004.67 ) !~ J deg^-1 kg^-1
REAL :: g !~ Acceleration due to gravity
PARAMETER ( g = 9.80665 ) !~ m s^-2
REAL :: RUNDEF
PARAMETER ( RUNDEF = -9.999E30 )
!~ Initialize variables
! --------------------
ostat = 0
CAPE = REAL ( 0 )
CIN = RUNDEF !Change CIN filling values from 0 to default filling. CIN should not initially be filled by 0, because 0 means no inhibition energy. Modified by Zhixiao
ZLFC = RUNDEF
PLFC = RUNDEF
!~ Look for submitted parcel definition
!~ 1 = Most unstable
!~ 2 = Mean layer
!~ 3 = Surface based
! -------------------------------------
IF ( parcel > 3 .or. parcel < 1 ) THEN
prcl = 1
ELSE
prcl = parcel
END IF
!~ Initalize our parcel to be (sort of) surface based. Because of
!~ issues we've been observing in the WRF model, specifically with
!~ excessive surface moisture values at the surface, using a true
!~ surface based parcel is resulting a more unstable environment
!~ than is actually occuring. To address this, our surface parcel
!~ is now going to be defined as the parcel between 25-50 hPa
!~ above the surface. UPDATE - now that this routine is in WRF,
!~ going to trust surface info. GAC 20140415
! ----------------------------------------------------------------
!~ Compute mixing ratio values for the layer
! -----------------------------------------
DO k = sfc, nz
ws ( k ) = SaturationMixingRatio ( tK(k), p(k) )
w ( k ) = ( rh(k)/100.0 ) * ws ( k )
!Removed by Zhixiao. Water vapor mixing ratio (w) is not conserved during parcel lifting processes. We should not use w to define MU layer.
!thetav(k) = Theta ( VirtualTemperature ( tK (k), w (k) ), p(k)/100.0 )
!Added by Zhixiao. Critical modification: We use the model level with maximum equivalent potential temperature (etheta) below 500mb to define the MU layer
!Because equivalent potential temperature is conserved in dry and moist adiabatic processes.
etheta(k) = Thetae( tK(k), p(k)/100.0, rh(k), w(k) )
END DO
srclev = sfc
srctK = tK ( sfc )
srcrh = rh ( sfc )
srcp = p ( sfc )
srcws = ws ( sfc )
srcw = w ( sfc )
srctheta = Theta ( tK(sfc), p(sfc)/100.0 )
srce = etheta (sfc) !Modified by Zhixiao
!~ Compute the profile mixing ratio. If the parcel is the MU parcel,
!~ define our parcel to be the most unstable parcel within the lowest
!~ 180 mb.
! -------------------------------------------------------------------
mlev = sfc + 1
!Change initial searching level from the second to first model level. Because we did not compute pdiff, and p(k-1) properties is unnecessary.
!Modified by Zhixiao.
DO k = sfc, nz
!~ Identify the last layer within 100 hPa of the surface
! -----------------------------------------------------
pdiff = ( p (sfc) - p (k) ) / REAL ( 100 )
IF ( pdiff <= REAL (100) ) mlev = k
!~ If we've made it past the lowest 500 hPa, exit the loop. MU layer is assumed below 500 hPa. Modified by Zhixiao.
! -------------------------------------------------------
IF ( p(k) <= REAL (50000) ) EXIT
IF ( prcl == 1 ) THEN
! Removed by Zhixiao, w can not used for defining MU layer
!IF ( (p(k) > 70000.0) .and. (w(k) > srcw) ) THEN
! Modified by Zhixiao, MU layer is featured by the max etheta
IF (etheta(k) > srce) THEN !Modified by Zhixiao.
srctheta = Theta ( tK(k), p(k)/100.0 )
srcw = w ( k )
srclev = k
srctK = tK ( k )
srcrh = rh ( k )
srcp = p ( k )
srce = etheta(k) !Modified by Zhixiao
END IF
END IF
END DO
!~ If we want the mean layer parcel, compute the mean values in the
!~ lowest 100 hPa.
! ----------------------------------------------------------------
lyrcnt = mlev - sfc + 1
IF ( prcl == 2 ) THEN
srclev = sfc
srctK = SUM ( tK (sfc:mlev) ) / REAL ( lyrcnt )
srcw = SUM ( w (sfc:mlev) ) / REAL ( lyrcnt )
srcrh = SUM ( rh (sfc:mlev) ) / REAL ( lyrcnt )
srcp = SUM ( p (sfc:mlev) ) / REAL ( lyrcnt )
srctheta = Theta ( srctK, srcp/100. )
END IF
srcthetaeK = Thetae ( srctK, srcp/100.0, srcrh, srcw )
!~ Calculate temperature and pressure of the LCL
! ---------------------------------------------
tlclK = TLCL ( tK(srclev), rh(srclev) )
plcl = p(srclev) * ( (tlclK/tK(srclev))**(Cp/Rd) )
!~ Now lift the parcel
! -------------------
buoy = REAL ( 0 )
pw = srcw
wflag = .false.
DO k = srclev, nz
IF ( p (k) <= plcl ) THEN
!~ The first level after we pass the LCL, we're still going to
!~ lift the parcel dry adiabatically, as we haven't added the
!~ the required code to switch between the dry adiabatic and moist
!~ adiabatic cooling. Since the dry version results in a greater
!~ temperature loss, doing that for the first step so we don't over
!~ guesstimate the instability.
! ----------------------------------------------------------------
IF ( wflag ) THEN
flag = .false.
!~ Above the LCL, our parcel is now undergoing moist adiabatic
!~ cooling. Because of the latent heating being undergone as
!~ the parcel rises above the LFC, must iterative solve for the
!~ parcel temperature using equivalant potential temperature,
!~ which is conserved during both dry adiabatic and
!~ pseudoadiabatic displacements.
! --------------------------------------------------------------
ptK = The2T ( srcthetaeK, p(k), flag )
!~ Calculate the parcel mixing ratio, which is now changing
!~ as we condense moisture out of the parcel, and is equivalent
!~ to the saturation mixing ratio, since we are, in theory, at
!~ saturation.
! ------------------------------------------------------------
pw = SaturationMixingRatio ( ptK, p(k) )
!~ Now we can calculate the virtual temperature of the parcel
!~ and the surrounding environment to assess the buoyancy.
! ----------------------------------------------------------
ptvK = VirtualTemperature ( ptK, pw )
tvK = VirtualTemperature ( tK (k), w (k) )
!~ Modification to account for water loading
! -----------------------------------------
freeze = 0.033 * ( 263.15 - pTvK )
IF ( freeze > 1.0 ) freeze = 1.0
IF ( freeze < 0.0 ) freeze = 0.0
!~ Approximate how much of the water vapor has condensed out
!~ of the parcel at this level
! ---------------------------------------------------------
freeze = freeze * 333700.0 * ( srcw - pw ) / 1005.7
pTvK = pTvK - pTvK * ( srcw - pw ) + freeze
dTvK ( k ) = ptvK - tvK
buoy ( k ) = g * ( dTvK ( k ) / tvK )
ELSE
!~ Since the theta remains constant whilst undergoing dry
!~ adiabatic processes, can back out the parcel temperature
!~ from potential temperature below the LCL
! --------------------------------------------------------
ptK = srctheta / ( 100000.0/p(k) )**(Rd/Cp)
!~ Grab the parcel virtual temperture, can use the source
!~ mixing ratio since we are undergoing dry adiabatic cooling
! ----------------------------------------------------------
ptvK = VirtualTemperature ( ptK, srcw )
!~ Virtual temperature of the environment
! --------------------------------------
tvK = VirtualTemperature ( tK (k), w (k) )
!~ Buoyancy at this level
! ----------------------
dTvK ( k ) = ptvK - tvK
buoy ( k ) = g * ( dtvK ( k ) / tvK )
wflag = .true.
END IF
ELSE
!~ Since the theta remains constant whilst undergoing dry
!~ adiabatic processes, can back out the parcel temperature
!~ from potential temperature below the LCL
! --------------------------------------------------------
ptK = srctheta / ( 100000.0/p(k) )**(Rd/Cp)
!~ Grab the parcel virtual temperture, can use the source
!~ mixing ratio since we are undergoing dry adiabatic cooling
! ----------------------------------------------------------
ptvK = VirtualTemperature ( ptK, srcw )
!~ Virtual temperature of the environment
! --------------------------------------
tvK = VirtualTemperature ( tK (k), w (k) )
!~ Buoyancy at this level
! ---------------------
dTvK ( k ) = ptvK - tvK
buoy ( k ) = g * ( dtvK ( k ) / tvK )
END IF
!~ Chirp
! -----
! WRITE ( *,'(I15,6F15.3)' )k,p(k)/100.,ptK,pw*1000.,ptvK,tvK,buoy(k)
END DO
!~ Add up the buoyancies, find the LFC
! -----------------------------------
flag = .false.
lfclev = -1
ellev = -1 !Modified by Zhixiao
DO k = sfc, nz !Modified by Zhixiao
!~ LFC is defiend as the highest level when negative buyancy turns postive.
! -----------------------------------
IF ( .not. flag .and. buoy (k) > REAL (0) .and. p (k) <= plcl ) THEN !Modified by Zhixiao
flag = .true.
lfclev = k
END IF
!~ Take the Highest EL as final result. Modified by Zhixiao
! ----------------------------------------------------------------
IF (k >= 2) THEN !Modified by Zhixiao
IF ( flag .and. buoy (k) < REAL (0) .and. buoy (k-1) >= REAL (0)) THEN !Modified by Zhixiao
ellev = k !Modified by Zhixiao
END IF
END IF
! When buoy turns negative again, reset LFC flag and keep the highest LFC as the effective output
IF (buoy (k) < REAL (0) .and. flag) THEN
flag = .false.
END IF
END DO
IF ((ellev >= 0) .and. (lfclev >= 0)) THEN !Modified by Zhixiao
plfc = p (lfclev)
pel = p (ellev)
CIN = REAL ( 0 )
DO k = sfc+1, nz
! Make CAPE and CIN consistent with AMS definition
! https://glossary.ametsoc.org/wiki/Convective_available_potential_energy
! https://glossary.ametsoc.org/wiki/Convective_inhibition
IF ( p (k) <= plcl .and. p (k) > plfc) THEN !Modified by Zhixiao
! CIN is the vertically integrated negative buoyant energy between LCL and LFC
CIN = CIN + MIN ( buoy (k), 0.0 ) * ( hgt (k) - hgt (k-1) )
END IF
IF ( p (k) <= plfc .and. p (k) > pel) THEN !Modified by Zhixiao
! CAPE is the vertically integrated positive buoyant energy between LFC and EL
CAPE = CAPE + MAX ( buoy (k), 0.0 ) * ( hgt (k) - hgt (k-1) )
END IF !Modified by Zhixiao
END DO !Modified by Zhixiao
END IF !Modified by Zhixiao
!~ Calculate lifted index by interpolating difference between
!~ parcel and ambient Tv to 500mb.
! ----------------------------------------------------------
DO k = sfc + 1, nz
pm = 50000.
pu = p ( k )
pd = p ( k - 1 )
!~ If we're already above 500mb just set lifted index to 0.
!~ --------------------------------------------------------
IF ( pd .le. pm ) THEN
lidx = 0.
EXIT
ELSEIF ( pu .le. pm .and. pd .gt. pm) THEN
!~ Found trapping pressure: up, middle, down.
!~ We are doing first order interpolation.
! ------------------------------------------
lidxu = -dTvK ( k ) * ( pu / 100000. ) ** (Rd/Cp)
lidxd = -dTvK ( k-1 ) * ( pd / 100000. ) ** (Rd/Cp)
lidx = ( lidxu * (pm-pd) + lidxd * (pu-pm) ) / (pu-pd)
EXIT
ENDIF
END DO
!~ Assuming the the LFC is at a pressure level for now
! ---------------------------------------------------
IF ( lfclev > 0 ) THEN
PLFC = p ( lfclev )
ZLFC = hgt ( lfclev )
END IF
IF ( PLFC /= PLFC .OR. PLFC < REAL (0) ) THEN
PLFC = REAL ( -1 )
ZLFC = REAL ( -1 )
END IF
IF ( CAPE /= CAPE ) cape = REAL ( 0 )
IF ( CIN /= CIN ) cin = RUNDEF
!~ Chirp
! -----
! WRITE ( *,* ) ' CAPE: ', cape, ' CIN: ', cin
! WRITE ( *,* ) ' LFC: ', ZLFC, ' PLFC: ', PLFC
! WRITE ( *,* ) ''
! WRITE ( *,* ) ' Exiting buoyancy.'
! WRITE ( *,* ) ' ==================================== '
! WRITE ( *,* ) ''
END FUNCTION Buoyancy
!$$$ SUBPROGRAM DOCUMENTATION BLOCK
! . . .
! SUBPROGRAM: NGMSLP NMC SEA LEVEL PRESSURE REDUCTION
! PRGRMMR: TREADON ORG: W/NP2 DATE: 93-02-02
!
! ABSTRACT:
!
! THIS ROUTINE COMPUTES SEA LEVEL PRESSURE USING THE
! HYDROSTATIC EQUATION WITH THE SHUELL CORRECTION. THE
! FOLLOWING IS BASED ON DOCUMENTATION IN SUBROUTINE
! OUTHYDRO OF THE NGM:
!
! THE FUNDAMENTAL HYDROSTATIC EQUATION IS
! D(HEIGHT)
! --------- = TAU = VIRTUAL TEMPERATURE * (RGAS/GRAVITY)
! D (Z)
! WHERE
! Z = MINUS LOG OF PRESSURE (-LN(P)).
!
! SEA-LEVEL PRESSURE IS COMPUTED FROM THE FORMULA
! PRESS(MSL) = PRESS(GROUND) * EXP( F)
! WHERE
! F = HEIGHT OF GROUND / MEAN TAU
! MEAN TAU = ( TAU(GRND) + TAU(SL) ) / 2
!
! IN THE NGM TAU(GRND) AND TAU(SL) ARE FIRST SET USING A
! 6.5DEG/KM LAPSE RATE FROM LOWEST MDL LEVEL. THIS IS MODIFIED
! BY A CORRECTION BASED ON THE CRITICAL TAU OF THE SHUELL
! CORRECTION:
! TAUCR=(RGASD/GRAVITY) * 290.66
!
! 1) WHERE ONLY TAU(SL) EXCEEDS TAUCR, CHANGE TAU(SL) TO TAUCR.
!
! 2) WHERE BOTH TAU(SL) AND TAU(GRND) EXCEED TAUCR,
! CHANGE TAU(SL) TO TAUCR-CONST*(TAU(GRND)-TAUCR )**2
! WHERE CONST = .005 (GRAVITY/RGASD)
!
! THE AVERAGE OF TAU(SL) AND TAU(GRND) IS THEN USED TOGETHER
! WITH THE GROUND HEIGHT AND PRESSURE TO DERIVE THE PRESSURE
! AT SEA LEVEL.
!
! HEIGHT OF THE 1000MB SURFACE IS COMPUTED FROM THE MSL PRESSURE
! FIELD USING THE FORMULA:
!
! P(MSL) - P(1000MB) = MEAN DENSITY * GRAVITY * HGT(1000MBS)
!
! WHERE P(MSL) IS THE SEA LEVEL PRESSURE FIELD WE HAVE JUST
! COMPUTED.
!
!
! MEB 6/13/02: THIS CODE HAS BEEN SIMPLIFIED CONSIDERABLY FROM
! THE ONE USED IN ETAPOST. HORIZONTAL SMOOTHING HAS BEEN
! REMOVED AND THE FIRST MODEL LEVEL IS USED RATHER
! THAN THE MEAN OF THE VIRTUAL TEMPERATURES IN
! THE LOWEST 30MB ABOVE GROUND TO COMPUTE TAU(GRND).
!
! .
!
! PROGRAM HISTORY LOG:
! 93-02-02 RUSS TREADON
! 98-06-08 T BLACK - CONVERSION FROM 1-D TO 2-D
! 00-01-04 JIM TUCCILLO - MPI VERSION
! 01-10-25 H CHUANG - MODIFIED TO PROCESS HYBRID MODEL OUTPUT
! 01-11-02 H CHUANG - MODIFIED LINE 234 FOR COMPUTATION OF
! SIGMA/HYBRID SLP
! 01-12-18 H CHUANG - INCLUDED SMOOTHING ALONG BOUNDARIES TO BE
! CONSISTENT WITH MESINGER SLP
! 02-06-13 MIKE BALDWIN - WRF VERSION
! 06-12-18 H CHUANG - BUG FIX TO CORRECT TAU AT SFC
! 14-04-17 G CREIGHTON - MODIFIED TO INSERT INTO AFWA DIAGNOSTICS IN WRF
!
!$$$
FUNCTION MSLP ( zsfc, psfc, zlev1, qlev1, tlev1 )
implicit none
! DECLARE VARIABLES
REAL, INTENT ( IN ) :: zsfc !~ Surface height ( m )
REAL, INTENT ( IN ) :: psfc !~ Surface height ( m )
REAL, INTENT ( IN ) :: zlev1 !~ Level 1 height ( m )
REAL, INTENT ( IN ) :: qlev1 !~ Level 1 mixing ratio ( kg/kg )
REAL, INTENT ( IN ) :: tlev1 !~ Level 1 temperature ( K )
real,PARAMETER :: G=9.81
real,PARAMETER :: GI=1./G
real,PARAMETER :: RD=287.0
real,PARAMETER :: ZSL=0.0
real,PARAMETER :: TAUCR=RD*GI*290.66,CONST=0.005*G/RD
real,PARAMETER :: GORD=G/RD,DP=60.E2
real,PARAMETER :: GAMMA=6.5E-3
real MSLP,TVRT,TVRSFC,TAUSFC,TVRSL,TAUSL,TAUAVG
!
!**********************************************************************
! START NGMSLP HERE.
!
MSLP = PSFC
!
! COMPUTE LAYER TAU (VIRTUAL TEMP*RD/G).
TVRT = TLEV1*(1.0+0.608*QLEV1)
!TAU = TVRT*RD*GI
!
! COMPUTE TAU AT THE GROUND (Z=ZSFC) AND SEA LEVEL (Z=0)
! ASSUMING A CONSTANT LAPSE RATE OF GAMMA=6.5DEG/KM.
TVRSFC = TVRT + (ZLEV1 - ZSFC)*GAMMA
TAUSFC = TVRSFC*RD*GI
TVRSL = TVRT + (ZLEV1 - ZSL)*GAMMA
TAUSL = TVRSL*RD*GI
!
! IF NEED BE APPLY SHEULL CORRECTION.
IF ((TAUSL.GT.TAUCR).AND.(TAUSFC.LE.TAUCR)) THEN
TAUSL=TAUCR
ELSEIF ((TAUSL.GT.TAUCR).AND.(TAUSFC.GT.TAUCR)) THEN
TAUSL = TAUCR-CONST*(TAUSFC-TAUCR)**2
ENDIF
!
! COMPUTE MEAN TAU.
TAUAVG = 0.5*(TAUSL+TAUSFC)
!
! COMPUTE SEA LEVEL PRESSURE.
MSLP = PSFC*EXP(ZSFC/TAUAVG)
END FUNCTION MSLP
!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
!~ ~!
!~ Name: ~!
!~ calc_fits ~!
!~ ~!
!~ Description: ~!
!~ This function computes Fighter Index Thermal Stress values given ~!
!~ dry bulb temperature, relative humidity, and pressure. ~!
!~ ~!
!~ Usage: ~!
!~ fitsval = calc_fits ( p, tK, rh ) ~!
!~ ~!
!~ Where: ~!
!~ p = Pressure ( Pa ) ~!
!~ tK = Temperature ( K ) ~!
!~ rh = Relative Humidity ( % ) ~!
!~ ~!
!~ Reference: ~!
!~ Stribley, R.F., S. Nunneley, 1978: Fighter Index of Thermal Stress: ~!
!~ Development of interim guidance for hot-weather USAF operations. ~!
!~ SAM-TR-78-6. Eqn. 9 ~!
!~ ~!
!~ Formula: ~!
!~ FITS = 0.8281*Twb + 0.3549*Tdb + 5.08 (degrees Celsius) ~!
!~ ~!
!~ Where: ~!
!~ Twb = Wet Bulb Temperature ~!
!~ Tdb = Dry Bulb Temperature ~!
!~ ~!
!~ Written: ~!
!~ Scott Rentschler, Software Engineering Services ~!
!~ Fine Scale Models Team ~!
!~ Air Force Weather Agency, 16WS/WXN ~!
!~ DSN: 271-3331 Comm: (402) 294-3331 ~!
!~ [email protected] ~!
!~ ~!