Exemplo n.º 1
0
 def save(self, nrmlfile):  # save on current file
     try:
         node = self.full_check()
     except:
         return
     with messagebox(self):
         # save to a temporary file
         with open(nrmlfile + '~', 'w+') as f:
             node_to_nrml(node, f)
         # only if there are no errors rename the file
         os.rename(nrmlfile + '~', nrmlfile)
Exemplo n.º 2
0
    def serialize(self, data):
        """
        Serialize a collection of ground motion fields to XML.

        :param data:
            An iterable of "GMF set" objects.
            Each "GMF set" object should:

            * have an `investigation_time` attribute
            * have an `stochastic_event_set_id` attribute
            * be iterable, yielding a sequence of "GMF" objects

            Each "GMF" object should:

            * have an `imt` attribute
            * have an `sa_period` attribute (only if `imt` is 'SA')
            * have an `sa_damping` attribute (only if `imt` is 'SA')
            * have a `rupture_id` attribute (to indicate which rupture
              contributed to this gmf)
            * be iterable, yielding a sequence of "GMF node" objects

            Each "GMF node" object should have:

            * a `gmv` attribute (to indicate the ground motion value
            * `lon` and `lat` attributes (to indicate the geographical location
              of the ground motion field)
        """
        gmf_set_nodes = []
        for gmf_set in data:
            gmf_set_node = node.Node('gmfSet')
            if gmf_set.investigation_time:
                gmf_set_node['investigationTime'] = str(
                    gmf_set.investigation_time)
            gmf_set_node['stochasticEventSetId'] = str(
                gmf_set.stochastic_event_set_id)
            gmf_set_node.nodes = gen_gmfs(gmf_set)
            gmf_set_nodes.append(gmf_set_node)

        gmf_container = node.Node('gmfCollection')
        gmf_container[SM_TREE_PATH] = self.sm_lt_path
        gmf_container[GSIM_TREE_PATH] = self.gsim_lt_path
        gmf_container.nodes = gmf_set_nodes

        with open(self.dest, 'w') as dest:
            node.node_to_nrml(gmf_container, dest)
Exemplo n.º 3
0
 def convert_to_nrml(self, out_archive=None):
     """
     From CSV files with the given prefix to .xml files; if the output
     directory is not specified, use the input archive to store the output.
     """
     fnames = []
     for man in self._getmanagers():
         with man:
             outname = man.prefix + '.xml'
             if out_archive is None:
                 out = man.archive.open(outname, 'w+')
             else:
                 out = out_archive.open(outname, 'w+')
             with out:
                 node = man.get_tableset().to_node()
                 node_to_nrml(node, out)
             fnames.append(out.name)
     return fnames
Exemplo n.º 4
0
 def convert_to_nrml(self, out_archive=None):
     """
     From CSV files with the given prefix to .xml files; if the output
     directory is not specified, use the input archive to store the output.
     """
     fnames = []
     for man in self._getmanagers():
         with man:
             outname = man.prefix + '.xml'
             if out_archive is None:
                 out = man.archive.open(outname, 'w+')
             else:
                 out = out_archive.open(outname, 'w+')
             with out:
                 node = man.get_tableset().to_node()
                 node_to_nrml(node, out)
             fnames.append(out.name)
     return fnames
Exemplo n.º 5
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
Exemplo n.º 6
0
    def serialize(self, data):
        """
        Serialize a collection of ground motion fields to XML.

        :param data:
            An iterable of "GMF set" objects.
            Each "GMF set" object should:

            * have an `investigation_time` attribute
            * have an `stochastic_event_set_id` attribute
            * be iterable, yielding a sequence of "GMF" objects

            Each "GMF" object should:

            * have an `imt` attribute
            * have an `sa_period` attribute (only if `imt` is 'SA')
            * have an `sa_damping` attribute (only if `imt` is 'SA')
            * have a `rupture_id` attribute (to indicate which rupture
              contributed to this gmf)
            * be iterable, yielding a sequence of "GMF node" objects

            Each "GMF node" object should have:

            * a `gmv` attribute (to indicate the ground motion value
            * `lon` and `lat` attributes (to indicate the geographical location
              of the ground motion field)
        """
        gmf_set_nodes = []
        for gmf_set in data:
            gmf_set_node = node.Node('gmfSet')
            gmf_set_node['investigationTime'] = str(gmf_set.investigation_time)
            gmf_set_node['stochasticEventSetId'] = str(
                gmf_set.stochastic_event_set_id)
            gmf_set_node.nodes = gen_gmfs(gmf_set)
            gmf_set_nodes.append(gmf_set_node)

        gmf_container = node.Node('gmfCollection')
        gmf_container[SM_TREE_PATH] = self.sm_lt_path
        gmf_container[GSIM_TREE_PATH] = self.gsim_lt_path
        gmf_container.nodes = gmf_set_nodes

        with open(self.dest, 'w') as dest:
            node.node_to_nrml(gmf_container, dest)
Exemplo n.º 7
0
    def serialize(self, data):
        """
        Serialize a collection of ground motion fields to XML.

        :param data:
            An iterable of "GMFScenario" objects.

            Each "GMFScenario" object should:

            * have an `imt` attribute
            * have an `sa_period` attribute (only if `imt` is 'SA')
            * have an `sa_damping` attribute (only if `imt` is 'SA')
            * be iterable, yielding a sequence of "GMF node" objects

            Each "GMF node" object should have:

            * an `gmv` attribute (to indicate the ground motion value
            * `lon` and `lat` attributes (to indicate the geographical location
              of the ground motion field
        """
        gmfset = node.Node('gmfSet', {}, nodes=gen_gmfs(data))
        with open(self.dest, 'w') as dest:
            node.node_to_nrml(gmfset, dest)
Exemplo n.º 8
0
    def serialize(self, data):
        """
        Serialize a collection of ground motion fields to XML.

        :param data:
            An iterable of "GMFScenario" objects.

            Each "GMFScenario" object should:

            * have an `imt` attribute
            * have an `sa_period` attribute (only if `imt` is 'SA')
            * have an `sa_damping` attribute (only if `imt` is 'SA')
            * be iterable, yielding a sequence of "GMF node" objects

            Each "GMF node" object should have:

            * an `gmv` attribute (to indicate the ground motion value
            * `lon` and `lat` attributes (to indicate the geographical location
              of the ground motion field
        """
        gmfset = node.Node('gmfSet', {}, nodes=gen_gmfs(data))
        with open(self.dest, 'w') as dest:
            node.node_to_nrml(gmfset, dest)