Пример #1
0
def test_parse_stations_file(tmpdir):
    """
    Tests parsing from a STATIONS file. tmpdir is a pytest fixture.
    """
    filename = os.path.join(tmpdir.dirname, "STATIONS")
    lines = (
        "AAK        II       10.     20.   1645.0    30.0",
        "BBK        AA       20.     30.   1645.0    30.0",
    )
    with open(filename, "wt") as fh:
        fh.write("\n".join(lines))

    receivers = Receiver.parse(filename)

    assert len(receivers) == 2

    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == (
        elliptic_to_geocentric_latitude(10.0),
        20.0,
        "II",
        "AAK",
    )

    rec = receivers[1]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == (
        elliptic_to_geocentric_latitude(20.0),
        30.0,
        "AA",
        "BBK",
    )
Пример #2
0
def test_parse_obspy_objects():
    filename = os.path.join(DATA, "TA.Q56A..BH.xml")
    inv = obspy.read_inventory(filename)

    receivers = Receiver.parse(inv)
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == (
        elliptic_to_geocentric_latitude(39.041),
        -79.1871,
        "TA",
        "Q56A",
    )

    receivers = Receiver.parse(inv[0])
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == (
        elliptic_to_geocentric_latitude(39.041),
        -79.1871,
        "TA",
        "Q56A",
    )

    receivers = Receiver.parse(inv[0][0], network_code="TA")
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == (
        elliptic_to_geocentric_latitude(39.041),
        -79.1871,
        "TA",
        "Q56A",
    )
Пример #3
0
def test_parse_cmtsolutions_file(tmpdir):
    """
    Tests parsing from a CMTSOLUTIONS file.
    """
    filename = os.path.join(str(tmpdir), "CMTSOLUTIONS")
    lines = (" PDE 2011  8 23 17 51  4.60  37.9400  -77.9300   6.0 5.9 5.8 "
             "VIRGINIA", "event name:     201108231751A",
             "time shift:      1.00", "half duration:   1.8000",
             "latitude:       37.9100", "longitude:     -77.9300",
             "depth:          12.0000", "Mrr:       4.710000e+24",
             "Mtt:       3.810000e+22", "Mpp:      -4.740000e+24",
             "Mrt:       3.990000e+23", "Mrp:      -8.050000e+23",
             "Mtp:      -1.230000e+24")
    with open(filename, "wt") as fh:
        fh.write("\n".join(lines))

    # This is the hypocentral time + 1 seconds (the time shift in the
    # CMTSOLUTION file).
    origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 5.6)

    src = Source.parse(filename)
    src_params = np.array([
        src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt,
        src.m_pp, src.m_rt, src.m_rp, src.m_tp
    ],
                          dtype="float64")
    # Latitude will have assumed to be WGS84 and converted to geocentric
    # latitude. The import machinery should do that.
    np.testing.assert_allclose(
        src_params,
        np.array((elliptic_to_geocentric_latitude(37.91), -77.93, 12000,
                  4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16, -1.23E17),
                 dtype="float64"))
    assert src.origin_time == origin_time
Пример #4
0
def _assert_src(src):
    """
    We constantly test the same event in various configurations.
    """
    # Latitude will have been assumed to be WGS84 and converted to geocentric!
    assert (
        src.latitude,
        src.longitude,
        src.depth_in_m,
        src.m_rr,
        src.m_tt,
        src.m_pp,
        src.m_rt,
        src.m_rp,
        src.m_tp,
    ) == (
        elliptic_to_geocentric_latitude(36.97),
        -3.54,
        609800.0,
        -2.16e18,
        5.36e17,
        1.62e18,
        1.3e16,
        3.23e18,
        1.75e18,
    )

    # Also check the time!
    assert src.origin_time == obspy.UTCDateTime("2010-04-11T22:08:12.800000Z")
Пример #5
0
def test_duplicate_receivers():
    """
    Many waveform files contain multiple channels of the same stations. Of
    course these duplicates need to be purged.
    """
    filename = os.path.join(DATA, "example.sac")
    st = obspy.read(filename)
    st += st.copy()
    st[1].stats.channel = "LHZ"

    receivers = Receiver.parse(st)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (
        round(rec.latitude, 3),
        round(rec.longitude, 3),
        rec.network,
        rec.station,
    ) == (
        round(elliptic_to_geocentric_latitude(34.94598), 3),
        round(-106.45713, 3),
        "IU",
        "ANMO",
    )
