Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass kwargs through from save_mfdataset to to_netcdf #6686

Merged
merged 3 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
taobrienlbl marked this conversation as resolved.
Show resolved Hide resolved
# 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