Skip to content

Commit c80c8e4

Browse files
authored
Merge pull request #618 from ESMValGroup/change_volume_stats_to_masked_result
Change volume stats to handle and output masked array result
2 parents 8306a65 + af0cd9b commit c80c8e4

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

esmvalcore/preprocessor/_volume.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,10 @@ def _create_cube_time(src_cube, data, times):
7878
Returns
7979
-------
8080
cube
81-
8281
.. note::
83-
8482
If there is only one level of interpolation, the resultant cube
8583
will be collapsed over the associated vertical dimension, and a
8684
scalar vertical coordinate will be added.
87-
8885
"""
8986
# Get the source cube vertical coordinate and associated dimension.
9087
src_times = src_cube.coord('time')
@@ -268,12 +265,14 @@ def volume_statistics(
268265
depth_volume.append(layer_vol)
269266
# ####
270267
# Calculate weighted mean over the water volumn
271-
result.append(np.average(column, weights=depth_volume))
268+
column = np.ma.array(column)
269+
depth_volume = np.ma.array(depth_volume)
270+
result.append(np.ma.average(column, weights=depth_volume))
272271

273272
# ####
274273
# Send time series and dummy cube to cube creating tool.
275274
times = np.array(cube.coord('time').points.astype(float))
276-
result = np.array(result)
275+
result = np.ma.array(result)
277276

278277
# #####
279278
# Create a small dummy output array for the output cube

tests/unit/preprocessor/_volume/test_volume.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_extract_volume(self):
8686
def test_volume_statistics(self):
8787
"""Test to take the volume weighted average of a (2,3,2,2) cube."""
8888
result = volume_statistics(self.grid_4d, 'mean')
89-
expected = np.array([1., 1.])
89+
expected = np.ma.array([1., 1.], mask=False)
9090
self.assert_array_equal(result.data, expected)
9191

9292
def test_volume_statistics_long(self):
@@ -97,7 +97,27 @@ def test_volume_statistics_long(self):
9797
different methods for small and large cubes.
9898
"""
9999
result = volume_statistics(self.grid_4d_2, 'mean')
100-
expected = np.array([1., 1., 1., 1.])
100+
expected = np.ma.array([1., 1., 1., 1.], mask=False)
101+
self.assert_array_equal(result.data, expected)
102+
103+
def test_volume_statistics_masked_level(self):
104+
"""
105+
Test to take the volume weighted average of a (2,3,2,2) cube
106+
where the last depth level is fully masked.
107+
"""
108+
self.grid_4d.data[:, -1, :, :] = np.ma.masked_all((2, 2, 2))
109+
result = volume_statistics(self.grid_4d, 'mean')
110+
expected = np.ma.array([1., 1.], mask=False)
111+
self.assert_array_equal(result.data, expected)
112+
113+
def test_volume_statistics_masked_timestep(self):
114+
"""
115+
Test to take the volume weighted average of a (2,3,2,2) cube
116+
where the first timestep is fully masked.
117+
"""
118+
self.grid_4d.data[0, :, :, :] = np.ma.masked_all((3, 2, 2))
119+
result = volume_statistics(self.grid_4d, 'mean')
120+
expected = np.ma.array([1., 1], mask=[True, False])
101121
self.assert_array_equal(result.data, expected)
102122

103123
def test_depth_integration_1d(self):

0 commit comments

Comments
 (0)