Skip to content

[Bug]: spatial average error following temporal resample #583

@arfriedman

Description

@arfriedman

What happened?

I am encountering an error when taking an xcdat spatial average following xarray temporal resampling.

I am working with monthly model precipitation output named pr_mon:

In [7]: pr_mon
Out[7]:
<xarray.Dataset>
Dimensions:   (lon: 192, lat: 96, time: 7056, bnds: 2)
Coordinates:
  * lon       (lon) float64 -180.0 -178.1 -176.2 -174.4 ... 174.4 176.2 178.1
  * lat       (lat) float64 88.57 86.72 84.86 83.0 ... -84.86 -86.72 -88.57
  * time      (time) object 1421-01-31 00:00:00 ... 2008-12-31 00:00:00
Dimensions without coordinates: bnds
Data variables:
    lon_bnds  (lon, bnds) float64 -180.9 -179.1 -179.1 ... 177.2 177.2 179.1
    lat_bnds  (lat, bnds) float64 89.5 87.65 87.65 85.79 ... -87.65 -87.65 -89.5
    precip    (time, lat, lon) float64 13.27 13.14 13.05 ... 3.607 3.466 3.355

Taking a spatial average of this monthly variable works fine:
In [8]: pr_mon_avg = pr_mon.spatial.average("precip")

However, after I temporally resample the data using xarray, xcdat spatial averaging returns an error. Here is an example for a three-month temporal sum:


In [10]: pr_3M = pr_mon.resample(time="3ME").sum()
In [11]: pr_3M
Out[11]:
<xarray.Dataset>
Dimensions:   (lon: 192, lat: 96, time: 2353, bnds: 2)
Coordinates:
  * lon       (lon) float64 -180.0 -178.1 -176.2 -174.4 ... 174.4 176.2 178.1
  * lat       (lat) float64 88.57 86.72 84.86 83.0 ... -84.86 -86.72 -88.57
  * time      (time) object 1421-01-31 00:00:00 ... 2009-01-31 00:00:00

In [12]: pr_3M_avg = pr_3M.spatial.average("precip")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[12], line 1
----> 1 pr_3M_avg = pr_3M.spatial.average("precip")

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

File ~/miniconda3/envs/Nile-Laki/lib/python3.11/site-packages/xcdat/spatial.py:687, in SpatialAccessor._validate_weights(self, data_var, axis)
    685     dim_name = get_dim_keys(data_var, key)
    686     if dim_name not in self._weights.dims:
--> 687         raise KeyError(
    688             f"The weights DataArray does not include an {key} axis, or the "
    689             "dimension names are not the same."
    690         )
    692 # Check the weight dim sizes equal data var dim sizes.
    693 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.'

Do you know what might account for this error?

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

No response

Minimal Complete Verifiable Example (MVCE)

No response

Relevant log output

No response

Anything else we need to know?

No response

Environment

xCDAT version 0.6.1

In [13]: xr.show_versions()
/home/andrew/miniconda3/envs/Nile-Laki/lib/python3.11/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit: None
python: 3.11.7 | packaged by conda-forge | (main, Dec 23 2023, 14:43:09) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.15.0-86-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2023.12.0
pandas: 2.1.4
numpy: 1.26.2
scipy: 1.11.4
netCDF4: 1.6.5
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: None
dask: 2023.12.1
distributed: 2023.12.1
matplotlib: 3.8.2
cartopy: 0.22.0
seaborn: None
numbagg: None
fsspec: 2023.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions