问题描述
我在 x/y 网格上有一个海面温度值的 xarray 数据集。 x
和 y
是一维向量坐标,所以它看起来像这个最小的例子:
<xarray.Dataset>
Dimensions: (x: 10,y: 10)
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8 9
* y (y) int64 0 1 2 3 4 5 6 7 8 9
Data variables:
data (x,y) float64 0.559 0.01037 0.1562 ... 0.08778 0.3272 0.8661
我能够从这个 x/y 网格计算纬度/经度,输出是 2 个二维数组。我可以将它们添加为坐标 ds.assign_coords
:
<xarray.Dataset>
Dimensions: (x: 10,y: 10)
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8 9
* y (y) int64 0 1 2 3 4 5 6 7 8 9
lat (x,y) float64 30.0 30.0 30.0 30.0 30.0 ... 39.0 39.0 39.0 39.0
lon (x,y) float64 -120.0 -119.0 -118.0 -117.0 ... -113.0 -112.0 -111.0
Data variables:
data (x,y) float64 0.559 0.01037 0.1562 ... 0.08778 0.3272 0.8661
但我想 .sel
沿着纬度/经度的切片。这目前是不可能的,因为我收到错误:
ds.sel(lat=slice(32,36),lon=slice(-118,-115))
ValueError Traceback (most recent call last)
<ipython-input-20-28c79202d5f3> in <module>
----> 1 ds.sel(lat=slice(32,-115))
~/.local/lib/python3.8/site-packages/xarray/core/dataset.py in sel(self,indexers,method,tolerance,drop,**indexers_kwargs)
2363 """
2364 indexers = either_dict_or_kwargs(indexers,indexers_kwargs,"sel")
-> 2365 pos_indexers,new_indexes = remap_label_indexers(
2366 self,indexers=indexers,method=method,tolerance=tolerance
2367 )
~/.local/lib/python3.8/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj,**indexers_kwargs)
419 }
420
--> 421 pos_indexers,new_indexes = indexing.remap_label_indexers(
422 obj,v_indexers,tolerance=tolerance
423 )
~/.local/lib/python3.8/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj,tolerance)
256 new_indexes = {}
257
--> 258 dim_indexers = get_dim_indexers(data_obj,indexers)
259 for dim,label in dim_indexers.items():
260 try:
~/.local/lib/python3.8/site-packages/xarray/core/indexing.py in get_dim_indexers(data_obj,indexers)
222 ]
223 if invalid:
--> 224 raise ValueError(f"dimensions or multi-index levels {invalid!r} do not exist")
225
226 level_indexers = defaultdict(dict)
ValueError: dimensions or multi-index levels ['lat','lon'] do not exist
所以我的问题是:如何将 data
的维度更改为 (lon: (10,10),lat: (10,10))
而不是 (x: 10,y: 10
?这甚至可能吗?
重现示例数据集的代码:
import numpy as np
import xarray as xr
# Create sample data
data = np.random.rand(10,10)
x = y = np.arange(10)
# Set up dataset
ds = xr.Dataset(
data_vars = dict(
data = (["x","y"],data)
),coords= {
"x" : x,"y" : y
}
)
# Create example lat/lon and assign to dataset
lon,lat = np.meshgrid(np.linspace(-120,-111,np.linspace(30,39,10))
ds = ds.assign_coords({
"lat": (["x",lat),"lon": (["x",lon)
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)