From b0c88cabbe0ed7fee93ddf107c855895b0e7fc1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=B6ne=2C=20Tjark=20Leon=20Raphael?=
 <tjark.leon.raphael.groene@uni-hamburg.de>
Date: Wed, 18 Jun 2025 13:05:18 +0200
Subject: [PATCH] Update file maxwell_integrate_to_h5.py

---
 maxwell_integrate_to_h5.py | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/maxwell_integrate_to_h5.py b/maxwell_integrate_to_h5.py
index 36823d8..11a030f 100644
--- a/maxwell_integrate_to_h5.py
+++ b/maxwell_integrate_to_h5.py
@@ -212,15 +212,22 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
                 h5.attrs["NX_class"] = "NXroot"
 
                 for idx, result in enumerate(results_data, start=1):
-                    
+
+                    if not result["q"].size or not result["I"].size:
+                        print(f"Skipping invalid scan data for entry {entry_name}")
+                        continue
+
                     entry_name = f"{idx:05d}.1"
                     entry = h5.create_group(entry_name)
                     entry["title"] = "Collected Q-I scans"
                     entry.attrs["NX_class"] = "NXentry"
                     
 
-                    entry.create_dataset("time", data=results_metadata[idx-1]["dateString"].encode('utf-8'), dtype=h5py.string_dtype(encoding='utf-8'))	
-                    
+                    # entry.create_dataset("time", data=results_metadata[idx-1]["dateString"].encode('utf-8'), dtype=h5py.string_dtype(encoding='utf-8'))	
+                    entry.attrs["time"] = results_metadata[idx-1]["dateString"].encode('utf-8')
+                    if image_sequence_number.isdigit():
+                        entry.attrs["image sequence number"] = int(image_sequence_number)
+
                     
                     if any(results_metadata[idx-1][key] for key in ["userComment1", "userComment2", "userComment3", "userComment4"]):
                         comments = entry.create_group("comments")
@@ -240,7 +247,7 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
                     detector.attrs["NX_class"] = "NXdetector"
                     chunk_size = 512
 
-                    detector.create_dataset("q", data=np.asarray(result["q"], dtype=np.float64), chunks=(chunk_size,), dtype="f8", compression_opts=4, compression="gzip")
+                    detector.create_dataset("q [1/Å]", data=np.asarray(result["q"], dtype=np.float64), chunks=(chunk_size,), dtype="f8", compression_opts=4, compression="gzip")
                     detector.create_dataset("I", data=np.asarray(result["I"], dtype=np.float64), chunks=(chunk_size,), dtype="f8", compression_opts=4, compression="gzip")
                     detector.create_dataset("dI", data=np.asarray(result["dI"], dtype=np.float64), chunks=(chunk_size,), dtype="f8", compression_opts=4, compression="gzip")
                     
@@ -255,14 +262,12 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
                     if width.isdigit() and height.isdigit():
                         det_size = detector.create_group("detector size")
                         det_size.attrs["NX_class"] = "NXcollection"
-                        det_size.create_dataset("detector width [pixel]", data=np.asarray([int(width)], dtype=np.int64), dtype="i4", compression_opts=4, compression="gzip")
-                        det_size.create_dataset("detector height [pixel]", data=np.asarray([], dtype=np.int64), dtype="i4", compression_opts=4, compression="gzip")
+                        det_size.create_dataset("detector width [pixel]", data=np.asarray([int(width)], dtype=np.int32), dtype="i4", compression_opts=4, compression="gzip")
+                        det_size.create_dataset("detector height [pixel]", data=np.asarray([int(height)], dtype=np.int32), dtype="i4", compression_opts=4, compression="gzip")
                     if exposure_time.isdigit():
-                        detector.create_dataset("exposure time", data=np.asarray([float(exposure_time)], dtype=np.float32), dtype="f4", compression_opts=4, compression="gzip")
+                        detector.create_dataset("exposure time [s]", data=np.asarray([float(exposure_time)], dtype=np.float32), dtype="f4", compression_opts=4, compression="gzip")
                     if summed_exposures.replace('.', '', 1).isdigit():
-                        detector.create_dataset("summed exposures", data=np.asarray([int(summed_exposures)], dtype=np.int64), dtype="i4", compression_opts=4, compression="gzip")
-                    if image_sequence_number.isdigit():
-                        detector.create_dataset("image sequence number", data=np.asarray([int(image_sequence_number)], dtype=np.int64), dtype="i4", compression_opts=4, compression="gzip")
+                        detector.create_dataset("summed exposures", data=np.asarray([int(summed_exposures)], dtype=np.int32), dtype="i4", compression_opts=4, compression="gzip")
 
                     # Add interpretation info (optional for PyMca)
                     detector["I"].attrs["interpretation"] = "spectrum"
@@ -276,12 +281,12 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
                     
 
                     meas["I"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/I")
-                    meas["q"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/q")
+                    meas["q [1/Å]"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/q [1/Å]")
                     meas["dI"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/dI")
 
                     # Optional display-friendly names
                     meas["I"].attrs["long_name"] = "Intensity"
-                    meas["q"].attrs["long_name"] = "Q [1/A]"
+                    meas["q [1/Å]"].attrs["long_name"] = "Q [1/Å]"
                     
                                         # Measurement group (holds soft links)
                     plotselect = entry.create_group("plotselect")
@@ -290,11 +295,11 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
                     plotselect.attrs["axes"] = "q"
 
                     plotselect["I"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/I")
-                    plotselect["q"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/q")
+                    plotselect["q [1/Å]"] = h5py.SoftLink(f"/{entry_name}/instrument/detector/q [1/Å]")
 
                     # Optional display-friendly names
                     plotselect["I"].attrs["long_name"] = "Intensity"
-                    plotselect["q"].attrs["long_name"] = "Q [1/A]"
+                    plotselect["q [1/Å]"].attrs["long_name"] = "Q [1/A]"
 
                     # For PyMca auto-plot:
                     entry.attrs["default"] = "plotselect"
-- 
GitLab