From faee68c2d6a6639eb6033ec924ef305467d8c0f9 Mon Sep 17 00:00:00 2001
From: Andrey Samartsev <andrey.samatrtsev@xfel.eu>
Date: Tue, 23 Feb 2021 02:04:03 +0100
Subject: [PATCH] changes after MR revision

---
 cal_tools/cal_tools/dssclib.py | 93 +++++++++++++++++-----------------
 1 file changed, 46 insertions(+), 47 deletions(-)

diff --git a/cal_tools/cal_tools/dssclib.py b/cal_tools/cal_tools/dssclib.py
index c9dc25e15..83271eb9a 100644
--- a/cal_tools/cal_tools/dssclib.py
+++ b/cal_tools/cal_tools/dssclib.py
@@ -76,16 +76,16 @@ def get_dssc_ctrl_data(in_folder, slow_data_pattern,
 
     ctrlloc = None
     filename = next(iter(ctrlDataFiles.values()))
-    ctlrh5file = h5py.File(filename, 'r')
-    if '/METADATA/dataSources/deviceId' in ctlrh5file:
-        ctrlloc = ctlrh5file['/METADATA/dataSources/deviceId'][0]
-        daq_format = ctlrh5file['/METADATA/dataFormatVersion'][0].decode(
-            "utf-8")
-    elif '/METADATA/deviceId' in ctlrh5file:
-        ctrlloc = ctlrh5file['/METADATA/deviceId'][0]
-    else:
-        print("No Slow Control Data found in files!")
-        return targetGainAll, encodedGainAll, operatingFreqAll
+    with h5py.File(filename, 'r') as ctlrh5file:
+        if '/METADATA/dataSources/deviceId' in ctlrh5file:
+            ctrlloc = ctlrh5file['/METADATA/dataSources/deviceId'][0]
+            daq_format = ctlrh5file['/METADATA/dataFormatVersion'][0].decode(
+                "utf-8")
+        elif '/METADATA/deviceId' in ctlrh5file:
+            ctrlloc = ctlrh5file['/METADATA/deviceId'][0]
+        else:
+            print("No Slow Control Data found in files!")
+            return targetGainAll, encodedGainAll, operatingFreqAll
 
     ctrlloc = ctrlloc.decode("utf-8")
     ctrlloc = ctrlloc[:ctrlloc.find('/')]
@@ -95,43 +95,42 @@ def get_dssc_ctrl_data(in_folder, slow_data_pattern,
     operatingFreqs = {}
     for quadrant, file in ctrlDataFiles.items():
         if len(file):
-            h5file = h5py.File(file)
-            if daq_format != '':
-                epcConfig = h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/epcRegisterFilePath/value'][0]\
-                    .decode("utf-8")
-                epcConfig = epcConfig[epcConfig.rfind('/') + 1:]
-
-                print(f"EPC configuration: {epcConfig}")
-                targGain = _extr_gainparam_conffilename(epcConfig)
-                tGain[quadrant] = float(
-                    targGain) if targGain is not None else 0.0
-                # 0.0 is default value for TG
-            else:
-                tGain[quadrant] = 0.0
-
-            gainSettingsMap = {}
-            for coarseParam in ['fcfEnCap', 'csaFbCap', 'csaResistor']:
-                gainSettingsMap[coarseParam] = int(
-                    h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/gain/{coarseParam}/value'][0])
-
-            iramp_path = f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/gain/irampFineTrm/value'
-            if daq_format != '':
-                irampSettings = h5file[iramp_path][0].decode("utf-8")
-            elif iramp_path in h5file:
-                irampSettings = h5file[iramp_path][0]
-            else:
-                irampSettings = "Various"
-
-            gainSettingsMap['trimmed'] = np.int64(
-                1) if irampSettings == "Various" else np.int64(0)
-
-            encodedGain[quadrant] = _get_gain_encoded_val(gainSettingsMap)
-
-            opFreq = h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/sequencer/cycleLength/value'][0]
-            # The Operating Frequency of the detector should be in MHz.
-            # Here the karabo operation mode is converted to acquisition rate:
-            # 22 corresponds to 4.5 MHz, 44 to 2.25 MHz, etc.
-            operatingFreqs[quadrant] = 4.5 * (22.0 / opFreq)
+            with h5py.File(file) as h5file:
+                if not daq_format:
+                    tGain[quadrant] = 0.0 # 0.0 is default value for TG
+                else:
+                    epcConfig = h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/epcRegisterFilePath/value'][0]\
+                        .decode("utf-8")
+                    epcConfig = epcConfig[epcConfig.rfind('/') + 1:]
+
+                    print(f"EPC configuration: {epcConfig}")
+                    targGain = _extr_gainparam_conffilename(epcConfig)
+                    tGain[quadrant] = float(
+                        targGain) if targGain is not None else 0.0                    
+
+                gainSettingsMap = {}
+                for coarseParam in ['fcfEnCap', 'csaFbCap', 'csaResistor']:
+                    gainSettingsMap[coarseParam] = int(
+                        h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/gain/{coarseParam}/value'][0])
+
+                iramp_path = f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/gain/irampFineTrm/value'
+                if daq_format != '':
+                    irampSettings = h5file[iramp_path][0].decode("utf-8")
+                elif iramp_path in h5file:
+                    irampSettings = h5file[iramp_path][0]
+                else:
+                    irampSettings = "Various"
+
+                gainSettingsMap['trimmed'] = np.int64(
+                    1) if irampSettings == "Various" else np.int64(0)
+
+                encodedGain[quadrant] = _get_gain_encoded_val(gainSettingsMap)
+
+                opFreq = h5file[f'/RUN/{ctrlloc}/FPGA/PPT_Q{quadrant}/sequencer/cycleLength/value'][0]
+                # The Operating Frequency of the detector should be in MHz.
+                # Here the karabo operation mode is converted to acquisition rate:
+                # 22 corresponds to 4.5 MHz, 44 to 2.25 MHz, etc.
+                operatingFreqs[quadrant] = 4.5 * (22.0 / opFreq)
         else:
             print(f"no slow data for quadrant {quadrant} is found")
 
-- 
GitLab