|
162 | 162 | "average", |
163 | 163 | "sd", |
164 | 164 | "standard_deviation", |
| 165 | + "sum", |
165 | 166 | "var", |
166 | 167 | "variance", |
167 | | - # 'sum_of_weights', |
168 | | - # 'sum_of_weights2', |
| 168 | + "sum_of_weights", |
| 169 | + "sum_of_weights2", |
169 | 170 | "integral", |
170 | 171 | "root_mean_square", |
171 | 172 | ) |
@@ -5002,6 +5003,9 @@ def weights( |
5002 | 5003 | comp[key] = self._weights_scale(w, scale) |
5003 | 5004 |
|
5004 | 5005 | for w in comp.values(): |
| 5006 | + if not measure: |
| 5007 | + w.override_units("1", inplace=True) |
| 5008 | + |
5005 | 5009 | mn = w.minimum() |
5006 | 5010 | if mn <= 0: |
5007 | 5011 | raise ValueError( |
@@ -6194,7 +6198,7 @@ def collapse( |
6194 | 6198 | group_span=None, |
6195 | 6199 | group_contiguous=1, |
6196 | 6200 | measure=False, |
6197 | | - scale=1, |
| 6201 | + scale=None, |
6198 | 6202 | radius="earth", |
6199 | 6203 | great_circle=False, |
6200 | 6204 | verbose=None, |
@@ -6719,6 +6723,10 @@ def collapse( |
6719 | 6723 | .. note:: By default *weights* is `None`, resulting in |
6720 | 6724 | **unweighted calculations**. |
6721 | 6725 |
|
| 6726 | + .. note:: Unless the *method* is ``'integral'``, the |
| 6727 | + units of the weights are not combined with |
| 6728 | + the field's units in the collapsed field. |
| 6729 | + |
6722 | 6730 | If the alternative form of providing the collapse method |
6723 | 6731 | and axes combined as a CF cell methods-like string via the |
6724 | 6732 | *method* parameter has been used, then the *axes* |
@@ -6762,57 +6770,58 @@ def collapse( |
6762 | 6770 | time you could set ``weights=('area', 'T')``. |
6763 | 6771 |
|
6764 | 6772 | measure: `bool`, optional |
6765 | | - Create weights which are cell measures, i.e. which |
6766 | | - describe actual cell sizes (e.g. cell area) with |
6767 | | - appropriate units (e.g. metres squared). By default the |
6768 | | - weights are normalised and have arbitrary units. |
| 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. |
6769 | 6778 |
|
6770 | 6779 | Cell measures can be created for any combination of |
6771 | | - axes. For example, cell measures for a time axis are the |
6772 | | - time span for each cell with canonical units of seconds; |
6773 | | - cell measures for the combination of four axes |
6774 | | - representing time and three dimensional space could have |
6775 | | - 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. |
6776 | 6785 |
|
6777 | | - When collapsing with the ``'integral'`` method, *measure* |
6778 | | - must be True, and the units of the weights are |
6779 | | - 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 |
6780 | 6789 | construct. |
6781 | 6790 |
|
6782 | 6791 | .. note:: Specifying cell volume weights via |
6783 | 6792 | ``weights=['X', 'Y', 'Z']`` or |
6784 | | - ``weights=['area', 'Z']`` (or other equivalents) |
6785 | | - will produce **an incorrect result if the |
6786 | | - vertical dimension coordinates do not define the |
6787 | | - actual height or depth thickness of every cell |
6788 | | - in the domain**. In this case, |
6789 | | - ``weights='volume'`` should be used instead, |
6790 | | - which requires the field construct to have a |
6791 | | - "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. |
6792 | 6802 |
|
6793 | | - If ``weights=True`` then care also needs to be |
6794 | | - taken, as a "volume" cell measure construct will |
6795 | | - be used if present, otherwise the cell volumes |
6796 | | - will be calculated using the size of the |
6797 | | - 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. |
6798 | 6808 |
|
6799 | 6809 | .. versionadded:: 3.0.2 |
6800 | 6810 |
|
6801 | 6811 | scale: number or `None`, optional |
6802 | 6812 | If set to a positive number then scale the weights so |
6803 | 6813 | that they are less than or equal to that number. If |
6804 | | - set to `None` the weights are not scaled. In general |
6805 | | - the default is for weights to be scaled to lie between |
6806 | | - 0 and 1; however if *measure* is True then the weights |
6807 | | - are never scaled and the value of *scale* is taken as |
6808 | | - `None`, regardless of its setting. |
| 6814 | + set to `None`, the default, then the weights are not |
| 6815 | + scaled. |
6809 | 6816 |
|
6810 | 6817 | *Parameter example:* |
6811 | 6818 | To scale all weights so that they lie between 0 and |
6812 | | - 10 ``scale=10``. |
| 6819 | + 1 ``scale=1``. |
6813 | 6820 |
|
6814 | 6821 | .. versionadded:: 3.0.2 |
6815 | 6822 |
|
| 6823 | + .. versionchanged:: 3.16.0 Default changed to `None` |
| 6824 | + |
6816 | 6825 | radius: optional |
6817 | 6826 | Specify the radius used for calculating the areas of |
6818 | 6827 | cells defined in spherical polar coordinates. The |
@@ -7698,6 +7707,12 @@ def collapse( |
7698 | 7707 | # ------------------------------------------------------------ |
7699 | 7708 | # Parse the methods and axes |
7700 | 7709 | # ------------------------------------------------------------ |
| 7710 | + if measure and scale is not None: |
| 7711 | + raise ValueError( |
| 7712 | + "'scale' must be None when 'measure' is True. " |
| 7713 | + f"Got: scale={scale!r}" |
| 7714 | + ) |
| 7715 | + |
7701 | 7716 | if ":" in method: |
7702 | 7717 | # Convert a cell methods string (such as 'area: mean dim3: |
7703 | 7718 | # dim2: max T: minimum height: variance') to a CellMethod |
@@ -7916,8 +7931,15 @@ def collapse( |
7916 | 7931 | g_weights = None |
7917 | 7932 | else: |
7918 | 7933 | if measure: |
7919 | | - # Never scale weights that are cell measures |
7920 | | - scale = None |
| 7934 | + if method not in ( |
| 7935 | + "integral", |
| 7936 | + "sum_of_weights", |
| 7937 | + "sum_of_weights2", |
| 7938 | + ): |
| 7939 | + raise ValueError( |
| 7940 | + f"Can't set measure=True for {method!r} " |
| 7941 | + "collapses" |
| 7942 | + ) |
7921 | 7943 | elif method == "integral": |
7922 | 7944 | raise ValueError( |
7923 | 7945 | f"Must set measure=True for {method!r} collapses" |
@@ -8006,8 +8028,14 @@ def collapse( |
8006 | 8028 | d_kwargs = {} |
8007 | 8029 | if weights is not None: |
8008 | 8030 | if measure: |
8009 | | - # Never scale weights that are cell measures |
8010 | | - scale = None |
| 8031 | + if method not in ( |
| 8032 | + "integral", |
| 8033 | + "sum_of_weights", |
| 8034 | + "sum_of_weights2", |
| 8035 | + ): |
| 8036 | + raise ValueError( |
| 8037 | + f"Can't set measure=True for {method!r} collapses" |
| 8038 | + ) |
8011 | 8039 | elif method == "integral": |
8012 | 8040 | raise ValueError( |
8013 | 8041 | f"Must set measure=True for {method!r} collapses" |
|
0 commit comments