jswhit / pygrib

Python interface for reading and writing GRIB data
https://jswhit.github.io/pygrib
MIT License
322 stars 97 forks source link

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

Open FlippingBinary opened 4 years ago

FlippingBinary commented 4 years ago

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.

jswhit commented 3 years ago

A pull request would be welcomed.

BluesCrossing commented 1 year ago

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.

grahmatt commented 6 months ago

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)