From e344bad5ac3952b3a34e78410ce4504a2f58dcb0 Mon Sep 17 00:00:00 2001
From: Steffen Hauf <steffen.hauf@xfel.eu>
Date: Thu, 5 Sep 2019 15:06:25 +0200
Subject: [PATCH] Introduce pulse limiting

---
 cal_tools/cal_tools/agipdlib.py                |  6 +++++-
 notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb | 11 ++++++-----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py
index f9f6d7bd3..43958f79d 100644
--- a/cal_tools/cal_tools/agipdlib.py
+++ b/cal_tools/cal_tools/agipdlib.py
@@ -1139,15 +1139,19 @@ class AgipdCorrections:
         max_cells = self.max_cells
         agipd_base = self.agipd_base
         allcells = np.squeeze(self.infile[agipd_base + "image/cellId"])
+        allpulses = np.squeeze(self.infile[agipd_base + "image/pulseId"])
         if self.valid_indices is not None:
             allcells = allcells[self.valid_indices]
+            allpulses = allpulses[self.valid_indices]
         else:
             allcells = allcells[first_index:last_index]
+            allpulses = allpulses[first_index:last_index]
 
         single_image = self.infile[agipd_base + "image/data"][first_index, ...]
         single_image = np.array(single_image)
 
-        can_calibrate = allcells < max_cells
+        can_calibrate = (allcells < max_cells) & (allpulses < np.max(allpulses[:self.max_pulses]))
+        
         if np.count_nonzero(can_calibrate) == 0:
             return
         allcells = allcells[can_calibrate]
diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 23e304ff0..398e73426 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -22,9 +22,9 @@
    },
    "outputs": [],
    "source": [
-    "in_folder = \"/gpfs/exfel/exp/MID/201931/p900090/raw/\" # the folder to read data from, required\n",
-    "run = 5 # runs to process, required\n",
-    "out_folder =  \"/gpfs/exfel/exp/MID/201931/p900090/proc/\"  # the folder to output to, required\n",
+    "in_folder = \"/gpfs/exfel/exp/SPB/201931/p900086/raw/\" # the folder to read data from, required\n",
+    "run = 34 # runs to process, required\n",
+    "out_folder =  \"/gpfs/exfel/exp/SPB/201931/p900086/proc/\"  # the folder to output to, required\n",
     "calfile =  \"/gpfs/exfel/data/scratch/haufs/agipd_on_demand/agipd_store_mid.h5\" # path to calibration file. Leave empty if all data should come from DB\n",
     "sequences =  [-1] # sequences to correct, set to -1 for all, range allowed\n",
     "mem_cells = 0 # number of memory cells used, set to 0 to automatically infer\n",
@@ -52,7 +52,7 @@
     "max_cells_db = 0 # set to a value different than 0 to use this value for DB queries\n",
     "chunk_size_idim = 1  # chunking size of imaging dimension, adjust if user software is sensitive to this.\n",
     "creation_date_offset = \"00:00:00\" # add an offset to creation date, e.g. to get different constants\n",
-    "instrument = \"MID\"  # the instrument the detector is installed at, required\n",
+    "instrument = \"SPB\"  # the instrument the detector is installed at, required\n",
     "force_hg_if_below = 1000 # set to a value other than 0 to force a pixel into high gain if it's high gain offset subtracted value is below this threshold\n",
     "force_mg_if_below = 1000 # set to a value other than 0 to force a pixel into medium gain if it's medium gain offset subtracted value is below this threshold\n",
     "mask_noisy_adc = 0.25 # set to a value other than 0 and below 1 to mask entire ADC if fraction of noisy pixels is above\n",
@@ -363,7 +363,7 @@
     "    from cal_tools.enums import BadPixels\n",
     "    from cal_tools.agipdlib import AgipdCorrections, SnowResolution\n",
     "    from cal_tools.agipdlib import get_num_cells, get_acq_rate\n",
-    "    \n",
+    "    from time import sleep\n",
     "  \n",
     "    #client = InfluxDBClient('exflqr18318', 8086, 'root', 'root', 'calstats')\n",
     "\n",
@@ -457,6 +457,7 @@
     "        except IOError:\n",
     "            return\n",
     "        if not nodb:\n",
+    "            # sleep(np.random.randint(60))\n",
     "            when = agipd_corr.initialize_from_db(dbparms, qm, only_dark=(fileparms != \"\"))\n",
     "            print(when)\n",
     "        if fileparms != \"\":\n",
-- 
GitLab