...
Excerpt | ||
---|---|---|
| ||
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) |
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):
...
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 reads each GTS message, stores some keys and then reads the enclosed BUFR message in a loop (To read GRIB messages, simply change the CODES_PRODUCT_BUFR
argument in the call to codes_new_from_file to CODES_PRODUCT_GRIB
)" to "codes_bufr_new_from_file".
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
from eccodes import * INPUT = sys.argv[1] mydict = {} # GTS loop cnt = 0 with open(INPUT, 'rb') as fin: while 1: offset_saved = fin.tell() # Store offset in file # Read the GTS bulletin gts = codes_gts_new_from_file(fin) if gts is None: break cnt# +=Get 1 the relevant keys from the GTS bulletin # Get the relevant keys from thecnt GTS+= bulletin1 tt = codes_get(gts, 'TT') aa = codes_get(gts, 'AA') ii = codes_get(gts, 'II') mydict[cnt] = {'TT': tt, 'AA': aa, codes_release(gts)'II': ii} fin.seek(offset_saved) # Rewind # Now read the BUFR messagecodes_release(gts) # BUFR/GRIB loop cnt = 0 with open(INPUT, 'rb') as fin: while 1: msgid = codes_grib_new_from_file(fin, CODES_PRODUCT_BUFR) if msgid is None: break cnt += 1 nssn = codes_get(msgid, 'numberOfSubsetsshortName') idict = mydict[cnt] # Access GTS values tt = td = codes_get(msgid, 'typicalDate')idict['TT'] aa = idict['AA'] ii = idict['II'] print(tt, aa, ii, ns, tdsn) 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.
Related articles
Content by Label | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...