Search code examples
python-3.xpython-2.7gribpython-irisgrib-api

Reading a grib2 message into an Iris cube


I am currently exploring the notion of using iris in a project to read forecast grib2 files using python.

My aim is to load/convert a grib message into an iris cube based on a grib message key having a specific value.

I have experimented with iris-grib, which uses gribapi. Using iris-grib I have not been to find the key in the grib2 file, althrough the key is visible with 'grib_ls -w...' via the cli. gribapi does the job, but I am not sure how to interface it with iris (which is what, I assume, iris-grib is for).

I was wondering if anyone knew of a way to get a message into an iris cube based on a grib message key having a specific value. Thank you


Solution

  • You can get at anything that the gribapi understands through the low-level grib interface in iris-grib, which is the iris_grib.GribMessage class.
    Typically you would use for msg in GribMessage.messages_from_filename(xxx): and then access it like e.g. msg.sections[4]['productDefinitionTemplateNumber']; msg.sections[4]['parameterNumber'] and so on.

    You can use this to identify required messages, and then convert to cubes with iris_grib.load_pairs_from_fields().

    However, Iris-grib only knows how to translate specific encodings into cubes : it is quite strict about exactly what it recognises, and will fail on anything else. So if your data uses any unrecognised templates or data encodings it will definitely fail to load.
    I'm just anticipating that you may have something unusual here, so that might be an issue?
    You can possibly check your expected message contents against the translation code at iris_grib:_load_convert.py, starting at the convert() routine.

    To get an Iris cube out of something not yet supported, you would either :

    • (a) extend the translation rules (i.e. a Github PR), or
    • (b) sometimes you can modify the message so that it looks like something that can be recognised.
      Failing that, you can
    • (c) simply build an Iris cube yourself from the data found in your GribMessage : That can be a little simpler than using 'gribapi' directly (possibly not, depending on detail).

    If you have a problem like that, you should definitely raise it as an issue on the github project (iris-grib issues) + we will try to help.

    P.S. as you have registered a Python3 interest, you may want to be aware that the newer "ecCodes" replacement for gribapi should shortly be available, making Python3 support for grib data possible at last.
    However, the Python3 version is still in beta and we are presently experiencing some problems with it, now raised with ECMWF, so it is still almost-but-not-quite achievable.