From 1f92b4ae800d41aa34d9a8f17f85de7f8ff860f1 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Tue, 27 Feb 2024 13:02:48 +0100
Subject: [PATCH] Improve errors when no lit frames to correct - When no data
 trains to correct skip plots gracefully before checking corrected data. -
 When no lit frames available to correct, make this very clear by replacing
 empty list with a message of no frames to correct - Show shutter
 configuration for SPB and MID. - check shutter function to check shutter
 configuration.

---
 notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb | 17 +++++++++++------
 src/cal_tools/agipdlib.py                      | 12 +++++++-----
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 49bbda86a..a0cb8b503 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -137,6 +137,7 @@
     "import math\n",
     "import multiprocessing\n",
     "import os\n",
+    "import sys\n",
     "import warnings\n",
     "from datetime import timedelta\n",
     "from logging import warning\n",
@@ -182,6 +183,7 @@
     "from cal_tools.step_timing import StepTimer\n",
     "from cal_tools.tools import (\n",
     "    calcat_creation_time,\n",
+    "    check_shutter,\n",
     "    map_modules_from_folder,\n",
     "    module_index_to_qm,\n",
     "    write_constants_fragment,\n",
@@ -520,6 +522,8 @@
     "        r = get_data[use_litframe_finder]()\n",
     "        cell_sel = LitFrameSelection(r, train_ids, max_pulses, energy_threshold, use_super_selection)\n",
     "        cell_sel.print_report()\n",
+    "        if np.count_nonzero(r.nLitFrame.value) == 0:  # No lit frames.\n",
+    "            check_shutter(dc, instrument, karabo_id_control)\n",
     "    except LitFrameFinderError as err:\n",
     "        warning(f\"Cannot use AgipdLitFrameFinder due to:\\n{err}\")\n",
     "        cell_sel = CellRange(max_pulses, max_cells=mem_cells)\n",
@@ -881,8 +885,6 @@
     "\n",
     "    Yields (file data slot, start index, stop index)\n",
     "    \"\"\"\n",
-    "    \n",
-    "    \n",
     "    for i_proc, n_img in enumerate(img_counts):\n",
     "        n_chunks = math.ceil(n_img / chunk_size)\n",
     "        for i in range(n_chunks):\n",
@@ -896,6 +898,7 @@
    "outputs": [],
    "source": [
     "step_timer.start()\n",
+    "all_imgs_counts = []\n",
     "if max_tasks_per_worker == -1:\n",
     "    max_tasks_per_worker = None\n",
     "with multiprocessing.Pool(maxtasksperchild=max_tasks_per_worker) as pool:\n",
@@ -911,7 +914,8 @@
     "        )\n",
     "        step_timer.done_step(f'Loading data from files')\n",
     "\n",
-    "        if img_counts == 0:\n",
+    "        all_imgs_counts += img_counts\n",
+    "        if np.all(img_counts) == 0:\n",
     "            # Skip any further processing and output if there are no images to\n",
     "            # correct in this file.\n",
     "            continue\n",
@@ -984,8 +988,10 @@
    "outputs": [],
    "source": [
     "if skip_plots:\n",
-    "    print('Skipping plots')\n",
-    "    import sys\n",
+    "    print(\"Skipping plots due to user request.\")\n",
+    "    sys.exit(0)\n",
+    "elif np.all(img_counts) == 0:\n",
+    "    print(\"Skipping plots because no data correction applied.\")\n",
     "    sys.exit(0)"
    ]
   },
@@ -1030,7 +1036,6 @@
     "        run_data = RunDirectory(data_folder, include)\n",
     "    except FileNotFoundError:\n",
     "        warning(f'No corrected files for {include}. Skipping plots.')\n",
-    "        import sys\n",
     "        sys.exit(0)\n",
     "    if tid is not None:\n",
     "        tid, data = run_data.select(\n",
diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 66128cc39..cacbfde83 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -1719,11 +1719,13 @@ class LitFrameSelection(CellSelection):
             t0, tN, st = (rec['train_range'] + (1,))[:3]
             ntrain = max((int(tN) - int(t0)) // int(st), 1)
             trsintf = ':'.join([str(n) for n in rec['train_range']])
-            print(("{pattern_no:2d} {trsintf:25s} {ntrain:5d} "
-                   "{nmissed_trains:4d} {npulse_exposed:4d} {ndataframe:3d} "
-                   "{nframe_total:3d} [{frmintf}]"
-                   ).format(frmintf=frmintf, ntrain=ntrain,
-                            trsintf=trsintf, **rec))
+            frmintf_display = f"[{frmintf}]" if frmintf else "No lit frames to correct!"
+            print(
+                f"{rec['pattern_no']:2d} {trsintf:25s} {ntrain:5d} "
+                f"{rec['nmissed_trains']:4d} {rec['npulse_exposed']:4d} "
+                f"{rec['ndataframe']:3d} {rec['nframe_total']:3d} "
+                f"{frmintf_display}"
+            )
         if nrec > max_lines:
             print(f"... {nrec - max_lines + 1} more lines skipped")
 
-- 
GitLab