NOAA-MDL / grib2io

Python interface to the NCEP G2C Library for reading and writing GRIB2 messages.

Home Page:https://noaa-mdl.github.io/grib2io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

grib2io equivalent to pygrib's fromstring() and tostring()

alexkorner opened this issue · comments

Include a clear description and an example or even a scholarly source of information about how the calculation, plot, etc. works
I'm looking to manipulate time metadata within a grib2 file without creating intermediate files in the process. In this example, gzipped files would be coming from /lfs/h1/ops/prod/dcom/ldmdata/obs/upperair/mrms/conus/MergedReflectivityQC/ on WCOSS2. Pygrib would handle similar to this:

with gzip.open(filename, 'rb') as infile:
file_content = infile.read()

    ##pygrib solution##
    grib_message = pygrib.fromstring(file_content)
    grib_message['second'] = 0

   ##reset valid date value after changing seconds
   pygrib.setdates(grib_message)
   file_content = grib_message.tostring()

Wondering if there is a grib2io equivalent. Thanks.

I have added support to read Gzipped GRIB2 files. An equivalent workflow in grib2io would be the following:

#!/usr/bin/env python

import grib2io

output_grib2 = grib2io.open("output.grib2",mode="w")

with grib2io.open(<input_grib2_file>) as g:
    for msg in g:
        msg.second = 0
        msg.pack()
        output_grib2.write(msg)

output_grib2.close()