From eb4ff5a16b77fc6db9dae4a5c2408ecb59669123 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Tue, 21 Feb 2023 17:55:37 +0100
Subject: [PATCH] Error out if dark constants are missing for all modules

---
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  | 46 +++++++++++--------
 ...retrieve_constants_precorrection_NBC.ipynb | 20 ++++----
 2 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index 639594abd..e4656e1d2 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -264,19 +264,18 @@
     "\n",
     "    # Validate the constants availability and raise/warn correspondingly.\n",
     "    for mod, calibrations in const_data.items():\n",
-    "        missing_dark_constants = {\"OffsetGotthard2\", \"BadPixelsDarkGotthard2\"} - set(calibrations)\n",
-    "        missing_gain_constants = {\"BadPixelsFFGotthard2\", \"RelativeGainGotthard2\"} - set(calibrations)\n",
     "\n",
-    "        if \"LUTGotthard2\" not in calibrations.keys():\n",
-    "            warning(f\"LUTGotthard2 is not available to correct {mod}\")\n",
-    "            karabo_da.remove(mod)\n",
+    "        dark_constants = {\"LUTGotthard2\"}\n",
+    "        if offset_correction:\n",
+    "            dark_constants |= {\"OffsetGotthard2\", \"BadPixelsDarkGotthard2\"}\n",
     "\n",
-    "        if offset_correction and missing_dark_constants:\n",
-    "            warning(f\"Dark constants {missing_dark_constants} are not available to correct {mod}.\")\n",
-    "            if \"OffsetGotthard2\" in missing_dark_constants:\n",
-    "                warning(\"Offset correction is disabled for all modules.\")\n",
-    "                offset_correction = False\n",
+    "        missing_dark_constants = dark_constants - set(calibrations)\n",
+    "        if missing_dark_constants:\n",
+    "            karabo_da.remove(mod)\n",
+    "            warning(f\"Dark constants {missing_dark_constants} are not available to correct {mod}.\")  # noqa\n",
     "\n",
+    "        missing_gain_constants = {\n",
+    "            \"BadPixelsFFGotthard2\", \"RelativeGainGotthard2\"} - set(calibrations)\n",
     "        if gain_correction and missing_gain_constants:\n",
     "            warning(f\"Gain constants {missing_gain_constants} are not retrieved for mod {mod}\")\n",
     "            if \"RelativeGainGotthard2\" in missing_gain_constants:\n",
@@ -284,13 +283,26 @@
     "                gain_correction = False\n",
     "\n",
     "        # Create the mask array.\n",
-    "        bpix = const_data[mod].get(\"BadPixelsDarkGotthard2\", np.zeros((1280, 2, 3), dtype=np.uint32))\n",
+    "        bpix = const_data[mod].get(\"BadPixelsDarkGotthard2\")\n",
+    "        if bpix is None:\n",
+    "            bpix = np.zeros((1280, 2, 3), dtype=np.uint32)\n",
     "        if const_data[mod].get(\"BadPixelsFFGotthard2\") is not None:\n",
     "            bpix |= const_data[mod][\"BadPixelsFFGotthard2\"]\n",
     "        const_data[mod][\"Mask\"] = bpix\n",
     "\n",
+    "        # Prepare empty arrays for missing constants.\n",
+    "        if const_data[mod].get(\"OffsetGotthard2\") is None:\n",
+    "            const_data[mod][\"OffsetGotthard2\"] = np.zeros(\n",
+    "                (1280, 2, 3), dtype=np.float32)\n",
+    "\n",
+    "        if const_data[mod].get(\"RelativeGainGotthard2\") is None:\n",
+    "            const_data[mod][\"RelativeGainGotthard2\"] = np.ones(\n",
+    "                (1280, 2, 3), dtype=np.float32)\n",
+    "        const_data[mod][\"RelativeGainGotthard2\"] = const_data[mod][\"RelativeGainGotthard2\"].astype(  # noqa\n",
+    "            np.float32, copy=False)  # Old gain constants are not float32.\n",
+    "\n",
     "if not karabo_da:\n",
