If I try to compute a volume mean using cf.Field.collapse() where the cell measure is time dependant (sometimes true for ocean fields), cf.Field.collapse() throws an error.
mean=data.collapse('volume: mean',weights='volume')
Traceback (most recent call last):
File "/gws/ssde/j25b/terrafirma/monitoring/references/cf_bug/./cf_collapse_test.py", line 7, in <module>
mean=data.collapse('volume: mean',weights='volume')
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cf/decorators.py", line 71, in precede_with_kwarg_deprecation_check
operation_method_result = operation_method(self, *args, **kwargs)
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cfdm/decorators.py", line 171, in verbose_override_wrapper
return method_with_verbose_kwarg(*args, **kwargs)
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cf/field.py", line 7470, in collapse
d_weights = f.weights(
weights,
...<5 lines>...
great_circle=great_circle,
)
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cf/field.py", line 3488, in weights
Weights.cell_measure(
~~~~~~~~~~~~~~~~~~~~^
self,
^^^^^
...<4 lines>...
auto=False,
^^^^^^^^^^^
)
^
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cf/weights.py", line 1553, in cell_measure
clm.squeeze(iaxes, inplace=True)
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cfdm/decorators.py", line 44, in inplace_wrapper
processed_copy = operation_method(self, *args, **kwargs)
File "/home/users/dlrhodso/miniforge3/envs/cf20/lib/python3.14/site-packages/cfdm/data/data.py", line 7178, in squeeze
raise ValueError(
...<2 lines>...
)
ValueError: Can't squeeze axis in position 1 from data with shape (1, 75, 332, 362): Axis size is greater than 1
cf.environment(paths=False)
Platform: Linux-5.14.0-611.54.1.el9_7.x86_64-x86_64-with-glibc2.34
Python: 3.14.4
packaging: 26.2
numpy: 2.4.3
cfdm.core: 1.13.1.0
udunits2 library: /home/users/dlrhodso/miniforge3/envs/cf20/lib/libudunits2.so.0
HDF5 library: 2.1.0
netcdf library: 4.10.0
netCDF4: 1.7.4
h5netcdf: 1.8.1
h5py: 3.16.0
pyfive: 1.1.2
zarr: 3.2.1
fsspec: 2026.4.0
scipy: 1.17.1
dask: 2026.3.0
distributed: 2026.3.0
cftime: 1.6.5
cfunits: 3.3.7
cfdm: 1.13.1.0
esmpy/ESMF: 8.9.1
psutil: 7.2.2
matplotlib: 3.10.9
activestorage: not available
cartopy: 0.25.0
cfplot: not available
cf: 3.20.0
Attached is a zip file with a demonstration of the bug.
Hi All,
If I try to compute a volume mean using cf.Field.collapse() where the cell measure is time dependant (sometimes true for ocean fields), cf.Field.collapse() throws an error.
This bug occurs due to a line in the
Weights.cell_meaure()function. The line:iaxes = [clm_axes0.index(axis) for axis in clm_axes]is intended to select redundant axes to be squeezed, but instead selects the non-redundant axes. This part of the code is only ever reached if the cell_measure has at least one axis of size 1. This occurs if cell_measure is time dependant and we are trying to collapse a field with a single time point.
What this line should do is to select all the axes in clm_axes0 that are not in clm_axes. This line:
iaxes = [clm_axes0.index(axis) for axis in (set(clm_axes0)-set(clm_axes))]does this. Replacing with this line fixes the bug.
Attached is a zip file with a demonstration of the bug.
cf_bug.zip