Ejemplo n.º 1
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.º 2
0
def Root(tag='h5rad', attrib={"version": H5RAD_VERSION}):
    """
    This is the root element: an INFO object with the right version
    attribute.

    Arguments:
      string tag: should always be "h5rad"
      dict attrib: should always be the h5rad version

    Returns:
      an INFO object
    """
    return rave_info.INFO(tag=tag, attrib=attrib)
Ejemplo n.º 3
0
def Root(tag='h5rad', attrib={"version": H5RAD_VERSION}):
    """
    This is the root element: an INFO object with the right version
    attribute and with a UNIDATA Conventions attribute.

    Arguments:
      string tag: should always be "h5rad"
      dict attrib: should always be the h5rad version

    Returns:
      an INFO object
    """
    this = rave_info.INFO(tag=tag, attrib=attrib)
    this.set('/Conventions', rave_defines.ODIM_CONVENTIONS)
    return this
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def __init__(self, filename=None, **args):
        """
        Initializes a RAVE object.
        
        Arguments:

        Returns:
        """
        self._fcp = None  # HDF5 file-creation properties

        if filename is not None:
            self.open(filename)

        else:
            import rave_info

            self.info = rave_info.INFO("h5rad", version=H5RAD_VERSION)
            self.data = {}
            self._h5nodes = None  # Only used when reading from HDF5 file
            self.__file__ = None  # Set when reading or writing HDF5 file

            if len(args.keys()):
                self.new(args)