From 8d1719a5834bea141209820a4780d8ecf8795461 Mon Sep 17 00:00:00 2001
From: woutdenolf <woutdenolf@users.sf.net>
Date: Wed, 12 Feb 2025 17:00:16 +0100
Subject: [PATCH] split_bliss_scan: waiting for the end of the scan should be
 optional

---
 src/est/core/process/split.py         |  3 +-
 src/est/core/split_monotonic.py       | 10 ++++-
 src/est/tests/test_split_monotonic.py | 61 +++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/src/est/core/process/split.py b/src/est/core/process/split.py
index dbdd3ce6..1d479773 100644
--- a/src/est/core/process/split.py
+++ b/src/est/core/process/split.py
@@ -6,7 +6,7 @@ from est.core.split_monotonic import split_bliss_scan
 class SplitBlissScan(
     Task,
     input_names=["filename", "scan_number", "monotonic_channel", "out_filename"],
-    optional_input_names=["retry_timeout", "retry_period"],
+    optional_input_names=["wait_finished", "retry_timeout", "retry_period"],
     output_names=["out_urls"],
 ):
     """Split a Bliss scan in subscans as determined by a channel which
@@ -21,6 +21,7 @@ class SplitBlissScan(
 
     :param optional_input_names:
 
+    * ``wait_finished``: Wait for the Bliss scan to be complete in HDF5.
     * ``retry_timeout``: Timeout of waiting for the Bliss scan to be complete in HDF5.
     * ``retry_period``: Check period of waiting for the Bliss scan to be complete in HDF5.
 
diff --git a/src/est/core/split_monotonic.py b/src/est/core/split_monotonic.py
index 3c616f75..deab4da6 100644
--- a/src/est/core/split_monotonic.py
+++ b/src/est/core/split_monotonic.py
@@ -18,6 +18,7 @@ def split_bliss_scan(
     scan_number: int,
     monotonic_channel: str,
     out_filename: str,
+    wait_finished: bool = True,
     **retry_args,
 ) -> List[str]:
     """Split a Bliss scan in subscans as determined by a channel which
@@ -27,12 +28,15 @@ def split_bliss_scan(
     :param scan_number: The Bliss scan number.
     :param monotonic_channel: HDF5 path relative to the scan group.
     :param out_filename: HDF5 file name to save subscans as a result of splitting the Bliss scan.
+    :param wait_finished: Wait for the Bliss scan to be complete in HDF5.
     :param retry_timeout: Timeout of waiting for the Bliss scan to be complete in HDF5.
     :param retry_period: Check period of waiting for the Bliss scan to be complete in HDF5.
     :returns: HDF5 URL's of the subscans as a result of splitting the Bliss scan.
     """
     entry_name = f"{scan_number}.1"
-    _wait_scan_finished(filename, entry_name, **retry_args)
+
+    if wait_finished:
+        _wait_scan_finished(filename, entry_name, **retry_args)
 
     out_urls = []
     with h5py_utils.File(filename) as nxroot_in:
@@ -41,6 +45,10 @@ def split_bliss_scan(
         monotonic_values = nxentry_in[monotonic_channel][()]
         monotonic_slices = split_piecewise_monotonic(monotonic_values)
 
+        if monotonic_slices and not wait_finished:
+            # The last subscan might not be complete.
+            monotonic_slices = monotonic_slices[:-1]
+
         for subscan_number, subscan_slice in enumerate(monotonic_slices, 1):
             out_url = _save_subscan(
                 nxentry_in, scan_number, subscan_number, out_filename, subscan_slice
diff --git a/src/est/tests/test_split_monotonic.py b/src/est/tests/test_split_monotonic.py
index 6cb14b72..a10fc488 100644
--- a/src/est/tests/test_split_monotonic.py
+++ b/src/est/tests/test_split_monotonic.py
@@ -90,6 +90,67 @@ def test_split_bliss_scan(tmp_path, split_function):
     assert split_data == expected_split_data
 
 
+@pytest.mark.parametrize("split_function", [split_bliss_scan, split_bliss_scan_task])
+def test_split_unfinished_bliss_scan(tmp_path, split_function):
+    bliss_scan_data = {
+        "1.1": {
+            "end_time": "",
+            "@attr1": "value1",
+            "dataset1": "value1",
+            "group1": {
+                "@attr2": 10,
+                "dataset2": "value2",
+                "dataset3": [0, 1, 2, 2, 1, 0],
+                "dataset4": [0, 1, 2, 5, 4, 3],
+                "dataset5": 10,
+                "dataset6": [10, 20],
+            },
+            "group2": {
+                "@attr3": "value3",
+                ">dataset3": "../group1/dataset3",
+                ">dataset4": "/1.1/group1/dataset4",
+            },
+        }
+    }
+
+    expected_split_data = {
+        "1.1": {
+            "@attr1": "value1",
+            "dataset1": "value1",
+            "end_time": "",
+            "group1": {
+                "@attr2": 10,
+                "dataset2": "value2",
+                "dataset3": [0, 1, 2],
+                "dataset4": [0, 1, 2],
+                "dataset5": 10,
+            },
+            "group2": {
+                "@attr3": "value3",
+                "dataset3": [0, 1, 2],
+                "dataset4": [0, 1, 2],
+            },
+        }
+    }
+
+    in_file = str(tmp_path / "in.h5")
+    dictdump.dicttonx(bliss_scan_data, in_file, add_nx_class=False)
+
+    out_file = str(tmp_path / "out.h5")
+    out_urls = split_function(
+        filename=in_file,
+        scan_number=1,
+        monotonic_channel="group1/dataset3",
+        out_filename=out_file,
+        wait_finished=False,
+    )
+
+    assert out_urls == [f"silx://{out_file}::/1.1"]
+
+    split_data = _normalize_h5data(dictdump.nxtodict(out_file, asarray=False))
+    assert split_data == expected_split_data
+
+
 @pytest.mark.parametrize("split_function", [split_bliss_scan, split_bliss_scan_task])
 def test_split_bliss_scan_timeout(tmp_path, split_function):
     bliss_scan_data = {
-- 
GitLab