Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Simplify code for Example 3

...

Code Block
#!/usr/bin/env python
import h5py
import sys

def print_hdf5_file_structure(file_name) :
    """Prints the HDF5 file structure"""
    file = h5py.File(file_name, 'r') # open read-only
    item = file #["/Configure:0000/EvrDataRun::ConfigV40000"]
    print_hdf5_item_structure(item)
    file.close()
    print '=== EOF ==='

def print_hdf5_item_structure(g, offset='    ') :
    """Prints the input file/group/dataset (g) name and begin iterations on its content"""
    print "Structure of the",
    if   isinstance(g,h5py.File) :
        print "g.file, '(File)'", g.name

    elif isinstance(g,h5py.GroupDataset) :
        print "'Group' from file",
'(Dataset)', g.name, '    len =', g.shape #, g.dtype

    elif isinstance(g,h5py.DatasetGroup) :
 print "'Dataset' from file",
    print g.file,"\n",'(Group)', g.name

    else :
 if     isinstance(g,h5py.Dataset):  print offset, "(Dateset)   len ="'WORNING: UNKNOWN ITEM IN HDF5 FILE', g.shapename
 #, subg.dtype
    else:  sys.exit ( "EXECUTION IS TERMINATED" )

    if isinstance(g, h5py.File) or isinstance(g, h5py.Group) :
        for key,val in print_group_contentdict(g,offset)

def print_group_content(g,offset='    '):
    """Prints content of the file/group/dataset iteratively, starting from the sub-groups of g"""
    for key,val in dict(g).iteritems():
        subg = val
        print offset, key, #,"   ", subg.name #, val, subg.len(), type(subg),
        if   isinstance(subg, h5py.Dataset):
            print " (Dateset)   len =", subg.shape #, subg.dtype
).iteritems() :
         elif   isinstance(subg, h5py.Group): = val
            print offset, key, #," (Group)  ", subg.name len#, ="val, subg.len(), type(subg),
            print_grouphdf5_item_contentstructure(subg, offset + '    ')

if __name__ == "__main__" :
    print_hdf5_file_structure('/reg/d/psdm/XPP/xppcom10/hdf5/xppcom10-r0546.h5')
    sys.exit ( "End of test" )

Example 4: Time-based syncronization of two datasets

...