From d8c2c325ce327a25586a9d531393879107628598 Mon Sep 17 00:00:00 2001 From: stevehenke <91344068+stevehenke@users.noreply.github.com> Date: Thu, 21 Dec 2023 13:27:34 -0600 Subject: [PATCH] Implement NanoMax DiffractionEndStation File Readers (#68) --- ptychodus/plugins/h5DiffractionFile.py | 16 ++++++--- ptychodus/plugins/lynxDiffractionFile.py | 2 +- ptychodus/plugins/nanoMaxScanFile.py | 41 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 ptychodus/plugins/nanoMaxScanFile.py diff --git a/ptychodus/plugins/h5DiffractionFile.py b/ptychodus/plugins/h5DiffractionFile.py index 8e66c8d4..1f9b5a4e 100644 --- a/ptychodus/plugins/h5DiffractionFile.py +++ b/ptychodus/plugins/h5DiffractionFile.py @@ -58,9 +58,12 @@ class H5DiffractionFileTreeBuilder: def _addAttributes(self, treeNode: SimpleTreeNode, attributeManager: h5py.AttributeManager) -> None: for name, value in attributeManager.items(): - stringInfo = h5py.check_string_dtype(value.dtype) - itemDetails = f'STRING = "{value.decode(stringInfo.encoding)}"' if stringInfo \ - else f'SCALAR {value.dtype} = {value}' + if isinstance(value, str): + itemDetails = f'STRING = "{value}"' + else: + stringInfo = h5py.check_string_dtype(value.dtype) + itemDetails = f'STRING = "{value.decode(stringInfo.encoding)}"' if stringInfo \ + else f'SCALAR {value.dtype} = {value}' treeNode.createChild([str(name), 'Attribute', itemDetails]) @@ -171,5 +174,10 @@ def registerPlugins(registry: PluginRegistry) -> None: registry.diffractionFileReaders.registerPlugin( H5DiffractionFileReader(dataPath='/dp'), simpleName='PtychoShelves', - displayName='PtychoShelves Diffraction Data Files (*.h5 *.hdf5)', + displayName='PtychoShelves Diffraction Files (*.h5 *.hdf5)', + ) + registry.diffractionFileReaders.registerPlugin( + H5DiffractionFileReader(dataPath='/entry/measurement/Eiger/data'), + simpleName='NanoMax', + displayName='NanoMax DiffractionEndStation Files (*.h5 *.hdf5)', ) diff --git a/ptychodus/plugins/lynxDiffractionFile.py b/ptychodus/plugins/lynxDiffractionFile.py index 52eb29bb..71961d0b 100644 --- a/ptychodus/plugins/lynxDiffractionFile.py +++ b/ptychodus/plugins/lynxDiffractionFile.py @@ -61,5 +61,5 @@ def registerPlugins(registry: PluginRegistry) -> None: registry.diffractionFileReaders.registerPlugin( LYNXDiffractionFileReader(), simpleName='LYNX', - displayName='LYNX Diffraction Data Files (*.h5 *.hdf5)', + displayName='LYNX Diffraction Files (*.h5 *.hdf5)', ) diff --git a/ptychodus/plugins/nanoMaxScanFile.py b/ptychodus/plugins/nanoMaxScanFile.py new file mode 100644 index 00000000..e67ae799 --- /dev/null +++ b/ptychodus/plugins/nanoMaxScanFile.py @@ -0,0 +1,41 @@ +from pathlib import Path +from typing import Final +import logging + +import h5py + +from ptychodus.api.plugins import PluginRegistry +from ptychodus.api.scan import Scan, ScanFileReader, ScanPoint, TabularScan + +logger = logging.getLogger(__name__) + + +class NanoMaxScanFileReader(ScanFileReader): + MICRONS_TO_METERS: Final[float] = 1.e-6 + + def read(self, filePath: Path) -> Scan: + pointList = list() + + with h5py.File(filePath, 'r') as h5File: + try: + xArray = h5File['/entry/measurement/pseudo/x'][()] + yArray = h5File['/entry/measurement/pseudo/y'][()] + except KeyError: + logger.exception('Unable to load scan.') + else: + for x, y in zip(xArray, yArray): + point = ScanPoint( + x=x * self.MICRONS_TO_METERS, + y=y * self.MICRONS_TO_METERS, + ) + pointList.append(point) + + return TabularScan.createFromPointIterable(pointList) + + +def registerPlugins(registry: PluginRegistry) -> None: + registry.scanFileReaders.registerPlugin( + NanoMaxScanFileReader(), + simpleName='NanoMax', + displayName='NanoMax DiffractionEndStation Scan Files (*.h5 *.hdf5)', + )