From 1046b1c22d2d1deebbfe3674ee94732ca29723e6 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Sun, 13 Apr 2025 20:39:12 -0400
Subject: [PATCH] Handle -9999 BUFR pressure, height values

---
 lib/xmet/bufr.py | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/lib/xmet/bufr.py b/lib/xmet/bufr.py
index 35af234..c5f33a3 100644
--- a/lib/xmet/bufr.py
+++ b/lib/xmet/bufr.py
@@ -20,8 +20,9 @@ class BUFRSounding(Sounding):
         super().__init__()
 
         self.samples_by_pressure = dict()
+        self.samples_by_height   = dict()
 
-    def sample(self, pressure: float) -> SoundingSample:
+    def sample_by_pressure(self, pressure: float) -> SoundingSample:
         sample = self.samples_by_pressure.get(pressure)
 
         if sample is None:
@@ -32,6 +33,17 @@ class BUFRSounding(Sounding):
 
         return sample
 
+    def sample_by_height(self, height: float) -> SoundingSample:
+        sample = self.samples_by_height.get(height)
+
+        if sample is None:
+            sample = SoundingSample()
+            sample.height = height
+
+            self.samples_by_height[height] = sample
+
+        return sample
+
     @staticmethod
     def init():
         DataAccessLayer.changeEDEXHost(BUFRSounding.EDEX_HOST)
@@ -73,25 +85,32 @@ class BUFRSounding(Sounding):
 
             if set(params) & BUFRSounding.BUFR_PARAMS_MAN:
                 pressure = item.getNumber('prMan') / 100.0
-                sample   = sounding.sample(pressure)
+                height   = item.getNumber('htMan')
+
+                if pressure == -99.99:
+                    sample = sounding.sample_by_height(height)
+                else:
+                    sample = sounding.sample_by_pressure(pressure)
 
-                sample.pressure   = pressure
-                sample.height     = item.getNumber('htMan')
                 sample.wind_speed = item.getNumber('wsMan')
                 sample.wind_dir   = item.getNumber('wdMan')
 
             if set(params) & BUFRSounding.BUFR_PARAMS_SIGT:
                 pressure = item.getNumber('prSigT') / 100.0
-                sample   = sounding.sample(pressure)
+                sample   = sounding.sample_by_pressure(pressure)
 
                 sample.temp     = celsius(item.getNumber('tpSigT'))
                 sample.dewpoint = celsius(item.getNumber('tdSigT'))
 
             if set(params) & BUFRSounding.BUFR_PARAMS_SIGW:
                 pressure = item.getNumber('prSigW') / 100.0
-                sample   = sounding.sample(pressure)
+                height   = item.getNumber('htSigW')
+
+                if pressure == -99.99:
+                    sample = sounding.sample_by_height(height)
+                else:
+                    sample = sounding.sample_by_pressure(pressure)
 
-                sample.height     = item.getNumber('htSigW')
                 sample.wind_speed = item.getNumber('wsSigW')
                 sample.wind_dir   = item.getNumber('wdSigW')