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

Ignore singleton coordinates without dims when attempting to generate bounds #281

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions tests/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def test_adds_bounds_to_the_dataset(self):
result = ds.bounds.add_missing_bounds()
assert result.identical(self.ds_with_bnds)

def test_adds_bounds_to_the_dataset_skips_nondimensional_axes(self):
# generate dataset with height coordinate
ds = generate_dataset(cf_compliant=True, has_bounds=True)
ds = ds.assign_coords({"height": 2})

# drop bounds
dsm = ds.drop_vars(["lat_bnds", "lon_bnds"]).copy()

# test bounds re-generation
result = dsm.bounds.add_missing_bounds()

# dataset with missing bounds added should match dataset with bounds
# and added height coordinate
assert result.identical(ds)


class TestGetBounds:
@pytest.fixture(autouse=True)
Expand Down
6 changes: 6 additions & 0 deletions xcdat/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def add_missing_bounds(self, width: float = 0.5) -> xr.Dataset:
except KeyError:
pass

# determine if the axis is also a dimension by determining
# if there is overlap between the CF axis names and the dimension
# names. If not, skip over axis for validation.
if len(set(CF_NAME_MAP[axis]) & set(self._dataset.dims.keys())) == 0:
continue

Comment on lines +143 to +148
Copy link
Collaborator Author

@pochedls pochedls Jul 26, 2022

Choose a reason for hiding this comment

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

Just to make it clear what is going on...

For an axis like lat, set(CF_NAME_MAP[axis]) yields {"Y", "latitude", "lat"}.

set(self._dataset.dims.keys())) yields {'axis_nbounds', 'lat', 'lon', 'time'} .

set(...) & set(...) determines the intersection between these two groups. If there are elements in the resulting set (i.e., len(...) > 0, it means that the axis is also a dimension. If there are no elements in the set, then the axis is not a dimension.

If the axis is not a dimension, there is no need to find bounds and we skip adding bounds.

if coord_var is not None:
try:
self.get_bounds(axis)
Expand Down