From abf3f98336762ea04ef25dd15a020a4faaaecf89 Mon Sep 17 00:00:00 2001
From: Martin Lange <martin.lange@ufz.de>
Date: Tue, 29 Nov 2022 00:20:05 +0100
Subject: [PATCH] add optional forcing of copy in to_xarray

---
 src/finam/data/tools.py | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/finam/data/tools.py b/src/finam/data/tools.py
index ca5fc4bd..3850516e 100644
--- a/src/finam/data/tools.py
+++ b/src/finam/data/tools.py
@@ -64,7 +64,7 @@ def _gen_dims(ndim, info):
     return dims
 
 
-def to_xarray(data, name, info, time=None, no_time_check=False):
+def to_xarray(data, name, info, time=None, force_copy=False, no_time_check=False):
     """
     Convert data to a xarray.DataArray.
 
@@ -78,7 +78,11 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
         Info associated with the data.
     time : :class:`datetime <datetime.datetime>` or None, optional
         Timestamp for the data, by default None
-    no_time_check : bool
+    force_copy : bool, optional
+        Forces the result to be a copy of the passed data. Default `False`.
+
+        If not used, the result is a view of the data if no units conversion needs to be done.
+    no_time_check : bool, optional
         Skips the time check for xarray input data. Used internally in adapter outputs.
 
     Returns
@@ -101,6 +105,12 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
             ignore_time=no_time_check,
             check_units_equivalent=False,
         )
+        if equivalent_units(info.units, data):
+            if force_copy:
+                return data.copy()
+
+            return data
+
         return to_units(data, info.units)
 
     units = UNITS.Unit(info.units)
@@ -110,9 +120,14 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
                 f"Given data has incompatible units. "
                 f"Got {data.units}, expected {units}."
             )
-        data = np.asarray(data.m_as(units))
+        if force_copy and equivalent_units(data.units, units):
+            data = np.asarray(data.m_as(units)).copy()
+        else:
+            data = np.asarray(data.m_as(units))
     else:
         data = np.asarray(data)
+        if force_copy:
+            data = data.copy()
 
     time_entries = (
         len(time) if time is not None and not isinstance(time, datetime.datetime) else 1
-- 
GitLab