xarray.Dataset.weighted(...) 的问题

Problem with xarray.Dataset.weighted(...)

我目前正在处理一些大气数据,我需要能够在每个时间步长(每天)取面积加权平均值,从而生成按日期索引的一维时间序列。我使用的数据集有 1 到 4 个变量。

在这种情况下,我的数据集只有一个,'sf' 表示降雪。坐标是经度、纬度、时间。这是我打印时的数据集:

<xarray.Dataset>
Dimensions:    (latitude: 121, longitude: 241, time: 14245)
Coordinates:  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... 60.5 60.25 60.0
  * longitude  (longitude) float32 70.0 70.25 70.5 70.75 ... 129.5 29.75 130.0
  * time       (time) datetime64[ns] 1980-01-01 1980-01-02 ... 2018-12-31
Data variables:
    sf         (time, latitude, longitude) float32 dask.array<chunksize=(366, 121, 241), meta=np.ndarray>
Attributes:
    history:  Thu May 21 16:12:13 2020: ncks -d latitude,60.,90. -d longitude...
    NCO:      netCDF Operators version 4.7.5 (Homepage = http://nco.sf.net, C...

使用 xarray 文档和此示例 http://xarray.pydata.org/en/stable/examples/area_weighted_temperature.html,我基本上重写了 "creating weights" 和 "weighted mean" 中的代码,并对我的数据进行了相关替换。

snow_data = xr.open_mfdataset(SOURCE_DIR+'*'+REGION_CODE+'*.nc', combine='by_coords')
weights = np.cos(np.deg2rad(snow_data.latitude))
weights.name = 'weights'

snow_data_weighted = snow_data.weighted(weights)
snow_data_mean = snow_data_weighted.mean(('longitude','latitude')) 

但是,我收到如下错误:

AttributeError: 'Dataset' object has no attribute 'weighted'

考虑到这应该从示例中顺利进行,我不知道可能出了什么问题,但也许我不知道它是如何工作的。我认为 xarray.Dataset.weighted() 的添加相对较新,但从那时起我已经更新了 xarray。不确定我可能需要提供哪些其他信息可以提供帮助。

欢迎提出任何建议 - 这是我的第一个 post,所以如果我做错了什么,我很乐意改正!

在现在 运行 的新环境中列出软件包会给出以下内容。其中一个以前肯定丢失了。

# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
_libgcc_mutex=0.1=main
blas=1.0=mkl
bokeh=2.0.2=py37_0
bzip2=1.0.8=h7b6447c_0
ca-certificates=2020.1.1=0
cartopy=0.17.0=py37hbb7e04d_1
certifi=2020.4.5.1=py37_0
cffi=1.14.0=py37he30daa8_1
cftime=1.1.2=py37heb32a55_0
chardet=3.0.4=py37_1003
click=7.1.2=py_0
cloudpickle=1.4.1=py_0
cryptography=2.9.2=py37h1ba5d50_0
curl=7.69.1=hbc83047_0
cycler=0.10.0=py37_0
cytoolz=0.10.1=py37h7b6447c_0
dask=2.16.0=py_0
dask-core=2.16.0=py_0
dbus=1.13.14=hb2f20db_0
distributed=2.16.0=py37_0
expat=2.2.6=he6710b0_0
fontconfig=2.13.0=h9420a91_0
freetype=2.9.1=h8a8886c_1
fsspec=0.7.1=py_0
geos=3.7.1=he6710b0_0
glib=2.63.1=h3eb4bd4_1
gst-plugins-base=1.14.0=hbbd80ab_1
gstreamer=1.14.0=hb31296c_0
hdf4=4.2.13=h3ca952b_2
hdf5=1.10.4=hb1b8bf9_0
heapdict=1.0.1=py_0
icu=58.2=he6710b0_3
idna=2.9=py_1
intel-openmp=2020.1=217
jinja2=2.11.2=py_0
jpeg=9b=h024ee3a_2
kiwisolver=1.2.0=py37hfd86e86_0
krb5=1.17.1=h173b8e3_0
ld_impl_linux-64=2.33.1=h53a641e_7
libcurl=7.69.1=h20c2e04_0
libedit=3.1.20181209=hc058e9b_0
libffi=3.3=he6710b0_1
libgcc-ng=9.1.0=hdf63c60_0
libgfortran-ng=7.3.0=hdf63c60_0
libnetcdf=4.7.3=hb80b6cc_0
libpng=1.6.37=hbc83047_0
libssh2=1.9.0=h1ba5d50_1
libstdcxx-ng=9.1.0=hdf63c60_0
libtiff=4.1.0=h2733197_0
libuuid=1.0.3=h1bed415_2
libxcb=1.13=h1bed415_1
libxml2=2.9.9=hea5a465_1
libxslt=1.1.33=h7d1a2b0_0
locket=0.2.0=py37_1
lxml=4.5.0=py37hefd8a0e_0
markupsafe=1.1.1=py37h7b6447c_0
matplotlib=3.1.3=py37_0
matplotlib-base=3.1.3=py37hef1b27d_0
mkl=2020.1=217
mkl-service=2.3.0=py37he904b0f_0
mkl_fft=1.0.15=py37ha843d7b_0
mkl_random=1.1.0=py37hd6b4f25_0
msgpack-python=1.0.0=py37hfd86e86_1
ncurses=6.2=he6710b0_1
netcdf4=1.5.3=py37hbf33ddf_0
numpy=1.18.1=py37h4f9e942_0
numpy-base=1.18.1=py37hde5b4d6_1
olefile=0.46=py37_0
openssl=1.1.1g=h7b6447c_0
owslib=0.18.0=py_0
packaging=20.3=py_0
pandas=1.0.3=py37h0573a6f_0
partd=1.1.0=py_0
pcre=8.43=he6710b0_0
pillow=7.1.2=py37hb39fc2d_0
pip=20.0.2=py37_3
proj4=5.2.0=he6710b0_1
psutil=5.7.0=py37h7b6447c_0
pycparser=2.20=py_0
pyepsg=0.4.0=py37_0
pykdtree=1.3.1=py37hdd07704_2
pyopenssl=19.1.0=py37_0
pyparsing=2.4.7=py_0
pyproj=1.9.6=py37h14380d9_0
pyqt=5.9.2=py37h05f1152_2
pyshp=2.1.0=py_0
pysocks=1.7.1=py37_0
python=3.7.7=hcff3b4d_5
python-dateutil=2.8.1=py_0
pytz=2020.1=py_0
pyyaml=5.3.1=py37h7b6447c_0
qt=5.9.7=h5867ecd_1
readline=8.0=h7b6447c_0
requests=2.23.0=py37_0
scipy=1.4.1=py37h0b6359f_0
setuptools=46.4.0=py37_0
shapely=1.6.4=py37h86c5351_0
sip=4.19.8=py37hf484d3e_0
six=1.14.0=py37_0
sortedcontainers=2.1.0=py37_0
sqlite=3.31.1=h62c20be_1
tblib=1.6.0=py_0
tk=8.6.8=hbc83047_0
toolz=0.10.0=py_0
tornado=6.0.4=py37h7b6447c_1
typing_extensions=3.7.4.1=py37_0
urllib3=1.25.8=py37_0
wheel=0.34.2=py37_0
xarray=0.15.1=py_0
xz=5.2.5=h7b6447c_0
yaml=0.1.7=had09818_2
zict=2.0.0=py_0
zlib=1.2.11=h7b6447c_3
zstd=1.3.7=h0b5b093_0

我遇到了和你一样的问题。我的 xarray 版本是 0.14.0。更新后,现在是0.16.1,加权法可以用了。但是,我认为它应该适合你,因为你的 xarray 版本是 0.15.1 (http://xarray.pydata.org/en/stable/whats-new.html#v0-15-1-23-mar-2020)。

因此,尝试使用 conda update xarray.

更新 xarray