Skip to content

Commit 4188259

Browse files
committed
collapse sum bug
1 parent f94fffd commit 4188259

3 files changed

Lines changed: 44 additions & 29 deletions

File tree

Changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
version 3.16.0
2+
--------------
3+
4+
**2023-??-??**
5+
6+
* Fix bug that caused `cf.Field.collapse` to give incorrect results
7+
for the "sum", "sum_of_weights" and "sum_of_weights2" methods, only
8+
in the case that weights have been requested
9+
(https://github.com/NCAS-CMS/cf-python/issues/701)
10+
111
version 3.15.4
212
--------------
313

cf/field.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,6 +5003,9 @@ def weights(
50035003
comp[key] = self._weights_scale(w, scale)
50045004

50055005
for w in comp.values():
5006+
if not measure:
5007+
w.override_units("1", inplace=True)
5008+
50065009
mn = w.minimum()
50075010
if mn <= 0:
50085011
raise ValueError(
@@ -6721,8 +6724,8 @@ def collapse(
67216724
**unweighted calculations**.
67226725

67236726
.. note:: Unless the *method* is ``'integral'``, the
6724-
units of weights not combined with the
6725-
field's units in the collapsed field.
6727+
units of the weights are not combined with
6728+
the field's units in the collapsed field.
67266729

67276730
If the alternative form of providing the collapse method
67286731
and axes combined as a CF cell methods-like string via the
@@ -6767,39 +6770,41 @@ def collapse(
67676770
time you could set ``weights=('area', 'T')``.
67686771

67696772
measure: `bool`, optional
6770-
Create weights which are cell measures, i.e. which
6771-
describe actual cell sizes (e.g. cell area) with
6772-
appropriate units (e.g. metres squared). By default the
6773-
weights units are ignored.
6773+
If True, and *weights* is not `None`, create weights
6774+
which are cell measures, i.e. which describe actual
6775+
cell sizes (e.g. cell area) with appropriate units
6776+
(e.g. metres squared). By default the weights units
6777+
are ignored.
67746778

67756779
Cell measures can be created for any combination of
6776-
axes. For example, cell measures for a time axis are the
6777-
time span for each cell with canonical units of seconds;
6778-
cell measures for the combination of four axes
6779-
representing time and three dimensional space could have
6780-
canonical units of metres cubed seconds.
6780+
axes. For example, cell measures for a time axis are
6781+
the time span for each cell with canonical units of
6782+
seconds; cell measures for the combination of four
6783+
axes representing time and three dimensional space
6784+
could have canonical units of metres cubed seconds.
67816785

6782-
When collapsing with the ``'integral'`` method, *measure*
6783-
must be True, and the units of the weights are
6784-
incorporated into the units of the returned field
6786+
When collapsing with the ``'integral'`` method,
6787+
*measure* must be True, and the units of the weights
6788+
are incorporated into the units of the returned field
67856789
construct.
67866790

67876791
.. note:: Specifying cell volume weights via
67886792
``weights=['X', 'Y', 'Z']`` or
6789-
``weights=['area', 'Z']`` (or other equivalents)
6790-
will produce **an incorrect result if the
6791-
vertical dimension coordinates do not define the
6792-
actual height or depth thickness of every cell
6793-
in the domain**. In this case,
6794-
``weights='volume'`` should be used instead,
6795-
which requires the field construct to have a
6796-
"volume" cell measure construct.
6793+
``weights=['area', 'Z']`` (or other
6794+
equivalents) will produce **an incorrect
6795+
result if the vertical dimension coordinates
6796+
do not define the actual height or depth
6797+
thickness of every cell in the domain**. In
6798+
this case, ``weights='volume'`` should be
6799+
used instead, which requires the field
6800+
construct to have a "volume" cell measure
6801+
construct.
67976802

6798-
If ``weights=True`` then care also needs to be
6799-
taken, as a "volume" cell measure construct will
6800-
be used if present, otherwise the cell volumes
6801-
will be calculated using the size of the
6802-
vertical coordinate cells.
6803+
If ``weights=True`` then care also needs to
6804+
be taken, as a "volume" cell measure
6805+
construct will be used if present, otherwise
6806+
the cell volumes will be calculated using
6807+
the size of the vertical coordinate cells.
68036808

68046809
.. versionadded:: 3.0.2
68056810

cf/test/test_collapse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def test_Field_collapse_sum_weights(self):
723723

724724
g = f.collapse("area: sum_of_weights", weights=w)
725725
self.assertTrue((g.array == wa.sum()).all())
726-
self.assertEqual(g.Units, cf.Units("m2"))
726+
self.assertEqual(g.Units, cf.Units("1"))
727727

728728
g = f.collapse("area: sum_of_weights", weights=w, measure=True)
729729
self.assertTrue((g.array == wa.sum()).all())
@@ -744,7 +744,7 @@ def test_Field_collapse_sum_weights2(self):
744744

745745
g = f.collapse("area: sum_of_weights2", weights=w)
746746
self.assertTrue((g.array == wa.sum()).all())
747-
self.assertEqual(g.Units, cf.Units("m4"))
747+
self.assertEqual(g.Units, cf.Units("1"))
748748

749749
g = f.collapse("area: sum_of_weights2", weights=w, measure=True)
750750
self.assertTrue((g.array == wa.sum()).all())

0 commit comments

Comments
 (0)