首页 > 解决方案 > 将 xarray DataArray 的一部分转换为 NumPy 数组

问题描述

我有一个太大而无法转换为 NumPy 数组的 DataArray:

da = xr.open_dataset('tprate.grib', engine='cfgrib').tprate
da.values

我有:

Unable to allocate 8.56 TiB for an array with shape (28, 7008, 185, 180, 360) and data type float32

如果我isel是前两个维度的 DataArray 的一部分,它能够转换:

ncep_da.isel(number=0, time=0).values

但是如果我isel最后两个维度,使用的内存保持不变:

ncep_da.isel(latitude=0, longitude=0).values

我仍然得到:

Unable to allocate 8.56 TiB for an array with shape (28, 7008, 185, 180, 360) and data type float32

我猜这是因为第二个子数组没有按顺序存储在磁盘上,因为最右边的维度是数据的内部循环。

有没有办法提取第二个子数组并将其有效地转换为 NumPy 数组?

标签: pythonpython-xarray

解决方案


推荐阅读