-    "    raise ValueError(\"LUTGotthard2 constants are not available for all modules.\")"
+    "    raise ValueError(\"Dark constants are not available for all modules.\")"
    ]
   },
   {
@@ -317,14 +329,8 @@
     "        data_corr[index, ...],\n",
     "        mask[index, ...],\n",
     "        g,\n",
-    "        const_data[mod].get(\n",
-    "            \"OffsetGotthard2\",\n",
-    "            np.zeros((1280, 2, 3), dtype=np.float32)\n",
-    "        ),\n",
-    "        const_data[mod].get(\n",
-    "            \"RelativeGainGotthard2\",\n",
-    "            np.ones((1280, 2, 3), dtype=np.float32)\n",
-    "        ).astype(np.float32, copy=False),  # Old gain constants are not float32.\n",
+    "        const_data[mod][\"OffsetGotthard2\"],\n",
+    "        const_data[mod][\"RelativeGainGotthard2\"],  \n",
     "        const_data[mod][\"Mask\"],\n",
     "        apply_offset=offset_correction,\n",
     "        apply_gain=gain_correction,\n",
diff --git a/notebooks/Gotthard2/Gotthard2_retrieve_constants_precorrection_NBC.ipynb b/notebooks/Gotthard2/Gotthard2_retrieve_constants_precorrection_NBC.ipynb
index 5dbdd63a6..679bc8506 100644
--- a/notebooks/Gotthard2/Gotthard2_retrieve_constants_precorrection_NBC.ipynb
+++ b/notebooks/Gotthard2/Gotthard2_retrieve_constants_precorrection_NBC.ipynb
@@ -156,25 +156,25 @@
     "# Retrieve metadata for all pnccd constants.\n",
     "g2_metadata = g2_cal.metadata(constant_names)\n",
     "\n",
-    "missing_lut_modules = set()\n",
+    "missing_dark_modules = set()\n",
     "# Validate the constants availability and raise/warn correspondingly.\n",
     "for mod, ccv_dict in g2_metadata.items():\n",
     "\n",
-    "    missing_dark_constants = {\"OffsetGotthard2\", \"BadPixelsDarkGotthard2\"} - set(ccv_dict)\n",
-    "    missing_gain_constants = {\"BadPixelsFFGotthard2\", \"RelativeGainGotthard2\"} - set(ccv_dict)\n",
-    "\n",
-    "    if \"LUTGotthard2\" not in ccv_dict.keys():\n",
-    "        warning(f\"LUTGotthard2 is not available to correct {mod}\")\n",
-    "        missing_lut_modules.add(mod)\n",
+    "    dark_constants = {\"LUTGotthard2\"}\n",
+    "    if offset_correction:\n",
+    "        dark_constants |= {\"OffsetGotthard2\", \"BadPixelsDarkGotthard2\"}\n",
+    "    missing_dark_constants = dark_constants - set(ccv_dict)\n",
     "\n",
-    "    if offset_correction and missing_dark_constants:\n",
+    "    if missing_dark_constants:\n",
     "        warning(f\"Dark constants {missing_dark_constants} are not available to correct {mod}\")\n",
+    "        missing_dark_modules.add(mod)\n",
     "\n",
+    "    missing_gain_constants = {\"BadPixelsFFGotthard2\", \"RelativeGainGotthard2\"} - set(ccv_dict)\n",
     "    if gain_correction and missing_gain_constants:\n",
     "        warning(f\"Gain constants {missing_gain_constants} are not retrieved for {mod}\")\n",
     "\n",
-    "if missing_lut_modules == set(karabo_da):\n",
-    "    raise ValueError(\"LUTGotthard2 constants are not available for all modules.\")\n",
+    "if missing_dark_modules == set(karabo_da):\n",
+    "    raise ValueError(f\"{missing_dark_constants} constants are not available for all modules.\")\n",
     "\n",
     "# Add constants metadata in retrieved_constants dict.\n",
     "for mod, ccv_dict in g2_metadata.items():\n",
-- 
GitLab