From 2729b7b4a28ccd5c6339950fcbf6bc75d00d1bfa Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Thu, 29 Apr 2021 10:07:32 +0200
Subject: [PATCH] Same as for LPD; save module_metadata

---
 .../DSSC/Characterize_DSSC_Darks_NBC.ipynb    | 52 ++++++++++++-------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/notebooks/DSSC/Characterize_DSSC_Darks_NBC.ipynb b/notebooks/DSSC/Characterize_DSSC_Darks_NBC.ipynb
index 9492f7d9b..4b3f242c5 100644
--- a/notebooks/DSSC/Characterize_DSSC_Darks_NBC.ipynb
+++ b/notebooks/DSSC/Characterize_DSSC_Darks_NBC.ipynb
@@ -390,6 +390,8 @@
     "\n",
     "print('Retrieve pre-existing constants for comparison.')\n",
     "for qm in offset_g.keys():\n",
+    "    old_const[qm] = {}\n",
+    "    old_mdata[qm] = {}\n",
     "    qm_db = qm_dict[qm]\n",
     "    karabo_da = qm_db[\"karabo_da\"]\n",
     "    for const in clist:\n",
@@ -417,21 +419,28 @@
     "                                  cal_db_interface, creation_time=creation_time,\n",
     "                                  verbosity=2, timeout=cal_db_timeout)\n",
     "\n",
-    "        old_const[const] = data\n",
-    "\n",
-    "        if mdata is not None and data is not None:\n",
-    "            time = mdata.calibration_constant_version.begin_at\n",
-    "            old_mdata[const] = time.isoformat()\n",
-    "            os.makedirs(f'{out_folder}/old/', exist_ok=True)\n",
-    "            save_const_to_h5(qm_db[\"db_module\"], karabo_id,\n",
-    "                             getattr(Constants.DSSC, const)(),\n",
-    "                             condition, data, file_loc, report,\n",
-    "                             creation_time,\n",
-    "                             f'{out_folder}/old/')\n",
+    "        old_const[qm][const] = data\n",
+    "\n",
+    "        if mdata is None or data is None:\n",
+    "            old_mdata[qm][const] = {\n",
+    "                \"timestamp\": \"Not found\",\n",
+    "                \"filepath\": None,\n",
+    "                \"h5path\": None\n",
+    "            }\n",
     "        else:\n",
-    "            old_mdata[const] = \"Not found\"\n",
-    "    with open(f\"{out_folder}/module_mapping_{qm}.yml\",\"w\") as fd:\n",
-    "        yaml.safe_dump({\"module_mapping\": {qm: qm_db[\"db_module\"]}}, fd)"
+    "            old_mdata[qm][const] = {\n",
+    "                \"timestamp\": mdata.calibration_constant_version.begin_at.isoformat(),\n",
+    "                \"filepath\": os.path.join(\n",
+    "                    mdata.calibration_constant_version.hdf5path,\n",
+    "                    mdata.calibration_constant_version.filename,\n",
+    "                ),\n",
+    "                \"h5path\": mdata.calibration_constant_version.h5path,\n",
+    "            }\n",
+    "    with open(f\"{out_folder}/module_metadata_{qm}.yml\", \"w\") as fd:\n",
+    "        yaml.safe_dump(\n",
+    "            {\"module\": qm, \"pdu\": qm_db[\"db_module\"], \"old-constants\": old_mdata[qm]},\n",
+    "            fd,\n",
+    "        )"
    ]
   },
   {
@@ -621,9 +630,12 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "display(Markdown('The following pre-existing constants are used for comparison: \\n'))\n",
-    "for key in old_mdata:\n",
-    "    display(Markdown('**{}** at {}'.format(key, old_mdata[key])))"
+    "time_summary = []\n",
+    "for qm, qm_data in old_mdata.items():\n",
+    "    time_summary.append(f\"The following pre-existing constants are used for comparison for module {qm}:\")\n",
+    "    for const, const_data in qm_data.items():\n",
+    "        time_summary.append(f\"- {const} created at {const_data['timestamp']}\")\n",
+    "display(Markdown(\"\\n\".join(time_summary)))"
    ]
   },
   {
@@ -643,8 +655,8 @@
     "\n",
     "        data = np.copy(res[qm][const])\n",
     "\n",
-    "        if old_const[const] is not None:\n",
-    "            dataold = np.copy(old_const[const])\n",
+    "        if old_const[qm][const] is not None:\n",
+    "            dataold = np.copy(old_const[qm][const])\n",
     "\n",
     "        f_list = [np.nanmedian, np.nanmean, np.nanstd, np.nanmin, np.nanmax]\n",
     "        n_list = ['Median', 'Mean', 'Std', 'Min', 'Max']\n",
@@ -652,7 +664,7 @@
     "        for i, f in enumerate(f_list):\n",
     "            line = [n_list[i]]\n",
     "            line.append('{:6.1f}'.format(f(data[...,gain])))\n",
-    "            if old_const[const] is not None:\n",
+    "            if old_const[qm][const] is not None:\n",
     "                line.append('{:6.1f}'.format(f(dataold[...,gain])))\n",
     "            else:\n",
     "                line.append('-')\n",
-- 
GitLab