ecmwf / eccodes-python

Python interface to the ecCodes GRIB/BUFR decoder/encoder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

read data from bytes object

tomkralidis opened this issue · comments

Our application uses the ecCodes Python bindings to process streams of BUFR data as part of real-time processing and publication of same.

Are there any examples of reading data from a bytes object? Currently we are having to manage a temporary file in order to process.

cc @david-i-berry @webb-ben

Example:

from io import BytesIO
from eccodes import eccodes_bufr_new_from_file

# example using data in https://github.com/wmo-im/bufr2geojson/blob/main/tests/A_ISIA21EIDB202100_C_EDZW_20220320210902_11839953.bin

with open('A_ISIA21EIDB202100_C_EDZW_20220320210902_11839953.bin', 'rb') as fh: 
    bytes_io = BytesIO(fh.read())
    bufr_handle = eccodes.codes_bufr_new_from_file(bytes_io)

Result:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    bufr_handle = eccodes.codes_bufr_new_from_file(bytes_io)
  File "/Users/tomkralidis/Dev/wmo/lib/python3.7/site-packages/gribapi/gribapi.py", line 367, in bufr_new_from_file
    ffi.NULL, fileobj, CODES_PRODUCT_BUFR
  File "/Users/tomkralidis/Dev/wmo/lib/python3.7/site-packages/gribapi/gribapi.py", line 158, in wrapper
    retval = func(*args)
io.UnsupportedOperation: fileno

cc @shahramn

Feel free to submit a Pull Request