Skip to content

Commit a06723f

Browse files
committed
dev
1 parent 5d44b6f commit a06723f

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Version NEXTVERSION
99
* Allow multiple conditions for the same axis in `cf.Field.subspace`
1010
and `cf.Field.indices`
1111
(https://github.com/NCAS-CMS/cf-python/issues/881)
12+
* Fix bug in `cf.Field.collapse` that causes an Exception to be raised
13+
for external cell measures data
14+
(https://github.com/NCAS-CMS/cf-python/issues/885)
1215
* New dependency: ``distributed>=2025.5.1``
1316

1417
----

cf/field.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,7 +3708,12 @@ def weights(
37083708
weights_axes.discard(xaxis)
37093709
weights_axes.discard(yaxis)
37103710
if not Weights.cell_measure(
3711-
self, "area", comp, weights_axes, methods=methods
3711+
self,
3712+
"area",
3713+
comp,
3714+
weights_axes,
3715+
methods=methods,
3716+
auto=True,
37123717
):
37133718
Weights.area_XY(
37143719
self,
@@ -4919,7 +4924,6 @@ def collapse(
49194924
scale=None,
49204925
radius="earth",
49214926
great_circle=False,
4922-
cell_measures=True,
49234927
verbose=None,
49244928
remove_vertical_crs=True,
49254929
_create_zero_size_cell_bounds=False,
@@ -5477,14 +5481,12 @@ def collapse(
54775481
the field construct's `weights` method:
54785482
``f.weights(weights, components=True, axes=axes,
54795483
measure=measure, scale=scale, radius=radius,
5480-
great_circle=great_circle,
5481-
cell_measures=cell_measures)``.
5484+
great_circle=great_circle)``.
54825485

5483-
See the *axes*, *measure*, *scale*, *radius*,
5484-
*great_circle*, and *cell_measures* parameters, and
5485-
`cf.Field.weights` for details; and note that the
5486-
value of *scale* may be modified depending on the
5487-
value of *measure*.
5486+
See the cf.Field.weights` and the *axes*, *measure*,
5487+
*scale*, *radius*, and *great_circle* parameters for
5488+
details; and note that the value of *scale* may be
5489+
modified depending on the value of *measure*.
54885490

54895491
.. warning:: By default *weights* is `None`, resulting
54905492
in **unweighted calculations**.
@@ -5500,9 +5502,10 @@ def collapse(
55005502
units of the weights are not combined with
55015503
the field's units in the collapsed field.
55025504

5503-
.. note:: A pre-calculated weights array may also be
5504-
provided as the *weights* parameter. See
5505-
`cf.Field.weights` for details
5505+
.. note:: A pre-calculated weights array or dictionary
5506+
may also be provided as the *weights*
5507+
parameter. See `cf.Field.weights` for
5508+
details
55065509

55075510
If the collapse method and axes have been provided as
55085511
a CF cell methods-like string via the *method*
@@ -5619,15 +5622,6 @@ def collapse(
56195622

56205623
.. versionadded:: 3.2.0
56215624

5622-
cell_measures: `bool`, optional
5623-
If True, the default, then area and volume cell
5624-
measure constructs are considered for weights creation
5625-
when *weights* is `True`, ``'area'``, or
5626-
``'volume'``. If False then cell measure constructs
5627-
are ignored for these *weights*.
5628-
5629-
.. versionadded:: NEXTVERSION
5630-
56315625
squeeze: `bool`, optional
56325626
If True then size 1 collapsed axes are removed from the
56335627
output data array. By default the axes which are collapsed
@@ -6733,7 +6727,6 @@ def collapse(
67336727
measure=measure,
67346728
radius=radius,
67356729
great_circle=great_circle,
6736-
cell_measures=cell_measures,
67376730
)
67386731
if g_weights:
67396732
# For grouped collapses, bring the weights
@@ -6768,7 +6761,6 @@ def collapse(
67686761
group_by=group_by,
67696762
axis_in=axes_in[0],
67706763
verbose=verbose,
6771-
cell_measures=cell_measures,
67726764
)
67736765

67746766
if regroup:
@@ -6839,7 +6831,6 @@ def collapse(
68396831
measure=measure,
68406832
radius=radius,
68416833
great_circle=great_circle,
6842-
cell_measures=cell_measures,
68436834
)
68446835
if d_weights:
68456836
d_kwargs["weights"] = d_weights
@@ -7113,7 +7104,6 @@ def _collapse_grouped(
71137104
coordinate=None,
71147105
measure=False,
71157106
weights=None,
7116-
cell_measures=True,
71177107
squeeze=None,
71187108
group_by=None,
71197109
axis_in=None,

cf/weights.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,15 +1508,16 @@ def cell_measure(
15081508
return False
15091509

15101510
raise ValueError(
1511-
f"Can't find weights: No {measure!r} cell measure"
1511+
f"Can't find weights for {f!r}: No {measure!r} cell measure"
15121512
)
15131513

15141514
elif len_m > 1:
15151515
if auto:
15161516
return False
15171517

15181518
raise ValueError(
1519-
f"Can't find weights: Multiple {measure!r} cell measures"
1519+
f"Can't find weights for {f!r}: Multiple {measure!r} cell "
1520+
"measures"
15201521
)
15211522

15221523
key, clm = m.popitem()
@@ -1526,9 +1527,11 @@ def cell_measure(
15261527
return False
15271528

15281529
raise ValueError(
1529-
f"Can't find weights: Cell measure {m!r} has no data, "
1530-
"possibly because it is external. "
1531-
"Consider setting cell_measures=False"
1530+
f"Can't find weights for {f!r}: Cell measure {clm!r} has no "
1531+
"data, possibly because it is in a missing external file. "
1532+
"Consider setting cell_measures=False or, if applicable, "
1533+
"re-reading the dataset whilst providing the external "
1534+
"file (see cf.read for details)."
15321535
)
15331536

15341537
clm_axes0 = f.get_data_axes(key)
@@ -1543,7 +1546,8 @@ def cell_measure(
15431546
return False
15441547

15451548
raise ValueError(
1546-
"Multiple weights specifications for "
1549+
f"Can't find weights for {f!r}: Multiple weights "
1550+
"specifications for "
15471551
f"{f.constructs.domain_axis_identity(axis)!r} axis"
15481552
)
15491553

0 commit comments

Comments
 (0)