Skip to content

Commit 87a3997

Browse files
authored
Merge pull request #732 from davidhassell/no-weights-error
Fix misleading error message when it is not possible to create area weights requested from `cf.Field.collapse`
2 parents cafeee5 + 3c2802f commit 87a3997

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

Changelog.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
version NEXT
2+
------------
3+
4+
**2024-??-??**
5+
6+
* Fix misleading error message when it is not possible to create area
7+
weights requested from `cf.Field.collapse`
8+
(https://github.com/NCAS-CMS/cf-python/issues/731)
9+
10+
----
11+
112
version 3.16.1
213
--------------
314

@@ -29,6 +40,8 @@ version 3.16.1
2940
(https://github.com/NCAS-CMS/cf-python/issues/713)
3041
* Changed dependency: ``1.11.1.0<=cfdm<1.11.2.0``
3142

43+
----
44+
3245
version 3.16.0
3346
--------------
3447

cf/field.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3836,18 +3836,17 @@ def weights(
38363836
# Found area weights from X and Y dimension
38373837
# coordinates
38383838
area_weights = True
3839-
else:
3840-
Weights.polygon_area(
3841-
self,
3842-
None,
3843-
comp,
3844-
weights_axes,
3845-
measure=measure,
3846-
radius=radius,
3847-
great_circle=great_circle,
3848-
methods=methods,
3849-
auto=False,
3850-
)
3839+
elif Weights.polygon_area(
3840+
self,
3841+
None,
3842+
comp,
3843+
weights_axes,
3844+
measure=measure,
3845+
radius=radius,
3846+
great_circle=great_circle,
3847+
methods=methods,
3848+
auto=True,
3849+
):
38513850
# Found area weights from UGRID/geometry cells
38523851
area_weights = True
38533852

cf/test/test_weights.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ def test_weights_cell_measures_coordinates(self):
326326
with self.assertRaises(ValueError):
327327
w = f.weights("area", cell_measures=False, coordinates=False)
328328

329+
def test_weights_exceptions(self):
330+
f = cf.example_field(0)
331+
f.coordinate("X").del_bounds()
332+
f.coordinate("Y").del_bounds()
333+
334+
with self.assertRaisesRegex(
335+
ValueError, "Can't create weights: Unable to find cell areas"
336+
):
337+
f.weights("area")
338+
329339

330340
if __name__ == "__main__":
331341
print("Run date:", datetime.datetime.now())

0 commit comments

Comments
 (0)