From ae5458a6ca9c3a96e36dbf34b5fba89ddd18acfc Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 3 May 2022 18:28:35 +0200
Subject: [PATCH] L2SS-785: Added geohashes

---
 docker-compose/lofar-device-base/Dockerfile            |  2 +-
 tangostationcontrol/requirements.txt                   |  1 +
 tangostationcontrol/tangostationcontrol/beam/geo.py    |  9 +++++++++
 .../tangostationcontrol/devices/recv.py                | 10 +++++++++-
 4 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/docker-compose/lofar-device-base/Dockerfile b/docker-compose/lofar-device-base/Dockerfile
index 9897528c5..5decbd092 100644
--- a/docker-compose/lofar-device-base/Dockerfile
+++ b/docker-compose/lofar-device-base/Dockerfile
@@ -1,7 +1,7 @@
 ARG SOURCE_IMAGE
 FROM ${SOURCE_IMAGE}
 
-RUN sudo apt-get update && sudo apt-get install -y git && sudo apt-get clean
+RUN sudo apt-get update && sudo apt-get install -y git g++ python3-dev && sudo apt-get clean
 
 COPY docker-compose/lofar-device-base/lofar-requirements.txt /lofar-requirements.txt
 RUN sudo pip3 install -r /lofar-requirements.txt
diff --git a/tangostationcontrol/requirements.txt b/tangostationcontrol/requirements.txt
index 6bb820aea..d1c66a8a2 100644
--- a/tangostationcontrol/requirements.txt
+++ b/tangostationcontrol/requirements.txt
@@ -16,3 +16,4 @@ etrs-itrs@git+https://github.com/brentjens/etrs-itrs # license pending
 # numpy must be manually added even though etrs-itrs requires it
 numpy >= 1.21.0 # BSD
 lofarantpos >= 0.5.0 # Apache 2
+python-geohash >= 0.8.5 # Apache 2MIT
diff --git a/tangostationcontrol/tangostationcontrol/beam/geo.py b/tangostationcontrol/tangostationcontrol/beam/geo.py
index 2c8195796..0c541ef5c 100644
--- a/tangostationcontrol/tangostationcontrol/beam/geo.py
+++ b/tangostationcontrol/tangostationcontrol/beam/geo.py
@@ -2,6 +2,7 @@ import etrsitrs
 import lofarantpos.geo
 import numpy
 import math
+import geohash
 
 """
    LOFAR station positions are measured in ETRS89, which are the coordinates of the position as it would be in 1989.
@@ -49,3 +50,11 @@ def ETRS_to_GEO(ETRS_coordinates: numpy.array) -> numpy.array:
 
 # Geo coordinates are only used for rough positioning. The difference between ITRF and ETRS matters little here
 ITRF_to_GEO = ETRS_to_GEO
+
+def GEO_to_GEOHASH(GEO_coordinates: numpy.array) -> numpy.array:
+    """ Convert an array of latitude/longitude (degrees) tuples to geohash strings. """
+
+    def GEO_to_GEOHASH_fn(geo_coords):
+        return geohash.encode(geo_coords[0], geo_coords[1], precision=32)
+
+    return _apply_fn_on_one_element_or_array(GEO_to_GEOHASH_fn, GEO_coordinates)
diff --git a/tangostationcontrol/tangostationcontrol/devices/recv.py b/tangostationcontrol/tangostationcontrol/devices/recv.py
index 090d6349c..326c31b67 100644
--- a/tangostationcontrol/tangostationcontrol/devices/recv.py
+++ b/tangostationcontrol/tangostationcontrol/devices/recv.py
@@ -22,7 +22,7 @@ from math import pi
 
 # Additional import
 from tangostationcontrol.beam.hba_tile import HBATAntennaOffsets
-from tangostationcontrol.beam.geo import ETRS_to_ITRF, ITRF_to_GEO
+from tangostationcontrol.beam.geo import ETRS_to_ITRF, ITRF_to_GEO, GEO_to_GEOHASH
 from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.common.lofar_logging import device_logging_to_python
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
@@ -250,6 +250,11 @@ class RECV(opcua_device):
         doc='Absolute reference position of each tile, in latitude/longitude (degrees)',
         dtype=((numpy.float,),), max_dim_x=2, max_dim_y=96)
 
+    HBAT_reference_GEOHASH_R = attribute(access=AttrWriteType.READ,
+        doc='Absolute reference position of each tile, as geohash strings',
+        dtype=(numpy.str,), max_dim_x=96,)
+
+
     def read_Antenna_Field_Reference_ITRF_R(self):
         # provide ITRF field coordinates if they were configured
         if self.Antenna_Field_Reference_ITRF:
@@ -288,6 +293,9 @@ class RECV(opcua_device):
     def read_HBAT_reference_GEO_R(self):
         return ITRF_to_GEO(self.read_HBAT_reference_ITRF_R())
 
+    def read_HBAT_reference_GEOHASH_R(self):
+        return GEO_to_GEOHASH(self.read_HBAT_reference_GEO_R())
+
     # ----------
     # Summarising Attributes
     # ----------
-- 
GitLab