From d1269d0e2fe65858529b3770f3442b1e8aa24be8 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Sat, 1 Mar 2025 21:04:43 -0500
Subject: [PATCH] Get better at parsing heights at different pressures

---
 lib/xmet/rawins.py | 71 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 50 insertions(+), 21 deletions(-)

diff --git a/lib/xmet/rawins.py b/lib/xmet/rawins.py
index 9a463bd..6bec9fb 100644
--- a/lib/xmet/rawins.py
+++ b/lib/xmet/rawins.py
@@ -110,37 +110,62 @@ class RawinsObs():
             'speed': wind_speed
         }
 
-    TTAA_HEIGHTS = {
+    TTAA_PRESSURES = {
         '00': 1000, '92':  925, '85':  850,
         '70':  700, '50':  500, '40':  400,
         '30':  300, '25':  250, '20':  200,
         '15':  150, '10':  100,
     }
 
-    def calc_1000mb_height(self, num: float):
-        if num >= 500:
-            return 0 - (num - 500)
+    def code_to_pressure(self, code: str):
+        if code in self.TTAA_PRESSURES:
+            return self.TTAA_PRESSURES[code]
 
-        return num
+        return None
+
+    def calc_1000mb_height(self, height: float) -> float:
+        if height >= 500:
+            return 0 - (height - 500)
+
+        return height
+
+    def calc_850mb_height(self, height: float) -> float:
+        return 1000.0 + height
+
+    def calc_500mb_height(self, height: float) -> float:
+        print(f"Calculating 500mb height")
+        return 10 * height
 
     def parse_height_pressure(self, value: str):
-        token = value[0:2]
-        num   = value[2:5]
+        code = value[0:2]
+        num  = value[2:5]
 
         if num == '///':
             return None
 
-        if token == '00':
-            return {
-                'height':   self.calc_1000mb_height(float(num)),
-                'pressure': 1000
-            }
+        #
+        # Ignore the tropopause or height of max wind velocity.
+        #
+        if code == '77' or code == '88':
+            return None
 
-        if token in self.TTAA_HEIGHTS:
-            return {
-                'height':   float(num),
-                'pressure': self.TTAA_HEIGHTS[token]
-            }
+        pressure = self.code_to_pressure(code)
+
+        if pressure is None:
+            return None
+        elif pressure == 1000:
+            height = self.calc_1000mb_height(float(num))
+        elif pressure <= 500:
+            height = self.calc_500mb_height(float(num))
+        elif pressure <= 850:
+            height = self.calc_850mb_height(float(num))
+        else:
+            height = float(num)
+
+        return {
+            'pressure': pressure,
+            'height':   height
+        }
 
     PRESSURE_SIG = {
         '11': True, '22': True, '33': True, '44': True, '55': True,
@@ -148,17 +173,21 @@ class RawinsObs():
     }
 
     def parse_significant_pressure(self, value: str):
-        if value[0:2] in self.PRESSURE_SIG:
+        code, pressure = value[0:2], value[2:5]
+
+        if code in self.PRESSURE_SIG:
             return {
                 'height':   None,
-                'pressure': float(value[2:5])
+                'pressure': float(pressure)
             }
 
     def parse_surface_pressure(self, value: str):
-        if value[0:2] == '99':
+        code, pressure = value[0:2], value[2:5]
+
+        if code == '99':
             return {
                 'height':   None,
-                'pressure': float(value[2:5])
+                'pressure': float(pressure)
             }
 
     def parse_sample_values(self, values: list[str]) -> RawinsSample: