Skip to content

Commit 6d8704e

Browse files
authored
Merge pull request #954 from davidhassell/volume-cell-measure
ix bug that caused weighted `cf.collapse` and `cf.weights` to fail when a cell meausures has a size 1 axis
2 parents 346e0dd + e7af87c commit 6d8704e

5 files changed

Lines changed: 35 additions & 4 deletions

File tree

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Version NEXTVERSION
55

66
* Fix bug in `cf.read` that prevented some OPeNDAP URLS being read
77
(https://github.com/NCAS-CMS/cf-python/issues/948)
8+
* Fix bug that caused weighted `cf.collapse` and `cf.weights` to fail
9+
when a cell meausures has a size 1 axis
10+
(https://github.com/NCAS-CMS/cf-python/issues/952)
811

912
----
1013

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ recursive-exclude cf/data *.rst
99
prune cf/test
1010
recursive-include cf/test __init__.py test_*.py
1111
recursive-include cf/test cfa_test.sh run_tests.py setup_create_field.py create_test_files*.py individual_tests.sh
12-
recursive-include cf/test test_file.nc test_file[2-4].nc file.nc file[1-9].nc ugrid_global_1.nc ugrid_global_2.nc create_test_files.npz create_test_files_2.npz wgdos_packed.pp extra_data.pp file1.pp *.cdl create_test_files.npz create_test_files_2.npz example_field_0.nc
12+
recursive-include cf/test test_file.nc test_file[2-4].nc file.nc file[1-9].nc ugrid_global_1.nc ugrid_global_2.nc create_test_files.npz create_test_files_2.npz wgdos_packed.pp extra_data.pp file1.pp *.cdl create_test_files.npz create_test_files_2.npz example_field_0.nc cell_measures.nc
1313
recursive-include cf/test/example_field_0.zarr[23] *
1414
include cf/test/example_field_0.kerchunk
1515
prune cf/test/dir

cf/test/cell_measures.nc

34.3 KB
Binary file not shown.

cf/test/test_collapse.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def setUp(self):
3737
self.filename2 = os.path.join(
3838
os.path.dirname(os.path.abspath(__file__)), "test_file2.nc"
3939
)
40+
self.cell_measures = os.path.join(
41+
os.path.dirname(os.path.abspath(__file__)), "cell_measures.nc"
42+
)
4043

4144
def test_Field_collapse_CLIMATOLOGICAL_TIME(self):
4245
verbose = False
@@ -853,6 +856,31 @@ def test_Field_collapse_HEALPix(self):
853856
g1.coordinate_reference("grid_mapping_name:latitude_longitude")
854857
)
855858

859+
def test_Field_cell_measures(self):
860+
"""Test collapse weights by area and volume cell measures."""
861+
f = cf.read(self.cell_measures)[0]
862+
self.assertEqual(f.shape, (1, 5, 5, 5))
863+
self.assertEqual(f.cell_measure("measure:area").shape, (5, 5))
864+
self.assertEqual(f.cell_measure("measure:volume").shape, (1, 5, 5, 5))
865+
866+
f.collapse("area: mean", weights="area")
867+
f.collapse("volume: mean", weights="volume")
868+
869+
self.assertEqual(f.weights("area").shape, (5, 5))
870+
self.assertEqual(f.weights("volume").shape, (5, 5, 5))
871+
872+
g = f[..., 0]
873+
874+
self.assertEqual(g.shape, (1, 5, 5, 1))
875+
self.assertEqual(g.cell_measure("measure:area").shape, (5, 1))
876+
self.assertEqual(g.cell_measure("measure:volume").shape, (1, 5, 5, 1))
877+
878+
g.collapse("area: mean", weights="area")
879+
g.collapse("volume: mean", weights="volume")
880+
881+
self.assertEqual(g.weights("area").shape, (5,))
882+
self.assertEqual(g.weights("volume").shape, (5, 5))
883+
856884

857885
if __name__ == "__main__":
858886
print("Run date:", datetime.datetime.now())

cf/weights.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,9 +1548,9 @@ def cell_measure(
15481548
)
15491549

15501550
clm = clm.get_data(_fill_value=False).copy()
1551-
if clm_axes != clm_axes0:
1552-
iaxes = [clm_axes0.index(axis) for axis in clm_axes]
1553-
clm.squeeze(iaxes, inplace=True)
1551+
1552+
# Squeeze out size 1 axes
1553+
clm.squeeze(inplace=True)
15541554

15551555
if methods:
15561556
weights[tuple(clm_axes)] = measure + " cell measure"

0 commit comments

Comments
 (0)