Exemple #1
0
    def test_chain_parse_serialize(self):
        # Chain a parser together to with a serializer and test that the
        # produced XML is unchanged.
        for example in ('hazard-curves-mean.xml', 'hazard-curves-pga.xml',
                        'hazard-curves-quantile.xml', 'hazard-curves-sa.xml'):
            infile = os.path.join('tests/data', example)
            hcp = parsers.HazardCurveParser(infile)
            parsed_model = hcp.parse()
            _, outfile = tempfile.mkstemp()
            try:
                hcw = writers.HazardCurveXMLWriter(outfile,
                                                   **parsed_model.__dict__)
                hcw.serialize(parsed_model)

                _utils.assert_xml_equal(infile, outfile)
            finally:
                os.unlink(outfile)
Exemple #2
0
    def test_serialize_quantile(self):
        # Test serialization of qunatile curves.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves statistics="quantile" quantileValue="0.15" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(investigation_time=self.TIME,
                        imt='SA',
                        imls=self.IMLS,
                        sa_period=0.025,
                        sa_damping=5.0,
                        statistics='quantile',
                        quantile_value=0.15)
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Exemple #3
0
    def test_serialize(self):
        # Just a basic serialization test.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves sourceModelTreePath="b1_b2_b4" gsimTreePath="b1_b4_b5" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(investigation_time=self.TIME,
                        imt='SA',
                        imls=self.IMLS,
                        sa_period=0.025,
                        sa_damping=5.0,
                        smlt_path='b1_b2_b4',
                        gsimlt_path='b1_b4_b5')
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Exemple #4
0
def export_hazard_curve_xml(output, target):
    """
    Export the specified hazard curve ``output`` to the ``target``.

    :param output:
        :class:`openquake.engine.db.models.Output` with an `output_type` of
        `hazard_curve`.
    :param target:
        The same ``target`` as :func:`export`.

    :returns:
        The same return value as defined by :func:`export`.
    """
    hc = models.HazardCurve.objects.get(output=output.id)

    hcd = _curve_data(hc)
    metadata = _curve_metadata(output, target)
    haz_calc = output.oq_job.hazard_calculation
    dest = _get_result_export_dest(haz_calc.id, target, hc)
    writers.HazardCurveXMLWriter(dest, **metadata).serialize(hcd)

    return dest