Example #1
0
def gen_sources(src_ids, apply_uncertainties, rupture_mesh_spacing,
                width_of_mfd_bin, area_source_discretization):
    """
    Nhlib source objects generator for a given set of sources.

    Performs lazy loading, converting and processing of sources.

    :param src_ids:
        A list of IDs for :class:`openquake.db.models.ParsedSource` records.
    :param apply_uncertainties:
        A function to be called on each generated source. See
        :meth:`openquake.input.logictree.LogicTreeProcessor.\
parse_source_model_logictree_path`

    For information about the other parameters, see
    :func:`openquake.input.source.nrml_to_nhlib`.
    """
    for src_id in src_ids:
        parsed_source = models.ParsedSource.objects.get(id=src_id)

        nhlib_source = source.nrml_to_nhlib(
            parsed_source.nrml, rupture_mesh_spacing, width_of_mfd_bin,
            area_source_discretization)

        apply_uncertainties(nhlib_source)
        yield nhlib_source
Example #2
0
    def test_serialize(self):
        parser = nrml_parsers.SourceModelParser(MIXED_SRC_MODEL)
        source_model = parser.parse()

        inp = models.Input(
            owner=helpers.default_user(),
            digest='fake',
            path='fake',
            input_type='source',
            size=0
        )
        inp.save()

        db_writer = source_input.SourceDBWriter(
            inp, source_model, MESH_SPACING, BIN_WIDTH, AREA_SRC_DISC
        )
        db_writer.serialize()

        # Check that everything was saved properly.

        # First, check the Input:
        # refresh the record
        [inp] = models.Input.objects.filter(id=inp.id)
        self.assertEquals(source_model.name, inp.name)

        # re-reparse the test file for comparisons:
        nrml_sources = list(
            nrml_parsers.SourceModelParser(MIXED_SRC_MODEL).parse()
        )

        parsed_sources = list(models.ParsedSource.objects.filter(input=inp.id))

        # compare pristine nrml sources to those stored in pickled form in the
        # database (by unpickling them first, of course):
        for i, ns in enumerate(nrml_sources):
            self.assertTrue(*helpers.deep_eq(ns, parsed_sources[i].nrml))

        # now check that the ParsedSource geometry is correct
        # it should be the same as the 'rupture-enclosing' geometry for the
        # nhlib representation of each source
        for i, (ns, ps) in enumerate(zip(nrml_sources, parsed_sources)):
            nhlib_src = source_input.nrml_to_nhlib(
                ns, MESH_SPACING, BIN_WIDTH, AREA_SRC_DISC
            )

            nhlib_poly = nhlib_src.get_rupture_enclosing_polygon()
            # nhlib tests the generation of wkt from a polygon, so we can trust
            # that it is well-formed.

            # Since we save the rupture enclosing polygon as geometry (not wkt)
            # in the database, the WKT we get back from the DB might have
            # slightly different coordinate values (a difference in precision).
            # shapely can help us compare two polygons (generated from wkt)
            # at a specific level of precision (default=6 digits after the
            # decimal point).
            expected_poly = wkt.loads(ps.polygon.wkt)
            actual_poly = wkt.loads(nhlib_poly.wkt)

            self.assertTrue(expected_poly.almost_equals(actual_poly))
Example #3
0
    def test_complex_to_nhlib(self):
        exp = self._expected_complex
        actual = source_input.nrml_to_nhlib(
            self.cmplx, MESH_SPACING, BIN_WIDTH, AREA_SRC_DISC
        )

        eq, msg = helpers.deep_eq(exp, actual)

        self.assertTrue(eq, msg)