From 9886fa3e330ab004dec7315b856457ab8691b50f Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 1 Sep 2022 14:14:43 +0200
Subject: [PATCH] Remove overwrite, try/except memory cells

---
 ...Jungfrau_Gain_Correct_and_Verify_NBC.ipynb | 35 ++++++-----------
 ...retrieve_constants_precorrection_NBC.ipynb | 39 +++++++------------
 .../ePix100/Correction_ePix100_NBC.ipynb      |  2 +-
 src/cal_tools/epix100/epix100lib.py           |  7 +---
 4 files changed, 27 insertions(+), 56 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
index a6eaec6a3..3e2ef30e0 100644
--- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
@@ -38,12 +38,11 @@
     "cal_db_timeout = 180000  # timeout on caldb requests\n",
     "\n",
     "# Parameters affecting corrected data.\n",
-    "overwrite = True  # set to True if existing data should be overwritten\n",
     "relative_gain = True  # do relative gain correction\n",
     "limit_images = 0  # ONLY FOR TESTING. process only first N images, Use 0 to process all.\n",
     "\n",
     "# Parameters for retrieving calibration constants\n",
-    "manual_slow_data = False  # if true, use manually entered bias_voltage and integration_time values\n",
+    "manual_slow_data = False  # if true, use manually entered bias_voltage, integration_time, gain_setting, and gain_mode values\n",
     "integration_time = 4.96  # integration time in us, will be overwritten by value in file\n",
     "gain_setting = 0  # 0 for dynamic gain, 1 for dynamic HG0, will be overwritten by value in file\n",
     "gain_mode = 0  # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True.\n",
@@ -118,10 +117,7 @@
     "run_dc = RunDirectory(run_folder)\n",
     "instrument_src = instrument_source_template.format(karabo_id, receiver_template)\n",
     "\n",
-    "if out_folder.exists() and not overwrite:\n",
-    "    raise AttributeError(\"Output path exists! Exiting\")\n",
-    "else:\n",
-    "    out_folder.mkdir(parents=True, exist_ok=True)\n",
+    "out_folder.mkdir(parents=True, exist_ok=True)\n",
     "\n",
     "print(f\"Run is: {run}\")\n",
     "print(f\"Instrument H5File source: {instrument_src}\")\n",
@@ -185,25 +181,16 @@
    "source": [
     "ctrl_src = ctrl_source_template.format(karabo_id_control)\n",
     "ctrl_data = JungfrauCtrl(run_dc, ctrl_src)\n",
-    "try:\n",
-    "    this_run_mcells, sc_start = ctrl_data.get_memory_cells()\n",
     "\n",
-    "    if this_run_mcells == 1:\n",
-    "        memory_cells = 1\n",
-    "        print(\"Run is in single cell mode.\\n\"\n",
-    "              f\"Storage cell start: {sc_start:02d}\")\n",
-    "    else:\n",
-    "        memory_cells = 16\n",
-    "        print(f\"Run is in burst mode.\\n\"\n",
-    "              f\"Storage cell start: {sc_start:02d}\")\n",
-    "except KeyError as e:\n",
-    "    print(\"WARNING: KeyError while reading number of memory cells.\")\n",
-    "    if mem_cells == 0:\n",
-    "        memory_cells = 1\n",
-    "    else:\n",
-    "        memory_cells = mem_cells\n",
-    "    print(f\"WARNING: Set memory cells to {memory_cells}, as \"\n",
-    "          \"it is not saved in control information.\")\n",
+    "if mem_cells < 0:\n",
+    "    memory_cells, sc_start = ctrl_data.get_memory_cells()\n",
+    "\n",
+    "    mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n",
+    "    print(f\"Run is in {mem_cells_name} mode.\\nStorage cell start: {sc_start:02d}\")\n",
+    "else:\n",
+    "    memory_cells = mem_cells\n",
+    "    mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n",
+    "    print(f\"Run is in manually set to {mem_cells_name} mode. With {memory_cells} memory cells\")\n",
     "\n",
     "if not manual_slow_data:\n",
     "    integration_time = ctrl_data.get_integration_time()\n",
diff --git a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb
index 74f6b5773..4f0e4686b 100644
--- a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb
@@ -38,15 +38,14 @@
     "cal_db_timeout = 180000  # timeout on cal db requests\n",
     "\n",
     "# Parameters affecting corrected data.\n",
-    "overwrite = True  # set to True if existing data should be overwritten\n",
     "relative_gain = True  # do relative gain correction\n",
     "\n",
     "# Parameters for retrieving calibration constants\n",
-    "manual_slow_data = False  # if true, use manually entered bias_voltage and integration_time values\n",
+    "manual_slow_data = False  # if true, use manually entered bias_voltage, integration_time, gain_setting, and gain_mode values\n",
     "integration_time = 4.96  # integration time in us, will be overwritten by value in file\n",
     "gain_setting = 0  # 0 for dynamic gain, 1 for dynamic HG0, will be overwritten by value in file\n",
     "gain_mode = 0  # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True.\n",
-    "mem_cells = 0  # leave memory cells equal 0, as it is saved in control information starting 2019.\n",
+    "mem_cells = -1  # leave memory cells equal 0, as it is saved in control information starting 2019.\n",
     "bias_voltage = 180  # will be overwritten by value in file"
    ]
   },
