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 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
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
39 changes: 37 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: str = "raise",
pratiman-91 marked this conversation as resolved.
Show resolved Hide resolved
**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,36 @@ def open_mfdataset(
open_ = open_dataset
getattr_ = getattr

datasets = [open_(p, **open_kwargs) for p in paths1d]
try:
if errors == "raise":
datasets = [open_(p, **open_kwargs) for p in paths1d]
elif errors == "ignore":
datasets = []
for p in paths1d:
try:
ds = open_(p, **open_kwargs)
datasets.append(ds)
except Exception:
continue
elif errors == "warn":
datasets = []
for p in paths1d:
try:
ds = open_(p, **open_kwargs)
datasets.append(ds)
except Exception:
warnings.warn(
f"Could not open {p}. Ignoring.", UserWarning, stacklevel=2
)
continue
else:
raise ValueError(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never being raised because it is catched by the outer try-except.

Also, to keep the complexity lower, check this before entering the if else block: if errors not in ("raise", "warn", "ignore"): raise ValueError(...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @headtr1ck for pointing this out. I have made changes to the logic based on your suggestions.

f"{errors} is an invalid option for the keyword argument ``errors``"
)
except ValueError:
for ds in datasets:
ds.close()

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