3

I am using netCDF4 Python package, I know that getattr() can get the value of the attribute of a variable of a dataset, e.g.

root = Dataset(file_name,'r')
for var in root.variables.values():
    print 'attrs of this variable:',var.ncattrs()
    for attr in var.ncattrs():
    print '<<attr name>> =', attr
    print '<<attr value>> =',getattr(var,attr)

I can get the name/value pair of the attribute successfully through the above code. Now I want to get the data type(int, float, etc.) of the attribute, but I can't find such method/function, does anyone know? I know there is such API in netCDF C package.

oldnavy
  • 177
  • 1
  • 3
  • 9

2 Answers2

2
nci = netCDF4.Dataset(file_name,'r')
g_attdict = nci.__dict__
for k,v in g_attdict.iteritems():
  print k, type(v)      

Sample output:

comment <type 'unicode'>
mooring_site_desc <type 'unicode'>
breakout_id <type 'numpy.int32'>
ending_julian_day_number <type 'numpy.float64'>
long_name <type 'unicode'>  
...
Eric Bridger
  • 3,751
  • 1
  • 19
  • 34
1

If you print the variable, the data type will be listed. To get the numpy dtype use the .dtype attribute:

for var in root.variables.values():
    print var
    print var.dtype
desired login
  • 1,138
  • 8
  • 15