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

Added additional Exception when user is trying to destagger unstaggered variable #148

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
21 changes: 16 additions & 5 deletions tests/test_destagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ def test_rename_staggered_coordinate(input_name, stagger_dim, unstag_dim_name, o


def test_destag_variable_dataarray():
with pytest.raises(ValueError):
_destag_variable(
xr.DataArray(
np.zeros((2, 2)), dims=('x_stag', 'y'), coords={'x_stag': [0, 1], 'y': [0, 1]}
)
with pytest.raises(ValueError) as exc_info:
mock_da = xr.DataArray(
np.zeros((2, 2)), dims=('x_stag', 'y'), coords={'x_stag': [0, 1], 'y': [0, 1]}
)
_destag_variable(mock_da)
assert (
exc_info.value.args[0] == f'Parameter datavar must be xarray.Variable, not {type(mock_da)}'
)


def test_destag_variable_unstaggered():
with pytest.raises(ValueError) as exc_info:
_destag_variable(xr.Variable(('x', 'y'), np.zeros((2, 2))))
assert (
exc_info.value.args[0]
== 'No dimension available to destagger. This variable does not appear to be staggered.'
)


def test_destag_variable_missing_dim():
Expand Down
5 changes: 5 additions & 0 deletions xwrf/destagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def _destag_variable(datavar, stagger_dim=None, unstag_dim_name=None):
'Expected a single destagger dimensions. Found multiple destagger dimensions: '
f'{stagger_dim}'
)
elif len(stagger_dim) == 0:
raise ValueError(
'No dimension available to destagger. This variable does not appear to be '
'staggered.'
)

# we need a string, not a list containing a string
stagger_dim = stagger_dim[0]
Expand Down