Пример #6
0
def test_parse_StationXML():
    filename = os.path.join(DATA, "TA.Q56A..BH.xml")
    receivers = Receiver.parse(filename)

    assert len(receivers) == 1
    rec = receivers[0]

    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
        (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
Пример #7
0
def test_parse_stationxml():
    filename = os.path.join(DATA, "TA.Q56A..BH.xml")
    receivers = Receiver.parse(filename)

    assert len(receivers) == 1
    rec = receivers[0]

    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
        (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
Пример #8
0
def test_parse_sac_files():
    filename = os.path.join(DATA, "example.sac")
    receivers = Receiver.parse(filename)

    assert len(receivers) == 1
    rec = receivers[0]
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
        (round(elliptic_to_geocentric_latitude(34.94598), 3),
         round(-106.45713, 3), 'IU', 'ANMO')
Пример #9
0
def test_parse_sac_files():
    filename = os.path.join(DATA, "example.sac")
    receivers = Receiver.parse(filename)

    assert len(receivers) == 1
    rec = receivers[0]
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
        (round(elliptic_to_geocentric_latitude(34.94598), 3),
         round(-106.45713, 3), 'IU', 'ANMO')
Пример #10
0
def test_dataless_seed_files():
    filename = os.path.join(DATA, "dataless.seed.BW_FURT")
    receivers = Receiver.parse(filename)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(48.162899), 3),
            round(11.2752, 3), 'BW', 'FURT')
Пример #11
0
def test_dataless_seed_files():
    filename = os.path.join(DATA, "dataless.seed.BW_FURT")
    receivers = Receiver.parse(filename)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(48.162899), 3),
            round(11.2752, 3), 'BW', 'FURT')
Пример #12
0
def _assert_src(src):
    """
    We constantly test the same event in various configurations.
    """
    # Latitude will have been assumed to be WGS84 and converted to geocentric!
    assert (src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt,
            src.m_pp, src.m_rt, src.m_rp, src.m_tp) == \
           (elliptic_to_geocentric_latitude(36.97), -3.54, 609800.0, -2.16E18,
            5.36E17, 1.62E18, 1.3E16, 3.23E18, 1.75E18)

    # Also check the time!
    assert src.origin_time == obspy.UTCDateTime("2010-04-11T22:08:12.800000Z")
Пример #13
0
def test_parse_obspy_objects():
    filename = os.path.join(DATA, "TA.Q56A..BH.xml")
    inv = obspy.read_inventory(filename)

    receivers = Receiver.parse(inv)
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
           (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")

    receivers = Receiver.parse(inv[0])
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
           (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")

    receivers = Receiver.parse(inv[0][0], network_code="TA")
    assert len(receivers) == 1
    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
           (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
Пример #14
0
def test_parse_obspy_waveform_objects():
    filename = os.path.join(DATA, "example.sac")
    st = obspy.read(filename)

    # From stream.
    receivers = Receiver.parse(st)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(34.94598), 3),
            round(-106.45713, 3), 'IU', 'ANMO')

    # From trace.
    receivers = Receiver.parse(st[0])
    assert len(receivers) == 1
    rec = receivers[0]
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(34.94598), 3),
            round(-106.45713, 3), 'IU', 'ANMO')
Пример #15
0
def test_parse_obspy_waveform_objects():
    filename = os.path.join(DATA, "example.sac")
    st = obspy.read(filename)

    # From stream.
    receivers = Receiver.parse(st)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(34.94598), 3),
            round(-106.45713, 3), 'IU', 'ANMO')

    # From trace.
    receivers = Receiver.parse(st[0])
    assert len(receivers) == 1
    rec = receivers[0]
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(34.94598), 3),
            round(-106.45713, 3), 'IU', 'ANMO')
