From aaf9d78c8bfdda0d505b5540c5b96f31c20463c8 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 1 Aug 2023 11:37:08 +0000
Subject: [PATCH] L2SS-1460: Always apply Field Attenuation

---
 README.md                                            |  1 +
 tangostationcontrol/VERSION                          |  2 +-
 .../default/devices/test_device_calibration.py       |  7 +++++--
 .../tangostationcontrol/common/calibration.py        | 12 ++++++++----
 .../tangostationcontrol/devices/antennafield.py      |  6 +++---
 5 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index 33801c770..05a8cbe2a 100644
--- a/README.md
+++ b/README.md
@@ -115,6 +115,7 @@ Next change the version in the following places:
 
 # Release Notes
 
+* 0.20.3 Fix application of Field_Attenuation_R
 * 0.20.2 Support only one parent in hierarchies
 * 0.20.1 Create an abstract AntennaMapper class which implements behavior of both AntennaToSdpMapper
          and AntennaToRecvMapper
diff --git a/tangostationcontrol/VERSION b/tangostationcontrol/VERSION
index 727d97b9b..144996ed2 100644
--- a/tangostationcontrol/VERSION
+++ b/tangostationcontrol/VERSION
@@ -1 +1 @@
-0.20.2
+0.20.3
diff --git a/tangostationcontrol/integration_test/default/devices/test_device_calibration.py b/tangostationcontrol/integration_test/default/devices/test_device_calibration.py
index cdd965006..e664942be 100644
--- a/tangostationcontrol/integration_test/default/devices/test_device_calibration.py
+++ b/tangostationcontrol/integration_test/default/devices/test_device_calibration.py
@@ -182,6 +182,9 @@ class TestCalibrationDevice(AbstractTestBases.TestDeviceBase):
         rcu_attenuator_db_pwr = self.antennafield_proxy.RCU_attenuator_dB_RW[:, 0]
         rcu_attenuator_db_ctrl = self.antennafield_proxy.RCU_attenuator_dB_RW[:, 1]
 
+        # gather settings
+        field_attenuation = self.antennafield_proxy.Field_Attenuation_R
+
         for mapping_name, rcu_attenuator_db in [
             ("power", rcu_attenuator_db_pwr),
             ("control", rcu_attenuator_db_ctrl),
@@ -203,9 +206,9 @@ class TestCalibrationDevice(AbstractTestBases.TestDeviceBase):
                 rcu_attenuator_db[1],
                 msg=f"{mapping_name} - rcu_attenuator_db={rcu_attenuator_db}",
             )
-            # longest cable should require no damping
+            # longest cable should require no damping, so only field attenuation applies
             self.assertEqual(
-                0,
+                field_attenuation,
                 rcu_attenuator_db[1],
                 msg=f"{mapping_name} - rcu_attenuator_db={rcu_attenuator_db}",
             )
diff --git a/tangostationcontrol/tangostationcontrol/common/calibration.py b/tangostationcontrol/tangostationcontrol/common/calibration.py
index 1978a06be..b6f759887 100644
--- a/tangostationcontrol/tangostationcontrol/common/calibration.py
+++ b/tangostationcontrol/tangostationcontrol/common/calibration.py
@@ -254,12 +254,16 @@ def calibrate_RCU_attenuator_dB(antenna_field: DeviceProxy):
     # -----------------------------------------------------------
 
     # Correct for signal loss in the cables
-    signal_delay_loss = (
-        antenna_field.Antenna_Cables_Loss_R - antenna_field.Field_Attenuation_R
-    )
+    signal_delay_loss = antenna_field.Antenna_Cables_Loss_R
 
-    # return coarse attenuation to apply
+    # return coarse attenuation to apply (weakest signal
+    # gets 0 attenuation).
     rcu_attenuator_db, _ = loss_compensation(signal_delay_loss)
+
+    # add field-wide attenuation
+    rcu_attenuator_db += antenna_field.Field_Attenuation_R
+
+    # apply on antenna field
     antenna_field.RCU_attenuator_dB_RW = (
         numpy.tile(
             numpy.array(rcu_attenuator_db, dtype=numpy.int64).flatten(),
diff --git a/tangostationcontrol/tangostationcontrol/devices/antennafield.py b/tangostationcontrol/tangostationcontrol/devices/antennafield.py
index 54efffcad..fda4e626f 100644
--- a/tangostationcontrol/tangostationcontrol/devices/antennafield.py
+++ b/tangostationcontrol/tangostationcontrol/devices/antennafield.py
@@ -192,9 +192,9 @@ class AntennaField(LOFARDevice):
 
     Field_Attenuation = device_property(
         doc="Attenuation value to apply on all inputs.",
-        dtype="DevFloat",
+        dtype="DevShort",
         mandatory=False,
-        default_value=10.0,
+        default_value=10,
     )
 
     # ----- Position information
@@ -348,7 +348,7 @@ class AntennaField(LOFARDevice):
 
     @attribute(
         access=AttrWriteType.READ,
-        dtype="DevFloat",
+        dtype="DevShort",
     )
     def Field_Attenuation_R(self):
         return self.Field_Attenuation
-- 
GitLab