Skip to content
Snippets Groups Projects
Commit abf3f983 authored by Martin Lange's avatar Martin Lange
Browse files

add optional forcing of copy in to_xarray

parent 3389893a
No related branches found
No related tags found
1 merge request!217Ensure data is a copy in outputs
...@@ -64,7 +64,7 @@ def _gen_dims(ndim, info): ...@@ -64,7 +64,7 @@ def _gen_dims(ndim, info):
return dims 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. Convert data to a xarray.DataArray.
...@@ -78,7 +78,11 @@ def to_xarray(data, name, info, time=None, no_time_check=False): ...@@ -78,7 +78,11 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
Info associated with the data. Info associated with the data.
time : :class:`datetime <datetime.datetime>` or None, optional time : :class:`datetime <datetime.datetime>` or None, optional
Timestamp for the data, by default None 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. Skips the time check for xarray input data. Used internally in adapter outputs.
Returns Returns
...@@ -101,6 +105,12 @@ def to_xarray(data, name, info, time=None, no_time_check=False): ...@@ -101,6 +105,12 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
ignore_time=no_time_check, ignore_time=no_time_check,
check_units_equivalent=False, check_units_equivalent=False,
) )
if equivalent_units(info.units, data):
if force_copy:
return data.copy()
return data
return to_units(data, info.units) return to_units(data, info.units)
units = UNITS.Unit(info.units) units = UNITS.Unit(info.units)
...@@ -110,9 +120,14 @@ def to_xarray(data, name, info, time=None, no_time_check=False): ...@@ -110,9 +120,14 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
f"Given data has incompatible units. " f"Given data has incompatible units. "
f"Got {data.units}, expected {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: else:
data = np.asarray(data) data = np.asarray(data)
if force_copy:
data = data.copy()
time_entries = ( time_entries = (
len(time) if time is not None and not isinstance(time, datetime.datetime) else 1 len(time) if time is not None and not isinstance(time, datetime.datetime) else 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment