Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using memory-based file-like object (BytesIO) #25

Open
cccassidy opened this issue May 23, 2020 · 3 comments
Open

Using memory-based file-like object (BytesIO) #25

cccassidy opened this issue May 23, 2020 · 3 comments

Comments

@cccassidy
Copy link

Is there a way to load a file-like object from memory? Looking for a way to work around local storage limitations and improve performance by using memory for input/output operations. The following example using a BytesIO object returns an error from the library.

This is with Python 3.6 and ecCodes 2.17.0.

Thanks!

f= open('file.grib','rb')
buffer = io.BytesIO(f.read())
codes_grib_new_from_file(buffer)
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-25-b3e3d2be12aa> in <module>
----> 1 codes_grib_new_from_file(stream)~/.local/lib/python3.6/site-packages/gribapi/gribapi.py in grib_new_from_file(fileobj, headers_only)
    403     # err, h = err_last(lib.grib_new_from_file)(ffi.NULL, fileobj, headers_only)
    404     err, h = err_last(lib.codes_handle_new_from_file)(
--> 405         ffi.NULL, fileobj, CODES_PRODUCT_GRIB
    406     )
    407     if err:~/.local/lib/python3.6/site-packages/gribapi/gribapi.py in wrapper(*args)
    150         err = ffi.new("int *")
    151         args += (err,)
--> 152         retval = func(*args)
    153         return err[0], retval
    154UnsupportedOperation: fileno
@shahramn
Copy link
Collaborator

Dear Christopher,
Please have a look here: https://confluence.ecmwf.int/display/UDOC/How+do+I+decode+messages+from+a+byte+stream+-+ecCodes+FAQ

I hope this helps :)

@cccassidy
Copy link
Author

Dear Christopher,
Please have a look here: https://confluence.ecmwf.int/display/UDOC/How+do+I+decode+messages+from+a+byte+stream+-+ecCodes+FAQ

I hope this helps :)

Hi shahramn,

This is perfect, thank you for passing that along! I have 2 followup questions as a result:

  1. For some of the data I work with I use a multi-part HTTP range request from the server to get the files. As a result in these files there is a text wrapper around each of the ranges according to the spec that requires this. This extra text causes the method above to fail because the data doesn't start with 'GRIB'. When loading these files from disk it successfully skips through these text blobs. Aside from adding a separate function to clean out those extraneous pieces is there a way with the existing library to accomplish this?

  2. I also use the indexing features quite heavily to pick selected messages out of a file. From the documentation it appears that I would have to loop through each of the GRIB messages in the buffer and keep or discard them as needed. Is my understanding on this correct or is there a way to integrate the indexing with this methodology?

Thank you again for the response!

@shahramn
Copy link
Collaborator

There is currently no support for the two cases you mentioned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants