Skip to content

Commit 31f131e

Browse files
committed
dev
2 parents 8249823 + f8c9573 commit 31f131e

5 files changed

Lines changed: 86 additions & 46 deletions

File tree

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 a ``ValueError`` to be raised
13+
for missing external cell measures data
14+
(https://github.com/NCAS-CMS/cf-python/issues/885)
1215
* Allow regridding for very large grids. New keyword parameter to
1316
`cf.Field.regrids` and `cf.Field.regridc`: ``dst_grid_partitions``
1417
(https://github.com/NCAS-CMS/cf-python/issues/878)

cf/field.py

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,7 +3689,12 @@ def weights(
36893689
weights_axes.discard(xaxis)
36903690
weights_axes.discard(yaxis)
36913691
if not Weights.cell_measure(
3692-
self, "area", comp, weights_axes, methods=methods
3692+
self,
3693+
"area",
3694+
comp,
3695+
weights_axes,
3696+
methods=methods,
3697+
auto=True,
36933698
):
36943699
Weights.area_XY(
36953700
self,
@@ -5453,52 +5458,60 @@ def collapse(
54535458

54545459
weights: optional
54555460
Specify the weights for the collapse axes. The weights
5456-
are, in general, those that would be returned by this
5457-
call of the field construct's `weights` method:
5458-
``f.weights(weights, axes=axes, measure=measure,
5459-
scale=scale, radius=radius, great_circle=great_circle,
5460-
components=True)``. See the *axes*, *measure*,
5461-
*scale*, *radius* and *great_circle* parameters and
5462-
`cf.Field.weights` for details, and note that the
5463-
value of *scale* may be modified depending on the
5464-
value of *measure*.
5461+
are created internally as the output of this call of
5462+
the field construct's `weights` method:
5463+
``f.weights(weights, components=True, axes=axes,
5464+
measure=measure, scale=scale, radius=radius,
5465+
great_circle=great_circle)``.
54655466

5466-
.. note:: By default *weights* is `None`, resulting in
5467-
**unweighted calculations**.
5467+
See the `cf.Field.weights` and the *axes*, *measure*,
5468+
*scale*, *radius*, and *great_circle* parameters for
5469+
details; and note that the value of *scale* may be
5470+
modified depending on the value of *measure*.
5471+
5472+
.. warning:: By default *weights* is `None`, resulting
5473+
in **unweighted calculations**.
5474+
5475+
.. note:: Setting *weights* to `True` is generally a
5476+
good way to ensure that all collapses are
5477+
appropriately weighted according to the
5478+
field construct's metadata. In this case, if
5479+
it is not possible to create weights for any
5480+
axis then an exception will be raised.
54685481

54695482
.. note:: Unless the *method* is ``'integral'``, the
54705483
units of the weights are not combined with
54715484
the field's units in the collapsed field.
54725485

5473-
If the alternative form of providing the collapse method
5474-
and axes combined as a CF cell methods-like string via the
5475-
*method* parameter has been used, then the *axes*
5476-
parameter is ignored and the axes are derived from the
5477-
*method* parameter. For example, if *method* is ``'T:
5478-
area: minimum'`` then this defines axes of ``['T',
5486+
.. note:: A pre-calculated weights array or dictionary
5487+
may also be provided as the *weights*
5488+
parameter. See `cf.Field.weights` for
5489+
details
5490+
5491+
If the collapse method and axes have been provided as
5492+
a CF cell methods-like string via the *method*
5493+
parameter, then the *axes* parameter is ignored and
5494+
the axes for weights instead inferred from that
5495+
string. For instance, if *method* is ``'T: area:
5496+
minimum'`` then this defines axes of ``['T',
54795497
'area']``. If *method* specifies multiple collapses,
5480-
e.g. ``'T: minimum area: mean'`` then this implies axes of
5481-
``'T'`` for the first collapse, and axes of ``'area'`` for
5482-
the second collapse.
5483-
5484-
.. note:: Setting *weights* to `True` is generally a good
5485-
way to ensure that all collapses are
5486-
appropriately weighted according to the field
5487-
construct's metadata. In this case, if it is not
5488-
possible to create weights for any axis then an
5489-
exception will be raised.
5490-
5491-
However, care needs to be taken if *weights* is
5492-
`True` when cell volume weights are desired. The
5493-
volume weights will be taken from a "volume"
5494-
cell measure construct if one exists, otherwise
5495-
the cell volumes will be calculated as being
5496-
proportional to the sizes of one-dimensional
5497-
vertical coordinate cells. In the latter case
5498-
**if the vertical dimension coordinates do not
5499-
define the actual height or depth thickness of
5500-
every cell in the domain then the weights will
5501-
be incorrect**.
5498+
e.g. ``'T: minimum area: mean'`` then this implies
5499+
axes of ``'T'`` for the first collapse, and axes of
5500+
``'area'`` for the second collapse.
5501+
5502+
.. warning:: Care needs to be taken if *weights* is
5503+
set to `True` when cell volume weights
5504+
are desired. The volume weights will be
5505+
taken from a "volume" cell measure
5506+
construct if one exists, otherwise the
5507+
cell volumes will be calculated as being
5508+
proportional to the sizes of
5509+
one-dimensional vertical coordinate
5510+
cells. In the latter case **if the
5511+
vertical dimension coordinates do not
5512+
define the actual height or depth
5513+
thickness of every cell in the domain
5514+
then the weights will be incorrect**.
55025515

55035516
*Parameter example:*
55045517
To specify weights based on the field construct's

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

docs/source/contributing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ ideas, code, and documentation to the cf library:
124124
* Mark Rhodes-Smith
125125
* Matt Brown
126126
* Michael Decker
127+
* Oliver Kotla
127128
* Sadie Bartholomew
128129
* Thibault Hallouin
129130
* Tim Bradshaw

docs/source/installation.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ Note that :ref:`some environment variables might also need setting
117117
<UNIDATA-UDUNITS-2-library>` in order for the Udunits library to work
118118
properly, although the defaults are usually sufficient.
119119

120+
.. _Jupyter Notebooks:
121+
122+
Jupyter Notebooks
123+
^^^^^^^^^^^^^^^^^
124+
125+
You may want to work with cf via a Jupyter Notebook within a ``conda`` environment.
126+
127+
A `known issue <https://github.com/NCAS-CMS/cf-python/issues/883/>`_ exists for Python 3.12 only when one wants to install the optional dependency ESMPy. Namely, the stricter dependencies of the Jupyter library clash with those of cf (namely ``zlib`` required by ESMPy) and installation fails.
128+
129+
A proven workaround is to install Jupyter *before* installing cf, like so:
130+
131+
.. code-block:: console
132+
:caption: *Install with conda alongside Jupyter.*
133+
134+
$ conda install jupyter
135+
$ conda install -c conda-forge cf-python cf-plot udunits2
136+
$ conda install -c conda-forge "esmpy>=8.7.0"
137+
138+
120139
----
121140

122141
.. _Source:

0 commit comments

Comments
 (0)