def dataset2trace(dataset, headonly=False): """Load trace from dataset.""" stats = AttribDict(dataset.attrs) for key, val in stats.items(): # decode bytes to utf-8 string for py3 if isinstance(val, bytes): stats[key] = val = val.decode('utf-8') if _is_utc(val): stats[key] = UTC(val) elif key == 'processing': # this block is only necessary for files written with old obspyh5 # versions (< 0.5.0) stats[key] = json.loads(val) jsondata = stats.pop('_json', None) if jsondata is not None: for k, v in json.loads(jsondata).items(): stats[k] = v if headonly: stats['npts'] = len(dataset) trace = Trace(header=stats) else: trace = Trace(data=dataset[...], header=stats) return trace
def test_write_with_extra_tags_and_read(self): """ Tests that a QuakeML file with additional custom "extra" tags gets written correctly and that when reading it again the extra tags are parsed correctly. """ filename = os.path.join(self.path, "quakeml_1.2_origin.xml") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") cat = _read_quakeml(filename) self.assertEqual(len(w), 0) # add some custom tags to first event: # - tag with explicit namespace but no explicit ns abbreviation # - tag without explicit namespace (gets obspy default ns) # - tag with explicit namespace and namespace abbreviation my_extra = AttribDict( {'public': {'value': False, 'namespace': 'http://some-page.de/xmlns/1.0', 'attrib': {'some_attrib': 'some_value', 'another_attrib': 'another_value'}}, 'custom': {'value': 'True', 'namespace': 'http://test.org/xmlns/0.1'}, 'new_tag': {'value': 1234, 'namespace': 'http://test.org/xmlns/0.1'}, 'tX': {'value': UTCDateTime('2013-01-02T13:12:14.600000Z'), 'namespace': 'http://test.org/xmlns/0.1'}, 'dataid': {'namespace': 'http://anss.org/xmlns/catalog/0.1', 'type': 'attribute', 'value': '00999999'}, # some nested tags : 'quantity': {'namespace': 'http://some-page.de/xmlns/1.0', 'attrib': {'attrib1': 'attrib_value1', 'attrib2': 'attrib_value2'}, 'value': { 'my_nested_tag1': { 'namespace': 'http://some-page.de/xmlns/1.0', 'value': 1.23E10}, 'my_nested_tag2': { 'namespace': 'http://some-page.de/xmlns/1.0', 'value': False}}}}) nsmap = {'ns0': 'http://test.org/xmlns/0.1', 'catalog': 'http://anss.org/xmlns/catalog/0.1'} cat[0].extra = my_extra.copy() # insert a pick with an extra field p = Pick() p.extra = {'weight': {'value': 2, 'namespace': 'http://test.org/xmlns/0.1'}} cat[0].picks.append(p) with NamedTemporaryFile() as tf: tmpfile = tf.name # write file cat.write(tmpfile, format='QUAKEML', nsmap=nsmap) # check contents with open(tmpfile, 'rb') as fh: # enforce reproducible attribute orders through write_c14n obj = etree.fromstring(fh.read()).getroottree() buf = io.BytesIO() obj.write_c14n(buf) buf.seek(0, 0) content = buf.read() # check namespace definitions in root element expected = [b'<q:quakeml', b'xmlns:catalog="http://anss.org/xmlns/catalog/0.1"', b'xmlns:ns0="http://test.org/xmlns/0.1"', b'xmlns:ns1="http://some-page.de/xmlns/1.0"', b'xmlns:q="http://quakeml.org/xmlns/quakeml/1.2"', b'xmlns="http://quakeml.org/xmlns/bed/1.2"'] for line in expected: self.assertIn(line, content) # check additional tags expected = [ b'<ns0:custom>True</ns0:custom>', b'<ns0:new_tag>1234</ns0:new_tag>', b'<ns0:tX>2013-01-02T13:12:14.600000Z</ns0:tX>', b'<ns1:public ' b'another_attrib="another_value" ' b'some_attrib="some_value">false</ns1:public>' ] for line in expected: self.assertIn(line, content) # now, read again to test if it's parsed correctly.. cat = _read_quakeml(tmpfile) # when reading.. # - namespace abbreviations should be disregarded # - we always end up with a namespace definition, even if it was # omitted when originally setting the custom tag # - custom namespace abbreviations should attached to Catalog self.assertTrue(hasattr(cat[0], 'extra')) def _tostr(x): if isinstance(x, bool): if x: return str('true') else: return str('false') elif isinstance(x, AttribDict): for key, value in x.items(): x[key].value = _tostr(value['value']) return x else: return str(x) for key, value in my_extra.items(): my_extra[key]['value'] = _tostr(value['value']) self.assertEqual(cat[0].extra, my_extra) self.assertTrue(hasattr(cat[0].picks[0], 'extra')) self.assertEqual( cat[0].picks[0].extra, {'weight': {'value': '2', 'namespace': 'http://test.org/xmlns/0.1'}}) self.assertTrue(hasattr(cat, 'nsmap')) self.assertEqual(getattr(cat, 'nsmap')['ns0'], nsmap['ns0'])
def test_write_with_extra_tags_and_read(self): """ Tests that a QuakeML file with additional custom "extra" tags gets written correctly and that when reading it again the extra tags are parsed correctly. """ filename = os.path.join(self.path, "quakeml_1.2_origin.xml") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") cat = readQuakeML(filename) self.assertEqual(len(w), 0) # add some custom tags to first event: # - tag with explicit namespace but no explicit ns abbreviation # - tag without explicit namespace (gets obspy default ns) # - tag with explicit namespace and namespace abbreviation my_extra = AttribDict( {'public': {'value': False, 'namespace': r"http://some-page.de/xmlns/1.0", 'attrib': {u"some_attrib": u"some_value", u"another_attrib": u"another_value"}}, 'custom': {'value': u"True", 'namespace': r'http://test.org/xmlns/0.1'}, 'new_tag': {'value': 1234, 'namespace': r"http://test.org/xmlns/0.1"}, 'tX': {'value': UTCDateTime('2013-01-02T13:12:14.600000Z'), 'namespace': r'http://test.org/xmlns/0.1'}, 'dataid': {'namespace': r'http://anss.org/xmlns/catalog/0.1', 'type': 'attribute', 'value': '00999999'}}) nsmap = {"ns0": r"http://test.org/xmlns/0.1", "catalog": r'http://anss.org/xmlns/catalog/0.1'} cat[0].extra = my_extra.copy() # insert a pick with an extra field p = Pick() p.extra = {'weight': {'value': 2, 'namespace': r"http://test.org/xmlns/0.1"}} cat[0].picks.append(p) with NamedTemporaryFile() as tf: tmpfile = tf.name # write file cat.write(tmpfile, format="QUAKEML", nsmap=nsmap) # check contents with open(tmpfile, "r") as fh: content = fh.read() # check namespace definitions in root element expected = ['<q:quakeml', 'xmlns:catalog="http://anss.org/xmlns/catalog/0.1"', 'xmlns:ns0="http://test.org/xmlns/0.1"', 'xmlns:ns1="http://some-page.de/xmlns/1.0"', 'xmlns:q="http://quakeml.org/xmlns/quakeml/1.2"', 'xmlns="http://quakeml.org/xmlns/bed/1.2"'] for line in expected: self.assertTrue(line in content) # check additional tags expected = [ '<ns0:custom>True</ns0:custom>', '<ns0:new_tag>1234</ns0:new_tag>', '<ns0:tX>2013-01-02T13:12:14.600000Z</ns0:tX>', '<ns1:public ' 'another_attrib="another_value" ' 'some_attrib="some_value">false</ns1:public>' ] for lines in expected: self.assertTrue(line in content) # now, read again to test if its parsed correctly.. cat = readQuakeML(tmpfile) # when reading.. # - namespace abbreviations should be disregarded # - we always end up with a namespace definition, even if it was # omitted when originally setting the custom tag # - custom namespace abbreviations should attached to Catalog self.assertTrue(hasattr(cat[0], "extra")) def _tostr(x): if isinstance(x, bool): if x: return str("true") else: return str("false") return str(x) for key, value in my_extra.items(): my_extra[key]['value'] = _tostr(value['value']) self.assertEqual(cat[0].extra, my_extra) self.assertTrue(hasattr(cat[0].picks[0], "extra")) self.assertEqual( cat[0].picks[0].extra, {'weight': {'value': '2', 'namespace': r'http://test.org/xmlns/0.1'}}) self.assertTrue(hasattr(cat, "nsmap")) self.assertTrue(getattr(cat, "nsmap")['ns0'] == nsmap['ns0'])
def test_write_with_extra_tags_and_read(self): """ Tests that a QuakeML file with additional custom "extra" tags gets written correctly and that when reading it again the extra tags are parsed correctly. """ filename = os.path.join(self.path, "quakeml_1.2_origin.xml") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") cat = _read_quakeml(filename) self.assertEqual(len(w), 0) # add some custom tags to first event: # - tag with explicit namespace but no explicit ns abbreviation # - tag without explicit namespace (gets obspy default ns) # - tag with explicit namespace and namespace abbreviation my_extra = AttribDict( { "public": { "value": False, "namespace": "http://some-page.de/xmlns/1.0", "attrib": {"some_attrib": "some_value", "another_attrib": "another_value"}, }, "custom": {"value": "True", "namespace": "http://test.org/xmlns/0.1"}, "new_tag": {"value": 1234, "namespace": "http://test.org/xmlns/0.1"}, "tX": {"value": UTCDateTime("2013-01-02T13:12:14.600000Z"), "namespace": "http://test.org/xmlns/0.1"}, "dataid": {"namespace": "http://anss.org/xmlns/catalog/0.1", "type": "attribute", "value": "00999999"}, # some nested tags : "quantity": { "namespace": "http://some-page.de/xmlns/1.0", "attrib": {"attrib1": "attrib_value1", "attrib2": "attrib_value2"}, "value": { "my_nested_tag1": {"namespace": "http://some-page.de/xmlns/1.0", "value": 1.23e10}, "my_nested_tag2": {"namespace": "http://some-page.de/xmlns/1.0", "value": False}, }, }, } ) nsmap = {"ns0": "http://test.org/xmlns/0.1", "catalog": "http://anss.org/xmlns/catalog/0.1"} cat[0].extra = my_extra.copy() # insert a pick with an extra field p = Pick() p.extra = {"weight": {"value": 2, "namespace": "http://test.org/xmlns/0.1"}} cat[0].picks.append(p) with NamedTemporaryFile() as tf: tmpfile = tf.name # write file cat.write(tmpfile, format="QUAKEML", nsmap=nsmap) # check contents with open(tmpfile, "rb") as fh: # enforce reproducible attribute orders through write_c14n obj = etree.fromstring(fh.read()).getroottree() buf = io.BytesIO() obj.write_c14n(buf) buf.seek(0, 0) content = buf.read() # check namespace definitions in root element expected = [ b"<q:quakeml", b'xmlns:catalog="http://anss.org/xmlns/catalog/0.1"', b'xmlns:ns0="http://test.org/xmlns/0.1"', b'xmlns:ns1="http://some-page.de/xmlns/1.0"', b'xmlns:q="http://quakeml.org/xmlns/quakeml/1.2"', b'xmlns="http://quakeml.org/xmlns/bed/1.2"', ] for line in expected: self.assertIn(line, content) # check additional tags expected = [ b"<ns0:custom>True</ns0:custom>", b"<ns0:new_tag>1234</ns0:new_tag>", b"<ns0:tX>2013-01-02T13:12:14.600000Z</ns0:tX>", b"<ns1:public " b'another_attrib="another_value" ' b'some_attrib="some_value">false</ns1:public>', ] for line in expected: self.assertIn(line, content) # now, read again to test if it's parsed correctly.. cat = _read_quakeml(tmpfile) # when reading.. # - namespace abbreviations should be disregarded # - we always end up with a namespace definition, even if it was # omitted when originally setting the custom tag # - custom namespace abbreviations should attached to Catalog self.assertTrue(hasattr(cat[0], "extra")) def _tostr(x): if isinstance(x, bool): if x: return str("true") else: return str("false") elif isinstance(x, AttribDict): for key, value in x.items(): x[key].value = _tostr(value["value"]) return x else: return str(x) for key, value in my_extra.items(): my_extra[key]["value"] = _tostr(value["value"]) self.assertEqual(cat[0].extra, my_extra) self.assertTrue(hasattr(cat[0].picks[0], "extra")) self.assertEqual(cat[0].picks[0].extra, {"weight": {"value": "2", "namespace": "http://test.org/xmlns/0.1"}}) self.assertTrue(hasattr(cat, "nsmap")) self.assertEqual(getattr(cat, "nsmap")["ns0"], nsmap["ns0"])