我正在使用Scientific.IO.NetCDF将NetCDF数据读入Python。我试图读取一个4d 32位变量的大小(366,30,476,460),但我最终在我的ndarray中使用了零。奇怪的是,如果我只读取3d数据(1,30,476,460),返回的值就可以了。
这就是我想要做的事情:
from Scientific.IO.NetCDF import NetCDFFile as Dataset
from collections import namedtuple
# Define output data structure as a named tuple
Roms_data=namedtuple('Roms_data','Ti Tf Nt U V W Zeta')
# Open the NetCDF file for reading.
ncfile = Dataset(data_file,'r')
if Tstart==-1:
ti=0
tf=NTsav-1
else:
ti=Tstart-1
tf=Tend-1
try:
udata = ncfile.variables['u'][:]
print str(udata.shape)
except:
print ' Failed to read u data from '+data_file
“[:]”表示我正在将整个4d变量“u”读入名为udata的ndarray中。这不起作用,udata充满了零。但是,如果我这样做:
try:
udata = ncfile.variables['u'][0,:,:,:]
print str(udata.shape)
except:
print ' Failed to read u data from '+data_file
然后“udata”现在是一个3d ndarray,它具有从NetCDF文件中读取的值。
有任何帮助吗?提前谢谢。
答案 0 :(得分:1)
我不清楚可能会导致您的问题,但我有一个替代建议,您可以尝试。您似乎正在从ROMS海洋模型中读取NetCDF4数据输出。我经常这样做,但我总是喜欢使用netcdf-python module来做到这一点:
from netCDF4 import Dataset
cdf=Dataset("ns8km_avg_16482_GLORYS2V1.nc","r")
u=cdf.variables["u"][:]
netcdf-python模块的一个好处是它可以自动调整netcdf文件中的offset, scale, and fill_value。因此,从netcdf文件读取的4D数组将包含屏蔽值。我想知道你的方法中的掩蔽是否没有正确完成。也许您可以尝试安装netcdf-python并使用这种方法读取您的数据,并希望它可以提供帮助。 干杯,特隆德