From b8186718168feebe551f99736b3aabaa8b0fdd75 Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Sun, 23 Feb 2025 17:12:58 -0500 Subject: [PATCH] That's better --- lib/xmet/skew_t.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/xmet/skew_t.py b/lib/xmet/skew_t.py index 87b6c88..1a47c52 100644 --- a/lib/xmet/skew_t.py +++ b/lib/xmet/skew_t.py @@ -1,6 +1,14 @@ import math import cairo +PRESSURE_MAX = 1050 # millibar +PRESSURE_MIN = 100 +PRESSURE_STEP = 50 + +PRESSURE_LOG_MAX = math.log(PRESSURE_MAX) +PRESSURE_LOG_MIN = math.log(PRESSURE_MIN) +PRESSURE_LOG_RANGE = PRESSURE_LOG_MAX - PRESSURE_LOG_MIN + TEMP_CENTER = 0 # degrees C TEMP_STEP = 5 @@ -38,8 +46,27 @@ class SkewT(): def graph_to_screen(self, x, y) -> tuple: return (self.width / 2) + x, self.height - y - def draw_pressure_lines(self, cr: cairo.Context): - pass + def pressure_y(self, pressure: float) -> float: + log_p = math.log(clamp(pressure, PRESSURE_MIN, PRESSURE_MAX)) + factor = (PRESSURE_LOG_MAX - log_p) / PRESSURE_LOG_RANGE + + return factor * self.height + + def sample_to_graph(self, temp: float, pressure: float): + x = (temp / TEMP_STEP) * self.temp_step_width + y = self.pressure_y(pressure) + + return x + y, y + + def draw_pressure_lines(self, cr: cairo.Context, x: float, y: float): + for pressure in range(PRESSURE_MIN, PRESSURE_MAX+1, PRESSURE_STEP): + coord = self.graph_to_screen(-self.width / 2, + self.pressure_y(pressure)) + + cr.set_source_rgba(0, 0, 0, 0.5) + cr.move_to(x + coord[0], y + coord[1]) + cr.rel_line_to(self.width, 0) + cr.stroke() def draw_isotherms(self, cr: cairo.Context, x: float, y: float): cr.set_source_rgba(0.1, 0.5, 0.1, 0.8) @@ -59,3 +86,4 @@ class SkewT(): def draw(self, cr: cairo.Context, x: float, y: float): self.draw_isotherms(cr, x, y) + self.draw_pressure_lines(cr, x, y)