From 58a704764a5637ff8481f392c2b3ad137dbec16d Mon Sep 17 00:00:00 2001 From: Ivan Usov <ivan.usov@psi.ch> Date: Tue, 4 Jun 2024 14:13:37 +0200 Subject: [PATCH] Fix KeyError if 'mf' or 'temp' not in data --- pyzebra/h5.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pyzebra/h5.py b/pyzebra/h5.py index 394f3c5..a8f0748 100644 --- a/pyzebra/h5.py +++ b/pyzebra/h5.py @@ -147,17 +147,21 @@ def read_detector_data(filepath, cami_meta=None): # optional parameters if "/entry1/sample/magnetic_field" in h5f: scan["mf"] = h5f["/entry1/sample/magnetic_field"][:] - # TODO: NaNs are not JSON compliant, so replace them with None - # this is not a great solution, but makes it safe to use the array in bokeh - scan["mf"] = np.where(np.isnan(scan["mf"]), None, scan["mf"]) + + if "mf" in scan: + # TODO: NaNs are not JSON compliant, so replace them with None + # this is not a great solution, but makes it safe to use the array in bokeh + scan["mf"] = np.where(np.isnan(scan["mf"]), None, scan["mf"]) if "/entry1/sample/temperature" in h5f: scan["temp"] = h5f["/entry1/sample/temperature"][:] elif "/entry1/sample/Ts/value" in h5f: scan["temp"] = h5f["/entry1/sample/Ts/value"][:] - # TODO: NaNs are not JSON compliant, so replace them with None - # this is not a great solution, but makes it safe to use the array in bokeh - scan["temp"] = np.where(np.isnan(scan["temp"]), None, scan["temp"]) + + if "temp" in scan: + # TODO: NaNs are not JSON compliant, so replace them with None + # this is not a great solution, but makes it safe to use the array in bokeh + scan["temp"] = np.where(np.isnan(scan["temp"]), None, scan["temp"]) # overwrite metadata from .cami if cami_meta is not None: -- GitLab