Versions Compared

Key

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

...

Read the GTS bulletin keys as well as GRIB/BUFR messages

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):

...

  1. 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)

    Code Block
    languagepy
    firstline1
    linenumberstrue
    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.

  2. Note: Sometimes a GTS envelope may include multiple GRIB/BUFR messages


Content by Label
showLabelsfalse
max5
spaces~usa
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel = "kb-how-to-article" and type = "page" and space = "~usa"
labelskb-how-to-article

...