diff --git a/docs/api.rst b/docs/api.rst index 0ef7377d..ebad1096 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -86,6 +86,21 @@ write the modified message to a new GRIB file: >>> pygrib.open('test.grb').readline() 1:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 240 hrs:from 201001011200 +you can also open an in-memory binary stream instead of on-disk file +by creating an io.BufferedReader instance from the binary string: + + >>> import requests, io + >>> fileurl = ("https://www.ncei.noaa.gov/data/" + ... "ncep-global-data-assimilation/access/historical/" + ... "201910/20191002/gdas.t00z.pgrb2.1p00.anl") + >>> response = requests.get(fileurl) + >>> buffer_io = io.BufferedReader(io.BytesIO(response.content)) + >>> grbs = pygrib.open(buffer_io) + >>> grb = grbs.select(shortName='gh',level=500)[0] + >>> print(grb) + 181:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 50000 Pa:fcst time 0 hrs:from 201910020000 + >>> grbs.close() + Module docstrings ----------------- diff --git a/test/test.py b/test/test.py index 26647c38..2ac05f73 100644 --- a/test/test.py +++ b/test/test.py @@ -243,6 +243,19 @@ def test(): >>> str(grb.packingType) 'grid_simple' >>> grbs.close() + + read binary stream from memory instead of a disk file + >>> import requests, io + >>> fileurl = ("https://www.ncei.noaa.gov/data/" + ... "ncep-global-data-assimilation/access/historical/" + ... "201910/20191002/gdas.t00z.pgrb2.1p00.anl") + >>> response = requests.get(fileurl) + >>> buffer_io = io.BufferedReader(io.BytesIO(response.content)) + >>> grbs = pygrib.open(buffer_io) + >>> grb = grbs.select(shortName='gh',level=500)[0] + >>> print(grb) + 181:Geopotential height:gpm (instant):regular_ll:isobaricInhPa:level 50000 Pa:fcst time 0 hrs:from 201910020000 + >>> grbs.close() """ if __name__ == "__main__":