|
163 | 163 | "average", |
164 | 164 | "sd", |
165 | 165 | "standard_deviation", |
| 166 | + "sum", |
166 | 167 | "var", |
167 | 168 | "variance", |
168 | 169 | "sum", |
@@ -3767,6 +3768,17 @@ def weights( |
3767 | 3768 | for key, w in comp.items(): |
3768 | 3769 | comp[key] = Weights.scale(w, scale) |
3769 | 3770 |
|
| 3771 | + for w in comp.values(): |
| 3772 | + if not measure: |
| 3773 | + w.override_units("1", inplace=True) |
| 3774 | + |
| 3775 | + mn = w.minimum() |
| 3776 | + if mn <= 0: |
| 3777 | + raise ValueError( |
| 3778 | + "All weights must be positive. " |
| 3779 | + f"Got a weight of {mn}" |
| 3780 | + ) |
| 3781 | + |
3770 | 3782 | if components or methods: |
3771 | 3783 | # -------------------------------------------------------- |
3772 | 3784 | # Return a dictionary of component weights, which may be |
@@ -4952,7 +4964,7 @@ def collapse( |
4952 | 4964 | group_span=None, |
4953 | 4965 | group_contiguous=1, |
4954 | 4966 | measure=False, |
4955 | | - scale=1, |
| 4967 | + scale=None, |
4956 | 4968 | radius="earth", |
4957 | 4969 | great_circle=False, |
4958 | 4970 | verbose=None, |
@@ -5477,6 +5489,10 @@ def collapse( |
5477 | 5489 | .. note:: By default *weights* is `None`, resulting in |
5478 | 5490 | **unweighted calculations**. |
5479 | 5491 |
|
| 5492 | + .. note:: Unless the *method* is ``'integral'``, the |
| 5493 | + units of the weights are not combined with |
| 5494 | + the field's units in the collapsed field. |
| 5495 | + |
5480 | 5496 | If the alternative form of providing the collapse method |
5481 | 5497 | and axes combined as a CF cell methods-like string via the |
5482 | 5498 | *method* parameter has been used, then the *axes* |
@@ -5520,57 +5536,58 @@ def collapse( |
5520 | 5536 | time you could set ``weights=('area', 'T')``. |
5521 | 5537 |
|
5522 | 5538 | measure: `bool`, optional |
5523 | | - Create weights which are cell measures, i.e. which |
5524 | | - describe actual cell sizes (e.g. cell area) with |
5525 | | - appropriate units (e.g. metres squared). By default the |
5526 | | - weights are normalised and have arbitrary units. |
| 5539 | + If True, and *weights* is not `None`, create weights |
| 5540 | + which are cell measures, i.e. which describe actual |
| 5541 | + cell sizes (e.g. cell area) with appropriate units |
| 5542 | + (e.g. metres squared). By default the weights units |
| 5543 | + are ignored. |
5527 | 5544 |
|
5528 | 5545 | Cell measures can be created for any combination of |
5529 | | - axes. For example, cell measures for a time axis are the |
5530 | | - time span for each cell with canonical units of seconds; |
5531 | | - cell measures for the combination of four axes |
5532 | | - representing time and three dimensional space could have |
5533 | | - canonical units of metres cubed seconds. |
| 5546 | + axes. For example, cell measures for a time axis are |
| 5547 | + the time span for each cell with canonical units of |
| 5548 | + seconds; cell measures for the combination of four |
| 5549 | + axes representing time and three dimensional space |
| 5550 | + could have canonical units of metres cubed seconds. |
5534 | 5551 |
|
5535 | | - When collapsing with the ``'integral'`` method, *measure* |
5536 | | - must be True, and the units of the weights are |
5537 | | - incorporated into the units of the returned field |
| 5552 | + When collapsing with the ``'integral'`` method, |
| 5553 | + *measure* must be True, and the units of the weights |
| 5554 | + are incorporated into the units of the returned field |
5538 | 5555 | construct. |
5539 | 5556 |
|
5540 | 5557 | .. note:: Specifying cell volume weights via |
5541 | 5558 | ``weights=['X', 'Y', 'Z']`` or |
5542 | | - ``weights=['area', 'Z']`` (or other equivalents) |
5543 | | - will produce **an incorrect result if the |
5544 | | - vertical dimension coordinates do not define the |
5545 | | - actual height or depth thickness of every cell |
5546 | | - in the domain**. In this case, |
5547 | | - ``weights='volume'`` should be used instead, |
5548 | | - which requires the field construct to have a |
5549 | | - "volume" cell measure construct. |
| 5559 | + ``weights=['area', 'Z']`` (or other |
| 5560 | + equivalents) will produce **an incorrect |
| 5561 | + result if the vertical dimension coordinates |
| 5562 | + do not define the actual height or depth |
| 5563 | + thickness of every cell in the domain**. In |
| 5564 | + this case, ``weights='volume'`` should be |
| 5565 | + used instead, which requires the field |
| 5566 | + construct to have a "volume" cell measure |
| 5567 | + construct. |
5550 | 5568 |
|
5551 | | - If ``weights=True`` then care also needs to be |
5552 | | - taken, as a "volume" cell measure construct will |
5553 | | - be used if present, otherwise the cell volumes |
5554 | | - will be calculated using the size of the |
5555 | | - vertical coordinate cells. |
| 5569 | + If ``weights=True`` then care also needs to |
| 5570 | + be taken, as a "volume" cell measure |
| 5571 | + construct will be used if present, otherwise |
| 5572 | + the cell volumes will be calculated using |
| 5573 | + the size of the vertical coordinate cells. |
5556 | 5574 |
|
5557 | 5575 | .. versionadded:: 3.0.2 |
5558 | 5576 |
|
5559 | 5577 | scale: number or `None`, optional |
5560 | 5578 | If set to a positive number then scale the weights so |
5561 | 5579 | that they are less than or equal to that number. If |
5562 | | - set to `None` the weights are not scaled. In general |
5563 | | - the default is for weights to be scaled to lie between |
5564 | | - 0 and 1; however if *measure* is True then the weights |
5565 | | - are never scaled and the value of *scale* is taken as |
5566 | | - `None`, regardless of its setting. |
| 5580 | + set to `None`, the default, then the weights are not |
| 5581 | + scaled. |
5567 | 5582 |
|
5568 | 5583 | *Parameter example:* |
5569 | 5584 | To scale all weights so that they lie between 0 and |
5570 | | - 10 ``scale=10``. |
| 5585 | + 1 ``scale=1``. |
5571 | 5586 |
|
5572 | 5587 | .. versionadded:: 3.0.2 |
5573 | 5588 |
|
| 5589 | + .. versionchanged:: 3.16.0 Default changed to `None` |
| 5590 | + |
5574 | 5591 | radius: optional |
5575 | 5592 | Specify the radius used for calculating the areas of |
5576 | 5593 | cells defined in spherical polar coordinates. The |
@@ -6456,6 +6473,12 @@ def collapse( |
6456 | 6473 | # ------------------------------------------------------------ |
6457 | 6474 | # Parse the methods and axes |
6458 | 6475 | # ------------------------------------------------------------ |
| 6476 | + if measure and scale is not None: |
| 6477 | + raise ValueError( |
| 6478 | + "'scale' must be None when 'measure' is True. " |
| 6479 | + f"Got: scale={scale!r}" |
| 6480 | + ) |
| 6481 | + |
6459 | 6482 | if ":" in method: |
6460 | 6483 | # Convert a cell methods string (such as 'area: mean dim3: |
6461 | 6484 | # dim2: max T: minimum height: variance') to a CellMethod |
@@ -6673,8 +6696,15 @@ def collapse( |
6673 | 6696 | g_weights = None |
6674 | 6697 | else: |
6675 | 6698 | if measure: |
6676 | | - # Never scale weights that are cell measures |
6677 | | - scale = None |
| 6699 | + if method not in ( |
| 6700 | + "integral", |
| 6701 | + "sum_of_weights", |
| 6702 | + "sum_of_weights2", |
| 6703 | + ): |
| 6704 | + raise ValueError( |
| 6705 | + f"Can't set measure=True for {method!r} " |
| 6706 | + "collapses" |
| 6707 | + ) |
6678 | 6708 | elif method == "integral": |
6679 | 6709 | raise ValueError( |
6680 | 6710 | f"Must set measure=True for {method!r} collapses" |
@@ -6770,8 +6800,14 @@ def collapse( |
6770 | 6800 | d_kwargs = {} |
6771 | 6801 | if weights is not None: |
6772 | 6802 | if measure: |
6773 | | - # Never scale weights that are cell measures |
6774 | | - scale = None |
| 6803 | + if method not in ( |
| 6804 | + "integral", |
| 6805 | + "sum_of_weights", |
| 6806 | + "sum_of_weights2", |
| 6807 | + ): |
| 6808 | + raise ValueError( |
| 6809 | + f"Can't set measure=True for {method!r} collapses" |
| 6810 | + ) |
6775 | 6811 | elif method == "integral": |
6776 | 6812 | raise ValueError( |
6777 | 6813 | f"Must set measure=True for {method!r} collapses" |
|
0 commit comments