J W*_*J W 1 python bigdata netcdf
我需要将大(+ 15GB)NetCDF文件读入一个程序,该程序包含一个3D变量(等时间作为记录维度,数据是纬度经度).
我正在以3级嵌套循环处理数据(如果NetCDF通过某个标准,则检查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
Run Code Online (Sandbox Code Playgroud)
无论如何,我一次可以在NetCDF文件中读取一次记录吗?大大减少内存使用量.任何帮助非常感谢.
我知道NCO运算符,但在使用脚本之前不希望使用这些方法来分解文件.
听起来你已经确定了一个解决方案,但我会抛出一个更优雅和矢量化(可能更快)的解决方案,使用xarray和dask.你的嵌套for循环效率非常低.组合xarray和dask,您可以在半矢量化的庄园中逐步处理文件中的数据.
由于您的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)
Run Code Online (Sandbox Code Playgroud)
Xarray/Dask docs:http://xarray.pydata.org/en/stable/dask.html