Пример #16
0
def test_parse_CMTSOLUTIONS_file(tmpdir):
    """
    Tests parsing from a CMTSOLUTIONS file.
    """
    filename = os.path.join(str(tmpdir), "CMTSOLUTIONS")
    lines = (
        "PDEW2011  8 23 17 51  4.60  37.9400  -77.9300   6.0 5.9 5.8 VIRGINIA",
        "event name:     201108231751A",
        "time shift:      1.1100",
        "half duration:   1.8000",
        "latitude:       37.9100",
        "longitude:     -77.9300",
        "depth:          12.0000",
        "Mrr:       4.710000e+24",
        "Mtt:       3.810000e+22",
        "Mpp:      -4.740000e+24",
        "Mrt:       3.990000e+23",
        "Mrp:      -8.050000e+23",
        "Mtp:      -1.230000e+24")
    with open(filename, "wt") as fh:
        fh.write("\n".join(lines))

    origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 4.6)

    src = Source.parse(filename)
    src_params = np.array([src.latitude, src.longitude, src.depth_in_m,
                           src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp,
                           src.m_tp], dtype="float64")
    # Latitude will have assumed to be WGS84 and converted to geocentric
    # latitude.
    np.testing.assert_allclose(src_params, np.array(
        (elliptic_to_geocentric_latitude(37.91), -77.93, 12000, 4.71E17,
         3.81E15, -4.74E17, 3.99E16, -8.05E16,
         -1.23E17), dtype="float64"))
    assert src.origin_time == origin_time

    # Write again. Reset latitude beforehand.
    filename = os.path.join(str(tmpdir), "CMTSOLUTIONS2")
    src.latitude = 37.91
    src.write_CMTSOLUTION_file(filename)

    # This time there is no need to convert latitudes. Writing will convert
    # to WGS84 and reading will convert back.
    src = Source.parse(filename)
    src_params = np.array([src.latitude, src.longitude, src.depth_in_m,
                           src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp,
                           src.m_tp], dtype="float64")
    np.testing.assert_allclose(src_params, np.array(
        (37.91, -77.93, 12000, 4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16,
         -1.23E17), dtype="float64"), rtol=1E-5)
    assert src.origin_time == origin_time
Пример #17
0
def test_parse_stations_file(tmpdir):
    """
    Tests parsing from a STATIONS file. tmpdir is a pytest fixture.
    """
    filename = os.path.join(tmpdir.dirname, "STATIONS")
    lines = (
        "AAK        II       10.     20.   1645.0    30.0",
        "BBK        AA       20.     30.   1645.0    30.0"
    )
    with open(filename, "wt") as fh:
        fh.write("\n".join(lines))

    receivers = Receiver.parse(filename)

    assert len(receivers) == 2

    rec = receivers[0]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
           (elliptic_to_geocentric_latitude(10.0), 20.0, "II", "AAK")

    rec = receivers[1]
    assert (rec.latitude, rec.longitude, rec.network, rec.station) == \
           (elliptic_to_geocentric_latitude(20.0), 30.0, "AA", "BBK")
Пример #18
0
def test_duplicate_receivers():
    """
    Many waveform files contain multiple channels of the same stations. Of
    course these duplicates need to be purged.
    """
    filename = os.path.join(DATA, "example.sac")
    st = obspy.read(filename)
    st += st.copy()
    st[1].stats.channel = "LHZ"

    receivers = Receiver.parse(st)
    assert len(receivers) == 1
    rec = receivers[0]
    # Coordinates are assumed to be WGS84 and will be converted to geocentric.
    assert (round(rec.latitude, 3), round(rec.longitude, 3),
            rec.network, rec.station) == \
           (round(elliptic_to_geocentric_latitude(34.94598), 3),
            round(-106.45713, 3), 'IU', 'ANMO')
Пример #19
0
def get_event_information(event_id, filename):
    """
    Return the event information for the given event id as a dictionary.
    """
    filename = os.path.abspath(filename)
    if filename not in CACHE:
        with open(filename, "rt") as fh:
            CACHE[filename] = json.load(fh)
    events = CACHE[filename]

    if event_id not in events:
        raise EventNotFoundError

    event = copy.deepcopy(events[event_id])
    event["origin_time"] = obspy.UTCDateTime(event["origin_time"])
    # Convert latitude to a geocentric latitude.
    event["latitude"] = elliptic_to_geocentric_latitude(event["latitude"])
    return event
