python - xarray 将新的二维坐标设置为维度
问题描述
我在 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,36), lon=slice(-118, -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, method, tolerance, **indexers_kwargs)
419 }
420
--> 421 pos_indexers, new_indexes = indexing.remap_label_indexers(
422 obj, v_indexers, method=method, tolerance=tolerance
423 )
~/.local/lib/python3.8/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, 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
所以我的问题是:如何更改 to 的尺寸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, 10), np.linspace(30, 39, 10))
ds = ds.assign_coords({
"lat": (["x", "y"], lat),
"lon": (["x", "y"], lon)
})
解决方案
推荐阅读
- javascript - 如何仅从 JSON 文件导入不和谐?
- reactjs - 每当我导入固定字符串而不是文件时,Webpack 是否可以运行我的自定义加载器?
- swift - 点击屏幕时如何让我的图像节点跳转?
- azure-devops - 如何列出过去移动到不同 sprint 的工作项?
- python - 如何将嵌套字典中的所有键插入新的嵌套字典?
- r - 替换R中数据框中的条目
- javascript - 使用 Typescript 时,Slick carousel 被识别为一个类
- go - 如何使用从 openapi-generator 生成的 SDK 管理 repo 的结构?
- python - 如何将输入 *.tif 文件堆叠到新的 output.tif 光栅文件
- android - 在 Twilio Chat SDK 的私人频道中添加成员时应用程序崩溃