From e84425cbc161155fe44fefc3d5063dea90acbbfe Mon Sep 17 00:00:00 2001
From: FZill <florian.zill@ufz.de>
Date: Fri, 15 Mar 2024 11:21:08 +0100
Subject: [PATCH] [propertylib] delta property from mesh_property

---
 ogstools/meshplotlib/core.py     | 16 +---------------
 ogstools/propertylib/property.py | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/ogstools/meshplotlib/core.py b/ogstools/meshplotlib/core.py
index 47911216d..028de6e77 100644
--- a/ogstools/meshplotlib/core.py
+++ b/ogstools/meshplotlib/core.py
@@ -657,21 +657,7 @@ def plot_diff(
         mesh_property = get_preset(mesh_property, data_shape)
     diff_mesh = deepcopy(mesh1)
     diff_mesh[mesh_property.data_name] -= mesh2[mesh_property.data_name]
-    data_property = mesh_property.replace(output_unit=mesh_property.data_unit)
-    diff_unit = str(
-        (
-            data_property.transform(1, strip_unit=False)
-            - data_property.transform(1, strip_unit=False)
-        ).units
-    )
-    diff_property = mesh_property.replace(
-        data_unit=diff_unit,
-        output_unit=diff_unit,
-        output_name=mesh_property.output_name + " difference",
-        bilinear_cmap=True,
-        cmap=mesh_property.cmap if mesh_property.bilinear_cmap else "coolwarm",
-    )
-    return plot(diff_mesh, diff_property, fig=fig, ax=ax)
+    return plot(diff_mesh, mesh_property.delta, fig=fig, ax=ax)
 
 
 def plot_limit(
diff --git a/ogstools/propertylib/property.py b/ogstools/propertylib/property.py
index 60142cccd..ff983d563 100644
--- a/ogstools/propertylib/property.py
+++ b/ogstools/propertylib/property.py
@@ -125,6 +125,24 @@ class Property:
         """
         return "%" if self.output_unit == "percent" else self.output_unit
 
+    @property
+    def delta(self) -> "Property":
+        "returns: A property relating to the difference in a quantity."
+        data_property = self.replace(output_unit=self.data_unit)
+        diff_unit = str(
+            (
+                data_property.transform(1, strip_unit=False)
+                - data_property.transform(1, strip_unit=False)
+            ).units
+        )
+        return self.replace(
+            data_unit=diff_unit,
+            output_unit=diff_unit,
+            output_name=self.output_name + " difference",
+            bilinear_cmap=True,
+            cmap=self.cmap if self.bilinear_cmap else "coolwarm",
+        )
+
     def is_mask(self) -> bool:
         """
         Check if the property is a mask.
-- 
GitLab