diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
index dd4b6f45042430ebf1845a23518e20e59f44fd69..00b35d2395e1a5891ac897f27bc1d5cd63627d9e 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
@@ -639,34 +639,30 @@
     "        slow_paths = control_names[gg], karabo_id_control\n",
     "        fast_paths = fast_data_filename, loc, channel\n",
     "        acq_rate = get_acq_rate(slow_paths, fast_paths)\n",
-    "    \n",
-    "    print(f'Rate is {acq_rate}')\n",
     "\n",
     "    thresholds_offset, thresholds_offset_sigma, thresholds_noise, thresholds_noise_sigma = bp_thresh \n",
     "    thresholds_offset_hard = thresholds_offset[gg]\n",
     "    thresholds_noise_hard = thresholds_noise[gg]\n",
-    "    infile = h5py.File(fast_data_filename, \"r\", driver=\"core\")\n",
     "    \n",
     "    h5path = h5path.format(channel)\n",
     "    h5path_idx = h5path_idx.format(channel)\n",
     "    \n",
-    "    if rawversion == 2:\n",
-    "        count = np.squeeze(infile[f\"{h5path_idx}/count\"])\n",
-    "        first = np.squeeze(infile[f\"{h5path_idx}/first\"])\n",
-    "        last_index = int(first[count != 0][-1]+count[count != 0][-1])\n",
-    "        first_index = int(first[count != 0][0])\n",
-    "    else:\n",
-    "        status = np.squeeze(infile[f\"{h5path_idx}/status\"])\n",
-    "        if np.count_nonzero(status != 0) == 0:\n",
-    "            return\n",
-    "        last = np.squeeze(infile[f\"{h5path_idx}/last\"])\n",
-    "        first = np.squeeze(infile[f\"{h5path_idx}/first\"])\n",
-    "        last_index = int(last[status != 0][-1]) + 1\n",
-    "        first_index = int(first[status != 0][0])\n",
-    "    im = np.array(infile[f\"{h5path}/data\"][first_index:last_index,...])    \n",
-    "    cellIds = np.squeeze(infile[f\"{h5path}/cellId\"][first_index:last_index,...]) \n",
-    "    \n",
-    "    infile.close()\n",
+    "    with h5py.File(fast_data_filename, \"r\", driver=\"core\") as infile:\n",
+    "        if rawversion == 2:\n",
+    "            count = np.squeeze(infile[f\"{h5path_idx}/count\"])\n",
+    "            first = np.squeeze(infile[f\"{h5path_idx}/first\"])\n",
+    "            last_index = int(first[count != 0][-1]+count[count != 0][-1])\n",
+    "            first_index = int(first[count != 0][0])\n",
+    "        else:\n",
+    "            status = np.squeeze(infile[f\"{h5path_idx}/status\"])\n",
+    "            if np.count_nonzero(status != 0) == 0:\n",
+    "                return\n",
+    "            last = np.squeeze(infile[f\"{h5path_idx}/last\"])\n",
+    "            first = np.squeeze(infile[f\"{h5path_idx}/first\"])\n",
+    "            last_index = int(last[status != 0][-1]) + 1\n",
+    "            first_index = int(first[status != 0][0])\n",
+    "        im = np.array(infile[f\"{h5path}/data\"][first_index:last_index,...])    \n",
+    "        cellIds = np.squeeze(infile[f\"{h5path}/cellId\"][first_index:last_index,...]) \n",
     "\n",
     "    if il_mode:\n",
     "        ga = im[1::2, 0, ...]\n",