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

Open mfdataset enchancement #9955

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ New Features
~~~~~~~~~~~~
- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Spencer Clark <https://github.com/spencerkclark>`_.
- Add new ``errors`` arg to :py:meth:`open_mfdataset` to better handle invalid files.
(:issue:`6736`). By `Pratiman Patel <https://github.com/pratiman-91>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
30 changes: 28 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import warnings
from collections.abc import (
Callable,
Hashable,
Expand Down Expand Up @@ -1393,6 +1394,7 @@ def open_mfdataset(
join: JoinOptions = "outer",
attrs_file: str | os.PathLike | None = None,
combine_attrs: CombineAttrsOptions = "override",
errors: ErrorOptionsWithWarn = "raise",
**kwargs,
) -> Dataset:
"""Open multiple files as a single dataset.
Expand Down Expand Up @@ -1519,7 +1521,11 @@ def open_mfdataset(

If a callable, it must expect a sequence of ``attrs`` dicts and a context object
as its only parameters.
**kwargs : optional
errors : {'ignore', 'raise', 'warn'}, default 'raise'
- If 'raise', then invalid dataset will raise an exception.
- If 'ignore', then invalid dataset will be ignored.
- If 'warn', then a warning will be issued for each invalid dataset.
**kwargs : optional
Additional arguments passed on to :py:func:`xarray.open_dataset`. For an
overview of some of the possible options, see the documentation of
:py:func:`xarray.open_dataset`
Expand Down Expand Up @@ -1611,7 +1617,27 @@ def open_mfdataset(
open_ = open_dataset
getattr_ = getattr

datasets = [open_(p, **open_kwargs) for p in paths1d]
if errors in ("raise", "warn", "ignore"):
pratiman-91 marked this conversation as resolved.
Show resolved Hide resolved
datasets = []
for p in paths1d:
try:
ds = open_(p, **open_kwargs)
datasets.append(ds)
except Exception:
if errors == "raise":
raise
elif errors == "ignore":
pratiman-91 marked this conversation as resolved.
Show resolved Hide resolved
warnings.warn(
f"Could not open {p}. Ignoring.", UserWarning, stacklevel=2
)
continue
else:
continue
else:
raise ValueError(
f"{errors} is an invalid option for the keyword argument ``errors``"
)

closers = [getattr_(ds, "_close") for ds in datasets]
if preprocess is not None:
datasets = [preprocess(ds) for ds in datasets]
Expand Down
Loading