From dcf2f2034ceb2fd1977c3a3400f05641d2f860c2 Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Mon, 24 Feb 2025 11:28:35 -0500 Subject: [PATCH] Implement separate class for rendering Skew-T legends --- lib/xmet/skew_t.py | 60 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/xmet/skew_t.py b/lib/xmet/skew_t.py index 980f5e4..a7e6915 100644 --- a/lib/xmet/skew_t.py +++ b/lib/xmet/skew_t.py @@ -13,7 +13,7 @@ PRESSURE_LOG_MAX = math.log(PRESSURE_MAX) PRESSURE_LOG_MIN = math.log(PRESSURE_MIN) PRESSURE_LOG_RANGE = PRESSURE_LOG_MAX - PRESSURE_LOG_MIN -TEMP_MAX = 50 +TEMP_MAX = 50 # degrees C TEMP_MIN = -50 TEMP_RANGE = TEMP_MAX - TEMP_MIN TEMP_CENTER = 0 # degrees C @@ -131,3 +131,61 @@ class SkewTGraph(): self.draw_samples(cr, x, y, samples, lambda s: s.dewpoint) cr.reset_clip() + +class SkewTLegend(): + @staticmethod + def draw_isotherm_legends_for_graph(cr: cairo.Context, + skew_t: SkewTGraph, + x: float, + y: float): + temp = TEMP_MAX + + while True: + text = "%d°C" % (temp) + extents = cr.text_extents(text) + + x_rel, y_rel = skew_t.sample_to_screen(temp, PRESSURE_MAX) + + if x_rel < 0: + y_rel = skew_t.height + x_rel + x_rel = 0 + + if y_rel <= 0: + break + + cr.move_to(x + x_rel - extents.width - 8, + y + y_rel + extents.height / 2) + else: + cr.move_to(x + x_rel - extents.width / 2, + y + y_rel + extents.height + 8) + + cr.show_text(text) + cr.stroke() + + temp -= 2 * TEMP_STEP + + @staticmethod + def draw_isobar_legends_for_graph(cr: cairo.Context, + skew_t: SkewTGraph, + x: float, + y: float): + for pressure in range(PRESSURE_MAX, PRESSURE_MIN-1, -2*PRESSURE_STEP): + x_rel = skew_t.width + y_rel = skew_t.height - skew_t.pressure_to_y(pressure) + + text = "%dmb" % (pressure) + extents = cr.text_extents(text) + + cr.move_to(x + x_rel + 8, + y + y_rel + extents.height / 2) + + cr.show_text(text) + cr.stroke() + + @staticmethod + def draw_for_graph(cr: cairo.Context, + skew_t: SkewTGraph, + x: float, + y: float): + SkewTLegend.draw_isotherm_legends_for_graph(cr, skew_t, x, y) + SkewTLegend.draw_isobar_legends_for_graph(cr, skew_t, x, y)