From 6cf16815748bf672b7e7602a1a9e4bd91c3142a2 Mon Sep 17 00:00:00 2001
From: Feliks Kiszkurno <feliks-kuba.kiszkurno@ufz.de>
Date: Wed, 20 Mar 2024 14:32:50 +0000
Subject: [PATCH] Add get_label() method to Property class

---
 ogstools/meshplotlib/core.py     | 12 ++----------
 ogstools/propertylib/property.py |  7 +++++++
 tests/test_propertylib.py        |  3 +++
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/ogstools/meshplotlib/core.py b/ogstools/meshplotlib/core.py
index dc81e85f8..691dcc356 100644
--- a/ogstools/meshplotlib/core.py
+++ b/ogstools/meshplotlib/core.py
@@ -154,9 +154,7 @@ def add_colorbars(
     # Formatting the colorbar label and ticks
 
     tick_labels, offset = get_ticklabels(ticks)
-    cb_label = mesh_property.output_name.replace("_", " ")
-    if (unit := mesh_property.get_output_unit()) != "":
-        cb_label += " / " + unit
+    cb_label = mesh_property.get_label()
     if offset is not None:
         if offset[0] == "-":
             cb_label += " + " + offset[1:]
@@ -753,14 +751,8 @@ def plot_probe(
     ax.set_axisbelow(True)
     ax.grid(which="major", color="lightgrey", linestyle="-")
     ax.grid(which="minor", color="0.95", linestyle="--")
-    unit_str = (
-        f" / {mesh_property.get_output_unit()}"
-        if mesh_property.get_output_unit()
-        else ""
-    )
-    y_label = mesh_property.output_name.replace("_", " ") + unit_str
     ax.set_xlabel(x_label)
-    ax.set_ylabel(y_label)
+    ax.set_ylabel(mesh_property.get_label())
     ax.label_outer()
     ax.minorticks_on()
     return fig
diff --git a/ogstools/propertylib/property.py b/ogstools/propertylib/property.py
index 2cc89232f..50a9e84f0 100644
--- a/ogstools/propertylib/property.py
+++ b/ogstools/propertylib/property.py
@@ -184,6 +184,13 @@ class Property:
             ]
         return mesh[self.data_name]
 
+    def get_label(self) -> str:
+        "Creates property label in format 'property_name / property_unit'"
+        unit_str = (
+            f" / {self.get_output_unit()}" if self.get_output_unit() else ""
+        )
+        return self.output_name.replace("_", " ") + unit_str
+
 
 @dataclass
 class Scalar(Property):
diff --git a/tests/test_propertylib.py b/tests/test_propertylib.py
index 9d3bdc72c..b31e3339b 100644
--- a/tests/test_propertylib.py
+++ b/tests/test_propertylib.py
@@ -208,3 +208,6 @@ class PhysicalPropertyTest(unittest.TestCase):
         )
 
         self.assertEqual(pp.strain, strain_copy)
+
+    def test_get_label(self):
+        self.assertEqual(pp.pressure.get_label(), "pore pressure / MPa")
-- 
GitLab