试图将Xarray WRF横截面保存到NetCDF时TypeError

发布于 2025-02-09 00:26:09 字数 2388 浏览 1 评论 0原文

我试图将下面的Xarray DataArray保存为NetCDF,但是我会遇到错误“ ValueRorr:无法在变量'xy_loc';中推断dtype; xarray无法序列化任意的python对象”。数据是使用WRF软件包中的Vertcross生成的横截面。 I have resolved an earlier issue ("invalid value for attr") by removing the problem attributes as suggested here (

xarray.DataArray 'QVAPOR_cross' (XTIME: 2, vertical: 59, cross_line_idx: 239)
array([[[          nan,           nan,           nan, ...,
                   nan,           nan,           nan],
        [5.2142618e-03, 5.4577263e-03, 5.6551830e-03, ...,
         1.1576526e-02, 1.1481316e-02, 1.2421611e-02],
        [4.7921482e-03, 4.8334384e-03, 4.8439419e-03, ...,
         5.1765046e-03, 5.3730649e-03, 5.0648679e-03],
        ...,
        [3.1173588e-06, 3.1196325e-06, 3.1220573e-06, ...,
         1.7669336e-06, 1.8619023e-06, 1.9090951e-06],
        [3.2124219e-06, 3.2157229e-06, 3.2187045e-06, ...,
         1.9386096e-06, 1.9239062e-06, 1.8803454e-06],
        [          nan,           nan,           nan, ...,
         2.2164093e-06, 2.1845497e-06, 2.1732367e-06]],

       [[          nan,           nan,           nan, ...,
                   nan,           nan,           nan],
        [5.0503691e-03, 5.2436758e-03, 5.4539447e-03, ...,
         1.2035056e-02, 1.1335670e-02, 1.2419974e-02],
        [4.7799554e-03, 4.7886986e-03, 4.7935634e-03, ...,
         5.3098863e-03, 5.4153400e-03, 5.0202534e-03],
        ...,
        [3.1646039e-06, 3.1631198e-06, 3.1618749e-06, ...,
         1.8017681e-06, 1.8647409e-06, 1.8782921e-06],
        [3.2718322e-06, 3.2725636e-06, 3.2733096e-06, ...,
         1.8897241e-06, 1.8277435e-06, 1.7775615e-06],
        [          nan,           nan,           nan, ...,
         2.1244794e-06, 2.1224316e-06, 2.1411674e-06]]], dtype=float32)

Coordinates:
XTIME    (XTIME)          float64  720.0 780.0
Time     ()               object   None
xy_loc   (cross_line_idx) object   CoordPair(x=162.0, y=246.0, lat=...
vertical (vertical)       float32  970.83813 964.3068 ... 10.491312

Attributes:
FieldType : 104
description : Water vapor mixing ratio
units : kg kg-1
stagger :
coordinates : XLONG XLAT XTIME
projection : LambertConformal(stand_lon=-78.0, moad_cen_lat=38.0000114440918, truelat1=30.0, truelat2=60.0, pole_lat=90.0, pole_lon=0.0)
orientation : (162.0, 246.0) to (168.0, 8.0)
missing_value : 9.969209968386869e+36
_FillValue : 9.969209968386869e+36

I am trying to save the xarray DataArray below as netcdf, but I get error "ValueError: unable to infer dtype on variable 'xy_loc'; xarray cannot serialize arbitrary Python objects". The data is a cross section produced using vertcross from the wrf package. I have resolved an earlier issue ("invalid value for attr") by removing the problem attributes as suggested here (TypeError when trying to save xarray to netcdf), but I do not understand what exactly about the xy_loc coordinate is a problem?

xarray.DataArray 'QVAPOR_cross' (XTIME: 2, vertical: 59, cross_line_idx: 239)
array([[[          nan,           nan,           nan, ...,
                   nan,           nan,           nan],
        [5.2142618e-03, 5.4577263e-03, 5.6551830e-03, ...,
         1.1576526e-02, 1.1481316e-02, 1.2421611e-02],
        [4.7921482e-03, 4.8334384e-03, 4.8439419e-03, ...,
         5.1765046e-03, 5.3730649e-03, 5.0648679e-03],
        ...,
        [3.1173588e-06, 3.1196325e-06, 3.1220573e-06, ...,
         1.7669336e-06, 1.8619023e-06, 1.9090951e-06],
        [3.2124219e-06, 3.2157229e-06, 3.2187045e-06, ...,
         1.9386096e-06, 1.9239062e-06, 1.8803454e-06],
        [          nan,           nan,           nan, ...,
         2.2164093e-06, 2.1845497e-06, 2.1732367e-06]],

       [[          nan,           nan,           nan, ...,
                   nan,           nan,           nan],
        [5.0503691e-03, 5.2436758e-03, 5.4539447e-03, ...,
         1.2035056e-02, 1.1335670e-02, 1.2419974e-02],
        [4.7799554e-03, 4.7886986e-03, 4.7935634e-03, ...,
         5.3098863e-03, 5.4153400e-03, 5.0202534e-03],
        ...,
        [3.1646039e-06, 3.1631198e-06, 3.1618749e-06, ...,
         1.8017681e-06, 1.8647409e-06, 1.8782921e-06],
        [3.2718322e-06, 3.2725636e-06, 3.2733096e-06, ...,
         1.8897241e-06, 1.8277435e-06, 1.7775615e-06],
        [          nan,           nan,           nan, ...,
         2.1244794e-06, 2.1224316e-06, 2.1411674e-06]]], dtype=float32)

Coordinates:
XTIME    (XTIME)          float64  720.0 780.0
Time     ()               object   None
xy_loc   (cross_line_idx) object   CoordPair(x=162.0, y=246.0, lat=...
vertical (vertical)       float32  970.83813 964.3068 ... 10.491312

Attributes:
FieldType : 104
description : Water vapor mixing ratio
units : kg kg-1
stagger :
coordinates : XLONG XLAT XTIME
projection : LambertConformal(stand_lon=-78.0, moad_cen_lat=38.0000114440918, truelat1=30.0, truelat2=60.0, pole_lat=90.0, pole_lon=0.0)
orientation : (162.0, 246.0) to (168.0, 8.0)
missing_value : 9.969209968386869e+36
_FillValue : 9.969209968386869e+36

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-02-16 00:26:09

错误消息说明了所有内容:“ ValueError:无法在变量'xy_loc'; xarray上推断dtype; xarray无法序列化任意python对象”。

xy_loc坐标包含一个类型对象的数组,这意味着它们不是均匀dtype的numpy数组,例如float64int32 int32 < /code>等。相反,numpy数组将指针固定在Python对象上。第一个元素是称为coordpair(x = 162.0,y = 246.0,lat = ...。我不确定这是什么,看来Xarray也不是Xarray。

当Xarray写入NetCdf时,它序列化数据,这意味着它从python内存数据结构(例如dataarray,坐标等)到具有特定编码的文件。编码数据的协议

。 必须做一些预处理以准备数据以进行存储。

# add non-dimensional coordinates for each attribute of each CoordPair
da.coords['x'] = ('xy_loc', ), [c.x for c in xy_loc.values]
da.coords['y'] = ('xy_loc', ), [c.y for c in xy_loc.values]
da.coords['lat'] = ('xy_loc', ), [c.lat for c in xy_loc.values]
da.coords['lon'] = ('xy_loc', ), [c.lon for c in xy_loc.values]

# finally, replace the coord xy_loc itself with a positional index
da.corods['xy_loc'] = np.arange(len(da.xy_loc))

您 应该能够将对象保存为NetCDF

The error message says it all: "ValueError: unable to infer dtype on variable 'xy_loc'; xarray cannot serialize arbitrary Python objects".

The xy_loc coordinate contains an array of type object, meaning they're not numpy arrays of uniform dtype, e.g. float64, int32, etc. Instead, the numpy array holds pointers to python objects. The first element is something called CoordPair(x=162.0, y=246.0, lat=.... I'm not sure what this is, and it seems neither is xarray.

When xarray writes to netCDF, it serializes the data, meaning it converts it from a python in-memory data structure (e.g. DataArray, Coordinate, etc) to a file with a specific encoding. The netCDF file specification doesn't have a built-in way to handle arbitrary python objects - it must have a protocol for encoding the data. numpy float64s get converted to netCDF long, dask.arrays get converted to netCDF chunks, etc.

If you have an object that is not something that xarray knows what to do with, you must do some preprocessing to prepare the data for storage. For example, can you change your xy_loc dim into a positional index, and add additional coordinates which give the information contained in the xy_loc object:

# add non-dimensional coordinates for each attribute of each CoordPair
da.coords['x'] = ('xy_loc', ), [c.x for c in xy_loc.values]
da.coords['y'] = ('xy_loc', ), [c.y for c in xy_loc.values]
da.coords['lat'] = ('xy_loc', ), [c.lat for c in xy_loc.values]
da.coords['lon'] = ('xy_loc', ), [c.lon for c in xy_loc.values]

# finally, replace the coord xy_loc itself with a positional index
da.corods['xy_loc'] = np.arange(len(da.xy_loc))

Now, you should be able to save the object as netCDF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文