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

Drop missing variables #267

Merged
merged 8 commits into from
Dec 7, 2021
Merged
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
35 changes: 19 additions & 16 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,24 +413,20 @@ def wrapper(obj: Union[DataArray, Dataset], key: str) -> List[DataArray]:
return cast(F, wrapper)


def _must_exist(func: F) -> F:
@functools.wraps(func)
def wrapper(obj: Union[DataArray, Dataset], key: str) -> List[DataArray]:
return [k for k in func(obj, key) if k in obj]

return cast(F, wrapper)
def _raise_if_not_single(key, results):
if len(results) > 1:
raise KeyError(
f"Multiple results for {key!r} found: {results!r}. I expected only one."
)
elif len(results) == 0:
raise KeyError(f"No results found for {key!r}.")


def _single(func: F) -> F:
@functools.wraps(func)
def wrapper(obj: Union[DataArray, Dataset], key: str):
results = func(obj, key)
if len(results) > 1:
raise KeyError(
f"Multiple results for {key!r} found: {results!r}. I expected only one."
)
elif len(results) == 0:
raise KeyError(f"No results found for {key!r}.")
_raise_if_not_single(key, results)
return results

wrapper.__doc__ = (
Expand Down Expand Up @@ -1879,6 +1875,10 @@ def __getitem__(self, key: Union[str, List[str]]) -> Union[DataArray, Dataset]:
"""
return _getitem(self, key)

def _drop_missing_variables(self, variables: List[str]) -> List[str]:

return [var for var in variables if var in self._obj]

@property
def formula_terms(self) -> Dict[str, Dict[str, str]]:
"""
Expand All @@ -1904,7 +1904,9 @@ def bounds(self) -> Dict[str, List[str]]:
keys = self.keys() | set(obj.variables)

vardict = {
key: apply_mapper(_must_exist(_get_bounds), obj, key, error=False)
key: self._drop_missing_variables(
apply_mapper(_get_bounds, obj, key, error=False)
)
for key in keys
}

Expand All @@ -1924,9 +1926,10 @@ def get_bounds(self, key: str) -> DataArray:
DataArray
"""

return apply_mapper(
_variables(_single(_must_exist(_get_bounds))), self._obj, key
)[0]
results = self.bounds.get(key, [])
_raise_if_not_single(key, results)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this is right. For a staggered grid you could have multiple lat/lon with different bounds though I don't know that anyone ever does that :)

Copy link
Member Author

@malmans2 malmans2 Nov 24, 2021

Choose a reason for hiding this comment

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

Mmmm, not sure. I think we would have to change how we pare da.attrs['bounds'], because I think right now we assume it's a single string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohh actually you're right, for example key="longitude" could be associated to multiple bounds. The problem is that I think other functions use this and need a single DataArray. Maybe it's better to address this in a separate PR...

Copy link
Contributor

Choose a reason for hiding this comment

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

Lets open an issue for that. And here lets not raise_if_single...


return self._obj[results[0]]

def get_bounds_dim_name(self, key: str) -> str:
"""
Expand Down