From 0aeb8c09100ddf2088c295a5f3534f7b4218ff35 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Tue, 30 Jan 2024 11:00:10 +0100
Subject: [PATCH] add idx to da_to_pdu to use in picking correct data_source

---
 .../Characterize_Darks_Gotthard2_NBC.ipynb    | 38 +++++++++++--------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
index 81dee07a5..8996ae973 100644
--- a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
@@ -212,15 +212,22 @@
     "calcat = CalCatApi(client=calibration_client())\n",
     "detector_id = calcat.detector(karabo_id)['id']\n",
     "pdus_by_da = calcat.physical_detector_units(detector_id, pdu_snapshot_at=creation_time)\n",
-    "da_to_pdu = {da: p['physical_name'] for (da, p) in pdus_by_da.items()}\n",
+    "# Module index is stored in the da_to_pdu dict to be used\n",
+    "# later in selecting the correct data source.\n",
+    "# TODO: This can be improved later by getting the module index and even data source\n",
+    "# when we start storing in the CALCAT.\n",
+    "da_to_pdu = dict()\n",
+    "for i, (da, p) in enumerate(sorted(pdus_by_da.items())):\n",
+    "    da_to_pdu[da] = {\n",
+    "        \"pdu\": p['physical_name'],\n",
+    "        \"idx\": i,\n",
+    "    }\n",
     "\n",
     "if karabo_da != [\"\"]:\n",
     "    # Filter DA connected to detector in CALCAT\n",
     "    karabo_da = [da for da in karabo_da if da in da_to_pdu]\n",
-    "    # Exclude non selected DA from processing.\n",
-    "    da_to_pdu = {da: da_to_pdu[da] for da in karabo_da}\n",
     "else:\n",
-    "    karabo_da = sorted(da_to_pdu.keys())\n",
+    "    karabo_da = da_to_pdu.keys()\n",
     "\n",
     "print(f\"Processing {karabo_da}\")"
    ]
@@ -314,13 +321,9 @@
     ")\n",
     "empty_lut = np.stack(1280 * [np.stack([empty_lut] * 2)], axis=0)\n",
     "for mod in karabo_da:\n",
-    "\n",
-    "    # TODO: A temporary workaround for using the correct data_source\n",
-    "    if gh2_detector == \"25um\":\n",
-    "        # Bad assumption for CALCAT DA names e.g. GH200/1 and GH200/2\n",
-    "        src = data_sources[int(mod[-1])-1]\n",
-    "    else:\n",
-    "        src = data_sources[0]\n",
+    "    # For 50um idx always 0. For 25um pick the right data source\n",
+    "    # from list of 2 sources.\n",
+    "    src = data_sources[da_to_pdu[mod][\"idx\"]]\n",
     "\n",
     "    # Retrieve LUT constant\n",
     "    lut, time = get_constant_from_db_and_time(\n",
@@ -447,7 +450,8 @@
     "step_timer.start()\n",
     "g_name = [\"G0\", \"G1\", \"G2\"]\n",
     "\n",
-    "for mod, pdu in da_to_pdu.items():\n",
+    "for mod in karabo_da:\n",
+    "    pdu = da_to_pdu[mod][\"pdu\"]\n",
     "    display(Markdown(f\"### Badpixels for module {mod}:\"))\n",
     "\n",
     "    badpixels_map[mod][\n",
@@ -484,7 +488,8 @@
    "source": [
     "if not local_output:\n",
     "    for cons, cname in zip([offset_map, noise_map], [\"Offset\", \"Noise\"]):\n",
-    "        for mod, pdu in da_to_pdu.items():\n",
+    "        for mod in karabo_da:\n",
+    "            pdu = da_to_pdu[mod][\"pdu\"]\n",
     "            display(Markdown(f\"### {cname} for module {mod}:\"))\n",
     "\n",
     "            for cell in [0, 1]:\n",
@@ -507,7 +512,8 @@
    "outputs": [],
    "source": [
     "step_timer.start()\n",
-    "for mod, db_mod in da_to_pdu.items():\n",
+    "for mod in karabo_da:\n",
+    "    db_mod = da_to_pdu[mod][\"pdu\"]\n",
     "    constants = {\n",
     "        \"Offset\": offset_map[mod],\n",
     "        \"Noise\": noise_map[mod],\n",
@@ -567,13 +573,13 @@
    "outputs": [],
    "source": [
     "# TODO: store old constants for comparison.\n",
-    "for mod, pdu in da_to_pdu.items():\n",
+    "for mod in karabo_da:\n",
     "    mod_file = mod.replace(\"/\", \"-\")\n",
     "    with open(f\"{metadata_folder or out_folder}/module_metadata_{mod_file}.yml\", \"w\") as fd:\n",
     "        yaml.safe_dump(\n",
     "            {\n",
     "                \"module\": mod,\n",
-    "                \"pdu\": pdu,\n",
+    "                \"pdu\": da_to_pdu[mod][\"pdu\"],\n",
     "            },\n",
     "            fd,\n",
     "        )"
-- 
GitLab