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 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
)
INPUT = sys.argv[1] 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 += 1 # Get the relevant keys from the GTS bulletin tt = codes_get(gts, 'TT') aa = codes_get(gts, 'AA') ii = codes_get(gts, 'II') codes_release(gts) fin.seek(offset_saved) # Rewind # Now read the BUFR message msgid = codes_new_from_file(fin, CODES_PRODUCT_BUFR) if msgid is None: break ns = codes_get(msgid, 'numberOfSubsets') td = codes_get(msgid, 'typicalDate') print(tt, aa, ii, ns, td) codes_release(msgid)
Each BUFR message is enclosed in the GTS header/footer so after reading the first GTS message, the file pointer has moved on after the enclosed BUFR so we need to rewind it before reading the next GTS message.
Note: Sometimes a GTS envelope may include multiple GRIB/BUFR messages