You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xarray中NetCDF变量mesh2d_sa1的coordinates属性无法读取问题

问题解决:NetCDF变量coordinates属性在xarray中无法通过attrs访问

原因分析

xarray读取NetCDF文件时,会自动识别并解析coordinates属性,将其中指定的变量转换为该数据数组的坐标变量,因此原始的coordinates字符串属性会被移除,不会保留在.attrs字典中。

解决办法

方法1:查看关联的坐标变量

直接通过.coords属性查看xarray自动绑定的坐标:

print(ds.mesh2d_sa1.coords)

你会看到mesh2d_face_x和mesh2d_face_y已经作为坐标关联到变量上。

方法2:用netCDF4库直接读取原始属性

如果需要获取原始的coordinates字符串内容,可以绕过xarray,直接用netCDF4库读取:

import netCDF4 as nc

with nc.Dataset('xxxxxxx.nc', 'r') as nc_ds:
    coordinates_str = nc_ds.variables['mesh2d_sa1'].getncattr('coordinates')
    print(coordinates_str)  # 输出: "mesh2d_face_x mesh2d_face_y"

方法3:升级xarray版本

部分旧版本xarray对属性的处理存在差异,尝试升级到最新版本:

pip install --upgrade xarray

内容的提问来源于stack exchange,提问作者Ken T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 04:02:58