Skip to content

Commit

Permalink
pass kwargs through from save_mfdataset to to_netcdf (#6686)
Browse files Browse the repository at this point in the history
* `xarray.save_mfdataset` add `**kwargs` (GH6684)

* adds a `**kwargs` option to `xarray.save_mfdataset` that passes through to
  `to_netcdf`

* Relocate test_save_mfdataset_pass_kwargs

* Move test_save_mfdataset_pass_kwargs to test_backends.py
* Simplify the test

* Update xarray/tests/test_backends.py

Co-authored-by: Anderson Banihirwe <[email protected]>

Co-authored-by: Travis A. O'Brien <[email protected]>
Co-authored-by: Anderson Banihirwe <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2022
1 parent aa1d1d1 commit 89b1fac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/taobrienlbl>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
20 changes: 18 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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)
]
Expand Down
23 changes: 23 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 89b1fac

Please sign in to comment.