From 4251181021cfae8dcbb1a3e770b0ce747557c038 Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Fri, 27 Sep 2019 16:55:11 +0200
Subject: [PATCH] Make BP injection steerable for instrument

---
 .../AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb | 45 +++++++++++++------
 1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb b/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
index af08e7044..fd917f539 100644
--- a/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
+++ b/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
@@ -38,12 +38,12 @@
    "source": [
     "in_folder = '/gpfs/exfel/exp/MID/201931/p900091/raw' # path to input data, required\n",
     "modules = [1,] # modules to work on, required, range allowed\n",
-    "out_folder = \"/gpfs/exfel/exp/SPB/201931/p900091/usr/PC/\" # path to output to, required\n",
+    "out_folder = \"/gpfs/exfel/exp/MID/201931/p900091/usr/PC/4.5_250/\" # path to output to, required\n",
     "runs = [30, 23, 24, 25, 26, 27, 28, 29] # runs to use, required, range allowed\n",
     "n_sequences = 3 # number of sequence files, starting for 0 to evaluate\n",
-    "cluster_profile = \"noDB44\" # The ipcluster profile to use\n",
+    "cluster_profile = \"noDB\" # The ipcluster profile to use\n",
     "local_output = True # output constants locally\n",
-    "db_output = False # output constants to database\n",
+    "db_output = True # output constants to database\n",
     "bias_voltage = 300 # detector bias voltage\n",
     "cal_db_interface = \"tcp://max-exfl016:8019\"  # the database interface to use\n",
     "mem_cells = 0.  # number of memory cells used, use 0 to auto-derive\n",
@@ -53,7 +53,8 @@
     "instrument = \"MID\"\n",
     "high_res_badpix_3d = False # set this to True if you need high-resolution 3d bad pixel plots. Runtime: ~ 1h\n",
     "acq_rate = 0. # the detector acquisition rate, use 0 to try to auto-determine\n",
-    "dont_use_dir_date = False"
+    "dont_use_dir_date = False\n",
+    "gain_setting = 0.1 # gain setting can have value 0 or 1, Default=0.1 for no (None) gain-setting"
    ]
   },
   {
@@ -122,7 +123,12 @@
     "    loc = \"MID_DET_AGIPD1M-1\"\n",
     "    dinstance = \"AGIPD1M2\"\n",
     "print(\"Detector in use is {}\".format(loc))\n",
-    "\n"
+    "\n",
+    "# Convert gain-setting in case of still being 0.1\n",
+    "if gain_setting == 0.1:\n",
+    "    gain_setting = None\n",
+    "    \n",
+    "print(\"Gain setting: {}\".format(gain_setting))"
    ]
   },
   {
@@ -524,7 +530,8 @@
     "                                                     Constants.AGIPD.Offset(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                     gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Offset for {} was injected on {}\".format(qm, when))\n",
@@ -534,7 +541,8 @@
     "                                                     Constants.AGIPD.Noise(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                     gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Noise for {} was injected on {}\".format(qm, when))\n",
@@ -544,7 +552,8 @@
     "                                                     Constants.AGIPD.ThresholdsDark(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                     gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Threshold for {} was injected on {}\".format(qm, when))\n",
@@ -1344,6 +1353,16 @@
     "    store_file.close()"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n",
+    "file_loc = proposal + ' ' + ' '.join(list(map(str,runs)))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -1355,8 +1374,6 @@
    },
    "outputs": [],
    "source": [
-    "#if instrument == \"MID\":\n",
-    "#    acq_rate = None\n",
     "if db_output:\n",
     "    for qm, r in fres.items():\n",
     "        metadata = ConstantMetaData()\n",
@@ -1366,11 +1383,12 @@
     "\n",
     "        # set the operating condition\n",
     "        condition = Conditions.Dark.AGIPD(memory_cells=maxcells, bias_voltage=bias_voltage,\n",
-    "                                          acquisition_rate=acq_rate)\n",
+    "                                          acquisition_rate=acq_rate, gain_setting=gain_setting)\n",
     "        metadata.detector_condition = condition\n",
     "\n",
     "        # specify the a version for this constant\n",
     "        metadata.calibration_constant_version = Versions.Now(device=getattr(getattr(Detectors, dinstance), qm))\n",
+    "        metadata.calibration_constant_version.raw_data_location = file_loc\n",
     "        metadata.send(cal_db_interface)\n",
     "        \n",
     "        # bad pixels\n",
@@ -1382,11 +1400,12 @@
     "        \n",
     "        # set the operating condition\n",
     "        condition = Conditions.Dark.AGIPD(memory_cells=maxcells, bias_voltage=bias_voltage,\n",
-    "                                          acquisition_rate=acq_rate)\n",
+    "                                          acquisition_rate=acq_rate, gain_setting=gain_setting)\n",
     "        metadata.detector_condition = condition\n",
     "\n",
     "        # specify the a version for this constant\n",
-    "        metadata.calibration_constant_version = Versions.Now(device=getattr(Detectors.AGIPD1M1, qm))\n",
+    "        metadata.calibration_constant_version = Versions.Now(device=getattr(getattr(Detectors, dinstance), qm))\n",
+    "        metadata.calibration_constant_version.raw_data_location = file_loc\n",
     "        metadata.send(cal_db_interface)"
    ]
   },
-- 
GitLab