summaryrefslogtreecommitdiff
path: root/silx/io/fabioh5.py
diff options
context:
space:
mode:
Diffstat (limited to 'silx/io/fabioh5.py')
-rw-r--r--silx/io/fabioh5.py57
1 files changed, 42 insertions, 15 deletions
diff --git a/silx/io/fabioh5.py b/silx/io/fabioh5.py
index 0811ae0..27ef3fd 100644
--- a/silx/io/fabioh5.py
+++ b/silx/io/fabioh5.py
@@ -40,16 +40,12 @@ import os
import fabio.file_series
import numpy
+import six
from . import commonh5
-from silx.third_party import six
from silx import version as silx_version
import silx.utils.number
-
-try:
- import h5py
-except ImportError as e:
- h5py = None
+import h5py
_logger = logging.getLogger(__name__)
@@ -197,7 +193,7 @@ class RawHeaderData(commonh5.LazyLoadableDataset):
else:
dtype = numpy.string_
- if dtype == numpy.unicode_ and h5py is not None:
+ if dtype == numpy.unicode_:
# h5py only support vlen unicode
dtype = h5py.special_dtype(vlen=six.text_type)
@@ -262,6 +258,30 @@ class ImageGroup(commonh5.LazyLoadableGroup):
self.add_node(detector)
+class NxDataPreviewGroup(commonh5.LazyLoadableGroup):
+ """Define the NxData group which is used as the default NXdata to show the
+ content of the file.
+ """
+
+ def __init__(self, name, fabio_reader, parent=None):
+ if fabio_reader.is_spectrum():
+ interpretation = "spectrum"
+ else:
+ interpretation = "image"
+ attrs = {
+ "NX_class": "NXdata",
+ "interpretation": interpretation,
+ "signal": "data",
+ }
+ commonh5.LazyLoadableGroup.__init__(self, name, parent, attrs)
+ self.__fabio_reader = fabio_reader
+
+ def _create_child(self):
+ basepath = self.parent.name
+ data = commonh5.SoftLink("data", path=basepath + "/instrument/detector_0/data")
+ self.add_node(data)
+
+
class SampleGroup(commonh5.LazyLoadableGroup):
"""Define the image group (sub group of measurement) using Fabio data.
"""
@@ -383,10 +403,11 @@ class FabioReader(object):
may fail.
"""
if self.__must_be_closed:
- # It looks like there is no close on FabioImage
- # self.__fabio_image.close()
- pass
- self.__fabio_image = None
+ # Make sure the API of fabio provide it a 'close' method
+ # TODO the test can be removed if fabio version >= 0.8
+ if hasattr(self.__fabio_file, "close"):
+ self.__fabio_file.close()
+ self.__fabio_file = None
def fabio_file(self):
return self.__fabio_file
@@ -917,14 +938,15 @@ class File(commonh5.File):
self.__fabio_reader = self.create_fabio_reader(file_name, fabio_image, file_series)
if fabio_image is not None:
file_name = fabio_image.filename
+ scan = self.create_scan_group(self.__fabio_reader)
attrs = {"NX_class": "NXroot",
"file_time": datetime.datetime.now().isoformat(),
- "creator": "silx %s" % silx_version}
+ "creator": "silx %s" % silx_version,
+ "default": scan.basename}
if file_name is not None:
attrs["file_name"] = file_name
commonh5.File.__init__(self, name=file_name, attrs=attrs)
- scan = self.create_scan_group(self.__fabio_reader)
self.add_node(scan)
def create_scan_group(self, fabio_reader):
@@ -934,8 +956,12 @@ class File(commonh5.File):
:param FabioReader fabio_reader: A reader for the Fabio image
:rtype: commonh5.Group
"""
-
- scan = commonh5.Group("scan_0", attrs={"NX_class": "NXentry"})
+ nxdata = NxDataPreviewGroup("image", fabio_reader)
+ scan_attrs = {
+ "NX_class": "NXentry",
+ "default": nxdata.basename,
+ }
+ scan = commonh5.Group("scan_0", attrs=scan_attrs)
instrument = commonh5.Group("instrument", attrs={"NX_class": "NXinstrument"})
measurement = MeasurementGroup("measurement", fabio_reader, attrs={"NX_class": "NXcollection"})
file_ = commonh5.Group("file", attrs={"NX_class": "NXcollection"})
@@ -949,6 +975,7 @@ class File(commonh5.File):
instrument.add_node(detector)
file_.add_node(raw_header)
scan.add_node(measurement)
+ scan.add_node(nxdata)
if fabio_reader.has_sample_information():
sample = SampleGroup("sample", fabio_reader)