pygrib icon indicating copy to clipboard operation
pygrib copied to clipboard

Request for feature: create a GRIB message iterator from a bytes object representing the contents of a GRIB file

Open FlippingBinary opened this issue 4 years ago • 3 comments

Normally, a GRIB file can be opened with pygrib.open(filename) and this works fine. It would be nice to be able to download a GRIB file and parse it with pygrib without first saving it to disk. The pygrib.fromstring() function seems to do something similar, but it does not create a GRIB message iterator. It only handles a single GRIB message.

FlippingBinary avatar May 11 '20 20:05 FlippingBinary

A pull request would be welcomed.

jswhit avatar Sep 05 '20 03:09 jswhit

Normally, a GRIB file can be opened with pygrib.open(filename) and this works fine. It would be nice to be able to download a GRIB file and parse it with pygrib without first saving it to disk. The pygrib.fromstring() function seems to do something similar, but it does not create a GRIB message iterator. It only handles a single GRIB message.

@FlippingBinary Hello, I recently met the same problem, and I've found a possible solution. As one GRIB message starts with b'GRIB' and ends with b'7777', it is possible to split the bytes into several bytes sequences by finding all the b'7777GRIB' in the bytes object, then using pygrib.fromstring() to decode all the sequences and put all of them in one list, this may be a substitutive solution. Hope this may help you.

BluesCrossing avatar Jan 19 '23 10:01 BluesCrossing

To save others time, here's an example of loading the first GRIB message from a GRIB byte string. data contains multiple GRIB messages in a byte string.

index = data.find(b"7777GRIB")
first_message = data[: index + 4]
grb_message = pygrib.fromstring(first_message)

grahmatt avatar Jan 11 '24 20:01 grahmatt