Ejemplo n.º 1
0
 def load_metadata(self, fileobj):
     """
     Parse the metadata file and set the .metadata node and the
     .fieldnames list.
     """
     try:
         self.metadata = node_from_xml(fileobj)
     except XMLSyntaxError as e:
         raise InvalidFile('%s:%s' % (fileobj.name, e))
     try:
         self.fieldnames = converter(self.metadata).get_fields()
     except Exception as e:
         # get_fields can raise different kind of errors for invalid
         # metadata; see for instance `test_could_not_extract_fieldnames`
         raise InvalidFile('%s: could not extract fieldnames: %s' %
                           (fileobj.name, e))
Ejemplo n.º 2
0
def build_node(tables, output=None):
    """
    Build a NRML node from a consistent set of table objects (i.e. all
    tables must have the same metadata tag). If output is a file-like
    object open for writing it also saves the node in NRML format.

    :param tables: list of :class:`openquake.nrmllib.tables.Table` instances
    :param output: a file-like object open for writing or None
    """
    assert len(tables) > 0
    tags = set(r.metadata.tag for r in tables)
    if len(tags) > 1:
        raise ValueError(
            'All tables must have the same tag, found %s instead' % tags)
    node = converter(tables[0].metadata).build_node(tables)
    if output is not None:
        node_to_nrml(node, output)
    return node
Ejemplo n.º 3
0
def convert_nrml_to_flat(fname, outname):
    """
    Convert a NRML file into .csv and .mdata files. Returns the path names
    of the generated files.

    :param fname: path to a NRML file of kind <path>.xml
    :param outfname: output path without extension
    """
    # extract the first node inside the <nrml> tag
    assert fname.endswith('.xml'), fname
    node = node_from_nrml(fname)[0]
    tables = list(converter(node).build_tables())
    suffixes = set(t.suffix for t in tables)
    if len(suffixes) < len(tables):
        raise ValueError('Duplicates in %s' % suffixes)
    tozip = []
    for table in tables:
        tozip.extend(table.save(outname))
    return tozip