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

use*_*050 4 python netcdf dataframe python-xarray

我有一个 pandas 空间数据数据框,我想将其转换为 netCDF。我找到了 xarray 的方法并将我的数据框转换为 xarray 数据集:

# create xray Dataset from Pandas DataFrame
xr = xarray.Dataset.from_dataframe(df)
Run Code Online (Sandbox Code Playgroud)

现在,我想将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
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏:D

Ori*_*ril 7

从如下所示的Dataset调用开始:ds

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

另一种类似(但不等效)的替代方法是使用ds.set_index(index=("lat", "lon"))which 将修改为带有索引和 的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
Run Code Online (Sandbox Code Playgroud)