mdf_pycdf.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #! /usr/bin/env python
  2. """
  3. NetCDF tools.
  4. """
  5. def show( fname ) :
  6. """
  7. List contents of NetCDF file.
  8. """
  9. # external:
  10. import pycdf
  11. # type names:
  12. typenames = { pycdf.NC.BYTE : 'byte', pycdf.NC.SHORT : 'short', pycdf.NC.INT : 'int',
  13. pycdf.NC.CHAR : 'char', pycdf.NC.FLOAT : 'float' }
  14. # start listing:
  15. print 'netcdf %s {' %fname
  16. # open file:
  17. ncid = pycdf.CDF(fname,NC.NOWRITE)
  18. # dimensions:
  19. dd = ncid.dimensions()
  20. print 'dimensions:'
  21. for key in dd.keys() :
  22. print ' %s = %i ;' % (key,dd[key])
  23. #endfor
  24. # variables:
  25. vv = ncid.variables()
  26. print 'variables:'
  27. for name in vv.keys() :
  28. varid = ncid.var(name)
  29. typnam = typenames[varid.inq_type()]
  30. print ' %s %s%s ;' % (typnam,name,varid.dimensions())
  31. aa = varid.attributes()
  32. for key in aa.keys() :
  33. print ' %s:%s = \"%s\" ;' % (name,key,aa[key])
  34. #endfor
  35. #endfor
  36. # global variables:
  37. ndim,nvar,nattr,unlimid = ncid.inq()
  38. print ''
  39. print '// global attributes:'
  40. # loop over attributes:
  41. for i in range(nattr) :
  42. # extract attribute:
  43. attrid = ncid.attr(i)
  44. # extract name:
  45. aname = attrid.inq_name()
  46. # netcdf files created by 'ncrcat' have something strange
  47. # in the history attribute which leads to a segmenation fault ...
  48. if aname == 'history' :
  49. print ' :%s = ... ;' % aname
  50. continue
  51. #endif
  52. # extract value:
  53. avalue = attrid.get()
  54. # show line:
  55. print ' :%s = \"%s\" ;' % (aname,repr(avalue))
  56. #endfor
  57. print '}'
  58. # close:
  59. ncid.close()
  60. #enddef
  61. # ***
  62. def get_varnames( fname ) :
  63. """
  64. Return variable names found in data file.
  65. """
  66. # external:
  67. import pycdf
  68. # open file:
  69. ncid = pycdf.CDF(fname,NC.NOWRITE)
  70. # variables:
  71. vv = ncid.variables()
  72. # extract names:
  73. varnames = vv.keys()
  74. # ok
  75. return varnames
  76. #enddef
  77. # ***
  78. def get_char( varid ) :
  79. # extract shape of character variable:
  80. shp = varid.shape()
  81. # print for single string (1D) or table (2D):
  82. if len(shp) == 1:
  83. # can only get single value without errors ...
  84. s = ''
  85. for i in range(shp[0]):
  86. c = varid.get_1( i )
  87. s = s+c
  88. #endfor
  89. # store:
  90. values = s.strip()
  91. elif len(shp) == 2:
  92. # init output:
  93. values = []
  94. # loop over strings:
  95. for j in range(shp[0]):
  96. # can only get single value without errors ...
  97. s = ''
  98. for i in range(shp[1]):
  99. c = varid.get_1( [j,i] )
  100. s = s+c
  101. #endfor
  102. # store:
  103. values.append(s.strip())
  104. #endfor
  105. else:
  106. # warning ..
  107. print " sorry, not implemented for shape : ", shp
  108. #endif
  109. # ok
  110. return values
  111. #enddef
  112. # ***
  113. def get_var( fname, vname ) :
  114. """
  115. Read variable from NetCDF file, return array in tzxy order:
  116. o3 = qnc.qget( 'output.nc', 'ozone' )
  117. """
  118. # external:
  119. import pycdf
  120. # open file:
  121. ncid = pycdf.CDF( fname )
  122. # variable:
  123. varid = ncid.var(vname)
  124. # extract variable type etc:
  125. info = varid.inq()
  126. # special for character ...
  127. if info[1] == pycdf.NC.CHAR :
  128. # get character variable:
  129. values = get_char( varid )
  130. else :
  131. # get variable:
  132. values = varid.get()
  133. #endif
  134. # close file:
  135. ncid.close()
  136. # ok
  137. return values
  138. #enddef
  139. # ***
  140. def get_attr( fname, aname ) :
  141. """
  142. Read global attribute from NetCDF file.
  143. """
  144. # external:
  145. import pycdf
  146. # open file:
  147. ncid = pycdf.CDF( fname )
  148. # id:
  149. attrid = ncid.attr(aname)
  150. # extract value:
  151. avalue = attrid.get()
  152. # close file:
  153. ncid.close()
  154. # ok
  155. return avalue
  156. #enddef
  157. # ***
  158. def get_var_attr( fname, vname, aname ) :
  159. """
  160. Read variable attribute from NetCDF file.
  161. """
  162. # external:
  163. import pycdf
  164. # open file:
  165. ncid = pycdf.CDF( fname )
  166. # variable:
  167. varid = ncid.var(vname)
  168. # attribute:
  169. attrid = varid.attr(aname)
  170. # extract value:
  171. avalue = attrid.get()
  172. # close file:
  173. ncid.close()
  174. # ok
  175. return avalue
  176. #enddef