From edf1af526fc34f84b01d9efc29c128a6ec333a84 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Sun, 2 Mar 2025 16:27:28 -0500
Subject: [PATCH] Try to automatically zoom into hodograph activity

---
 lib/xmet/hodograph.py | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/lib/xmet/hodograph.py b/lib/xmet/hodograph.py
index 008d770..b3f2100 100644
--- a/lib/xmet/hodograph.py
+++ b/lib/xmet/hodograph.py
@@ -22,8 +22,13 @@ def knots(ms: float) -> float:
 class Hodograph():
     def __init__(self, width, height):
         self.zoom     = 1.2
-        self.offset_x = 0.1
-        self.offset_y = 0.9
+        self.offset_x = 0.1 # Percentage of the upper left quadrant rendered
+        self.offset_y = 0.9 # Percentage of the upper right quadrant rendered
+
+        self.min_x = 0
+        self.min_y = 0
+        self.max_x = 0
+        self.max_y = 0
 
         self.width  = min(width, height)
         self.height = min(width, height)
@@ -45,6 +50,32 @@ class Hodograph():
             self.offset_y * self.height + gy
         )
 
+    def find_extents(self, sounding: Sounding):
+        """
+        Determine the boundaries of the sounding to set the center and zoom
+        appropriately.
+        """
+        for sample in sounding.samples:
+            x, y = self.sample_to_graph(sample.wind_speed, sample.wind_dir)
+
+            if self.min_x > x:
+                self.min_x = x
+
+            if self.min_y > y:
+                self.min_y = y
+
+            if self.max_x < x:
+                self.max_x = x
+
+            if self.max_y < y:
+                self.max_y = y
+
+        self.offset_x = self.min_x / self.radius + 0.1
+        self.offset_y = (self.radius - self.max_y) / self.radius
+        self.zoom     = min(self.width, self.height) / max(self.max_x - self.min_x,
+                            self.max_y - self.min_y) / 2
+        self.radius = min(self.width, self.height) * self.zoom
+
     def draw_speed_lines(self, cr: cairo.Context, x, y):
         cr.save()
 
@@ -199,6 +230,8 @@ class Hodograph():
             offset += interval
 
     def draw(self, cr: cairo.Context, x, y, sounding: Sounding):
+        self.find_extents(sounding)
+
         cr.rectangle(x, y, self.width, self.height)
         cr.clip()