Skip to content

Commit

Permalink
confirm if units satisfy pint.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Jan 30, 2025
1 parent bf3f4fb commit 91e624d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/pynxtools/dataconverter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)

logger = logging.getLogger("pynxtools") # pylint: disable=C0103

ureg = pint.UnitRegistry()

def does_path_exist(path, h5py_obj) -> bool:
"""Returns true if the requested path exists in the given h5py object."""
Expand Down Expand Up @@ -260,7 +260,20 @@ def _put_data_into_hdf5(self):
def add_units_key(dataset, path):
units_key = f"{path}/@units"
units = self.data.get(units_key, None)
units = str(units) if isinstance(units, pint.Unit) else units
if units is None:
return
if isinstance(units, pint.Unit):
units = str(units)
else:
try:
ureg.Unit(units)
except pint.errors.UndefinedUnitError as exc:
massage = (
f"Units provided for path: '{path}@units' are not valid."
f" Please provide a valid unit."
)
raise InvalidDictProvided(massage) from exc

if units:
dataset.attrs["units"] = units

Expand Down

0 comments on commit 91e624d

Please sign in to comment.