简体   繁体   中英

Reading 4d var from NetCDF file in Python

I am using Scientific.IO.NetCDF to read NetCDF data into Python. I am trying to read a 4d 32bit variable with size (366,30,476,460) but I end up with zeros in my ndarray. Strangely if I read just the 3d data (1,30,476,460), the returned values are ok.

This is what I am trying to do:

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

The "[:]" means i am reading the whole 4d variable 'u' into an ndarray called udata. This does not work and udata is full of zeros. However, if I do:

try:
  udata = ncfile.variables['u'][0,:,:,:]
  print str(udata.shape)
except:
  print ' Failed to read u data from '+data_file

then "udata" that is now a 3d ndarray has the values it is supposed to read from the NetCDF file.

Any help? Thanks in advance.

It is unclear to me what may cause your problem, but I have one alternative suggestionyou may try. It seems you are reading NetCDF4 data output from a ROMS ocean model. I do this regularily, but I always prefer to use the netcdf-python module for this:

 from netCDF4 import Dataset
 cdf=Dataset("ns8km_avg_16482_GLORYS2V1.nc","r")
 u=cdf.variables["u"][:]

One benefit of the netcdf-python module is that it automatically adjusts for offset, scale, and fill_value in a netcdf file. The 4D array read from the netcdf file will therefore contain masked values. I wonder if the masking in your approach is not done correctly. Perhaps you could try installing netcdf-python and read your data with this approach and hopefully it could help. Cheers, Trond

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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