Commit e1d6cf41 authored by Eva Schiffer's avatar Eva Schiffer
Browse files

better info display for h5 files

parent 791a8d91
...@@ -659,6 +659,7 @@ class h5(object): ...@@ -659,6 +659,7 @@ class h5(object):
""" """
_h5 = None _h5 = None
_path = None _path = None
_var_map = { }
def __init__(self, filename, allowWrite=False): def __init__(self, filename, allowWrite=False):
self.attributeCache = CaseInsensitiveAttributeCache(self) self.attributeCache = CaseInsensitiveAttributeCache(self)
...@@ -670,33 +671,63 @@ class h5(object): ...@@ -670,33 +671,63 @@ class h5(object):
LOG.error('h5py module is not installed and is needed in order to read h5 files') LOG.error('h5py module is not installed and is needed in order to read h5 files')
assert(h5py is not None) assert(h5py is not None)
self._h5 = h5py.File(filename, mode) self._h5 = h5py.File(filename, mode)
self._var_map = h5.get_variables_in_h5(self._h5)
def __call__(self): def __call__(self):
"""
variableList = [ ] Get the list of all the variable names in the file
def testFn (name, obj) : """
#print ('checking name: ' + name)
#print ('object: ' + str(obj)) #variableList = [ ]
#def testFn (name, obj) :
if isinstance(obj, h5py.Dataset) : # #print ('checking name: ' + name)
try : # #print ('object: ' + str(obj))
tempType = obj.dtype # this is required to provoke a type error for closed data sets #
# if isinstance(obj, h5py.Dataset) :
#LOG.debug ('type: ' + str(tempType)) # try :
variableList.append(name) # tempType = obj.dtype # this is required to provoke a type error for closed data sets
except TypeError : #
# #LOG.debug ('type: ' + str(tempType))
# variableList.append(name)
# except TypeError :
# LOG.debug('TypeError prevents the use of variable ' + name
# + '. This variable will be ignored')
#
#self._h5.visititems(testFn)
#
#LOG.debug('variables from visiting h5 file structure: ' + str(variableList))
#
#return variableList
return self._var_map.keys()
@staticmethod
def get_variables_in_h5(file_obj, ):
variableMap = {}
def testFn(name, obj):
# print ('checking name: ' + name)
# print ('object: ' + str(obj))
if isinstance(obj, h5py.Dataset):
try:
tempType = obj.dtype # this is required to provoke a type error for closed data sets
# LOG.debug ('type: ' + str(tempType))
variableMap[name] = obj
except TypeError:
LOG.debug('TypeError prevents the use of variable ' + name LOG.debug('TypeError prevents the use of variable ' + name
+ '. This variable will be ignored') + '. This variable will be ignored')
self._h5.visititems(testFn) file_obj.visititems(testFn)
LOG.debug('variables from visiting h5 file structure: ' + str(variableList)) return variableMap
return variableList # trav gets a variable object from inside a h5 file object; pth is the path and var name
#@staticmethod
@staticmethod #def trav(h5_file_obj, pth):
def trav(h5,pth): # return reduce(lambda x,a: x[a] if a else x, pth.split('/'), h5_file_obj)
return reduce( lambda x,a: x[a] if a else x, pth.split('/'), h5)
# this returns a numpy array with a copy of the full, scaled # this returns a numpy array with a copy of the full, scaled
# data for this variable, if the data type must be changed to allow # data for this variable, if the data type must be changed to allow
...@@ -745,7 +776,8 @@ class h5(object): ...@@ -745,7 +776,8 @@ class h5(object):
return scaled_data_copy return scaled_data_copy
def get_variable_object(self,name): def get_variable_object(self,name):
return h5.trav(self._h5, name) #return h5.trav(self._h5, name)
return self._var_map[name]
def missing_value(self, name): def missing_value(self, name):
...@@ -863,12 +895,27 @@ class h5(object): ...@@ -863,12 +895,27 @@ class h5(object):
returns a string, describing the file in a user readable format. returns a string, describing the file in a user readable format.
""" """
# TODO, this is a temporary implementation to be replaced with more details soon # identify the file by it's path
to_return = "File path: " + self._path + "\n" to_return = "File path: " + self._path + "\n"
# add detailed variables info
to_return += "\tvariables:\n"
temp_vars = self() temp_vars = self()
for var_name in temp_vars : for var_name in temp_vars :
to_return += "\t" + var_name + "\n" v_object = self.get_variable_object(var_name)
to_return += "\t\t" + str(v_object.dtype) + " " + var_name + " " + str(v_object.shape) + "\n"
# todo, handle single value variables
if show_attrs :
temp_attrs = self.get_variable_attributes(var_name, caseInsensitive=False,)
for attr_name in temp_attrs :
to_return += "\t\t\t " + attr_name + " = " + str(temp_attrs[attr_name]) + "\n"
# if appropriate, add global attributes info
if show_attrs:
to_return += "\tglobal attributes: \n"
temp_g_attrs = self.get_global_attributes(caseInsensitive=False, )
for g_attr_name in temp_g_attrs:
to_return += "\t\t" + g_attr_name + " = " + str(temp_g_attrs[g_attr_name]) + "\n"
return to_return return to_return
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment