Beispiel #1
0
def curve2nrml(job_ctxt, hc):
    """Write a single hazard curve to a NRML file.

    :param job_ctxt: the `JobContext` instance to use.
    :param hc: the :py:class:`openquake.db.models.HazardCurve` instance to dump
    """
    LOG.debug("> curve2nrml")
    rtype = hc.statistic_type if hc.statistic_type is not None else "curve"
    datum = hc.end_branch_label if rtype == "curve" else hc.quantile

    _, path, hc_meta = hcs_meta(job_ctxt, rtype, datum)
    LOG.debug("*> path: '%s'" % path)
    check_target_dir(path)

    writer = hzrd_out.HazardCurveXMLWriter(path)
    hc_data = []
    db_data = hc.hazardcurvedata_set.all()
    for hcd in db_data:
        hc_attrib = {
            "investigationTimeSpan": job_ctxt["INVESTIGATION_TIME"],
            "IMLValues": job_ctxt.imls,
            "IMT": job_ctxt["INTENSITY_MEASURE_TYPE"],
            "PoEValues": hcd.poes
        }
        hc_attrib.update(hc_meta)
        site = shapes.Site(longitude=hcd.location.x, latitude=hcd.location.y)
        hc_data.append((site, hc_attrib))

    writer.serialize(hc_data)
    LOG.debug("< curve2nrml")
Beispiel #2
0
    def test_end_to_end_from_kvs_to_nrml(self):
        # storing in kvs from java side
        self.java_client.set("KEY", ONE_CURVE_MODEL)

        time.sleep(0.3)

        # reading in python side
        nrmls = self.reader.for_nrml("KEY")

        LOG.debug("Nrmls are %s", nrmls)

        # writing result
        writer = hazard_output.HazardCurveXMLWriter(
                os.path.join(helpers.DATA_DIR, TEST_FILE))

        writer.serialize(nrmls)

        # reading and checking
        constraint = shapes.RegionConstraint.from_simple(
                (1.5, 1.5), (2.5, 0.5))

        reader = hazard_parser.NrmlFile(
                os.path.join(helpers.DATA_DIR, TEST_FILE))

        number_of_curves = 0

        data = {shapes.Site(2.0, 1.0): {
            "IMT": "PGA",
            "IDmodel": "FIXED",
            "timeSpanDuration": 50.0,
            "endBranchLabel": "label",
            "IMLValues": [1.0, 2.0, 3.0],
            "Values": [0.1,0.2,0.3]}}

        for nrml_point, nrml_values in reader.filter(constraint):
            number_of_curves += 1

            self.assertTrue(nrml_point in data.keys())
            for key, val in nrml_values.items():
                self.assertEqual(val, data.values()[0][key])

        self.assertEqual(1, number_of_curves)
Beispiel #3
0
    def write_hazardcurve_file(self, curve_keys):
        """Generate a NRML file with hazard curves for a collection of
        hazard curves from KVS, identified through their KVS keys.

        curve_keys is a list of KVS keys of the hazard curves to be
        serialized.

        The hazard curve file can be written
        (1) for a set of hazard curves belonging to the same realization
            (= endBranchLabel) and a set of sites.
        (2) for a mean hazard curve at a set of sites
        (3) for a quantile hazard curve at a set of sites

        Mixing of these three cases is not allowed, i.e., all hazard curves
        from the set of curve_keys have to be either for the same realization,
        mean, or quantile.
        """

        if _is_mean_hazard_curve_key(curve_keys[0]):
            hc_attrib_update = {'statistics': 'mean'}
            filename_part = 'mean'
            curve_mode = 'mean'

        elif _is_quantile_hazard_curve_key(curve_keys[0]):

            # get quantile value from KVS key
            quantile_value = tokens.quantile_value_from_hazard_curve_key(
                curve_keys[0])
            hc_attrib_update = {
                'statistics': 'quantile',
                'quantileValue': quantile_value
            }
            filename_part = "quantile-%.2f" % quantile_value
            curve_mode = 'quantile'

        elif _is_realization_hazard_curve_key(curve_keys[0]):
            realization_reference_str = \
                tokens.realization_value_from_hazard_curve_key(curve_keys[0])
            hc_attrib_update = {'endBranchLabel': realization_reference_str}
            filename_part = realization_reference_str
            curve_mode = 'realization'

        else:
            error_msg = "no valid hazard curve type found in KVS key"
            raise RuntimeError(error_msg)

        nrml_file = "%s-%s.xml" % (HAZARD_CURVE_FILENAME_PREFIX, filename_part)

        nrml_path = os.path.join(self['BASE_PATH'], self['OUTPUT_DIR'],
                                 nrml_file)
        iml_list = [
            float(param)
            for param in self.params['INTENSITY_MEASURE_LEVELS'].split(",")
        ]

        LOG.debug("Generating NRML hazard curve file for mode %s, "\
            "%s hazard curves: %s" % (curve_mode, len(curve_keys), nrml_file))
        LOG.debug("IML: %s" % iml_list)

        xmlwriter = hazard_output.HazardCurveXMLWriter(nrml_path)
        hc_data = []

        for hc_key in curve_keys:

            if curve_mode == 'mean' and not _is_mean_hazard_curve_key(hc_key):
                error_msg = "non-mean hazard curve key found in mean mode"
                raise RuntimeError(error_msg)

            elif curve_mode == 'quantile':
                if not _is_quantile_hazard_curve_key(hc_key):
                    error_msg = "non-quantile hazard curve key found in "\
                                "quantile mode"
                    raise RuntimeError(error_msg)

                elif tokens.quantile_value_from_hazard_curve_key(hc_key) != \
                    quantile_value:
                    error_msg = "quantile value must be the same for all "\
                                "hazard curves in an instance file"
                    raise ValueError(error_msg)

            elif curve_mode == 'realization':
                if not _is_realization_hazard_curve_key(hc_key):
                    error_msg = "non-realization hazard curve key found in "\
                                "realization mode"
                    raise RuntimeError(error_msg)
                elif tokens.realization_value_from_hazard_curve_key(
                        hc_key) != realization_reference_str:
                    error_msg = "realization value must be the same for all "\
                                "hazard curves in an instance file"
                    raise ValueError(error_msg)

            hc = kvs.get_value_json_decoded(hc_key)

            site_obj = shapes.Site(float(hc['site_lon']),
                                   float(hc['site_lat']))

            # use hazard curve ordinate values (PoE) from KVS
            # NOTE(fab): At the moment, the IMLs are stored along with the
            # PoEs in KVS. However, we are using the IML list from config.
            # The IMLs from KVS are ignored. Note that IMLs from KVS are
            # in logarithmic form, but the ones from config are not.
            # The way of storing the HC data in KVS is not very
            # efficient, we should store the abscissae and ordinates
            # separately as lists and not make pairs of them
            curve_poe = []
            for curve_pair in hc['curve']:
                curve_poe.append(float(curve_pair['y']))

            hc_attrib = {
                'investigationTimeSpan': self.params['INVESTIGATION_TIME'],
                'IMLValues': iml_list,
                'IMT': self.params['INTENSITY_MEASURE_TYPE'],
                'PoEValues': curve_poe
            }

            hc_attrib.update(hc_attrib_update)
            hc_data.append((site_obj, hc_attrib))

        xmlwriter.serialize(hc_data)
        return nrml_path
    def _initialize_writer(self, path):
        if os.path.isfile(path):
            os.remove(path)

        self.writer = hazard_output.HazardCurveXMLWriter(path)