NetCDF4通过xarray保存期间的MemoryError

2024-05-15 17:53:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过xarray加载我的499 NetCDF文件并连接它们,但是,似乎我在保存该文件时遇到了麻烦

以下是我的代码片段:

import pandas as pd
import numpy as np
import xarray as xr
import os

files_xr = [f for f in os.listdir(os.getcwd()) if f.startswith("Precipitation") and f.endswith(".nc")]
files_xr_mer = xr.open_mfdataset(files_xr, combine='by_coords')
files_xr_mer['units'] = 'mm'
new_filename_1 = './prec_file_testing.nc'
files_xr_mer.to_netcdf(path=new_filename_1)

Traceback (most recent call last)
MemoryError: Unable to allocate 2.80 GiB for an array with shape (29, 3601, 7199) and data type float32 

谢谢你的建议!我肯定会使用python和NCO或CDO作为最后一个选项


Tags: and文件toimportnewforosas
1条回答
网友
1楼 · 发布于 2024-05-15 17:53:51

您可以尝试在open_mfdataset中传递chunks关键字的值。这将启用数据流,并不是所有的东西都一次加载到内存中。 https://xarray.pydata.org/en/stable/generated/xarray.open_mfdataset.html

例如chunks={"time": 1}如果时间是你的维度之一,那么会导致一个接一个地加载块。可能与连接有一些交互,您可能必须考虑连接是如何发生的,以使其(更)高效

另请参见本文档: https://xarray.pydata.org/en/stable/dask.html#chunking-and-performance

相关问题 更多 >