Пример #20
0
def get_event_information(event_id, filename):
    """
    Return the event information for the given event id as a dictionary.
    """
    filename = os.path.abspath(filename)
    if filename not in CACHE:
        with open(filename, "rt") as fh:
            CACHE[filename] = json.load(fh)
    events = CACHE[filename]

    if event_id not in events:
        raise EventNotFoundError

    event = copy.deepcopy(events[event_id])
    event["origin_time"] = obspy.UTCDateTime(event["origin_time"])
    # Convert latitude to a geocentric latitude.
    event["latitude"] = elliptic_to_geocentric_latitude(event["latitude"])
    return event
Пример #21
0
def get_coordinates(cursor, networks=(), stations=(), debug=False):
    # Build query
    query = "SELECT * FROM coordinates"

    network_queries = []
    for network in networks:
        if "*" in network or "?" in network:
            network_queries.append("network GLOB '%s'" % network)
        else:
            network_queries.append("network='%s'" % network)

    station_queries = []
    for station in stations:
        if "*" in station or "?" in station:
            station_queries.append("station GLOB '%s'" % station)
        else:
            station_queries.append("station='%s'" % station)

    if network_queries or station_queries:
        query += " WHERE"

    if network_queries:
        query = query + " ({network_queries})".format(
            network_queries=" OR ".join(network_queries))

    if network_queries and station_queries:
        query += " AND"

    if station_queries:
        query = query + " ({station_queries})".format(
            station_queries=" OR ".join(station_queries))

    if debug:
        print("Constructed query: %s" % query)

    # Convert to geocentric coordinates.
    return [{
        "network": i[0],
        "station": i[1],
        "latitude": elliptic_to_geocentric_latitude(i[2]),
        "longitude": i[3]
    } for i in cursor.execute(query).fetchall()]
Пример #22
0
def get_coordinates(cursor, networks=(), stations=(), debug=False):
    # Build query
    query = "SELECT * FROM coordinates"

    network_queries = []
    for network in networks:
        if "*" in network or "?" in network:
            network_queries.append("network GLOB '%s'" % network)
        else:
            network_queries.append("network='%s'" % network)

    station_queries = []
    for station in stations:
        if "*" in station or "?" in station:
            station_queries.append("station GLOB '%s'" % station)
        else:
            station_queries.append("station='%s'" % station)

    if network_queries or station_queries:
        query += " WHERE"

    if network_queries:
        query = query + " ({network_queries})".format(
            network_queries=" OR ".join(network_queries))

    if network_queries and station_queries:
        query += " AND"

    if station_queries:
        query = query + " ({station_queries})".format(
            station_queries=" OR ".join(station_queries))

    if debug:
        print("Constructed query: %s" % query)

    # Convert to geocentric coordinates.
    return [{"network": i[0], "station": i[1],
             "latitude": elliptic_to_geocentric_latitude(i[2]), "longitude": i[3]}
            for i in cursor.execute(query).fetchall()]
Пример #23
0
def test_parse_cmtsolutions_file(tmpdir):
    """
    Tests parsing from a CMTSOLUTIONS file.
    """
    filename = os.path.join(str(tmpdir), "CMTSOLUTIONS")
    lines = (
        " PDE 2011  8 23 17 51  4.60  37.9400  -77.9300   6.0 5.9 5.8 "
        "VIRGINIA",
        "event name:     201108231751A",
        "time shift:      1.00",
        "half duration:   1.8000",
        "latitude:       37.9100",
        "longitude:     -77.9300",
        "depth:          12.0000",
        "Mrr:       4.710000e+24",
        "Mtt:       3.810000e+22",
        "Mpp:      -4.740000e+24",
        "Mrt:       3.990000e+23",
        "Mrp:      -8.050000e+23",
        "Mtp:      -1.230000e+24")
    with open(filename, "wt") as fh:
        fh.write("\n".join(lines))

    # This is the hypocentral time + 1 seconds (the time shift in the
    # CMTSOLUTION file).
    origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 5.6)

    src = Source.parse(filename)
    src_params = np.array([src.latitude, src.longitude, src.depth_in_m,
                           src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp,
                           src.m_tp], dtype="float64")
    # Latitude will have assumed to be WGS84 and converted to geocentric
    # latitude. The import machinery should do that.
    np.testing.assert_allclose(src_params, np.array(
        (elliptic_to_geocentric_latitude(37.91), -77.93, 12000, 4.71E17,
         3.81E15, -4.74E17, 3.99E16, -8.05E16,
         -1.23E17), dtype="float64"))
    assert src.origin_time == origin_time