Skip to content

Commit 9a40616

Browse files
authored
Merge pull request #2827 from BENR0/fix_to_xr_dataset_merge_error
2 parents d96b46f + 43f8ce9 commit 9a40616

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

satpy/scene.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,12 +1079,16 @@ def to_hvplot(self, datasets=None, *args, **kwargs):
10791079

10801080

10811081

1082-
def to_xarray_dataset(self, datasets=None):
1082+
def to_xarray_dataset(self, datasets=None, compat="minimal"):
10831083
"""Merge all xr.DataArrays of a scene to a xr.DataSet.
10841084
10851085
Parameters:
10861086
datasets (list):
10871087
List of products to include in the :class:`xarray.Dataset`
1088+
compat (str):
1089+
How to compare variables with the same name for conflicts.
1090+
See :func:`xarray.merge` for possible options. Defaults to
1091+
"minimal" which drops conflicting variables.
10881092
10891093
Returns: :class:`xarray.Dataset`
10901094
@@ -1100,7 +1104,7 @@ def to_xarray_dataset(self, datasets=None):
11001104
mdata = combine_metadata(*tuple(i.attrs for i in dataarrays))
11011105
if mdata.get("area") is None or not isinstance(mdata["area"], SwathDefinition):
11021106
# either don't know what the area is or we have an AreaDefinition
1103-
ds = xr.merge(ds_dict.values())
1107+
ds = xr.merge(ds_dict.values(), compat=compat)
11041108
else:
11051109
# we have a swath definition and should use lon/lat values
11061110
lons, lats = mdata["area"].get_lonlats()

satpy/tests/scene_tests/test_conversions.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"""Unit tests for Scene conversion functionality."""
1818

1919
import datetime as dt
20+
from datetime import datetime
2021

22+
import numpy as np
2123
import pytest
2224
import xarray as xr
2325
from dask import array as da
@@ -51,14 +53,6 @@ def test_serialization_with_readers_and_data_arr(self):
5153
class TestSceneConversions:
5254
"""Test Scene conversion to geoviews, xarray, etc."""
5355

54-
def test_to_xarray_dataset_with_empty_scene(self):
55-
"""Test converting empty Scene to xarray dataset."""
56-
scn = Scene()
57-
xrds = scn.to_xarray_dataset()
58-
assert isinstance(xrds, xr.Dataset)
59-
assert len(xrds.variables) == 0
60-
assert len(xrds.coords) == 0
61-
6256
def test_geoviews_basic_with_area(self):
6357
"""Test converting a Scene to geoviews with an AreaDefinition."""
6458
from pyresample.geometry import AreaDefinition
@@ -164,6 +158,43 @@ def single_area_scn(self):
164158
scn["var1"] = data_array
165159
return scn
166160

161+
def test_to_xarray_dataset_with_conflicting_variables(self):
162+
"""Test converting Scene with DataArrays with conflicting variables.
163+
164+
E.g. "acq_time" in the seviri_l1b_nc reader
165+
"""
166+
from pyresample.geometry import AreaDefinition
167+
area = AreaDefinition("test", "test", "test",
168+
{"proj": "geos", "lon_0": -95.5, "h": 35786023.0},
169+
2, 2, [-200, -200, 200, 200])
170+
scn = Scene()
171+
172+
acq_time_1 = ("y", [np.datetime64("1958-01-02 00:00:01"),
173+
np.datetime64("1958-01-02 00:00:02")])
174+
ds = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
175+
attrs={"start_time": datetime(2018, 1, 1), "area": area})
176+
ds["acq_time"] = acq_time_1
177+
178+
scn["ds1"] = ds
179+
180+
acq_time_2 = ("y", [np.datetime64("1958-02-02 00:00:01"),
181+
np.datetime64("1958-02-02 00:00:02")])
182+
ds2 = ds.copy()
183+
ds2["acq_time"] = acq_time_2
184+
185+
scn["ds2"] = ds2
186+
187+
# drop case (compat="minimal")
188+
xrds = scn.to_xarray_dataset()
189+
assert isinstance(xrds, xr.Dataset)
190+
assert "acq_time" not in xrds.coords
191+
192+
# override: pick variable from first dataset
193+
xrds = scn.to_xarray_dataset(datasets=["ds1", "ds2"], compat="override")
194+
assert isinstance(xrds, xr.Dataset)
195+
assert "acq_time" in xrds.coords
196+
xr.testing.assert_equal(xrds["acq_time"], ds["acq_time"])
197+
167198
@pytest.fixture
168199
def multi_area_scn(self):
169200
"""Define Scene with multiple area."""

0 commit comments

Comments
 (0)