import h5py as h5 from h5lib import h5dump, print_attrs f = h5.File('example.h5') print '\nHDF5 file \'example.h5\' just loaded:\n\n%r' % f print '\nWe see that the file has been opened in read-mode, hence the \'mode +r\'...' print '\nThe loaded .h5 file is of type...\n%s' % type(f) print '\nLoaded file is of type \'File\'.' h5dump(f) print '\nStored groups and datasets within .h5 file:\n'.upper() for each_item in f.items(): print each_item print '\nPrinting out all the keys in the imported hdf5 file.\n' for n, each_key in enumerate(f.keys()): print 'Key %d:\t%s' % (n + 1, each_key) print '\n' print_attrs('Example SDS', f['Example SDS']) print_attrs('MonthlyRain', f['MonthlyRain']) print '''\n Here, - we see 'MonthlyRain' is a group belonging to the group 'Vgroup'. - ...also see there are 2 additional members (or subgroups) attached to this group. - the subgroups can be accessed with the .keys() method. \n''' for n, each_key in enumerate(f['MonthlyRain']): print 'Key %d:\t%s' % (n+1, each_key) print '''\n We can check/confirm what object type each group/dataset is, by using the 'type()' function.\n ''' print 'f[\'MonthlyRain\'] is a of type:\n\n%s\n' % type(f['MonthlyRain']) print_attrs('Data Fields Metadata', f['MonthlyRain']['Data Fields']) print '\nHere we see 2 members of the Data Fields group, so we can access \n\ additional fields with the .keys() method.\n' print 'Additional groups/datasets:\n' for n, each_key in enumerate(f['MonthlyRain']['Data Fields']): print 'Key %d:\t%s' % (n+1, each_key) type(f['MonthlyRain']['Data Fields']['TbOceanRain']) print '\n', f['MonthlyRain']['Data Fields']['TbOceanRain'].value