Step-by-step guide
You have an input file containing GRIB or BUFR messages but each one of the messages also has a GTS wrapper. You want to print the GTS keys as well as the GRIB/BUFR keys for all these messages.
Each block looks something like this (repeated for each message):
GTS header | Enclosed GRIB/BUFR message | GTS footer | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SOH | CR | CR | LF | 010100100100111..... | CR | CR | LF | ETX |
The example below shows a Python script which has two loops: The first one over the GTS messages and the second loop over the GRIB messages. In the first pass we store the GTS key values into a dictionary for later access (a dictionary whose key is the message number and whose value is another dictionary storing the GTS keys and their values)
When we come to the GRIB messages we access the dictionary to get the relevant GTS keys.
To read BUFR messages instead, simply change the call from "codes_grib_new_from_file" to "codes_bufr_new_from_file".
from eccodes import * INPUT = sys.argv[1] mydict = {} # GTS loop cnt = 0 with open(INPUT, 'rb') as fin: while 1: gts = codes_gts_new_from_file(fin) if gts is None: break # Get the relevant keys from the GTS bulletin cnt += 1 tt = codes_get(gts, 'TT') aa = codes_get(gts, 'AA') ii = codes_get(gts, 'II') mydict[cnt] = {'TT': tt, 'AA': aa, 'II': ii} codes_release(gts) # BUFR/GRIB loop cnt = 0 with open(INPUT, 'rb') as fin: while 1: msgid = codes_grib_new_from_file(fin) if msgid is None: break cnt += 1 sn = codes_get(msgid, 'shortName') idict = mydict[cnt] # Access GTS values tt = idict['TT'] aa = idict['AA'] ii = idict['II'] print(tt, aa, ii, sn) codes_release(msgid)
Note: Sometimes a GTS envelope may include multiple GRIB/BUFR messages. The example above assumes that for each GTS wrapper there is exactly one GRIB/BUFR message.