From bc9a2977a5c564db99824d5baefd0cb722d524b2 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Wed, 5 Mar 2025 00:35:21 -0500
Subject: [PATCH] Initial implementation of moist adiabat plotting (it sucks)

---
 lib/xmet/skew_t.py | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/lib/xmet/skew_t.py b/lib/xmet/skew_t.py
index d41f41a..0c3822e 100644
--- a/lib/xmet/skew_t.py
+++ b/lib/xmet/skew_t.py
@@ -4,7 +4,7 @@ import cairo
 from typing import Callable
 
 from xmet.sounding import Sounding
-from xmet.thermo   import pressure_height, lapse
+from xmet.thermo   import pressure_height, lapse, moist_lapse_rate
 
 PRESSURE_MAX  = 1050 # millibar
 PRESSURE_MIN  =  100
@@ -104,26 +104,42 @@ class SkewTGraph():
                      x: float,
                      y: float,
                      start_temp: float,
-                     start_pressure: float):
+                     start_pressure: float,
+                     moist: bool=False):
         start_height = pressure_height(start_pressure)
-        sx_last = None
-        sy_last = None
+        sx_last      = None
+        sy_last      = None
+        height_last  = None
 
+        temp     = start_temp
         pressure = start_pressure
 
         while pressure >= PRESSURE_MIN:
-            height   = pressure_height(pressure)
-            temp_cur = lapse(start_temp, height - start_height)
+            height = pressure_height(pressure)
 
-            sx, sy = self.sample_to_screen(temp_cur, pressure)
+            if height_last is None:
+                height_last = height
+
+            if moist:
+                try:
+                    rate = moist_lapse_rate(temp, temp, pressure)
+                except OverflowError:
+                    break
+
+                temp = lapse(temp, height - height_last, rate)
+            else:
+                temp = lapse(start_temp, height - start_height)
+
+            sx, sy = self.sample_to_screen(temp, pressure)
 
             if sx_last is not None:
                 cr.move_to(x + sx, y + sy)
                 cr.line_to(x + sx_last, y + sy_last)
                 cr.stroke()
 
-            sx_last = sx
-            sy_last = sy
+            sx_last     = sx
+            sy_last     = sy
+            height_last = height
 
             pressure -= 10.0
 
@@ -137,6 +153,9 @@ class SkewTGraph():
         for temp in range(-140, 140, 10):
             self.draw_adiabat(cr, x, y, temp, PRESSURE_MAX)
 
+        for temp in range(-140, 140, 10):
+            self.draw_adiabat(cr, x, y, temp, PRESSURE_MAX, True)
+
         cr.restore()
 
     def draw_sounding(self,