From 0d183c568bac57e569478b47b6c2e3bbd1753495 Mon Sep 17 00:00:00 2001 From: "Phillip J. Wolfram" Date: Wed, 22 Mar 2017 09:13:32 -0600 Subject: [PATCH] Ensures open_mfdataset attributes are retained Uses attributes from first file opened by `open_mfdataset` to populate ds.attrs. --- xarray/backends/api.py | 5 ++++- xarray/tests/test_backends.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 54cf95cb832..03bcde8f292 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -437,7 +437,8 @@ def open_mfdataset(paths, chunks=None, concat_dim=_CONCAT_DIM_DEFAULT, lock=None, **kwargs): """Open multiple files as a single dataset. - Experimental. Requires dask to be installed. + Requires dask to be installed. Attributes from the first dataset file + are used for the combined dataset. Parameters ---------- @@ -515,6 +516,8 @@ def open_mfdataset(paths, chunks=None, concat_dim=_CONCAT_DIM_DEFAULT, else: combined = auto_combine(datasets, concat_dim=concat_dim, compat=compat) combined._file_obj = _MultiFileCloser(file_objs) + combined.attrs = datasets[0].attrs + return combined diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index bc561e4f21f..5cd1606ae89 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1057,6 +1057,25 @@ def test_open_mfdataset(self): with self.assertRaisesRegexp(IOError, 'no files to open'): open_mfdataset('foo-bar-baz-*.nc') + def test_attrs_mfdataset(self): + original = Dataset({'foo': ('x', np.random.randn(10))}) + with create_tmp_file() as tmp1: + with create_tmp_file() as tmp2: + ds1 = original.isel(x=slice(5)) + ds2 = original.isel(x=slice(5, 10)) + ds1.attrs['test1'] = 'foo' + ds2.attrs['test2'] = 'bar' + ds1.to_netcdf(tmp1) + ds2.to_netcdf(tmp2) + with open_mfdataset([tmp1, tmp2]) as actual: + # presumes that attributes inherited from + # first dataset loaded + self.assertEqual(actual.test1, ds1.test1) + # attributes from ds2 are not retained, e.g., + with self.assertRaisesRegexp(AttributeError, + 'no attribute'): + actual.test2 + def test_preprocess_mfdataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) with create_tmp_file() as tmp: