将 xarray 数据变量重新分配给 xarray 坐标



我有一个空间数据的熊猫数据帧,我想将其转换为netCDF。我已经找到了使用 xarray 的方法,并将我的数据帧转换为 xarray 数据集:

# create xray Dataset from Pandas DataFrame
xr = xarray.Dataset.from_dataframe(df)

现在,我想将lonlat变量设置为我的 xarray 数据集的坐标。 我已经尝试了xarray.Dataset.assign_coords但似乎无法让它工作?

我的 xarray 数据集如下所示:

<xarray.Dataset>
Dimensions:  (index: 58705)
Coordinates:
* index    (index) int64 0 1 2 3 4 5 6 ... 58699 58700 58701 58702 58703 58704
Data variables:
x_km     (index) float64 5.274e+03 5.273e+03 ... 2.873e+03 2.873e+03
y_km     (index) float64 0.0 46.02 92.03 138.0 ... -75.23 -50.15 -25.07 -0.0
z_km     (index) float64 3.575e+03 3.575e+03 ... 1.947e+03 1.947e+03
dv_v     (index) float64 0.2407 0.1774 0.1786 ... -0.2163 -0.2035 -0.3197
rxy      (index) float64 5.274e+03 5.273e+03 ... 2.873e+03 2.873e+03
lon      (index) float64 0.0 0.5 1.0 1.5 2.0 ... -2.0 -1.5 -1.0 -0.5 -0.0
lat      (index) float64 34.13 34.13 34.13 34.13 ... 34.11 34.12 34.12 34.13
rxyz     (index) float64 6.371e+03 6.371e+03 ... 3.471e+03 3.471e+03
depth    (index) float64 0.04665 0.04747 0.04766 ... 2.9e+03 2.9e+03 2.9e+03
Attributes:
Conventions:  CF-1.6
title:        Data
summary:      Data generated

任何帮助都值得赞赏:D

从一个名为dsDataset开始

,如下所示:
Dimensions:  (index: 10)
Coordinates:
* index    (index) int64 0 1 2 3 4 5 6 7 8 9
Data variables:
dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
lon      (index) int64 15 7 9 17 18 1 12 2 6 8
lat      (index) int64 6 8 5 17 15 16 9 19 11 14
rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
depth    (index) int64 11 18 5 19 3 14 7 17 0 4

您可以使用ds.set_coords(("lat", "lon"))latlon转换为坐标。结果如下:

Dimensions:  (index: 10)
Coordinates:
* index    (index) int64 0 1 2 3 4 5 6 7 8 9
lon      (index) int64 15 7 9 17 18 1 12 2 6 8
lat      (index) int64 6 8 5 17 15 16 9 19 11 14
Data variables:
dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
depth    (index) int64 11 18 5 19 3 14 7 17 0 4

另一个类似(但不等效(的替代方案是使用ds.set_index(index=("lat", "lon"))它将index修改为具有索引latlon的多级索引。输出如下:

Dimensions:  (index: 10)
Coordinates:
* index    (index) MultiIndex
- lat      (index) int64 6 8 5 17 15 16 9 19 11 14
- lon      (index) int64 15 7 9 17 18 1 12 2 6 8
Data variables:
dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
depth    (index) int64 11 18 5 19 3 14 7 17 0 4

最新更新