Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#!/usr/bin/env python

import h5py
import time

#-----------------------------------------------------

def print_time(t_sec, t_nsec):
    """Converts seconds in human-readable time and prints formatted time"""

    tloc = time.localtime(t_sec) # converts sec to the tuple struct_time in local
    print 'Input time :',t_sec,'sec,',  t_nsec,'nsec, '
    print 'Local time :', time.strftime('%Y-%m-%d %H:%M:%S',tloc)

#-----------------------------------------------------

file_name = '/reg/d/psdm/xpp/xpp22510/hdf5/xpp22510-r0100.h5'
file = h5py.File(file_name, 'r') # open read-only

print "EXAMPLE: Get time from the group attributes:"

group = file["/Configure:0000"]
t_sec  = group.attrs.values()[0]
t_nsec = group.attrs.values()[1]
print_time(t_sec, t_nsec)


print "EXAMPLE: Get time from the data record 'time':"

dataset = file['/Configure:0000/Run:0000/CalibCycle:0002/Acqiris::DataDescV1/XppLas.0:Acqiris.0/time']
index = 0
time = dataset[ind]
t_sec  = time[0]
t_nsec = time[1]
print_time(t_sec, t_nsec)

f.close()

#----------------------------------------------------

...

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

def print_hdf5_file_group(g,offset='    'structure(file_name):
    """Prints the inputHDF5 file/group/dataset (g) name and begin iterations on its content structure"""
    printfile "Structure of the",
    if   isinstance(g,h5py.File):    print "'File'",
    elif isinstance(g,h5py.Group):   print "'Group' from file",
    elif isinstance(g,h5py.Dataset): print "'Dataset' from file",
    print g.file,"\n",g.name
    if   isinstance(g,h5py.Dataset): print offset, "(Dateset)   len =", g.shape #, subg.dtype
    else:                            print_group_content(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),
= h5py.File(file_name, 'r') # open read-only
    item = file #["/Configure:0000/EvrData::ConfigV4"]
    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 "'File'",
    elif isinstance(g,h5py.Group):   print "'Group' from file",
    elif isinstance(g,h5py.Dataset): print "'Dataset' from file",
    print g.file,"\n",g.name
    if   isinstance(g,h5py.Dataset): print offset, "(Dateset)   len =", g.shape #, subg.dtype
    else:                         if   isinstance(subg, h5py.Datasetprint_group_content(g,offset)

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

def print_hdf5_file_structure(file_name):
 " (Dateset)   len """Prints the HDF5 file structure"""
=", subg.shape #, subg.dtype
       file =elif h5py.File(file_name, 'r') # open read-only
isinstance(subg, h5py.Group):
            print_group(file)
 " (Group)    file.close()
len =",len(subg)
            print_group_content(subg,offset + '===   EOF ===')

if __name__ == "__main__" :
    print_hdf5_file_structure('/reg/d/psdm/xppXPP/xpp22510xppcom10/hdf5/xpp22510xppcom10-r0100r0546.h5')