From 62192dc15e4d4a55982b3e0fde75a3189a47338c Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Mon, 19 Apr 2021 11:25:07 +0200
Subject: [PATCH] Old const background retrieval, handle qm, minor cleanup

This commit does too many things, sorry, but stuff was all tangled up:
- clean up a bit (imports, unused variables)
- map to PDU with one query instead of in loop (each query gets all anyway)
- start fetching old constants (for comparison) in the background
- add qm layer to dict holding old constants (see issue #49)
---
 .../Characterize_AGIPD_Gain_Darks_NBC.ipynb   | 47 ++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
index 90492a174..bb9cf91e0 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
@@ -116,7 +116,6 @@
     "from cal_tools.tools import (\n",
     "    get_dir_creation_date,\n",
     "    get_from_db,\n",
-    "    get_pdu_from_db,\n",
     "    get_random_db_interface,\n",
     "    get_report,\n",
     "    map_gain_stages,\n",
@@ -569,6 +568,52 @@
     "report = get_report(out_folder)"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# TODO: add db_module when received from myMDC\n",
+    "# Create the modules dict of karabo_das and PDUs\n",
+    "qm_dict = OrderedDict()\n",
+    "for i, k_da in zip(modules, karabo_da):\n",
+    "    qm = module_index_to_qm(i)\n",
+    "    qm_dict[qm] = {\n",
+    "        \"karabo_da\": k_da,\n",
+    "        \"db_module\": \"\"\n",
+    "    }\n",
+    "\n",
+    "# going through tools.get_pdu_from_db seems wasteful\n",
+    "all_pdus = iCalibrationDB.ConstantMetaData().retrieve_pdus_for_detector(\n",
+    "    karabo_id=karabo_id,\n",
+    "    receiver=cal_db_interface,\n",
+    "    snapshot_at=creation_time.isoformat(),\n",
+    "    timeout=cal_db_timeout\n",
+    ")\n",
+    "karabo_da_to_pdu = {d[\"karabo_da\"]: d[\"pdu_physical_name\"] for d in all_pdus}\n",
+    "for qm_attr in qm_dict.values():\n",
+    "    qm_attr[\"db_module\"] = karabo_da_to_pdu[qm_attr[\"karabo_da\"]]\n",
+    "for pdu_dict in all_pdus:\n",
+    "    this_karabo_da = pdu_dict[\"karabo_da\"]\n",
+    "    if this_karabo_da in qm_dict:\n",
+    "        qm_dict[this_karabo_da][\"db_module\"] = pdu_dict[\"pdu_physical_name\"]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "with open(f\"{out_folder}/module_mapping.yml\",\"w\") as fd:\n",
+    "    yaml.safe_dump({\n",
+    "        \"module_mapping\": {\n",
+    "            qm: qm_dict[qm][\"db_module\"] for qm in qm_dict\n",
+    "        }\n",
+    "    }, fd)"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
-- 
GitLab