简体   繁体   English

在python中使用netCDF4创建一个大的netcdf文件(> 10Gb)

[英]Creating a big netcdf file (>10Gb) with netCDF4 in python

I am having problems trying to create a very big netCDF file in python in a machine with 8gb of RAM.我在尝试在具有 8GB RAM 的机器上用 python 创建一个非常大的 netCDF 文件时遇到问题。

I created a very big array with numpy.memmap in order to have this array in disk and not in ram because its size exceeds the available ram and swap space.我用 numpy.memmap 创建了一个非常大的数组,以便将这个数组放在磁盘而不是 ram 中,因为它的大小超过了可用的 ram 和交换空间。 (ram and swap = 8 gb each) (内存和交换 = 8 GB)

I created a variable in the nc file with我在 nc 文件中创建了一个变量

var = ncout.createVariable('data',ARRAY.dtype,\
                       ('time','latitude','longitude',),\
                        chunksizes=(5000,61,720))

var[:]=ARRAY[:]

When the code reach this point It loads into the ram the ARRAY that is saved in disk and then I have memory error.当代码到达这一点时,它会将保存在磁盘中的 ARRAY 加载到 ram 中,然后出现内存错误。

How can I save such a big files?这么大的文件怎么保存?

Thanks.谢谢。

The best way to read and write large NetCDF4 files is with Xarray , which reads and writes data in chunks automatically using Dask below the hood.读取和写入大型 NetCDF4 文件的最佳方法是使用Xarray ,它使用引擎盖下的 Dask 自动以块的形式读取和写入数据。

import xarray as xr
ds = xr.open_dataset('my_big_input_file.nc', 
            chunks={'time':5000, ,'latitude':61, ,'longitude':720})
ds.to_netcdf('my_big_output_file.nc',mode='w')

You can speed things up by using parallel computing with Dask .您可以通过将并行计算与 Dask 结合使用来加快速度。

Iterating directly over an array gives you the slices along the first dimension.直接在数组上迭代为您提供沿第一维的切片。 Using enumerate will give you both the slice and the index:使用enumerate将为您提供切片和索引:

for ind, slice in enumerate(ARRAY):
    var[ind] = slice

I'm not positive whether netCDF4-python will keep the slices around in memory, though.不过,我不确定 netCDF4-python 是否会将切片保留在内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM