Repository: MITgcm/xmitgcm

Issue:
swap_dims doesn't work with xarray 0.18
See https://github.com/MITgcm/xmitgcm/runs/2601480632?check_suite_focus=true#step:6:231

```
_______________________ test_swap_dims[barotropic_gyre] ________________________

all_mds_datadirs = ('/tmp/pytest-of-runner/pytest-0/mdsdata2/barotropic_gyre', {'all_iters': [0, 10], 'delta_t': 3600, 'dlink': 'https://ndownloader.figshare.com/files/14066618', 'dtype': dtype('float32'), ...})

    def test_swap_dims(all_mds_datadirs):
        """See if we can swap dimensions."""
    
        dirname, expected = all_mds_datadirs
        kwargs = dict(iters=None, read_grid=True, geometry=expected['geometry'])
    
        expected_dims = ['XC', 'XG', 'YC', 'YG', 'Z', 'Zl', 'Zp1', 'Zu']
    
        # make sure we never swap if not reading grid
        assert 'i' in xmitgcm.open_mdsdataset(dirname,
            iters=None, read_grid=False, geometry=expected['geometry'])
        if expected['geometry'] in ('llc', 'cs', 'curvilinear'):
            # make sure swapping is not the default
            ds = xmitgcm.open_mdsdataset(dirname, **kwargs)
            assert 'i' in ds
            # and is impossible
            with pytest.raises(ValueError) as excinfo:
                ds = xmitgcm.open_mdsdataset(
                            dirname, swap_dims=True, **kwargs)
        else:
            # make sure swapping *IS* the default
            assert 'i' not in xmitgcm.open_mdsdataset(dirname, **kwargs)
            ds = xmitgcm.open_mdsdataset(
                        dirname, geometry=expected['geometry'],
                        iters=None, read_grid=True, swap_dims=True,
                        grid_vars_to_coords=True)
    
    
            # check for comodo metadata needed by xgcm
            assert ds['XC'].attrs['axis'] == 'X'
            assert ds['XG'].attrs['axis'] == 'X'
            assert ds['XG'].attrs['c_grid_axis_shift'] == -0.5
            assert ds['YC'].attrs['axis'] == 'Y'
            assert ds['YG'].attrs['axis'] == 'Y'
            assert ds['YG'].attrs['c_grid_axis_shift'] == -0.5
            assert ds['Z'].attrs['axis'] == 'Z'
            assert ds['Zl'].attrs['axis'] == 'Z'
            assert ds['Zl'].attrs['c_grid_axis_shift'] == -0.5
    
            # add extra layers dimensions if needed
            if 'layers' in expected:
                for layer_name in expected['layers']:
                    extra_dims = ['layer_' + layer_name + suffix for suffix in
                                  ['_bounds', '_center', '_interface']]
                    expected_dims += extra_dims
    
            assert list(ds.dims.keys()) == expected_dims
    
            # make sure swapping works with multiple iters
            ds = xmitgcm.open_mdsdataset(dirname, geometry=expected['geometry'],
                                         prefix=['S'])
            #print(ds)
            ds.load()
>           assert 'XC' in ds['S'].dims
E           AssertionError: assert 'XC' in ('time', 'j', 'i')
E            +  where ('time', 'j', 'i') = <xarray.DataArray 'S' (time: 2, j: 60, i: 60)>\narray([[[10., 10., 10., ..., 10., 10.,  0.],\n        [10., 10., 10., .....e True ... False False False False\n    iter     (time) int64 0 10\n  * time     (time) timedelta64[ns] 00:00:00 00:00:10.dims

xmitgcm/test/test_mds_store.py:298: AssertionError
```

Discussion:
AaronDavidSchneider: I think the tests fail due to maintainance of the ecco portal (#266). Unrelated to this PR.
rabernat: Fantastic! Thanks so much @AaronDavidSchneider!
rabernat: When I look at the test logs, I still see failing calls to `swap_dims` in the xarray-master setup: https://github.com/MITgcm/xmitgcm/pull/267/checks?check_run_id=2645589137

Any idea what is going on there?
AaronDavidSchneider: Oh 😧 
I will debug again and try to see where the problem originates from.
AaronDavidSchneider: `expected_dims` and `ds.dims` are in different order. Don't know how this happened. I fixed the xarray master test (at least locally) by changing the test to comparing sets.
What do you think @rabernat?

BTW: Could you take a look at #246? Should be ready. Thank you and sorry for being so german about it :D 
rabernat: > `expected_dims` and `ds.dims` are in different order.

I think we can live with this for now.

Patch:
diff --git a/xmitgcm/mds_store.py b/xmitgcm/mds_store.py
index a7830a8..0d5fc51 100644
--- a/xmitgcm/mds_store.py
+++ b/xmitgcm/mds_store.py
@@ -259,8 +259,10 @@ def open_mdsdataset(data_dir, grid_dir=None,
                     ds = xr.auto_combine(datasets)
                 elif xr.__version__ < '0.15.2':
                     ds = xr.combine_by_coords(datasets)
-                else:
+                elif xr.__version__ < '0.18.0':
                     ds = xr.combine_by_coords(datasets, compat='override', coords='minimal', combine_attrs='drop')
+                else:
+                    ds = xr.combine_by_coords(datasets, compat='override', coords='minimal', combine_attrs='drop_conflicts')
 
                 if swap_dims:
                     ds = _swap_dimensions(ds, geometry)


Test patch:
diff --git a/xmitgcm/test/test_mds_store.py b/xmitgcm/test/test_mds_store.py
index e7bea65..b8c3802 100644
--- a/xmitgcm/test/test_mds_store.py
+++ b/xmitgcm/test/test_mds_store.py
@@ -288,7 +288,7 @@ def test_swap_dims(all_mds_datadirs):
                               ['_bounds', '_center', '_interface']]
                 expected_dims += extra_dims
 
-        assert list(ds.dims.keys()) == expected_dims
+        assert set(ds.dims.keys()) == set(expected_dims)
 
         # make sure swapping works with multiple iters
         ds = xmitgcm.open_mdsdataset(dirname, geometry=expected['geometry'],