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

Fix destagger attrs #97

Merged
merged 5 commits into from
Sep 16, 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
12 changes: 12 additions & 0 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def test_dataarray_destagger(test_grid):
xr.testing.assert_allclose(destaggered['XLAT'], test_grid['XLAT'])
xr.testing.assert_allclose(destaggered['XLONG'], test_grid['XLONG'])

# Check attributes are preserved
assert set(destaggered.attrs.keys()) == set(data.attrs.keys()) - {
'stagger',
}


@pytest.mark.parametrize('test_grid', ['lambert_conformal', 'mercator'], indirect=True)
def test_dataarray_destagger_with_exclude(test_grid):
Expand Down Expand Up @@ -110,5 +115,12 @@ def test_dataset_destagger(test_grid):
or destaggered[varname].attrs['stagger'] == ''
)

# Check preservation of variable attrs
for varname in set(test_grid.data_vars).intersection(set(destaggered.data_vars)):
# because of xwrf.postprocess, the destaggered attrs will include more information
assert set(test_grid[varname].attrs.keys()) - {'stagger', 'units'} <= set(
destaggered[varname].attrs.keys()
)

# Check that attrs are preserved
assert destaggered.attrs == test_grid.attrs
6 changes: 4 additions & 2 deletions tests/test_destagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ def test_destag_variable_multiple_dims():
],
)
def test_destag_variable_1d(unstag_dim_name, expected_output_dim_name):
staggered = xr.Variable(('bottom_top_stag',), np.arange(5), attrs={'stagger': 'Z'})
staggered = xr.Variable(
('bottom_top_stag',), np.arange(5), attrs={'foo': 'bar', 'stagger': 'Z'}
)
output = _destag_variable(staggered, unstag_dim_name=unstag_dim_name)
# Check values
np.testing.assert_array_almost_equal(output.values, 0.5 + np.arange(4))
# Check dim name
assert output.dims[0] == expected_output_dim_name
# Check attrs
assert not output.attrs
assert output.attrs == {'foo': 'bar'}


def test_destag_variable_2d():
Expand Down
4 changes: 2 additions & 2 deletions xwrf/destagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def _destag_variable(datavar, stagger_dim=None, unstag_dim_name=None):
return xr.Variable(
dims=tuple(str(unstag_dim_name) if dim == stagger_dim else dim for dim in center_mean.dims),
data=center_mean.data,
attrs=_drop_attrs(center_mean.attrs, ('stagger',)),
encoding=center_mean.encoding,
attrs=_drop_attrs(datavar.attrs, ('stagger',)),
encoding=datavar.encoding,
fastpath=True,
)

Expand Down