Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

netCDF Python: How to get the data type of an attribute of a variable?

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.

like image 388
oldnavy Avatar asked Dec 20 '25 19:12

oldnavy


2 Answers

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'>  
...
like image 115
Eric Bridger Avatar answered Dec 22 '25 10:12

Eric Bridger


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
like image 35
desired login Avatar answered Dec 22 '25 11:12

desired login



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!