@@ -84,10 +83,7 @@
     "run_folder = in_folder / f'r{run:04d}'\n",
     "run_dc = RunDirectory(run_folder)\n",
     "\n",
-    "if out_folder.exists() and not overwrite:\n",
-    "    raise AttributeError(\"Output path exists! Exiting\")\n",
-    "else:\n",
-    "    out_folder.mkdir(parents=True, exist_ok=True)\n",
+    "out_folder.mkdir(parents=True, exist_ok=True)\n",
     "\n",
     "creation_time = None\n",
     "if use_dir_creation_date:\n",
@@ -106,25 +102,16 @@
    "source": [
     "ctrl_src = ctrl_source_template.format(karabo_id_control)\n",
     "ctrl_data = JungfrauCtrl(run_dc, ctrl_src)\n",
-    "try:\n",
-    "    this_run_mcells, sc_start = ctrl_data.get_memory_cells()\n",
     "\n",
-    "    if this_run_mcells == 1:\n",
-    "        memory_cells = 1\n",
-    "        print(\"Run is in single cell mode.\\n\"\n",
-    "              f\"Storage cell start: {sc_start:02d}\")\n",
-    "    else:\n",
-    "        memory_cells = 16\n",
-    "        print(f\"Run is in burst mode.\\n\"\n",
-    "              f\"Storage cell start: {sc_start:02d}\")\n",
-    "except KeyError as e:\n",
-    "    print(\"WARNING: KeyError while reading number of memory cells.\")\n",
-    "    if mem_cells == 0:\n",
-    "        memory_cells = 1\n",
-    "    else:\n",
-    "        memory_cells = mem_cells\n",
-    "    print(f\"WARNING: Set memory cells to {memory_cells}, as \"\n",
-    "          \"it is not saved in control information.\")\n",
+    "if mem_cells < 0:\n",
+    "    memory_cells, sc_start = ctrl_data.get_memory_cells()\n",
+    "\n",
+    "    mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n",
+    "    print(f\"Run is in {mem_cells_name} mode.\\nStorage cell start: {sc_start:02d}\")\n",
+    "else:\n",
+    "    memory_cells = mem_cells\n",
+    "    mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n",
+    "    print(f\"Run is in manually set to {mem_cells_name} mode. With {memory_cells} memory cells\")\n",
     "\n",
     "if not manual_slow_data:\n",
     "    integration_time = ctrl_data.get_integration_time()\n",
@@ -247,7 +234,7 @@
     "            module_timestamps[cname] = \"NA\"\n",
     "\n",
     "time_summary = retrieved_constants.setdefault(\"time-summary\", {})\n",
-    "time_summary[\"SAll\"] = timestamps\n",
+    "time_summary = timestamps\n",
     "\n",
     "metadata.save()"
    ]
diff --git a/notebooks/ePix100/Correction_ePix100_NBC.ipynb b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
index 35f8c78a4..cad001b9c 100644
--- a/notebooks/ePix100/Correction_ePix100_NBC.ipynb
+++ b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
@@ -47,7 +47,7 @@
     "bias_voltage = 200  # bias voltage\n",
     "in_vacuum = False  # detector operated in vacuum\n",
     "integration_time = -1  # Detector integration time, Default value -1 to use the value from the slow data.\n",
-    "fix_temperature = -1  # fixed temperature value in Kelvin, Default value -1 to use the value from files.\n",
+    "fix_temperature = 290  # fixed temperature value in Kelvin, Default value -1 to use the value from files.\n",
     "gain_photon_energy = 9.0  # Photon energy used for gain calibration\n",
     "photon_energy = 0.  # Photon energy to calibrate in number of photons, 0 for calibration in keV\n",
     "\n",
diff --git a/src/cal_tools/epix100/epix100lib.py b/src/cal_tools/epix100/epix100lib.py
index 19a196178..ee501960d 100644
--- a/src/cal_tools/epix100/epix100lib.py
+++ b/src/cal_tools/epix100/epix100lib.py
@@ -22,8 +22,5 @@ class epix100Ctrl():
             self.ctrl_src, 'expTime.value'].as_single_value(reduce_by='first')
 
     def get_temprature(self):
-        temp = self.run_dc[
-            self.instrument_src, 'data.backTemp'].as_single_value(
-                reduce_by='mean', rtol=1) / 100
-
-        return temp[0] if hasattr(temp, '__len__') else temp
+        return self.run_dc[
+            self.instrument_src, 'data.backTemp'].ndarray().mean() / 100
-- 
GitLab