Versions Compared

Key

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

...

Tip

Remember to load the ecmwf-toolbox and python3 !

No Format
$> module load ecmwf-toolbox
$> module load python3


This tutorial covers the following topics:

...

Expand
titleClick here to see solution...

The part of the script to change is in the example function, specifically line 47 should be commented (add a '#' to the start of the line containing "codes_dump(gid)") and replaced with calls to codes_get for each key value needed:

Code Block
languagepy
firstline32
linenumberstrue
def example():
    f = open(INPUT,'rb')

    count = 1
    while 1:
        gid = codes_grib_new_from_file(f)
        if gid is None: break
        print("\n\n-- GRIB %d --" % count)

        #   codes_dump should only be used for diagnostic purposes.
        #
        #   To process the data and grib headers, you will have to use
        #   codes_get and request the decoding of the keys you need,
        #   e.g. the date, the time, the parameter, the "level".

        codes_dump(gid)

        # Replace this call to codes_dump with calls to codes_get
        # for each key/value pair needed, e.g.:
        #
        #   date = codes _get(gid,'date')

        codes_release(gid)
        count += 1

    f.close()


A possible solution is:

Code Block
languagepy
firstline32
linenumberstrue
def example():
    f = open(INPUT,'rb')

    count = 1 
    while 1:
        gid = codes_grib_new_from_file(f)
        if gid is None: break
        print ("\n\n-- GRIB %d --" % (count))
        # codes_dump should only be used for diagnostic purposes.
        #   
        #   To process the data and grib headers, you will have to use 
        #   codes_get and request the decoding of the keys you need,
        #   e.g. the date, the time, the parameter, the "level".
        #   

        # codes_dump(gid)

        #   
        # Replace this call to codes_dump with calls to codes_get
        # for each key/value pair needed, e.g.:
        #    
        #   idate = codes_get(gid,'date')

        edition = codes_get(gid,'edition')
        shortName = codes_get(gid,'shortName')
        date = codes_get(gid,'date')
        time = codes_get(gid,'time')
        levType = codes_get(gid,'typeOfLevel')
        level = codes_get(gid,'level')

        print("Edition=%d Parameter=%s typeOfLevel=%s level=%d date=%d time=%d\n" % 
              (edition, shortName, levType, level, date, time))
        #print (edition, shortName, levType, level, date, time)

        values = codes_get_array(gid,'values')

        print("First 20 values:\n----------------\n")
        for i in range(20):
            print("%.4f" % (values[i]))

        maximum = codes_get(gid,'max')
        minimum = codes_get(gid,'min')
        average = codes_get(gid,'average')

        print("max = %.4f  min = %.4f  average = %.4f\n" % (maximum, minimum, average))

        codes_release(gid)
        count += 1

    f.close()


This example solution goes a little further than needed and also decodes and prints the first 20 data values for each message as well as the maximum, minimum and average of all the data values.

...