【问题标题】:xarray - cannot serialize coordinatesxarray - 无法序列化坐标
【发布时间】:2019-04-25 07:54:28
【问题描述】:

我正在尝试使用 xarray 来追加和编写 netCDF 文件。原始文件的一些信息如下。

Dimensions:    (Time: 744, south_north: 289, west_east: 339)
Coordinates:
XLAT       (Time, south_north, west_east) float32 ...
XLONG      (Time, south_north, west_east) float32 ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
Times      (Time) |S19 ...
RAINNC     (Time, south_north, west_east) float32 ...
I_RAINNC   (Time, south_north, west_east) int32 ...
SNOWNC     (Time, south_north, west_east) float32 ...
GRAUPELNC  (Time, south_north, west_east) float32 ...
HAILNC     (Time, south_north, west_east) float32 ...  

这是附加文件的一些信息(添加了两个变量)。

Dimensions:    (Time: 744, south_north: 289, west_east: 339)
Coordinates:
XLAT       (Time, south_north, west_east) float32 ...
XLONG      (Time, south_north, west_east) float32 ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
Times      (Time) |S19 ...
RAINNC     (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0
I_RAINNC   (Time, south_north, west_east) int32 0 0 0 0 0 0 0 0 0 0 0 0 
SNOWNC     (Time, south_north, west_east) float32 ...
GRAUPELNC  (Time, south_north, west_east) float32 ...
HAILNC     (Time, south_north, west_east) float32 ...
PRCP       (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0 
CUMPRCP    (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0

我正在尝试使用 xarray.to_netcdf() 编写一个新文件,但收到此错误:

ValueError: cannot serialize coordinates because variable RAINNC 
already has an attribute 'coordinates'

关于如何解决此错误的任何想法?

编辑:

数据是通过天气和研究预测模型 (WRF) 生成的。数据通过 open_dataset() 加载,默认为 decode_cf。两个文件的 RAINNC 属性相同,如下所列。

OrderedDict([('FieldType', 104),
         ('MemoryOrder', 'XY '),
         ('description', 'ACCUMULATED TOTAL GRID SCALE PRECIPITATION'),
         ('units', 'mm'),
         ('stagger', ''),
         ('coordinates', 'XLONG XLAT XTIME')])

【问题讨论】:

    标签: python netcdf python-xarray xarray


    【解决方案1】:

    您是如何加载第一个 xarray.Dataset 的? RAINNC 变量有哪些属性,(1) 在 netCDF 文件中,(2) 在使用 xarray 加载的原始数据集中和 (3) 在组合数据集中?

    此错误消息告诉您RAINNC 在您的组合数据集中有一个'coordinates' 属性。 Xarray 引发错误,因为它使用'coordinates' 属性(根据CF 约定)将坐标(在您的情况下为XLATXLONG)保存到netCDF 文件。但是,如果存在预先存在的'coordinates' 属性,则不会这样做,以避免覆盖现有数据。

    通常不会出现这种情况,因为在 xarray 的数据模型中将变量添加到 coords 时,磁盘上 netCDF 文件中预先存在的 'coordinates' 属性会被删除。但是,如果您将 xarray.open_dataset()decode_cf=False 一起使用,或者使用某种手动逻辑来创建数据集,则可能会出现这种情况。

    一个简单的解决方法是从组合数据集中的数据变量中删除任何 'coordinates' 属性(例如,del ds['RAINNC'].attrs['coordinates']),但您绝对应该首先查看这些值以确保您没有删除重要的元数据。

    【讨论】:

    • 数据是通过天气和研究预测模型 (WRF) 生成的。数据通过 open_dataset() 加载,默认为 decode_cf。两个文件的 RAINNC 属性相同。有效的解决方法,谢谢。
    【解决方案2】:

    我在使用 WRF 文件时遇到了同样的问题,并找到了一个更简单的解决方法,即在调用 xarray.open_dataset() 时设置 decode_coords=False,例如:

    xr.open_dataset('wrfout_d01_2019-04-16_15_00_00', decode_coords=False).to_netcdf('test.nc')
    

    进一步讨论here

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2021-12-12
      • 2019-04-06
      • 1970-01-01
      • 2020-10-15
      • 2020-07-06
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      相关资源
      最近更新 更多