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.
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'>
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With