From 18f6ad62b0d95eede9916ead7d04dc37948003c7 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Mon, 22 Jan 2024 16:37:31 +0100
Subject: [PATCH] another fix for injecting constants with gain modes 1 and 0

---
 .../Jungfrau_Create_Gain_maps_NBC.ipynb       | 117 +++++++-----------
 1 file changed, 48 insertions(+), 69 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
index df26224c0..b471f83bc 100755
--- a/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
@@ -531,62 +531,49 @@
     "out_folder = Path(out_folder)\n",
     "out_folder.mkdir(parents=True, exist_ok=True)\n",
     "\n",
-    "# Inject constants for fixed and adaptive gain maps.\n",
-    "conditions = Conditions.Dark.jungfrau(\n",
-    "    memory_cells=memory_cells, \n",
-    "    bias_voltage=bias_voltage,\n",
-    "    integration_time=integration_time,\n",
-    "    gain_setting=gain_setting,\n",
-    "    gain_mode=gain_mode\n",
-    ")\n",
-    "for parm in conditions.parameters:\n",
-    "    if parm.name == \"Integration Time\":\n",
-    "        print('Setting integration time limits.')\n",
-    "        parm.lower_deviation = integration_time - integration_time_lims[0]\n",
-    "        parm.upper_deviation = integration_time_lims[1] - integration_time\n",
-    "\n",
-    "for parm in conditions.parameters:\n",
-    "    if parm.name == \"Sensor Bias Voltage\":\n",
-    "        print('Setting bias voltage limits.')\n",
-    "        parm.lower_deviation = bias_voltage - bias_voltage_lims[0]\n",
-    "        parm.upper_deviation = bias_voltage_lims[1] - bias_voltage\n",
     "\n",
-    "for da in karabo_da:\n",
-    "    pdu = da_to_pdu[da]\n",
+    "for mode in [0, 1]:\n",
+    "    # Inject constants for fixed and adaptive gain maps.\n",
+    "    conditions = Conditions.Dark.jungfrau(\n",
+    "        memory_cells=memory_cells, \n",
+    "        bias_voltage=bias_voltage,\n",
+    "        integration_time=integration_time,\n",
+    "        gain_setting=gain_setting,\n",
+    "        gain_mode=mode,\n",
+    "    )\n",
+    "    for parm in conditions.parameters:\n",
+    "        if parm.name == \"Integration Time\":\n",
+    "            print('Setting integration time limits.')\n",
+    "            parm.lower_deviation = integration_time - integration_time_lims[0]\n",
+    "            parm.upper_deviation = integration_time_lims[1] - integration_time\n",
+    "\n",
+    "    for parm in conditions.parameters:\n",
+    "        if parm.name == \"Sensor Bias Voltage\":\n",
+    "            print('Setting bias voltage limits.')\n",
+    "            parm.lower_deviation = bias_voltage - bias_voltage_lims[0]\n",
+    "            parm.upper_deviation = bias_voltage_lims[1] - bias_voltage\n",
     "    constants = {\n",
-    "        \"RelativeGain\": gmap_new[da],  # 512, 1024, 1, 3\n",
     "        \"BadPixelsFF\": bad_pixel_maps[da],  # 512, 1024, 1, 3\n",
     "    }\n",
