From a242d6f7fe229bff4028c04d3845ee8b2a9d8e4e Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Fri, 4 Aug 2023 09:17:48 +0000
Subject: [PATCH] Bugfixes in statistics writer

---
 README.md                                     |  2 +
 VERSION                                       |  2 +-
 .../statistics/writer/hdf5.py                 | 52 +++++++++++--------
 3 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index eb7c1df..21b8073 100644
--- a/README.md
+++ b/README.md
@@ -105,6 +105,8 @@ tox -e debug tests.requests.test_prometheus
 ```
 
 ## Releasenotes
+- 0.15.7 - Fix: Recording LBA statistics does not request HBA-only metadata
+         - Fix: Syntax error when querying SDP metadata
 - 0.15.6 - Represent BSTs in 488x2 arrays
          - l2ss-statistics-writer: Fix syntax error in printing help
          - More resilience against errors when interfacing with Tango
diff --git a/VERSION b/VERSION
index c619394..3b1c794 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.15.6
+0.15.7
diff --git a/lofar_station_client/statistics/writer/hdf5.py b/lofar_station_client/statistics/writer/hdf5.py
index f8dadd3..c232acf 100644
--- a/lofar_station_client/statistics/writer/hdf5.py
+++ b/lofar_station_client/statistics/writer/hdf5.py
@@ -375,38 +375,20 @@ class HDF5Writer(ABC):
                 matrix_name,
             )
 
-    def get_matrix(self):
-        """Constructs a matrix for storing in the hdf5 file"""
-
-        matrix = self.get_matrix_data()
-
-        # add generic info
-        matrix.nof_payload_errors = self.current_collector.parameters[
-            "nof_payload_errors"
-        ]
-        matrix.nof_valid_payloads = self.current_collector.parameters[
-            "nof_valid_payloads"
-        ]
-        matrix.gn_indices = self.current_collector.parameters["gn_indices"]
-
-        # Stores the header of the packet received for this matrix as a list of
-        # attributes
-        if self.statistics_packet_header:
-            for k, val in _dict_to_hdf5_attrs(self.statistics_packet_header).items():
-                if hasattr(matrix, k):
-                    setattr(matrix, k, val)
-
+    def _add_device_metadata(self, matrix: StatisticsData):
         # add station state
         if self.antennafield_device:
             try:
-                matrix.hbat_pwr_on = self.antennafield_device.HBAT_PWR_on_R
+                if self.antennafield_device.Antenna_Type_R != "LBA":
+                    matrix.hbat_pwr_on = self.antennafield_device.HBAT_PWR_on_R
+
                 matrix.frequency_band = self.antennafield_device.Frequency_Band_R
             except (DevFailed, AttributeError):
                 logger.exception(
                     "Failed to read from %s", self.antennafield_device.name()
                 )
 
-        if self.sdp_device and self.antenna_sdp_mapping:
+        if self.sdp_device and self.antenna_sdp_mapping is not None:
             try:
                 nyquist_zones = self.sdp_device.nyquist_zone_RW
                 fpga_spectral_inversion = self.sdp_device.FPGA_spectral_inversion_R
@@ -463,6 +445,30 @@ class HDF5Writer(ABC):
             except (DevFailed, AttributeError):
                 logger.exception("Failed to read from %s", self.tilebeam_device.name())
 
+    def get_matrix(self):
+        """Constructs a matrix for storing in the hdf5 file"""
+
+        matrix = self.get_matrix_data()
+
+        # add generic info
+        matrix.nof_payload_errors = self.current_collector.parameters[
+            "nof_payload_errors"
+        ]
+        matrix.nof_valid_payloads = self.current_collector.parameters[
+            "nof_valid_payloads"
+        ]
+        matrix.gn_indices = self.current_collector.parameters["gn_indices"]
+
+        # Stores the header of the packet received for this matrix as a list of
+        # attributes
+        if self.statistics_packet_header:
+            for k, val in _dict_to_hdf5_attrs(self.statistics_packet_header).items():
+                if hasattr(matrix, k):
+                    setattr(matrix, k, val)
+
+        # Add metadata of Tango devices
+        self._add_device_metadata(matrix)
+
         return matrix
 
     @abstractmethod
-- 
GitLab