From 28e0e5f0d343644bfa3e13edc1ce45309b051955 Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cydanil@gmail.com>
Date: Sun, 20 Sep 2020 10:54:18 +0200
Subject: [PATCH] Clarify documentation in agipdlib

---
 cal_tools/cal_tools/agipdlib.py | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py
index 6268fcfe6..5622e9e65 100644
--- a/cal_tools/cal_tools/agipdlib.py
+++ b/cal_tools/cal_tools/agipdlib.py
@@ -30,11 +30,11 @@ def get_acq_rate(slow_paths: Tuple[str, str],
     """Get the acquisition rate from said detector module.
 
     If the data is available from the middlelayer FPGA_COMP device, then it is
-    retrieved from there. If not, the rate is calculated from two different pulses
-    time.
+    retrieved from there. If not, the rate is calculated from two different
+    pulses time.
 
-    The first entry is deliberatly not used, as the detector just began operating,
-    and it might have skipped a train.
+    The first entry is deliberatly not used, as the detector just began
+    operating, and it might have skipped a train.
 
     :param slow_paths: in which file and h5 path to look for slow data.
                        The first string is the filename with complete path,
@@ -43,19 +43,24 @@ def get_acq_rate(slow_paths: Tuple[str, str],
     :param fast_paths: in which module file and h5 path to look for pulses.
                        The first string is the filename with complete path,
                        the second string is the module device name `karabo_id`,
-                       the third parameter is the module number, used to navigate
-                       through the h5 file structure.
+                       the third parameter is the module number, used to
+                       navigate through the h5 file structure.
 
-    :return acq_rate: the acquisition rate. If not found in either files, return None.
+    :return acq_rate: the acquisition rate.
+                      If not found in either files, return None.
     """
     # Attempt to look for acquisition rate in slow data
     slow_data_file, karabo_id_control = slow_paths
     slow_data_file = Path(slow_data_file)
-    if slow_data_file.is_file():  
+    if slow_data_file.is_file():
         slow_data_path = f'CONTROL/{karabo_id_control}/MDL/FPGA_COMP/bunchStructure/repetitionRate/value'  # noqa
         with h5py.File(slow_data_file, "r") as fin:
             if slow_data_path in fin:
-                return fin[slow_data_path][3]
+                # The rep. rate value is stored in a 1D array of type float.
+                # Use the 3rd value, arbitrarily chosen. It's okay to loose
+                # precision here because the usage is about defining the rate
+                # for meta-data.
+                return round(fin[slow_data_path][3], 1)
 
     # Compute acquisition rate from fast data
     fast_data_file, karabo_id, module = fast_paths
@@ -64,6 +69,9 @@ def get_acq_rate(slow_paths: Tuple[str, str],
         fast_data_path = f'INSTRUMENT/{karabo_id}/DET/{module}CH0:xtdf/image/pulseId'  # noqa
         with h5py.File(fast_data_file) as fin:
             if fast_data_path in fin:
+                # pulses is of shape (NNNN, 1), of type uint8.
+                # Squeeze out the data, and subtract the 3rd entry from the 2nd
+                # to get a rate.
                 pulses = np.squeeze(fin[fast_data_path][1:3])
                 diff = pulses[1] - pulses[0]
                 options = {8: 0.5, 4: 1.1, 2: 2.2, 1: 4.5}
@@ -92,7 +100,7 @@ def get_gain_setting(fname: str, h5path_ctrl: str) -> int:
     with h5py.File(fname, "r") as fin:
         if gain_path in fin:
             return fin[gain_path][0]
- 
+
         # Get the index at which the train is not zero.
         train_id = fin["INDEX/trainId"][()]
         idx = np.nonzero(train_id)[0][0]
-- 
GitLab