-    "    for cname, cdata in constants.items():\n",
-    "        if local_output:  # Store locally adaptive gain maps only.\n",
-    "            md = save_const_to_h5(\n",
-    "                db_module=pdu,\n",
-    "                karabo_id=karabo_id,\n",
-    "                constant=getattr(Constants.jungfrau, cname)(),\n",
-    "                condition=conditions,\n",
-    "                data=cdata,\n",
-    "                file_loc=file_loc,\n",
-    "                report=report,\n",
-    "                creation_time=creation_time,\n",
-    "                out_folder=out_folder,\n",
-    "            )\n",
-    "            print(f\"Calibration constant {cname} is stored locally at {out_folder}.\\n\")\n",
-    "\n",
-    "        if db_output:\n",
-    "            for mode in [0, 1]:\n",
+    "    if mode:\n",
+    "        # 512, 1024, 1, 3\n",
+    "        constants[\"RelativeGain\"] = gmap_new[da].copy()\n",
+    "        constants[\"RelativeGain\"][..., 1:] *= -1\n",
+    "    else:\n",
+    "        constants[\"RelativeGain\"] = gmap_new[da]\n",
+    "\n",
+    "    for da in karabo_da:\n",
+    "        pdu = da_to_pdu[da]\n",
+    "        for cname, cdata in constants.items():\n",
+    "            if db_output:\n",
+    "                print(f\"Storing maps for {'fixed' if mode else 'adaptive'} gain mode.\")\n",
     "                # Inject both fixed and adaptive gain maps.\n",
     "                for parm in conditions.parameters:\n",
     "                    if parm.name == \"Gain mode\":\n",
-    "                        parm.value =  mode\n",
-    "                if mode and cname == \"RelativeGain\":  # Fixed_gain\n",
-    "                    # Flip sign for medium and low gain.\n",
-    "                    data_to_inject = cdata.copy()\n",
-    "                    data_to_inject[..., 1:] *= -1\n",
-    "                else:\n",
-    "                    data_to_inject = cdata\n",
-    "                print(f\"Storing maps for {'fixed' if mode else 'adaptive'} gain mode.\")\n",
+    "                        parm.value = mode\n",
+    "\n",
     "                const = getattr(Constants.jungfrau, cname)()\n",
-    "                const.data = data_to_inject\n",
+    "                const.data = cdata\n",
     "                send_to_db(\n",
     "                    db_module=pdu,\n",
     "                    karabo_id=karabo_id, \n",
@@ -597,27 +584,19 @@
     "                    cal_db_interface=cal_db_interface,\n",
     "                    creation_time=creation_time,\n",
     "                )\n",
-    "                # TODO: WHY BADPIXELSFF had a different flag to decide injection.\n",
-    "                # bpix_ff = Constants.jungfrau.BadPixelsFF()\n",
-    "                # # TODO: WHY CONDITION DEVIATIONS ARE NOT CONSIDERED FOR THE BADPIXELS\n",
-    "                # condition = Conditions.Dark.jungfrau(\n",
-    "                #     memory_cells=memory_cells, \n",
-    "                #     bias_voltage=bias_voltage,\n",
-    "                #     integration_time=integration_time,\n",
-    "                #     gain_setting=gain_setting,\n",
-    "                # )\n",
-    "\n",
-    "                # bpix_ff.data = constants[\"BadPixelsFF\"]\n",
-    "                # send_to_db(\n",
-    "                #     db_module=pdu,\n",
-    "                #     karabo_id=karabo_id,\n",
-    "                #     constant=bpix_ff, \n",
-    "                #     condition=conditions, \n",
-    "                #     file_loc=file_loc,\n",
-    "                #     report_path='',\n",
-    "                #     cal_db_interface=cal_db_interface,\n",
-    "                #     creation_time=creation_time,\n",
-    "                # )"
+    "            if local_output and mode == 0:  # Store locally adaptive gain maps only.\n",
+    "                md = save_const_to_h5(\n",
+    "                    db_module=pdu,\n",
+    "                    karabo_id=karabo_id,\n",
+    "                    constant=getattr(Constants.jungfrau, cname)(),\n",
+    "                    condition=conditions,\n",
+    "                    data=cdata,\n",
+    "                    file_loc=file_loc,\n",
+    "                    report=report,\n",
+    "                    creation_time=creation_time,\n",
+    "                    out_folder=out_folder,\n",
+    "                )\n",
+    "                print(f\"Calibration constant {cname} is stored locally at {out_folder}.\\n\")"
    ]
   },
   {
-- 
GitLab