使用 xarray to_netcdf 写入磁盘会丢失坐标变量

发布于 2025-01-13 07:36:46 字数 1032 浏览 2 评论 0原文

我正在尝试使用 xarray to_netcdf 保存数据集,但是当我将数据集写入磁盘然后读回时,坐标已更改。这只是一个偶然的问题,我无法重现它。

编辑:我已经发现问题与附加到变量的编码有关,现在可以用一个虚拟示例重现该问题:

lon = [[-99.83, -99.32], [-99.79, -99.23]]
lat = [[42.25, 42.21], [42.63, 42.59]]
time = pd.date_range("2014-09-06", periods=3)

ds = xr.Dataset(
    data_vars=dict(
        temperature=(["x", "y", "time"], temperature),),
    coords=dict(
        lon=(["x", "y"], lon),
        lat=(["x", "y"], lat),
        time=time,),
    attrs=dict(description="Weather related data."),)


ds.temperature.encoding={'zlib': False,
 'shuffle': False,
 'complevel': 0,
 'fletcher32': False,
 'contiguous': True,
 'chunksizes': None,
 'source': 'fileloc/file.nc',
 'original_shape': (3, 2, 2),
 'coordinates': 'time, lat, lon'}

print(ds) 给出了我期望的坐标,但是 ds.to_netcdf('dummy.nc')ds2=xr.open_dataset('dummy.nc') 丢失 'lon' 作为坐标并设置它作为数据变量。

有谁知道这种编码中的什么导致“lon”坐标发生变化?我看不到任何与“lon”具体相关的内容。

我已经更新到最新版本的 xarray 并尝试了decode_coords='all',但仍然看到这个问题。

I am trying to save a dataset using xarray to_netcdf, but when I write the dataset to disk then read it back in, the coordinates have changed. This is only an occasional problem and I am unable to reproduce it.

EDIT: I have worked out that the problem is to do with the encoding attached to the variable, and can now reproduce the problem with a dummy example:

lon = [[-99.83, -99.32], [-99.79, -99.23]]
lat = [[42.25, 42.21], [42.63, 42.59]]
time = pd.date_range("2014-09-06", periods=3)

ds = xr.Dataset(
    data_vars=dict(
        temperature=(["x", "y", "time"], temperature),),
    coords=dict(
        lon=(["x", "y"], lon),
        lat=(["x", "y"], lat),
        time=time,),
    attrs=dict(description="Weather related data."),)


ds.temperature.encoding={'zlib': False,
 'shuffle': False,
 'complevel': 0,
 'fletcher32': False,
 'contiguous': True,
 'chunksizes': None,
 'source': 'fileloc/file.nc',
 'original_shape': (3, 2, 2),
 'coordinates': 'time, lat, lon'}

print(ds) gives the coordinates as I'd expect, but ds.to_netcdf('dummy.nc'), ds2=xr.open_dataset('dummy.nc') loses 'lon' as a coordinate and sets it as a data variable.

Does anybody know what in this encoding causes the 'lon' coordinate to shift? I can't see anything relating to 'lon' specifically.

I have updated to the latest version of xarray and tried decode_coords='all' but am still seeing this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文