1

我正在尝试使用 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')])
4

2 回答 2

3

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

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

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

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

于 2018-05-22T20:25:14.483 回答
1

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

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

这里进一步讨论。

于 2019-04-24T23:54:28.260 回答