From 6a7aa2eeaeeee7f15a84dfeb2e1936bff0380218 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: Mon, 16 Jun 2025 23:38:27 +0200
Subject: [PATCH] Update file maxwell_integrate_to_h5.py

---
 maxwell_integrate_to_h5.py | 193 +++++++++++++++++++++++++++++++++----
 1 file changed, 176 insertions(+), 17 deletions(-)

diff --git a/maxwell_integrate_to_h5.py b/maxwell_integrate_to_h5.py
index a79f9ec..9181d12 100644
--- a/maxwell_integrate_to_h5.py
+++ b/maxwell_integrate_to_h5.py
@@ -16,6 +16,7 @@ from watchdog.events import PatternMatchingEventHandler
 from multiprocessing.pool import ThreadPool as Pool
 import pandas as pd
 from silx.io.dictdump import h5todict, dicttoh5
+import re
 
 
 
@@ -123,28 +124,186 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
             subdir_name = os.path.basename(os.path.normpath(subdir_path_int))
             results_df.to_csv(os.path.join(subdir_path_int, f"{subdir_name}.csv"), index=False)
 
-            # Prepare data for HDF5 file using silx
-            hdf5_data = {
-                subdir_name: {
-                    "@NX_class": "NXroot",
-                }
-            }
 
             # Sort results_data by filename
-            results_data = sorted(results_data, key=lambda x: x["filename"].lower())
+            def natural_sort_key(item):
+                return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', item["filename"])]
 
-            for idx, result in enumerate(results_data, start=1):
-                group_name = f"{idx}.1"
-                hdf5_data[subdir_name][group_name] = {
+            results_data = sorted(results_data, key=natural_sort_key)
+            # Prepare data for HDF5 file using silx
+            hdf5_data = {
+                subdir_name: {
                     "@NX_class": "NXentry",
-                    "description": os.path.basename(result["filename"]),
-                    "measurements": {
-                        "@NX_class": "NXcollection",
-                        "q": result["q"].tolist(),
-                        "I": result["I"].tolist(),
-                        "dI": result["dI"].tolist()
-                    }
+                    "description": f"{subdir_name}",
+                    "files": [
+                        {
+                            "name": f"{idx}.1",
+                            "path": f"/{idx}.1",
+                            "attributes": [
+                                {
+                                    "name": "NX_class",
+                                    "shape": [],
+                                    "type": {
+                                        "class": "String",
+                                        "charSet": "UTF-8",
+                                        "strPad": "null-terminated"
+                                    },
+                                    "value": "NXentry"
+                                },
+                                {
+                                    "name": "default",
+                                    "shape": [],
+                                    "type": {
+                                        "class": "String",
+                                        "charSet": "UTF-8",
+                                        "strPad": "null-terminated"
+                                    },
+                                    "value": "measurement"
+                                },
+                                {
+                                    "name": "plotselect",
+                                    "shape": [],
+                                    "type": {
+                                        "class": "String",
+                                        "charSet": "UTF-8",
+                                        "strPad": "null-terminated"
+                                    },
+                                    "value": "q,I"
+                                }
+                            ],
+                            "kind": "group",
+                            "children": [
+                                {
+                                    "name": "measurement",
+                                    "path": f"/{idx}.1/measurement",
+                                    "attributes": [
+                                        {
+                                            "name": "NX_class",
+                                            "shape": [],
+                                            "type": {
+                                                "class": "String",
+                                                "charSet": "UTF-8",
+                                                "strPad": "null-terminated"
+                                            },
+                                            "value": "NXcollection"
+                                        }
+                                    ],
+                                    "kind": "group",
+                                    "children": [
+                                        {
+                                            "name": "q",
+                                            "path": f"/{idx}.1/measurement/q",
+                                            "attributes": [],
+                                            "kind": "dataset",
+                                            "shape": [len(result["q"])],
+                                            "type": {
+                                                "class": "Float",
+                                                "endianness": "little-endian",
+                                                "size": 64
+                                            },
+                                            "chunks": [min(len(result["q"]), 1000)],
+                                            "filters": [],
+                                            "rawType": {
+                                                "signed": False,
+                                                "type": 1,
+                                                "vlen": False,
+                                                "littleEndian": True,
+                                                "size": 8,
+                                                "total_size": len(result["q"])
+                                            },
+                                            "value": result["q"]
+                                        },
+                                        {
+                                            "name": "I",
+                                            "path": f"/{idx}.1/measurement/I",
+                                            "attributes": [],
+                                            "kind": "dataset",
+                                            "shape": [len(result["I"])],
+                                            "type": {
+                                                "class": "Float",
+                                                "endianness": "little-endian",
+                                                "size": 64
+                                            },
+                                            "chunks": [min(len(result["I"]), 1000)],
+                                            "filters": [],
+                                            "rawType": {
+                                                "signed": False,
+                                                "type": 1,
+                                                "vlen": False,
+                                                "littleEndian": True,
+                                                "size": 8,
+                                                "total_size": len(result["I"])
+                                            },
+                                            "value": result["I"]
+                                        },
+                                        {
+                                            "name": "dI",
+                                            "path": f"/{idx}.1/measurement/dI",
+                                            "attributes": [],
+                                            "kind": "dataset",
+                                            "shape": [len(result["dI"])],
+                                            "type": {
+                                                "class": "Float",
+                                                "endianness": "little-endian",
+                                                "size": 64
+                                            },
+                                            "chunks": [min(len(result["dI"]), 1000)],
+                                            "filters": [],
+                                            "rawType": {
+                                                "signed": False,
+                                                "type": 1,
+                                                "vlen": False,
+                                                "littleEndian": True,
+                                                "size": 8,
+                                                "total_size": len(result["dI"])
+                                            },
+                                            "value": result["dI"]
+                                        }
+                                    ]
+                                },
+                                {
+                                    "name": "plotselect",
+                                    "path": f"/{idx}.1/plotselect",
+                                    "attributes": [
+                                        {
+                                            "name": "NX_class",
+                                            "shape": [],
+                                            "type": {
+                                                "class": "String",
+                                                "charSet": "UTF-8",
+                                                "strPad": "null-terminated"
+                                            },
+                                            "value": "NXcollection"
+                                        },
+                                        {
+                                            "name": "axes",
+                                            "shape": [1],
+                                            "type": {
+                                                "class": "String",
+                                                "charSet": "UTF-8",
+                                                "strPad": "null-terminated"
+                                            },
+                                            "value": "q"
+                                        },
+                                        {
+                                            "name": "signal",
+                                            "shape": [],
+                                            "type": {
+                                                "class": "String",
+                                                "charSet": "UTF-8",
+                                                "strPad": "null-terminated"
+                                            },
+                                            "value": "I"
+                                        }
+                                    ],
+                                    "kind": "group"
+                                }
+                            ]
+                        }
+                        for idx, result in enumerate(results_data, start=1)
+                    ]
                 }
+            }
 
             # Save to HDF5 file using silx
             hdf5_file_path = os.path.join(subdir_path_int, f"{subdir_name}.h5")
-- 
GitLab