wgrib2: rpn to Calculate Dewpoint and TotalTotal Index
Introduction
The rpn option runs a reverse Polish notation
(RPN)
calculator. In this example, I will show how to
calculate the dewpoint and totaltotal index in wgrib2. The totaltotal index
correlates with the thunderstorm strength and coverage.
TT = (T850T500) + (Td850  T500)
Td850 = dewp point temperature at 850 mb in C
T500 = temperature at the 500 mb level in C
T850 = temperature at the 850 mb level in C
The above expression for TT can be written in reverse Polish (postfix)
notation as,
T850 T500  Td850 T500  +
The conversion to reverse Polish is not unique and can be done without electronic aids.
However, we will be doing more complicated expressions and online converters
are handy. In a web search, the first two sites that I found were,
https://www.mathblog.dk/tools/infixpostfixconverter/
https://scanftree.com/Data_Structure/prefixpostfixinfixonlineconverter
To use these sites, enter the expression in the infix box and press the convert
button. By the way, this list is not a endorsement. If I had more patience, I could have found
more sites. With a different search engine, I may have found two different
sites. Anyways with the above postfix expression, the sh script to calculate
the totaltotal index is,
1 #!/bin/sh
2 # calculate total total index
3 #
4 # file = grib2 file with T850 T500 and Td850 (DPT)
5 #
6 # calulate total total index by
7 # TT = (T850  T500) + (Td850  T500)
8 # TT in reverse polish is T850 T500  Td850 T500  +
9
10 file=gdas1.t00z.pgrbanl.grib2
11
12 wgrib2 $file \
13 grib out.grb2 \
14 if ":TMP:500 mb:" rpn "273.15::sto_1" print "saved T500 in reg1" fi \
15 if ":TMP:850 mb:" rpn "273.15::sto_2" print "saved T850 in reg2" fi \
16 if ":DPT:850 mb:" rpn "273.15::sto_3" print "saved Td850 in reg3" fi \
17 if_reg "1:2:3" print ":ready to do work" \
18 rpn "rcl_2:rcl_1::rcl_3:rcl_1::+" \
19 set_var TOTALX grib_out out.grb2 \
20 if_reg "1:2:3" rpn "clr_1:clr_2:clr_3" fi
Line 13 saves each field in file out.grb2
Line 14 saves T500 (C) in register 1, converts from K to C.
Line 15 saves T850 (C) in register 2
Line 16 saves Td850 (C) in register 3
Line 17 if register 1, 2 and 3 are defined then
Line 18 calculates TT
Line 19 writes TT to out.grb2
Line 20 clears registers so that lines 1719 are only done once
The problem with using the above script with the GFS is that the standard
GFS output does not include dewpoint temperature. You have to compute the
dewpoint from the relative or specific humidity. See the comments
in the following script for the formula used.
1 #!/bin/sh
2 # calculate total total index
3 #
4 # file = grib2 file with T850 T500 and RH850
5 #
6 # step 1: calculate 850 mb dewpoint temp
7 # got formula from here: https://en.wikipedia.org/wiki/Dew_point (retrieved 12/2013)
8 #
9 # gamma = (ln(RH / 100) + (b * T) / (c + T)
10 # let r3 = ln(RH/100)
11 # gamma: r3 b T * c T + / +
12 # Td = (c*gamma) / (bgamma)
13 # Td: c gamma * b gamma  /
14 #
15 # some values of (b,c): 18.678, 257.14C Bolton 1980 MWR
16 # some values of (b,c): 17.62, 243.12C Sontag 1990
17 # some values of (b,c): 17.27, 2137.7C Paroscientific
18 #
19 #
20 # calulate total total index by
21 # TT = (T850  T500) + (Td850  T500)
22
23 file=gdas1.t00z.pgrbanl.grib2
24
25 wgrib2 $file \
26 grib out.grb2 \
27 if ":TMP:500 mb:" rpn "273.15::sto_1" print "saved T500 in reg1" fi \
28 if ":TMP:850 mb:" rpn "273.15::sto_2" print "saved T850 in reg2" fi \
29 if ":RH:850 mb:" rpn "100:/:ln:sto_3" print "saved LN(RH850/100) in reg3" fi \
30 if_reg "1:2:3" print ":calc Td850" \
31 rpn "rcl_3:18.678:rcl_2:*:257.14:rcl_2:+:/:+:sto_4:" \
32 rpn "257.14:rcl_4:*:18.678:rcl_4::/:sto_5:273.15:+" set_var DPT grib_out out.grb2 \
33 if_reg "5" rpn "rcl_2:rcl_1::rcl_5:rcl_1::+" set_var TOTALX grib_out out.grb2 \
34 if_reg "5" rpn "clr_1:clr_2:clr_3:clr_4:clr_5" fi
Line 26 saves each field in file out.grb2
Line 27 saves T500 (C) in register 1, converts from K to C.
Line 28 saves T850 (C) in register 2
Line 29 saves ln(RH850/100) in register 3
Line 3032 computes dewpoint and saves in register 5 (C) and writes to grib file (K)
Line 33 computes totaltotal index and writes it out
Line 34 clears registers
Note that the above script requires wgrib2 v2.0.0 to run. Previous versions
had a problem with the ln function. In hindsight, if should have been
always closed by fi rather than an output or a fi.
