Commit 44fb395a authored by payno's avatar payno
Browse files

[io] replace default silx 'get_data' by one using

parent 8dc29be0
......@@ -31,8 +31,110 @@ __date__ = "07/05/2021"
from est import settings
import os
def get_data(url):
"""Returns a numpy data from an URL.
>>> # 1st frame from an EDF using
>>> data ="silx:/users/foo/image.edf::/scan_0/instrument/detector_0/data[0]")
>>> # 1st frame from an EDF using fabio
>>> data ="fabio:/users/foo/image.edf::[0]")
Yet 2 schemes are supported by the function.
- If `silx` scheme is used, the file is opened using
and the data is reach using usually NeXus paths.
- If `fabio` scheme is used, the file is opened using :meth:``
from the FabIO library.
No data path have to be specified, but each frames can be accessed
using the data slicing.
This shortcut of :meth:`` allow to have a faster access to
the data.
.. seealso:: :class:``
:param Union[str,]: A data URL
:rtype: Union[numpy.ndarray, numpy.generic]
:raises ImportError: If the mandatory library to read the file is not
:raises ValueError: If the URL is not valid or do not match the data
:raises IOError: If the file is not found or in case of internal error of
:meth:`` or :meth:``. In this last case more
informations are displayed in debug mode.
if not isinstance(url,
url =
if not url.is_valid():
raise ValueError("URL '%s' is not valid" % url.path())
if not os.path.exists(url.file_path()):
raise IOError("File '%s' not found" % url.file_path())
if url.scheme() == "silx":
data_path = url.data_path()
data_slice = url.data_slice()
with, "r") as h5:
if data_path not in h5:
raise ValueError("Data path from URL '%s' not found" % url.path())
data = h5[data_path]
if not
raise ValueError(
"Data path from URL '%s' is not a dataset" % url.path()
if data_slice is not None:
data =, index=data_slice)
# works for scalar and array
data =
elif url.scheme() == "fabio":
import fabio
data_slice = url.data_slice()
if data_slice is None:
data_slice = (0,)
if data_slice is None or len(data_slice) != 1:
raise ValueError(
"Fabio slice expect a single frame, but %s found" % data_slice
index = data_slice[0]
if not isinstance(index, int):
raise ValueError(
"Fabio slice expect a single integer, but %s found" % data_slice
fabio_file =
except Exception:
raise IOError(
"Error while opening %s with fabio (use debug for more information)"
% url.path()
if fabio_file.nframes == 1:
if index != 0:
raise ValueError(
"Only a single frame available. Slice %s out of range" % index
data =
data = fabio_file.getframe(index).data
# There is no explicit close
fabio_file = None
raise ValueError("Scheme '%s' not supported" % url.scheme())
return data
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment