From bc2f0d23a8b90514c3616c1c8c288aa7ea9cfd63 Mon Sep 17 00:00:00 2001 From: Marie Spitoni Date: Tue, 26 Nov 2024 14:12:56 +0100 Subject: [PATCH] Make start positioners more generic --- src/PyMca5/PyMcaCore/NexusTools.py | 31 +++++++++++++++++++++++- src/PyMca5/PyMcaGui/io/hdf5/NexusInfo.py | 13 ++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/PyMca5/PyMcaCore/NexusTools.py b/src/PyMca5/PyMcaCore/NexusTools.py index 96b46e830..8d2e0c584 100644 --- a/src/PyMca5/PyMcaCore/NexusTools.py +++ b/src/PyMca5/PyMcaCore/NexusTools.py @@ -515,7 +515,11 @@ def getStartingPositionersGroup(h5file, path): Retrieve the start positioners group associated to a path retrieving them from the same entry. - This method assumes the positioner group is NXentry/NXinstrument/positioners_start. + It assumes they are either in: + + - NXentry/NXinstrument/positioners_start or + - NXentry/NXinstrument/positioners or + - NXentry/measurement/pre_scan_snapshot """ entry_path = getEntryName(path, h5file=h5file) @@ -528,8 +532,33 @@ def getStartingPositionersGroup(h5file, path): positioners = instrument[key] if not isGroup(positioners): positioners = None + if positioners is None: + positioners = getPositionersGroup(h5file, path) return positioners +def getStartingPositionerValues(h5file, path): + """ + Retrieve the start positioners names, values and units associated to a path + retrieving them from the same entry. + + It assumes they are either in: + + - NXentry/NXinstrument/positioners_start or + - NXentry/NXinstrument/positioners or + - NXentry/measurement/pre_scan_snapshot + + """ + nxpositioners = getStartingPositionersGroup(h5file, path) + positions = list() + if nxpositioners is None: + return positions + for name, dset in nxpositioners.items(): + if not isinstance(dset, h5py.Dataset): + continue + idx = (0,) * dset.ndim + positions.append((name, dset[idx], dset.attrs.get("units", ""))) + return positions + def getMeasurementGroup(h5file, path): """ Retrieve the measurement group associated to a path diff --git a/src/PyMca5/PyMcaGui/io/hdf5/NexusInfo.py b/src/PyMca5/PyMcaGui/io/hdf5/NexusInfo.py index 53356cf9e..65d0d5ab9 100644 --- a/src/PyMca5/PyMcaGui/io/hdf5/NexusInfo.py +++ b/src/PyMca5/PyMcaGui/io/hdf5/NexusInfo.py @@ -1,7 +1,7 @@ import h5py from PyMca5.PyMcaGui import PyMcaQt as qt -from PyMca5.PyMcaCore.NexusTools import getStartingPositionersGroup +from PyMca5.PyMcaCore.NexusTools import getStartingPositionerValues from . import HDF5Info @@ -96,15 +96,6 @@ def get_motor_positions(hdf5File, node): if not isinstance(nxentry, h5py.Group): return dict() - nxpositioners = getStartingPositionersGroup(hdf5File, nxentry_name) - if nxpositioners is None or not isinstance(nxpositioners, h5py.Group): - return dict() - - positions = [ - (name, dset[()], dset.attrs.get("units", "")) - for name, dset in nxpositioners.items() - if isinstance(dset, h5py.Dataset) and dset.ndim == 0 - ] + positions = getStartingPositionerValues(hdf5File, nxentry_name) column_names = "Name", "Value", "Units" - return dict(zip(column_names, zip(*positions)))