mdf_netCDF4.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """
  2. NetCDF4 tools.
  3. """
  4. def show( fname ) :
  5. """
  6. List contents of NetCDF file.
  7. """
  8. import netCDF4
  9. # type names:
  10. typenames = { 'int8' : 'byte', 'int16': 'short', 'int32' : 'int',
  11. 'string8' : 'char', 'float32' : 'float','float64' : 'double' }
  12. # open file:
  13. ncid = netCDF4.Dataset( fname, 'r' )
  14. # start listing:
  15. print '%s %s {' % (ncid.file_format,fname)
  16. # dimensions:
  17. print 'dimensions:'
  18. for key in ncid.dimensions.keys() :
  19. print ' %s = %i ;' % (key,len(ncid.dimensions.get(key)))
  20. # variables:
  21. print 'variables:'
  22. # loop over variable names:
  23. for name in ncid.variables.keys() :
  24. # variable ojbect:
  25. varid = ncid.variables.get(name)
  26. # convert type description:
  27. typnam = typenames[varid.dtype.name]
  28. # show line:
  29. print ' %s %s%s ;' % (typnam,name,str(varid.shape))
  30. # loop over attribute names:
  31. for aname in varid.ncattrs() :
  32. # extract value:
  33. avalue = varid.getncattr(aname)
  34. # show line:
  35. print ' %s:%s = %s ;' % (name,aname,repr(avalue))
  36. # global atrributes:
  37. print ''
  38. print '// global attributes:'
  39. # loop over attributes:
  40. for aname in ncid.ncattrs() :
  41. ## netcdf files created by 'ncrcat' have something strange
  42. ## in the history attribute which leads to a segmenation fault ...
  43. #if aname == 'history' :
  44. # print ' :%s = ... ;' % aname
  45. # continue
  46. ##endif
  47. # extract value:
  48. avalue = getattr(ncid,aname)
  49. # show line:
  50. print ' :%s = %s ;' % (aname,repr(avalue))
  51. # closure:
  52. print '}'
  53. ncid.close()
  54. # ***
  55. def get_varnames( fname ) :
  56. """
  57. Return variable names found in data file.
  58. """
  59. import netCDF4
  60. # open file:
  61. ncid = netCDF4.Dataset( fname, 'r' )
  62. # variable names:
  63. varnames = ncid.variables.keys()
  64. # ok
  65. return varnames
  66. # ***
  67. #def get_char( varid ) :
  68. #
  69. # # extract shape of character variable:
  70. # shp = varid.shape()
  71. # # print for single string (1D) or table (2D):
  72. # if len(shp) == 1:
  73. # # can only get single value without errors ...
  74. # s = ''
  75. # for i in range(shp[0]):
  76. # c = varid.get_1( i )
  77. # s = s+c
  78. # #endfor
  79. # # store:
  80. # values = s.strip()
  81. # elif len(shp) == 2:
  82. # # init output:
  83. # values = []
  84. # # loop over strings:
  85. # for j in range(shp[0]):
  86. # # can only get single value without errors ...
  87. # s = ''
  88. # for i in range(shp[1]):
  89. # c = varid.get_1( [j,i] )
  90. # s = s+c
  91. # #endfor
  92. # # store:
  93. # values.append(s.strip())
  94. # #endfor
  95. # else:
  96. # # warning ..
  97. # print " sorry, not implemented for shape : ", shp
  98. #
  99. # # ok
  100. # return values
  101. #
  102. ##enddef
  103. # ***
  104. def get_var( fname, vname, sample=None ) :
  105. """
  106. Read variable from NetCDF file.
  107. """
  108. import logging
  109. import netCDF4
  110. if sample != None :
  111. logging.error( 'argument "sample" not supported for mdf_netCDF.get_var' )
  112. raise ValueError
  113. ncid = netCDF4.Dataset( fname ) # open
  114. varid = ncid.variables.get(vname) # variable
  115. values = varid[:] # extract values
  116. # special
  117. if varid.dtype.name == 'string8' :
  118. # convert from n-D array of characters to (n-1)-D array of strings:
  119. if len(values) !=0 : values = netCDF4.chartostring(values)
  120. ncid.close()
  121. return values
  122. # ***
  123. def get_attr( fname, aname ) :
  124. """
  125. Read global attribute from NetCDF file.
  126. """
  127. import netCDF4
  128. # open file:
  129. ncid = netCDF4.Dataset( fname, 'r' )
  130. # extract value:
  131. avalue = ncid.getncattr(aname)
  132. # close file:
  133. ncid.close()
  134. # ok
  135. return avalue
  136. # ***
  137. def get_var_attr( fname, vname, aname ) :
  138. """
  139. Read variable attribute from NetCDF file.
  140. """
  141. # external:
  142. import netCDF4
  143. # open file:
  144. ncid = netCDF4.Dataset( fname, 'r' )
  145. # variable:
  146. varid = ncid.variables.get(vname)
  147. # extract value:
  148. avalue = varid.getncattr(aname)
  149. # close file:
  150. ncid.close()
  151. # ok
  152. return avalue
  153. #enddef