From 5514356fa2f5b56bdea5df900be7cdb25013defc Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Wed, 18 Sep 2019 14:38:52 +0200
Subject: [PATCH] avoid retrieving with 0 mem cells

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

diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py
index 455ee524d..b7281dd8d 100644
--- a/cal_tools/cal_tools/agipdlib.py
+++ b/cal_tools/cal_tools/agipdlib.py
@@ -15,6 +15,8 @@ def get_num_cells(fname, loc, module):
     with h5py.File(fname, "r") as f:
         cells = \
         f["INSTRUMENT/{}/DET/{}CH0:xtdf/image/cellId".format(loc, module)][()]
+        if cells.shape[0] == 0:
+            return None
         maxcell = np.max(cells)
         options = [4, 32, 64, 76, 128, 176, 202, 250]
         dists = [abs(o - maxcell) for o in options]
@@ -23,10 +25,13 @@ def get_num_cells(fname, loc, module):
 
 def get_acq_rate(fname, loc, module):
     with h5py.File(fname, "r") as f:
-        pulses = \
-            np.squeeze(f["INSTRUMENT/{}/DET/{}CH0:xtdf/image/pulseId".format(
-                loc, module)][:2])
-        diff = pulses[1] - pulses[0]
+        try:
+            pulses = \
+                np.squeeze(f["INSTRUMENT/{}/DET/{}CH0:xtdf/image/pulseId".format(
+                    loc, module)][:2])
+            diff = pulses[1] - pulses[0]
+        except:
+            diff = 0
         options = {8: 0.5, 4: 1.1, 2: 2.2, 1: 4.5}
         return options.get(diff, None)
 
diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 23e304ff0..06c8ed1f4 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -414,7 +414,11 @@
     "        \n",
     "        if max_cells == 0:\n",
     "            max_cells = get_num_cells(filename, loc, channel)\n",
-    "            cells = np.arange(max_cells)\n",
+    "            if max_cells == 0:\n",
+    "                raise ValueError(\"Number of memory cells are zero, \"\n",
+    "                                 \"No processed data for {}\".format(qm))\n",
+    "            else:\n",
+    "                cells = np.arange(max_cells)\n",
     "            \n",
     "        if acq_rate == 0.:\n",
     "            acq_rate = get_acq_rate(filename, loc, channel)\n",
-- 
GitLab