diff --git a/doc/whats-new.rst b/doc/whats-new.rst index da28f1dba87..833f475d38c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -30,6 +30,10 @@ Deprecations Bug fixes ~~~~~~~~~ +- :py:meth:`xarray.save_mfdataset` now passes ``**kwargs`` on to ``to_netcdf``, + allowing the ``encoding`` and ``unlimited_dims`` options with ``save_mfdataset``. + (:issue:`6684`) + By `Travis A. O'Brien `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/backends/api.py b/xarray/backends/api.py index d1166624569..dbd332cb9e3 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -1258,7 +1258,14 @@ def dump_to_store( def save_mfdataset( - datasets, paths, mode="w", format=None, groups=None, engine=None, compute=True + datasets, + paths, + mode="w", + format=None, + groups=None, + engine=None, + compute=True, + **kwargs, ): """Write multiple datasets to disk as netCDF files simultaneously. @@ -1280,6 +1287,7 @@ def save_mfdataset( these locations will be overwritten. format : {"NETCDF4", "NETCDF4_CLASSIC", "NETCDF3_64BIT", \ "NETCDF3_CLASSIC"}, optional + **kwargs : additional arguments are passed along to ``to_netcdf`` File format for the resulting netCDF file: @@ -1358,7 +1366,15 @@ def save_mfdataset( writers, stores = zip( *[ to_netcdf( - ds, path, mode, format, group, engine, compute=compute, multifile=True + ds, + path, + mode, + format, + group, + engine, + compute=compute, + multifile=True, + **kwargs, ) for ds, path, group in zip(datasets, paths, groups) ] diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 12f6dd1508c..bbfae73eadd 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3706,6 +3706,29 @@ def test_save_mfdataset_pathlib_roundtrip(self) -> None: ) as actual: assert_identical(actual, original) + def test_save_mfdataset_pass_kwargs(self): + # create a timeseries to store in a netCDF file + times = [0, 1] + time = xr.DataArray(times, dims=("time",)) + + # create a simple dataset to write using save_mfdataset + test_ds = xr.Dataset() + test_ds["time"] = time + + # make sure the times are written as double and + # turn off fill values + encoding = dict(time=dict(dtype="double")) + unlimited_dims = ["time"] + + # set the output file name + output_path = "test.nc" + + # attempt to write the dataset with the encoding and unlimited args + # passed through + xr.save_mfdataset( + [test_ds], [output_path], encoding=encoding, unlimited_dims=unlimited_dims + ) + def test_open_and_do_math(self) -> None: original = Dataset({"foo": ("x", np.random.randn(10))}) with create_tmp_file() as tmp: