def parse(self): """ Read hazard curve data from a GeoJSON source into a :class:`openquake.nrmllib.models.HazardCurveModel`. """ metadata = OrderedDict() with openquake.nrmllib.NRMLFile(self.source) as fh: data = json.load(fh) oqmetadata = data['oqmetadata'] metadata = {} metadata['statistics'] = oqmetadata.get('statistics') metadata['quantile_value'] = oqmetadata.get('quantileValue') metadata['smlt_path'] = oqmetadata.get('sourceModelTreePath') metadata['gsimlt_path'] = oqmetadata.get('gsimTreePath') metadata['imt'] = oqmetadata.get('IMT') metadata['investigation_time'] = oqmetadata.get('investigationTime') metadata['sa_period'] = oqmetadata.get('saPeriod') metadata['sa_damping'] = oqmetadata.get('saDamping') metadata['imls'] = oqmetadata.get('IMLs') features = data['features'] data_iter = [] for feature in features: lon, lat = feature['geometry']['coordinates'] loc = models.Location(lon, lat) curve = models.HazardCurveData(loc, feature['properties']['poEs']) data_iter.append(curve) return models.HazardCurveModel(data_iter=data_iter, **metadata)
def _parse(self, tree): header = OrderedDict() for event, element in tree: if element.tag == self._CURVES_TAG and event == 'start': a = element.attrib header['statistics'] = a.get('statistics') header['quantile_value'] = a.get('quantileValue') header['smlt_path'] = a.get('sourceModelTreePath') header['gsimlt_path'] = a.get('gsimTreePath') header['imt'] = a['IMT'] header['investigation_time'] = a['investigationTime'] header['sa_period'] = a.get('saPeriod') header['sa_damping'] = a.get('saDamping') header['imls'] = map(float, element[0].text.split()) yield header elif element.tag == self._CURVE_TAG and event == 'end': point, poes = element x, y = [float(v) for v in point[0].text.split()] location = models.Location(x, y) poes_array = map(float, poes.text.split()) yield models.HazardCurveData(location, poes_array)
class HazardCurveParserTestCase(unittest.TestCase): EXPECTED_CURVE_1 = models.HazardCurveData(models.Location( -122.5, 37.5), [9.8728e-01, 9.8266e-01, 9.4957e-01]) EXPECTED_CURVE_2 = models.HazardCurveData(models.Location( -123.5, 37.5), [9.8728e-02, 9.8266e-02, 9.4957e-02]) EXPECTED = { 'examples/hazard-curves-pga.xml': [ { 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGA', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': None, 'sa_period': None, 'statistics': None }, EXPECTED_CURVE_1, EXPECTED_CURVE_2, ], 'examples/hazard-curves-sa.xml': [ { 'imls': [0.005, 0.007, 0.0137], 'imt': 'SA', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': '5.0', 'sa_period': '0.025', 'statistics': None }, EXPECTED_CURVE_1, EXPECTED_CURVE_2, ], 'examples/hazard-curves-quantile.xml': [ { 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGD', 'investigation_time': '50.0', 'quantile_value': '0.6', 'sa_damping': None, 'sa_period': None, 'statistics': 'quantile' }, EXPECTED_CURVE_1, EXPECTED_CURVE_2, ], 'examples/hazard-curves-mean.xml': [ { 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGD', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': None, 'sa_period': None, 'statistics': 'mean' }, EXPECTED_CURVE_1, EXPECTED_CURVE_2, ], } def test_parse(self): for curve, expected in self.EXPECTED.iteritems(): parser = parsers.HazardCurveParser(curve) model = parser.parse() got = [{ 'imls': model.imls, 'imt': model.imt, 'investigation_time': model.investigation_time, 'quantile_value': model.quantile_value, 'sa_damping': model.sa_damping, 'sa_period': model.sa_period, 'statistics': model.statistics }] + list(model) equal, err = _utils.deep_eq(expected, got) self.assertTrue(equal, err) 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)
class HazardCurveParserTestCase(unittest.TestCase): EXPECTED_CURVE_1 = models.HazardCurveData(models.Location( -122.5, 37.5), [9.8728e-01, 9.8266e-01, 9.4957e-01]) EXPECTED_CURVE_2 = models.HazardCurveData(models.Location( -123.5, 37.5), [9.8728e-02, 9.8266e-02, 9.4957e-02]) EXPECTED = { 'examples/hazard-curves-pga.xml': models.HazardCurveModel( **{ 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGA', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': None, 'sa_period': None, 'statistics': None, 'gsimlt_path': 'b1_b7', 'smlt_path': 'b1_b2_b3', 'data_iter': iter([EXPECTED_CURVE_1, EXPECTED_CURVE_2]) }), 'examples/hazard-curves-sa.xml': models.HazardCurveModel( **{ 'imls': [0.005, 0.007, 0.0137], 'imt': 'SA', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': '5.0', 'sa_period': '0.025', 'statistics': None, 'gsimlt_path': 'b1_b2', 'smlt_path': 'b1_b2_b4', 'data_iter': iter([EXPECTED_CURVE_1, EXPECTED_CURVE_2]) }), 'examples/hazard-curves-quantile.xml': models.HazardCurveModel( **{ 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGD', 'investigation_time': '50.0', 'quantile_value': '0.6', 'sa_damping': None, 'sa_period': None, 'statistics': 'quantile', 'gsimlt_path': None, 'smlt_path': None, 'data_iter': iter([EXPECTED_CURVE_1, EXPECTED_CURVE_2]) }), 'examples/hazard-curves-mean.xml': models.HazardCurveModel( **{ 'imls': [0.005, 0.007, 0.0137], 'imt': 'PGD', 'investigation_time': '50.0', 'quantile_value': None, 'sa_damping': None, 'sa_period': None, 'statistics': 'mean', 'gsimlt_path': None, 'smlt_path': None, 'data_iter': iter([EXPECTED_CURVE_1, EXPECTED_CURVE_2]) }), } def test_parse(self): for curve, expected in self.EXPECTED.iteritems(): parser = parsers.HazardCurveXMLParser(curve) model = parser.parse() equal, err = _utils.deep_eq(expected, model) self.assertTrue(equal, err) 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(DATADIR, example) hcp = parsers.HazardCurveXMLParser(infile) parsed_model = hcp.parse() _, outfile = tempfile.mkstemp() try: hcw = writers.HazardCurveXMLWriter(outfile, **parsed_model.metadata) hcw.serialize(parsed_model) _utils.assert_xml_equal(infile, outfile) finally: os.unlink(outfile) def test_geojson_parsing(self): # Test geojson parsing by comparing the # parsed values from the xml and geojson parsers. # The xml parser is already well tested, so this should # be sufficient. example_xml = [ 'examples/hazard-curves-pga.xml', 'examples/hazard-curves-sa.xml', 'examples/hazard-curves-quantile.xml', 'examples/hazard-curves-mean.xml', ] example_geojson = [ 'examples/hazard-curves-pga.geojson', 'examples/hazard-curves-sa.geojson', 'examples/hazard-curves-quantile.geojson', 'examples/hazard-curves-mean.geojson', ] for xml, geo in zip(example_xml, example_geojson): xp = parsers.HazardCurveXMLParser(xml) gp = parsers.HazardCurveGeoJSONParser(geo) equal, err = _utils.deep_eq(xp.parse(), gp.parse()) self.assertTrue(equal, err)