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

Change volume stats to handle and output masked array result #618

Merged
merged 12 commits into from
May 27, 2020
7 changes: 3 additions & 4 deletions esmvalcore/preprocessor/_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ def _create_cube_time(src_cube, data, times):
Returns
-------
cube

.. note::

If there is only one level of interpolation, the resultant cube
will be collapsed over the associated vertical dimension, and a
scalar vertical coordinate will be added.

"""
# Get the source cube vertical coordinate and associated dimension.
src_times = src_cube.coord('time')
Expand Down Expand Up @@ -268,7 +265,9 @@ def volume_statistics(
depth_volume.append(layer_vol)
# ####
# Calculate weighted mean over the water volumn
result.append(np.average(column, weights=depth_volume))
column = np.ma.array(column)
depth_volume = np.ma.array(depth_volume)
result.append(np.ma.average(column, weights=depth_volume))

# ####
# Send time series and dummy cube to cube creating tool.
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/preprocessor/_volume/test_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def test_volume_statistics_long(self):
expected = np.array([1., 1., 1., 1.])
self.assert_array_equal(result.data, expected)

def test_volume_statistics_masked_level(self):
"""
Test to take the volume weighted average of a (2,3,2,2) cube
where the last depth level is fully masked.
"""
self.grid_4d.data[:, -1, :, :] = np.ma.masked_all((2, 2, 2))
result = volume_statistics(self.grid_4d, 'mean')
expected = np.array([1., 1.])
self.assert_array_equal(result.data, expected)

def test_depth_integration_1d(self):
"""Test to take the depth integration of a 3 layer cube."""
result = depth_integration(self.grid_3d[:, 0, 0])
Expand Down