Skip to content

Commit

Permalink
Merge pull request #87 from zakv/fix-86
Browse files Browse the repository at this point in the history
Fix 86
  • Loading branch information
philipstarkey committed Nov 8, 2020
2 parents fcfda48 + bcede15 commit eaca856
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lyse/__init__.py
Expand Up @@ -686,15 +686,43 @@ def append_expansion(name, obj):
return expansion_dict

def get_units(self, group=None):
units_dict = {}
"""Get the units of globals.
This method retrieves the values in the "Units" column of runmanager for
this shot. The values are returned in a dictionary where the keys are
the names of globals and the values are the corresponding units.
Args:
group (str, optional): The name of the globals group for which the
units will be retrieved. Globals and units from other globals
groups will not be included in the returned dictionary. If set
to `None` then all globals from all globals groups will be
returned. If `group` is set to a value that isn't the name of a
globals group, then an empty dictionary will be returned, but no
error will be raised. Defaults to `None`.
Returns:
dict: A dictionary in which each key is a string giving the name of
a global, and each value is a string specifying the corresponding
value in the "Units" column of runmanager. An empty dictionary will
be returned if `group` is set to a value that isn't the name of a
globals group.
"""
path = 'globals'
if group is not None:
path = path + '/{group}'.format(group=group)
units = {}
# Define method that when applied to an hdf5 group adds all of its
# globals and units to the units dict.
def append_units(name, obj):
if 'units' in name:
temp_dict = dict(obj.attrs)
for key, val in temp_dict.items():
units_dict[key] = val
with h5py.File(self.h5_path, 'r') as h5_file:
h5_file['globals'].visititems(append_units)
return units_dict
units.update(dict(obj.attrs))
try:
with h5py.File(self.h5_path, 'r') as h5_file:
h5_file[path].visititems(append_units)
except KeyError:
pass
return units

def globals_groups(self):
with h5py.File(self.h5_path, 'r') as h5_file:
Expand Down

0 comments on commit eaca856

Please sign in to comment.