From c9bcf7388c936c9c2f41dbcc064b3bedb1ae4f74 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Mon, 4 Mar 2024 12:36:24 +0000 Subject: [PATCH 1/2] Fix error message when can't create area weights --- Changelog.rst | 13 +++++++++++++ cf/field.py | 23 +++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index e0346890b2..1d8a4bcb34 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -1,3 +1,14 @@ +version NEXT +------------ + +**2024-??-??** + +* Fix misleading error message when it is not possible to create area + weights requested from `cf.Field.collapse` + (https://github.com/NCAS-CMS/cf-python/issues/731) + +---- + version 3.16.1 -------------- @@ -29,6 +40,8 @@ version 3.16.1 (https://github.com/NCAS-CMS/cf-python/issues/713) * Changed dependency: ``1.11.1.0<=cfdm<1.11.2.0`` +---- + version 3.16.0 -------------- diff --git a/cf/field.py b/cf/field.py index 75b32313f7..c9456f27a2 100644 --- a/cf/field.py +++ b/cf/field.py @@ -3836,18 +3836,17 @@ def weights( # Found area weights from X and Y dimension # coordinates area_weights = True - else: - Weights.polygon_area( - self, - None, - comp, - weights_axes, - measure=measure, - radius=radius, - great_circle=great_circle, - methods=methods, - auto=False, - ) + elif Weights.polygon_area( + self, + None, + comp, + weights_axes, + measure=measure, + radius=radius, + great_circle=great_circle, + methods=methods, + auto=True, + ): # Found area weights from UGRID/geometry cells area_weights = True From 3c2802f42f51c8fad667f257c9c6c4a2dd5f3d19 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 5 Mar 2024 15:26:32 +0000 Subject: [PATCH 2/2] weights error test --- cf/test/test_weights.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cf/test/test_weights.py b/cf/test/test_weights.py index e72c3399da..bd202672a3 100644 --- a/cf/test/test_weights.py +++ b/cf/test/test_weights.py @@ -326,6 +326,16 @@ def test_weights_cell_measures_coordinates(self): with self.assertRaises(ValueError): w = f.weights("area", cell_measures=False, coordinates=False) + def test_weights_exceptions(self): + f = cf.example_field(0) + f.coordinate("X").del_bounds() + f.coordinate("Y").del_bounds() + + with self.assertRaisesRegex( + ValueError, "Can't create weights: Unable to find cell areas" + ): + f.weights("area") + if __name__ == "__main__": print("Run date:", datetime.datetime.now())