summaryrefslogtreecommitdiff
path: root/src/silx/io/fioh5.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/silx/io/fioh5.py')
-rw-r--r--src/silx/io/fioh5.py200
1 files changed, 116 insertions, 84 deletions
diff --git a/src/silx/io/fioh5.py b/src/silx/io/fioh5.py
index 0a86bbf..a88d35b 100644
--- a/src/silx/io/fioh5.py
+++ b/src/silx/io/fioh5.py
@@ -154,15 +154,17 @@ logger1 = logging.getLogger(__name__)
if h5py.version.version_tuple[0] < 3:
text_dtype = h5py.special_dtype(vlen=str) # old API
else:
- text_dtype = 'O' # variable-length string (supported as of h5py > 3.0)
+ text_dtype = "O" # variable-length string (supported as of h5py > 3.0)
ABORTLINENO = 5
-dtypeConverter = {'STRING': text_dtype,
- 'DOUBLE': 'f8',
- 'FLOAT': 'f4',
- 'INTEGER': 'i8',
- 'BOOLEAN': '?'}
+dtypeConverter = {
+ "STRING": text_dtype,
+ "DOUBLE": "f8",
+ "FLOAT": "f4",
+ "INTEGER": "i8",
+ "BOOLEAN": "?",
+}
def is_fiofile(filename):
@@ -192,56 +194,51 @@ def is_fiofile(filename):
class FioFile(object):
- """This class opens a FIO file and reads the data.
-
- """
+ """This class opens a FIO file and reads the data."""
def __init__(self, filepath):
# parse filename
filename = os.path.basename(filepath)
- fnowithsuffix = filename.split('_')[-1]
+ fnowithsuffix = filename.split("_")[-1]
try:
- self.scanno = int(fnowithsuffix.split('.')[0])
+ self.scanno = int(fnowithsuffix.split(".")[0])
except Exception:
self.scanno = None
logger1.warning("Cannot parse scan number of file %s", filename)
- with open(filepath, 'r') as fiof:
-
+ with open(filepath, "r") as fiof:
prev = 0
line_counter = 0
- while(True):
+ while True:
line = fiof.readline()
- if line.startswith('!'): # skip comments
+ if line.startswith("!"): # skip comments
prev = fiof.tell()
line_counter = 0
continue
- if line.startswith('%c'): # comment section
+ if line.startswith("%c"): # comment section
line_counter = 0
- self.commentsection = ''
+ self.commentsection = ""
line = fiof.readline()
- while(not line.startswith('%')
- and not line.startswith('!')):
+ while not line.startswith("%") and not line.startswith("!"):
self.commentsection += line
prev = fiof.tell()
line = fiof.readline()
- if line.startswith('%p'): # parameter section
+ if line.startswith("%p"): # parameter section
line_counter = 0
- self.parameterssection = ''
+ self.parameterssection = ""
line = fiof.readline()
- while(not line.startswith('%')
- and not line.startswith('!')):
+ while not line.startswith("%") and not line.startswith("!"):
self.parameterssection += line
prev = fiof.tell()
line = fiof.readline()
- if line.startswith('%d'): # data type definitions
+ if line.startswith("%d"): # data type definitions
line_counter = 0
self.datacols = []
self.names = []
self.dtypes = []
line = fiof.readline()
- while(line.startswith(' Col')):
+ while line.startswith(" Col"):
splitline = line.split()
name = splitline[-2]
self.names.append(name)
@@ -255,13 +252,16 @@ class FioFile(object):
line_counter += 1
if line_counter > ABORTLINENO:
- raise IOError("Invalid fio file: Found no data "
- "after %s lines" % ABORTLINENO)
+ raise IOError(
+ "Invalid fio file: Found no data "
+ "after %s lines" % ABORTLINENO
+ )
- self.data = numpy.loadtxt(fiof,
- dtype={'names': tuple(self.names),
- 'formats': tuple(self.dtypes)},
- comments="!")
+ self.data = numpy.loadtxt(
+ fiof,
+ dtype={"names": tuple(self.names), "formats": tuple(self.dtypes)},
+ comments="!",
+ )
# ToDo: read only last line of file,
# which sometimes contains the end of acquisition timestamp.
@@ -271,7 +271,7 @@ class FioFile(object):
# parse parameter section:
try:
for line in self.parameterssection.splitlines():
- param, value = line.split(' = ')
+ param, value = line.split(" = ")
self.parameter[param] = value
except Exception:
logger1.warning("Cannot parse parameter section")
@@ -288,7 +288,7 @@ class FioFile(object):
raise Exception("acquisition str not found")
self.user = l2[:acqpos][4:].strip()
- self.start_time = l2[acqpos+len(acquiMarker):].strip()
+ self.start_time = l2[acqpos + len(acquiMarker) :].strip()
commentlines = commentlines[2:]
self.comments = "\n".join(commentlines[2:])
@@ -324,15 +324,13 @@ class FioH5NodeDataset(commonh5.Dataset):
data_kind = array.dtype.kind
if data_kind in ["S", "U"]:
- value = numpy.asarray(array,
- dtype=text_dtype)
+ value = numpy.asarray(array, dtype=text_dtype)
else:
value = array # numerical data is already the correct datatype
commonh5.Dataset.__init__(self, name, value, parent, attrs)
def __getattr__(self, item):
- """Proxy to underlying numpy array methods.
- """
+ """Proxy to underlying numpy array methods."""
if hasattr(self[()], item):
return getattr(self[()], item)
@@ -363,11 +361,12 @@ class FioH5(commonh5.File):
except Exception as e:
raise IOError("FIO file %s cannot be read.") from e
- attrs = {"NX_class": to_h5py_utf8("NXroot"),
- "file_time": to_h5py_utf8(
- datetime.datetime.now().isoformat()),
- "file_name": to_h5py_utf8(filename),
- "creator": to_h5py_utf8("silx fioh5 %s" % silx_version)}
+ attrs = {
+ "NX_class": to_h5py_utf8("NXroot"),
+ "file_time": to_h5py_utf8(datetime.datetime.now().isoformat()),
+ "file_name": to_h5py_utf8(filename),
+ "creator": to_h5py_utf8("silx fioh5 %s" % silx_version),
+ }
commonh5.File.__init__(self, filename, attrs=attrs)
if fiof.scanno is not None:
@@ -387,33 +386,40 @@ class FioScanGroup(commonh5.Group):
:param str scan_key: Scan key (e.g. "1.1")
:param scan: FioFile object
"""
- if hasattr(scan, 'user'):
+ if hasattr(scan, "user"):
userattr = to_h5py_utf8(scan.user)
else:
- userattr = to_h5py_utf8('')
- commonh5.Group.__init__(self, scan_key, parent=parent,
- attrs={"NX_class": to_h5py_utf8("NXentry"),
- "user": userattr})
+ userattr = to_h5py_utf8("")
+ commonh5.Group.__init__(
+ self,
+ scan_key,
+ parent=parent,
+ attrs={"NX_class": to_h5py_utf8("NXentry"), "user": userattr},
+ )
# 'title', 'start_time' and 'user' are defaults
# in Sardana created files:
- if hasattr(scan, 'title'):
+ if hasattr(scan, "title"):
title = scan.title
else:
title = scan_key # use scan number as default title
- self.add_node(FioH5NodeDataset(name="title",
- data=to_h5py_utf8(title),
- parent=self))
+ self.add_node(
+ FioH5NodeDataset(name="title", data=to_h5py_utf8(title), parent=self)
+ )
- if hasattr(scan, 'start_time'):
+ if hasattr(scan, "start_time"):
start_time = scan.start_time
- self.add_node(FioH5NodeDataset(name="start_time",
- data=to_h5py_utf8(start_time),
- parent=self))
-
- self.add_node(FioH5NodeDataset(name="comments",
- data=to_h5py_utf8(scan.comments),
- parent=self))
+ self.add_node(
+ FioH5NodeDataset(
+ name="start_time", data=to_h5py_utf8(start_time), parent=self
+ )
+ )
+
+ self.add_node(
+ FioH5NodeDataset(
+ name="comments", data=to_h5py_utf8(scan.comments), parent=self
+ )
+ )
self.add_node(FioInstrumentGroup(parent=self, scan=scan))
self.add_node(FioMeasurementGroup(parent=self, scan=scan))
@@ -426,14 +432,18 @@ class FioMeasurementGroup(commonh5.Group):
:param parent: parent Group
:param scan: FioFile object
"""
- commonh5.Group.__init__(self, name="measurement", parent=parent,
- attrs={"NX_class": to_h5py_utf8("NXcollection")})
+ commonh5.Group.__init__(
+ self,
+ name="measurement",
+ parent=parent,
+ attrs={"NX_class": to_h5py_utf8("NXcollection")},
+ )
for label in scan.names:
safe_label = label.replace("/", "%")
- self.add_node(FioH5NodeDataset(name=safe_label,
- data=scan.data[label],
- parent=self))
+ self.add_node(
+ FioH5NodeDataset(name=safe_label, data=scan.data[label], parent=self)
+ )
class FioInstrumentGroup(commonh5.Group):
@@ -443,14 +453,20 @@ class FioInstrumentGroup(commonh5.Group):
:param parent: parent Group
:param scan: FioFile object
"""
- commonh5.Group.__init__(self, name="instrument", parent=parent,
- attrs={"NX_class": to_h5py_utf8("NXinstrument")})
+ commonh5.Group.__init__(
+ self,
+ name="instrument",
+ parent=parent,
+ attrs={"NX_class": to_h5py_utf8("NXinstrument")},
+ )
self.add_node(FioParameterGroup(parent=self, scan=scan))
self.add_node(FioFileGroup(parent=self, scan=scan))
- self.add_node(FioH5NodeDataset(name="comment",
- data=to_h5py_utf8(scan.comments),
- parent=self))
+ self.add_node(
+ FioH5NodeDataset(
+ name="comment", data=to_h5py_utf8(scan.comments), parent=self
+ )
+ )
class FioFileGroup(commonh5.Group):
@@ -460,16 +476,24 @@ class FioFileGroup(commonh5.Group):
:param parent: parent Group
:param scan: FioFile object
"""
- commonh5.Group.__init__(self, name="fiofile", parent=parent,
- attrs={"NX_class": to_h5py_utf8("NXcollection")})
-
- self.add_node(FioH5NodeDataset(name="comments",
- data=to_h5py_utf8(scan.commentsection),
- parent=self))
-
- self.add_node(FioH5NodeDataset(name="parameter",
- data=to_h5py_utf8(scan.parameterssection),
- parent=self))
+ commonh5.Group.__init__(
+ self,
+ name="fiofile",
+ parent=parent,
+ attrs={"NX_class": to_h5py_utf8("NXcollection")},
+ )
+
+ self.add_node(
+ FioH5NodeDataset(
+ name="comments", data=to_h5py_utf8(scan.commentsection), parent=self
+ )
+ )
+
+ self.add_node(
+ FioH5NodeDataset(
+ name="parameter", data=to_h5py_utf8(scan.parameterssection), parent=self
+ )
+ )
class FioParameterGroup(commonh5.Group):
@@ -479,11 +503,19 @@ class FioParameterGroup(commonh5.Group):
:param parent: parent Group
:param scan: FioFile object
"""
- commonh5.Group.__init__(self, name="parameter", parent=parent,
- attrs={"NX_class": to_h5py_utf8("NXcollection")})
+ commonh5.Group.__init__(
+ self,
+ name="parameter",
+ parent=parent,
+ attrs={"NX_class": to_h5py_utf8("NXcollection")},
+ )
for label in scan.parameter:
safe_label = label.replace("/", "%")
- self.add_node(FioH5NodeDataset(name=safe_label,
- data=to_h5py_utf8(scan.parameter[label]),
- parent=self))
+ self.add_node(
+ FioH5NodeDataset(
+ name=safe_label,
+ data=to_h5py_utf8(scan.parameter[label]),
+ parent=self,
+ )
+ )