Skip to content

Commit

Permalink
Fix memory source documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorkertesz committed Sep 18, 2024
1 parent 60cfad3 commit 60a7424
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
27 changes: 21 additions & 6 deletions docs/guide/sources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,37 @@ memory
.. py:function:: from_source("memory", buffer)
:noindex:

The ``memory`` source will read data from a memory buffer. Currently it only works for a ``buffer`` storing a single :ref:`grib` message or CoverageJson data.
The ``memory`` source will read data from a memory buffer. Currently it only works for a ``buffer`` storing a single :ref:`grib` message or CoverageJson data. The result is a FieldList object storing all the data in memory.

Please note that a buffer can always be read as a :ref:`stream source <data-sources-stream>` using ``io.BytesIO``.
.. code-block:: python
import earthkit.data
# buffer storing a GRIB message
buffer = ...
ds = earthkit.data.from_source("memory", bufr)
# f is the only GribField in ds
f = ds[0]
Please note that a buffer can always be read as a :ref:`stream source <data-sources-stream>` using ``io.BytesIO``. The equivalent code to the example above using a stream is:

.. code-block:: python
import io
import earthkit.data
# buffer stores GRIB messages
# buffer storing a GRIB message
buffer = ...
stream = io.BytesIO(buffer)
ds = earthkit.data.from_source("stream", stream)
for f in ds:
print(f.metadata("param"))
ds = earthkit.data.from_source("stream", stream, real_all=True)
# f is the only GribField in ds
f = ds[0]
.. _data-sources-multi:
Expand Down
1 change: 1 addition & 0 deletions tests/grib/test_grib_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_grib_from_memory():
assert len(fs) == 1
sn = fs.metadata("param")
assert sn == ["2t"]
assert fs[0].metadata("shortName") == "2t"


def test_grib_save_when_loaded_from_memory():
Expand Down

0 comments on commit 60a7424

Please sign in to comment.