Commit f4f57ab4 authored by payno's avatar payno
Browse files

[hdf5scan] add x_pixel_size and y_pixel_size property

- add util _get_value which try to read from 'unit' or 'units' attribute the unit of the given value.
parent a8194826
......@@ -38,6 +38,7 @@ from silx.io.url import DataUrl
from silx.utils.enum import Enum as _Enum
from tomoscan.utils import docstring
from silx.io.utils import get_data
from ..unitsystem import metricsystem
import logging
import typing
......@@ -83,6 +84,10 @@ class HDF5TomoScan(TomoScanBase):
_IMG_KEY_PATH = 'instrument/detector/image_key'
_X_PIXEL_SIZE_PATH = 'instrument/detector/x_pixel_size'
_Y_PIXEL_SIZE_PATH = 'instrument/detector/y_pixel_size'
_SCHEME = 'silx'
_EPSILON_ROT_ANGLE = 0.02
......@@ -127,7 +132,8 @@ class HDF5TomoScan(TomoScanBase):
# scan range, in degree
self._dim_1, self._dim_2 = None, None
# image dimensions
self._pixel_size = None
self._x_pixel_size = None
self._y_pixel_size = None
# pixel dimensions (tuple)
self._frames = None
self._image_keys = None
......@@ -143,7 +149,8 @@ class HDF5TomoScan(TomoScanBase):
self._ref_n = None
self._scan_range = None
self._dim_1, self._dim_2 = None, None
self._pixel_size = None
self._x_pixel_size = None
self._y_pixel_size = None
self._rotation_angles = None
@staticmethod
......@@ -402,11 +409,29 @@ class HDF5TomoScan(TomoScanBase):
@property
def pixel_size(self):
if self._pixel_size is None and self.master_file and os.path.exists(self.master_file):
with h5py.File(self.master_file, 'r') as h5_file:
assert self._DET_META_PATH in h5_file[self._entry]
self._pixel_size = h5_file[self._entry][self._DET_META_PATH]['pixel_size'][()]
return self._pixel_size
return self.x_pixel_size
@property
def x_pixel_size(self):
if self._x_pixel_size is None and self.master_file and os.path.exists(self.master_file):
self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
return self._x_pixel_size
def _get_x_y_pixel_values(self):
"""read x and y pixel values"""
with h5py.File(self.master_file, 'r') as h5_file:
x_pixel_dataset = h5_file[self._entry][self._X_PIXEL_SIZE_PATH]
_x_pixel_size = self._get_value(x_pixel_dataset, default_unit='mm')
y_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_SIZE_PATH]
_y_pixel_size = self._get_value(y_pixel_dataset, default_unit='mm')
return _x_pixel_size, _y_pixel_size
@property
def y_pixel_size(self):
if self._y_pixel_size is None and self.master_file and os.path.exists(self.master_file):
self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
return self._y_pixel_size
@property
def frames(self) -> tuple:
......@@ -474,6 +499,21 @@ class HDF5TomoScan(TomoScanBase):
self.master_file,
self.entry)
@staticmethod
def _get_value(node: h5py.Group, default_unit: str):
"""convert the value contained in the node to the adapted unit.
Unit can be defined in on of the group attributes. It it is the case
will pick this unit, otherwise will use the default unit
"""
value = node[()]
if 'unit' in node.attrs:
unit = node.attrs['unit']
if 'units' in node.attrs:
unit = node.attrs['units']
else:
unit = default_unit
return value * metricsystem.MetricSystem.from_value(unit).value
class Frame:
"""class to store all metadata information of a frame"""
......
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