File: vgread.py

package info (click to toggle)
python-hdf4 0.11.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 9,252; python: 3,445; makefile: 192
file content (88 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download | duplicates (5)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import print_function

from pyhdf.HDF import *
from pyhdf.V   import *
from pyhdf.VS  import *
from pyhdf.SD  import *

import sys

def describevg(refnum):

    # Describe the vgroup with the given refnum.

    # Open vgroup in read mode.
    vg = v.attach(refnum)
    print("----------------")
    print("name:", vg._name, "class:",vg._class, "tag,ref:",vg._tag, vg._refnum)

    # Show the number of members of each main object type.
    print("# members:  ", vg._nmembers,\
           "# datasets:", vg.nrefs(HC.DFTAG_NDG),\
           "# vdatas:  ", vg.nrefs(HC.DFTAG_VH),\
           "# vgroups: ", vg.nrefs(HC.DFTAG_VG))

    # Read the contents of the vgroup.
    members = vg.tagrefs()

    # Display info about each member.
    index = -1
    for tag, ref in members:
        index += 1
        print("member index", index)
        # Vdata tag
        if tag == HC.DFTAG_VH:
            vd = vs.attach(ref)
            nrecs, intmode, fields, size, name = vd.inquire()
            print("  vdata:",name, "tag,ref:",tag, ref)
            print("    fields:",fields)
            print("    nrecs:",nrecs)
            vd.detach()

        # SDS tag
        elif tag == HC.DFTAG_NDG:
            sds = sd.select(sd.reftoindex(ref))
            name, rank, dims, type, nattrs = sds.info()
            print("  dataset:",name, "tag,ref:", tag, ref)
            print("    dims:",dims)
            print("    type:",type)
            sds.endaccess()

        # VS tag
        elif tag == HC.DFTAG_VG:
            vg0 = v.attach(ref)
            print("  vgroup:", vg0._name, "tag,ref:", tag, ref)
            vg0.detach()

        # Unhandled tag
        else:
            print("unhandled tag,ref",tag,ref)

    # Close vgroup
    vg.detach()

# Open HDF file in readonly mode.
filename = sys.argv[1]
hdf = HDF(filename)

# Initialize the SD, V and VS interfaces on the file.
sd = SD(filename)
vs = hdf.vstart()
v  = hdf.vgstart()

# Scan all vgroups in the file.
ref = -1
while True:
    try:
        ref = v.getid(ref)
    except HDF4Error as msg:    # no more vgroup
        break
    describevg(ref)

# Terminate V, VS and SD interfaces.
v.end()
vs.end()
sd.end()

# Close HDF file.
hdf.close()