Calculate Source-Receiver GeometryΒΆ

This simple example demonstrates a fast way to extract the source-receiver geometry from an ASDF file. It assumes that the event_id has been correctly set for each waveform and that these events are part of the global QuakeML file.

 1import pyasdf
 2
 3with pyasdf.ASDFDataSet("./asdf_example.h5", mode="r") as ds:
 4    # Get dictionary of resource_id -> Lat/Lng pairs
 5    events = {
 6        str(e.resource_id): [
 7            (e.preferred_origin() or e.origins[0]).get(i)
 8            for i in ["latitude", "longitude"]
 9        ]
10        for e in ds.events
11    }
12
13    # Loop over all stations.
14    for s in ds.waveforms:
15        try:
16            coords = s.coordinates
17        except pyasdf.ASDFException:
18            continue
19
20        # Get set of all event ids.
21        #
22        # Get set for all event ids - the `get_waveform_attributes()`
23        # method is fairly new. If you version of pyasdf does not yet
24        # have it please update or use:
25        # group = s._WaveformAccessor__hdf5_group
26        # event_ids = list({group[i].attrs.get("event_id", None)
27        #                   for i in s.list()})
28        # event_ids = [i.decode() for i in event_ids if i]
29
30        # Note that this assumes only one event id per waveform.
31        event_ids = set(
32            _i["event_ids"][0]
33            for _i in s.get_waveform_attributes().values()
34            if "event_ids" in _i
35        )
36
37        for e_id in event_ids:
38            if e_id not in events:
39                continue
40            # Do what you want - this will be called once per src/rec pair.
41            print(
42                "%.2f %.2f %.2f %.2f"
43                % (
44                    events[e_id][0],
45                    events[e_id][1],
46                    coords["latitude"],
47                    coords["longitude"],
48                )
49            )