繁体   English   中英

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

[英]reassigning xarray data variable to xarray coordinate

我有一个空间数据的熊猫数据框,我想将其转换为 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM