Ejemplo n.º 1
0
    def set(self, path, value):
        """
        Creates and sets the value of a new infoset element. 
        
        Arguments:
          string path: This is given in the infoset format, ie. "/path/name".
                       If the payload already exists, it will be modified with
                       the new value. If not, it will be created. Groups are
                       created automatically.

          value: The value to set. This must be an int, float, string, list
                 of strings, or a numpy array.

        Returns: Nothing if successful, a ValueError exception if the data
                 type is invalid or the payload is illegal.
        """
        from H5radHelper import h5type, findelem, seth5attr, addelem
        from xml.etree.ElementTree import Element

        h5typ = h5type(value)
        if not h5typ:
            raise ValueError("Unsupported type %s" % type(value))
        if self.info is None:
            self.info = Element("h5rad", version=H5RAD_VERSION)
        e = findelem(self.info, path)
        if e is None:
            e = addelem(self.info, path)
        seth5attr(e, self.data, h5typ, path, value)
Ejemplo n.º 2
0
def open_hdf5(filename):
    datadict = {}

    a = _pyhl.read_nodelist(filename)
    a.selectAll()
    a.fetch()
    node_names = a.getNodeNames()

    items = []
    for nodename in node_names.keys():
        b = a.getNode(nodename)
        items.append((nodename, b, b.type()))

    items.sort() # guarantees groups before attributes

    h5rad = rave_info.INFO("h5rad", version=H5RAD_VERSION)

    groupmapping = {"" : h5rad}
    for nodename, node, typ in items:
        index = nodename.rindex("/")
        parentname, tag = nodename[:index], nodename[index+1:]
        # Deal with (ignore) H5IM stubs
        #if tag in ["CLASS", "IMAGE_VERSION"]:
        if os.path.split(parentname)[1] == 'data':
            continue
        e = SubElement(groupmapping[parentname], tag)
        if typ==1:
            groupmapping[nodename] = e # save the nodename to element mapping
        elif typ==0:
            t = h5type(node.data())
            if t == "sequence":
                # convert list to string
                nodes = []
                for n in node.data():
                    node = n.strip()
                    node = remove_nulls(str(node))
                    nodes.append(("'"+node+"'"))
                e.text = ", ".join(nodes)
            else:
                e.text = remove_nulls(str(node.data()))
            if t != "string":
                e.attrib["type"] = t
        elif typ==2:
            datadict[nodename] = node.data()
            e.attrib["type"] = "dataset"
            e.text=nodename            
##             label = string.replace(parentname, "/", "")
##             print parentname, label
##             if label.startswith("profile"):  # relic from 717 ...
##                 label = label + "_" + tag
##             datadict[label] = node.data()
##             e.attrib["type"] = "dataset"
##             e.text=label

    return h5rad, datadict, items
Ejemplo n.º 3
0
def get_metadata(filename):

    a = _pyhl.read_nodelist(filename)
    a.selectMetadata()
    a.fetch()
    node_names = a.getNodeNames()

    items = []
    for nodename in node_names.keys():
        b = a.getNode(nodename)
        items.append((nodename, b, b.type()))

    items.sort() # guarantees groups before attributes

    h5rad = rave_info.INFO("h5rad", version=H5RAD_VERSION)

    groupmapping = {"" : h5rad}
    for nodename, node, typ in items:
        index = nodename.rindex("/")
        parentname, tag = nodename[:index], nodename[index+1:]
        e = SubElement(groupmapping[parentname], tag)
        if typ==1:
            groupmapping[nodename] = e # save the nodename to element mapping
        elif typ==0:
            t = h5type(node.data())
            if t == "sequence":
                # convert list to string
                nodes = []
                for n in node.data():
                    node = n.strip()
                    node = remove_nulls(str(node))
                    nodes.append(("'"+node+"'"))
                e.text = ", ".join(nodes)
            else:
                e.text = remove_nulls(str(node.data()))
            if t != "string":
                e.attrib["type"] = t
        # Skip typ==2, dataset array

    # return only h5rad
    return h5rad