From 320dde542d468335ee1b1ccf534ea0c29639c99d Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Wed, 6 May 2020 00:25:50 +0200
Subject: [PATCH] get memory cells and acquistion rate from file

---
 ...aracterize_AGIPD_Gain_FlatFields_NBC.ipynb | 80 +++++++++++--------
 1 file changed, 47 insertions(+), 33 deletions(-)

diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
index 89bb40836..a72c2e167 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
@@ -38,10 +38,10 @@
     "sequences = [0,1,2,3]#,4,5,6,7,8] #,5,6,7,8,9,10] # sequences files to use, range allowed\n",
     "cluster_profile = \"noDB\" # The ipcluster profile to use\n",
     "local_output = True # output constants locally\n",
-    "db_output = True # output constants to database\n",
+    "db_output = False # output constants to database\n",
     "bias_voltage = 300 # detector bias voltage\n",
     "cal_db_interface = \"tcp://max-exfl016:8026#8035\"  # the database interface to use\n",
-    "mem_cells = 250  # number of memory cells used\n",
+    "mem_cells = 0  # number of memory cells used\n",
     "interlaced = False # assume interlaced data format, for data prior to Dec. 2017\n",
     "fit_hook = True # fit a hook function to medium gain slope\n",
     "rawversion = 2 # RAW file format version\n",
@@ -51,7 +51,7 @@
     "high_res_badpix_3d = False # set this to True if you need high-resolution 3d bad pixel plots. Runtime: ~ 1h\n",
     "db_input = True # retreive data from calibration database, setting offset-store will overwrite this\n",
     "deviation_threshold = 0.75 # peaks with an absolute location deviation larger than the medium are are considere bad\n",
-    "acqrate = 2.2 # acquisition rate\n",
+    "acqrate = 0. # acquisition rate\n",
     "use_dir_creation_date = True\n",
     "creation_time = \"\" # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC.ms e.g. 2019-07-04 11:02:41.00\n",
     "gain_setting = 0.1 # gain setting can have value 0 or 1, Default=0.1 for no (None) gain-setting\n",
@@ -61,12 +61,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {
-    "ExecuteTime": {
-     "end_time": "2019-09-13T07:34:24.830847Z",
-     "start_time": "2019-09-13T07:34:24.745094Z"
-    }
-   },
+   "metadata": {},
    "outputs": [],
    "source": [
     "# std library imports\n",
@@ -101,13 +96,24 @@
     "profile = cluster_profile\n",
     "\n",
     "from iCalibrationDB import ConstantMetaData, Constants, Conditions, Detectors, Versions\n",
-    "from cal_tools.agipdlib import get_gain_setting\n",
+    "from cal_tools.agipdlib import get_num_cells, get_acq_rate, get_gain_setting\n",
     "from cal_tools.enums import BadPixels\n",
     "from cal_tools.influx import InfluxLogger\n",
     "from cal_tools.plotting import show_overview, plot_badpix_3d\n",
-    "from cal_tools.tools import gain_map_files, parse_runs, run_prop_seq_from_path, get_notebook_name, get_dir_creation_date, get_random_db_interface\n",
-    "\n",
-    "\n",
+    "from cal_tools.tools import gain_map_files, parse_runs, run_prop_seq_from_path, get_notebook_name, get_dir_creation_date, get_random_db_interface\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2019-09-13T07:34:24.830847Z",
+     "start_time": "2019-09-13T07:34:24.745094Z"
+    }
+   },
+   "outputs": [],
+   "source": [
     "# usually no need to change these lines\n",
     "sensor_size = [128, 512]\n",
     "block_size = [128, 512]\n",
@@ -115,13 +121,6 @@
     "MODULES_PER_QUAD = 4\n",
     "DET_FILE_INSET = \"AGIPD\"\n",
     "\n",
-    "IL_MODE = interlaced\n",
-    "max_cells = mem_cells if not interlaced else mem_cells*2\n",
-    "memory_cells = mem_cells\n",
-    "\n",
-    "\n",
-    "\n",
-    "\n",
     "# Define constant creation time.\n",
     "if creation_time:\n",
     "    try:\n",
@@ -156,36 +155,51 @@
     "limit_trains = 20\n",
     "limit_trains_eval = None\n",
     "\n",
-    "\n",
     "print(\"Parameters are:\")\n",
-    "print(\"Memory cells: {}/{}\".format(memory_cells, max_cells))\n",
+    "\n",
     "print(\"Runs: {}\".format(runs))\n",
     "print(\"Modules: {}\".format(modules))\n",
     "print(\"Sequences: {}\".format(sequences))\n",
-    "print(\"Interlaced mode: {}\".format(IL_MODE))\n",
     "print(\"Using DB: {}\".format(db_output))\n",
     "\n",
+    "\n",
+    "if instrument == \"SPB\":\n",
+    "    loc = \"SPB_DET_AGIPD1M-1\"\n",
+    "    dinstance = \"AGIPD1M1\"\n",
+    "else:\n",
+    "    loc = \"MID_DET_AGIPD1M-1\"\n",
+    "    dinstance = \"AGIPD1M2\"\n",
+    "\n",
+    "cal_db_interface = get_random_db_interface(cal_db_interface)\n",
+    "\n",
     "# these lines can usually stay as is\n",
     "fbase = \"{}/{{}}/RAW-{{}}-AGIPD{{:02d}}-S{{:05d}}.h5\".format(in_folder)\n",
     "gains = np.arange(3)\n",
-    "cells = np.arange(max_cells)\n",
-    "\n",
     "\n",
     "\n",
     "run, prop, seq = run_prop_seq_from_path(in_folder)\n",
     "logger = InfluxLogger(detector=\"AGIPD\", instrument=instrument, mem_cells=mem_cells,\n",
     "                      notebook=get_notebook_name(), proposal=prop)\n",
-    "\n",
-    "if acqrate == 0:\n",
-    "    acqrate = None\n",
+    "channel = 0\n",
+    "fname = fbase.format(runs[0], runs[0].upper(), channel, 0)\n",
+    "if acqrate == 0.:\n",
+    "    acqrate = get_acq_rate(fname, loc, channel)\n",
     "    \n",
-    "if instrument == \"SPB\":\n",
-    "    dinstance = \"AGIPD1M1\"\n",
-    "else:\n",
-    "    dinstance = \"AGIPD1M2\"\n",
+    "\n",
+    "if mem_cells == 0:\n",
+    "    cells = get_num_cells(fname, loc, channel)\n",
+    "    mem_cells = cells  # avoid setting twice\n",
     "    \n",
     "\n",
-    "cal_db_interface = get_random_db_interface(cal_db_interface)"
+    "IL_MODE = interlaced\n",
+    "max_cells = mem_cells if not interlaced else mem_cells*2\n",
+    "memory_cells = mem_cells\n",
+    "print(\"Interlaced mode: {}\".format(IL_MODE))\n",
+    "\n",
+    "cells = np.arange(max_cells)\n",
+    "\n",
+    "print(f\"Acquisition rate and memory cells set from file {fname} are \" \n",
+    "      f\"{acqrate} MHz and {max_cells}, respectively\")"
    ]
   },
   {
-- 
GitLab