Reading 4d var from NetCDF file in Python -
i using scientific.io.netcdf read netcdf data python. trying read 4d 32bit variable size (366,30,476,460) end zeros in ndarray. strangely if read 3d data (1,30,476,460), returned values ok.
this trying do:
from scientific.io.netcdf import netcdffile dataset collections import namedtuple # define output data structure named tuple roms_data=namedtuple('roms_data','ti tf nt u v w zeta') # open netcdf file 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 read u data '+data_file
the "[:]" means reading whole 4d variable 'u' ndarray called udata. not work , udata full of zeros. however, if do:
try: udata = ncfile.variables['u'][0,:,:,:] print str(udata.shape) except: print ' failed read u data '+data_file
then "udata" 3d ndarray has values supposed read netcdf file.
any help? in advance.
it unclear me may cause problem, have 1 alternative suggestionyou may try. seems reading netcdf4 data output roms ocean model. regularily, prefer use netcdf-python module this:
netcdf4 import dataset cdf=dataset("ns8km_avg_16482_glorys2v1.nc","r") u=cdf.variables["u"][:]
one benefit of netcdf-python module automatically adjusts offset, scale, , fill_value in netcdf file. 4d array read netcdf file therefore contain masked values. wonder if masking in approach not done correctly. perhaps try installing netcdf-python , read data approach , help. cheers, trond
Comments
Post a Comment