1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
from __future__ import print_function
from pyhdf.HDF import *
from pyhdf.VS import *
f = HDF('inventory.hdf') # open 'inventory.hdf' in read mode
vs = f.vstart() # init vdata interface
vd = vs.attach('INVENTORY') # attach vdata 'INVENTORY' in read mode
# Display some vdata attributes
print("status:", vd.status)
print("vdata: ", vd._name) # predefined attribute: vdata name
print("nrecs: ", vd._nrecs) # predefined attribute: num records
# Display value of attribute 'unit' for all fields on which
# this attribute is set
print("units: ", end=' ')
for fieldName in vd._fields: # loop over all field names
try:
# instantiate field and obtain value of attribute 'unit'
v = vd.field(fieldName).unit
print("%s: %s" % (fieldName, v), end=' ')
except: # no 'unit' attribute: ignore
pass
print("")
print("")
# Display table header.
header = "%-7s %-12s %3s %4s %8s" % tuple(vd._fields)
print("-" * len(header))
print(header)
print("-" * len(header))
# Loop over the vdata records, displaying each record as a table row.
# Current record position is 0 after attaching the vdata.
while True:
try:
rec = vd.read() # read next record
# equivalent to:
# rec = vd[vd.tell()]
print("%-7s %-12s %3d %4.1f %8.2f" % tuple(rec[0]))
except HDF4Error: # end of vdata reached
break
vd.detach() # "close" the vdata
vs.end() # terminate the vdata interface
f.close() # close the HDF file
|