Skip to content

[Bug]: spatial.average() error #797

@mzelinka

Description

@mzelinka

What happened?

I am trying to compute a global average and getting an error I do not understand.

What did you expect to happen? Are there are possible answers you came across?

I expected this to compute a global average.

Minimal Complete Verifiable Example (MVCE)

ds = xc.open_dataset('/global/homes/z/zelinka1/scripts/tmp.nc')
avg = ds.spatial.average('ALL_SWcld_tot')

Relevant log output

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[21], line 2
      1 ds = xc.open_dataset('/global/homes/z/zelinka1/scripts/tmp.nc')
----> 2 avg = ds.spatial.average('ALL_SWcld_tot')

File ~/miniconda3/envs/xclim3/lib/python3.13/site-packages/xcdat/spatial.py:198, in SpatialAccessor.average(self, data_var, axis, weights, keep_weights, lat_bounds, lon_bounds)
    195 elif isinstance(weights, xr.DataArray):
    196     self._weights = weights
--> 198 self._validate_weights(dv, axis)
    199 ds[dv.name] = self._averager(dv, axis)
    201 if keep_weights:

File ~/miniconda3/envs/xclim3/lib/python3.13/site-packages/xcdat/spatial.py:690, in SpatialAccessor._validate_weights(self, data_var, axis)
    688     dim_name = get_dim_keys(data_var, key)
    689     if dim_name not in self._weights.dims:
--> 690         raise KeyError(
    691             f"The weights DataArray does not include an {key} axis, or the "
    692             "dimension names are not the same."
    693         )
    695 # Check the weight dim sizes equal data var dim sizes.
    696 dim_sizes = {key: data_var.sizes[key] for key in self._weights.sizes.keys()}

KeyError: 'The weights DataArray does not include an X axis, or the dimension names are not the same.'

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:10:22) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 5.14.21-150500.55.97_13.0.78-cray_shasta_c
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.4
libnetcdf: 4.9.2

xarray: 2025.3.0
pandas: 2.2.3
numpy: 2.1.3
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.4
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.4.2
dask: 2025.3.0
distributed: 2025.3.0
matplotlib: 3.10.1
cartopy: 0.24.1
seaborn: None
numbagg: None
fsspec: 2025.3.2
cupy: None
pint: 0.24.4
sparse: 0.15.4
flox: None
numpy_groupies: None
setuptools: 78.1.1
pip: 25.1
conda: None
pytest: None
mypy: None
IPython: 9.0.2
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugInconsistencies or issues which will cause an issue or problem for users or implementors.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions