NetCDF 大数据

NetCDF Big data

我需要将大型 (+15GB) NetCDF 文件读入一个程序,该程序包含一个 3D 变量(等时间作为记录维度,数据是经度纬度)。

我正在 3 级嵌套循环中处理数据(检查 NetCDF 的每个块是否通过特定条件。例如;

from netCDF4 import Dataset                   
import numpy as np

File = Dataset('Somebigfile.nc', 'r')
Data = File.variables['Wind'][:]

Getdimensions = np.shape(Data)
Time = Getdimensions[0]
Latdim  = Getdimensions[1]
Longdim = Getdimensions[2]

for t in range(0,Time):
    for i in range(0,Latdim):
        for j in range(0,Longdim):

            if Data[t,i,j] > Somethreshold:
                #Do something

有没有办法在NetCDF文件中一次读取一条记录?大大减少内存使用。非常感谢任何帮助。

我知道 NCO 运算符,但不想在使用脚本之前使用这些方法来分解文件。

听起来您已经确定了一个解决方案,但我会抛出一个使用 xarraydask 的更优雅和矢量化(可能更快)的解决方案。您的嵌套 for 循环将非常低效。结合 xarraydask,您可以在半矢量化庄园中以增量方式处理文件中的数据。

由于您的 Do something 步骤不是那么具体,您必须从我的示例中进行推断。

import xarray as xr

# xarray will open your file but doesn't load in any data until you ask for it
# dask handles the chunking and memory management for you
# chunk size can be optimized for your specific dataset.
ds = xr.open_dataset('Somebigfile.nc', chunks={'time': 100})

# mask out values below the threshold
da_thresh = ds['Wind'].where(ds['Wind'] > Somethreshold)

# Now just operate on the values greater than your threshold
do_something(da_thresh)

Xarray/Dask 文档:http://xarray.pydata.org/en/stable/dask.html