Skip to content

Commit c8bd171

Browse files
committed
dev
2 parents da72ae5 + f8c9573 commit c8bd171

5 files changed

Lines changed: 84 additions & 45 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
* New method: `cf.Field.create_latlon_coordinates`
1316
(https://github.com/NCAS-CMS/cf-python/issues/???)
1417
* New HEALPix methods: `cf.Field.healpix_info`,

cf/field.py

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,7 +3639,12 @@ def weights(
36393639
weights_axes.discard(xaxis)
36403640
weights_axes.discard(yaxis)
36413641
if not Weights.cell_measure(
3642-
self, "area", comp, weights_axes, methods=methods
3642+
self,
3643+
"area",
3644+
comp,
3645+
weights_axes,
3646+
methods=methods,
3647+
auto=True,
36433648
):
36443649
Weights.area_XY(
36453650
self,
@@ -6080,53 +6085,60 @@ def collapse(
60806085

60816086
weights: optional
60826087
Specify the weights for the collapse axes. The weights
6083-
are, in general, those that would be returned by this
6084-
call of the field construct's `weights` method:
6085-
``f.weights(weights, axes=axes, measure=measure,
6086-
scale=scale, radius=radius, great_circle=great_circle,
6087-
components=True)``. See the *axes*, *measure*,
6088-
*scale*, *radius* and *great_circle* parameters and
6089-
`cf.Field.weights` for details, and note that the
6090-
value of *scale* may be modified depending on the
6091-
value of *measure*.
6088+
are created internally as the output of this call of
6089+
the field construct's `weights` method:
6090+
``f.weights(weights, components=True, axes=axes,
6091+
measure=measure, scale=scale, radius=radius,
6092+
great_circle=great_circle)``.
60926093

6093-
.. note:: By default *weights* is `None`, resulting in
6094-
**unweighted calculations**.
6094+
See the `cf.Field.weights` and the *axes*, *measure*,
6095+
*scale*, *radius*, and *great_circle* parameters for
6096+
details; and note that the value of *scale* may be
6097+
modified depending on the value of *measure*.
60956098

6096-
.. note:: Unless the *method* is ``'integral'``, the
6097-
units of the weights are not combined with
6098-
the field's units in the collapsed field.
6099-
6100-
If the alternative form of providing the collapse method
6101-
and axes combined as a CF cell methods-like string via the
6102-
*method* parameter has been used, then the *axes*
6103-
parameter is ignored and the axes are derived from the
6104-
*method* parameter. For example, if *method* is ``'T:
6105-
area: minimum'`` then this defines axes of ``['T',
6106-
'area']``. If *method* specifies multiple collapses,
6107-
e.g. ``'T: minimum area: mean'`` then this implies axes of
6108-
``'T'`` for the first collapse, and axes of ``'area'`` for
6109-
the second collapse.
6099+
.. warning:: By default *weights* is `None`, resulting
6100+
in **unweighted calculations**.
61106101

61116102
.. note:: Setting *weights* to `True` is generally a
61126103
good way to ensure that all collapses are
61136104
appropriately weighted according to the
61146105
field construct's metadata. In this case, if
61156106
it is not possible to create weights for any
6116-
of the collapsed axes then an exception will
6117-
be raised.
6107+
axis then an exception will be raised.
61186108

6119-
However, care needs to be taken if *weights* is
6120-
`True` when cell volume weights are desired. The
6121-
volume weights will be taken from a "volume"
6122-
cell measure construct if one exists, otherwise
6123-
the cell volumes will be calculated as being
6124-
proportional to the sizes of one-dimensional
6125-
vertical coordinate cells. In the latter case
6126-
**if the vertical dimension coordinates do not
6127-
define the actual height or depth thickness of
6128-
every cell in the domain then the weights will
6129-
be incorrect**.
6109+
.. note:: Unless the *method* is ``'integral'``, the
6110+
units of the weights are not combined with
6111+
the field's units in the collapsed field.
6112+
6113+
.. note:: A pre-calculated weights array or dictionary
6114+
may also be provided as the *weights*
6115+
parameter. See `cf.Field.weights` for
6116+
details
6117+
6118+
If the collapse method and axes have been provided as
6119+
a CF cell methods-like string via the *method*
6120+
parameter, then the *axes* parameter is ignored and
6121+
the axes for weights instead inferred from that
6122+
string. For instance, if *method* is ``'T: area:
6123+
minimum'`` then this defines axes of ``['T',
6124+
'area']``. If *method* specifies multiple collapses,
6125+
e.g. ``'T: minimum area: mean'`` then this implies
6126+
axes of ``'T'`` for the first collapse, and axes of
6127+
``'area'`` for the second collapse.
6128+
6129+
.. warning:: Care needs to be taken if *weights* is
6130+
set to `True` when cell volume weights
6131+
are desired. The volume weights will be
6132+
taken from a "volume" cell measure
6133+
construct if one exists, otherwise the
6134+
cell volumes will be calculated as being
6135+
proportional to the sizes of
6136+
one-dimensional vertical coordinate
6137+
cells. In the latter case **if the
6138+
vertical dimension coordinates do not
6139+
define the actual height or depth
6140+
thickness of every cell in the domain
6141+
then the weights will be incorrect**.
61306142

61316143
*Parameter example:*
61326144
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
@@ -1504,15 +1504,16 @@ def cell_measure(
15041504
return False
15051505

15061506
raise ValueError(
1507-
f"Can't find weights: No {measure!r} cell measure"
1507+
f"Can't find weights for {f!r}: No {measure!r} cell measure"
15081508
)
15091509

15101510
elif len_m > 1:
15111511
if auto:
15121512
return False
15131513

15141514
raise ValueError(
1515-
f"Can't find weights: Multiple {measure!r} cell measures"
1515+
f"Can't find weights for {f!r}: Multiple {measure!r} cell "
1516+
"measures"
15161517
)
15171518

15181519
key, clm = m.popitem()
@@ -1522,9 +1523,11 @@ def cell_measure(
15221523
return False
15231524

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

15301533
clm_axes0 = f.get_data_axes(key)
@@ -1539,7 +1542,8 @@ def cell_measure(
15391542
return False
15401543

15411544
raise ValueError(
1542-
"Multiple weights specifications for "
1545+
f"Can't find weights for {f!r}: Multiple weights "
1546+
"specifications for "
15431547
f"{f.constructs.domain_axis_identity(axis)!r} axis"
15441548
)
15451549

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)