【问题标题】:Reading 4d var from NetCDF file in Python在 Python 中从 NetCDF 文件中读取 4d var
【发布时间】:2013-09-08 00:54:23
【问题描述】:

我正在使用 Scientific.IO.NetCDF 将 NetCDF 数据读入 Python。我正在尝试读取大小为 (366,30,476,460) 的 4d 32 位变量,但最终在我的 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

那么现在是 3d ndarray 的“udata”具有它应该从 NetCDF 文件中读取的值。

有什么帮助吗?提前致谢。

【问题讨论】:

  • 如果可能,一个建议可能是将您的 netCDF 文件放在保管箱(或一些等效服务)上,以便我们进行试验。

标签: python netcdf


【解决方案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 并使用这种方法读取您的数据,希望它可以提供帮助。 干杯,特隆德

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2014-04-12
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多