From d1f961ecf2031ed0e629319a509f57ce45b6f0e0 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Tue, 15 Apr 2025 12:34:47 -0400
Subject: [PATCH] Obtain and set surface elevation where possible

---
 lib/xmet/bufr.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/xmet/bufr.py b/lib/xmet/bufr.py
index d26ef60..25099fb 100644
--- a/lib/xmet/bufr.py
+++ b/lib/xmet/bufr.py
@@ -115,6 +115,9 @@ class BUFRSounding(Sounding):
         sounding.timestamp_observed = timestamp
         sounding.timestamp_released = timestamp - datetime.timedelta(minutes=45)
 
+        surface_pressure = None
+        surface_height   = None
+
         for item in response:
             params = item.getParameters()
 
@@ -124,6 +127,9 @@ class BUFRSounding(Sounding):
                 wind_dir   = item.getNumber('wsMan')
                 wind_speed = item.getNumber('wdMan')
 
+                if surface_pressure is None and height == -9999.0:
+                    surface_pressure = pressure
+
                 sounding.record_wind(pressure, height, wind_dir, wind_speed)
 
             if set(params) & BUFRSounding.BUFR_PARAMS_SIGT:
@@ -142,6 +148,13 @@ class BUFRSounding(Sounding):
 
                 sounding.record_wind(pressure, height, wind_speed, wind_dir)
 
+            if set(params) & set(['staElev']):
+                if surface_pressure is not None and surface_height is None:
+                    surface_height = item.getNumber('staElev')
+
+        if surface_pressure is not None and surface_height is not None:
+            sounding.sample_by_pressure(surface_pressure).height = surface_height
+
         for pressure in sorted(sounding.samples_by_pressure.keys(), reverse=True):
             sounding.samples.append(sounding.samples_by_pressure[pressure])