How can a GRIB file be opened with pygrib without first downloading the file?

Viewed 1443

The documentation for pygrib shows a function called fromstring which creates a gribmessage instance from a python bytes object representing a binary grib message. I might be misunderstanding the purpose of this function, but it leads me to believe I can use it in place of downloading a GRIB file and using the open function on it. Unfortunately, my attempts to open a multi-message GRIB file from NLDAS2 have failed. Does anyone else know how to use pygrib on GRIB data without first saving the file? My code below shows how I would like it to work. Instead, it gives the error TypeError: expected bytes, int found on the line for grib in gribs:

from urllib import request
import pygrib

url = "<remote address of desired file>"
username = "<username>"
password = "<password>"

redirectHandler = request.HTTPRedirectHandler()
cookieProcessor = request.HTTPCookieProcessor()
passwordManager = request.HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, "https://urls.earthdata.nasa.gov", username, password)
authHandler = request.HTTPBasicAuthHandler(passwordManager)
opener = request.build_opener(redirectHandler, cookieProcessor, authHandler)
request.install_opener(opener)

with request.urlopen(url) as response:
    data = response.read()
    gribs = pygrib.fromstring(data)
    for grib in gribs:
        print(grib)

Edit to add the entire error output:

Traceback (most recent call last):
  File ".\example.py", line 19, in <module>
    for grb in grbs:
  File "pygrib.pyx", line 1194, in pygrib.gribmessage.__getitem__
TypeError: expected bytes, int found

Edit: This interface does not support multi-message GRIB files, but the authors are open to a pull request if anyone wants to write up the code. Unfortunately, my research focus has shifted and I don't have time to contribute myself.

1 Answers

As stated by jasonharper you can use pygrib.fromstring(). I just tried it myself and this works.

Here is the link to the documentation.

Related