Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Excerpt
hiddentrue

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. When we come to the GRIB messages we access the dictionary to get the relevant GTS keys that wrap our GRIB message.

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
languagepy
firstline1
linenumberstrue
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  ns+= 1
        sn = codes_get(msgid, 'numberOfSubsets')shortName')
        idict = mydict[cnt] # Access GTS values
        tt = idict['TT']
        tdaa = codes_get(msgid, 'typicalDate') idict['AA']
        ii = idict['II']
        print(tt, aa, ii, ns, tdsn)
        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. The example above assumes that for each GTS wrapper there is exactly one GRIB/BUFR message.


Content by Label
showLabelsfalse
max5
spaces~usa
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel in ("kb-how-to-article","eccodes-faqs","bufr","grib","gts") and type = "page" and space = "UDOC"
labelskb-how-to-article

...