Beispiel #1
0
def test_sum_adj_to_base():
    array = np.array([1., 2., 3., 4., 5.])
    adjbase = AdjointSource(
        "cc_traveltime_misfit", 2.0, 1.0, 17, 40, "BHZ",
        adjoint_source=array, network="II", station="AAK",
        location="", starttime=UTCDateTime(1990, 1, 1))

    adj1 = deepcopy(adjbase)
    adj2 = deepcopy(adjbase)
    sa.sum_adj_to_base(adj1, adj2, 0.0)
    adjoint_equal(adj1, adjbase)

    adj2 = AdjointSource(
        "cc_traveltime_misfit", 2.0, 1.0, 40, 100, "BHZ",
        adjoint_source=array, network="II", station="AAK",
        location="00", starttime=UTCDateTime(1990, 1, 1))
    sa.sum_adj_to_base(adj1, adj2, 1.0)
    adj2.component = "BHR"
    with pytest.raises(ValueError):
        sa.sum_adj_to_base(adj1, adj2, 1.0)

    adj1 = deepcopy(adjbase)
    adj2 = AdjointSource(
        "cc_traveltime_misfit", 10.0, 1.0, 40, 100, "BHZ",
        adjoint_source=array, network="II", station="AAK",
        location="", starttime=UTCDateTime(1990, 1, 1))

    sa.sum_adj_to_base(adj1, adj2, 0.1)
    npt.assert_almost_equal(adj1.adjoint_source,
                            adjbase.adjoint_source * 1.1)
    npt.assert_almost_equal(adj1.misfit, 3.0)
    npt.assert_almost_equal(adj1.min_period, 17)
    npt.assert_almost_equal(adj1.max_period, 100)
Beispiel #2
0
def test_dump_adjsrc():
    array = np.array([1., 2., 3., 4., 5.])
    adj = AdjointSource("cc_traveltime_misfit",
                        2.0,
                        1.0,
                        17,
                        40,
                        "BHZ",
                        adjoint_source=array,
                        network="II",
                        station="AAK",
                        location="",
                        starttime=UTCDateTime(1990, 1, 1))
    station_info = {
        "latitude": 1.0,
        "longitude": 2.0,
        "depth_in_m": 3.0,
        "elevation_in_m": 4.0
    }
    adj_array, adj_path, parameters = sa.dump_adjsrc(adj, station_info)

    npt.assert_array_almost_equal(adj_array, array)
    for key in station_info:
        npt.assert_almost_equal(station_info[key], parameters[key])
    assert adj_path == "II_AAK_BHZ"
    npt.assert_almost_equal(parameters["misfit"], 2.0)
    npt.assert_almost_equal(parameters["dt"], 1.0)
    npt.assert_almost_equal(parameters["min_period"], 17.0)
    npt.assert_almost_equal(parameters["max_period"], 40.0)
    assert parameters["adjoint_source_type"] == "cc_traveltime_misfit"
    assert parameters["station_id"] == "II.AAK"
    assert parameters["component"], "BHZ"
    assert UTCDateTime(parameters["starttime"]) == UTCDateTime(1990, 1, 1)
    assert parameters["units"] == "m"
Beispiel #3
0
def load_to_adjsrc(adj, event_time):
    """
    Load from asdf file adjoint source to pyadjoint.AdjointSources
    """
    starttime = event_time + adj.parameters["time_offset"]
    _id = adj.parameters["station_id"]
    nw, sta = _id.split(".")
    comp = adj.parameters["component"]
    loc = adj.parameters["location"]

    new_adj = AdjointSource(adj.parameters["adjoint_source_type"],
                            adj.parameters["misfit"],
                            adj.parameters["dt"],
                            adj.parameters["min_period"],
                            adj.parameters["max_period"],
                            comp,
                            adjoint_source=np.array(adj.data),
                            network=nw,
                            station=sta,
                            location=loc,
                            starttime=starttime)

    station_info = {
        "latitude": adj.parameters["latitude"],
        "longitude": adj.parameters["longitude"],
        "elevation_in_m": adj.parameters["elevation_in_m"],
        "depth_in_m": adj.parameters["depth_in_m"],
        "station": sta,
        "network": nw,
        "location": loc
    }
    return new_adj, station_info
def test_convert_adjs_to_trace():
    array = np.array([1., 2., 3., 4., 5.])
    starttime = UTCDateTime(1990, 1, 1)
    adj = AdjointSource("cc_traveltime_misfit",
                        0,
                        1.0,
                        17,
                        40,
                        "BHZ",
                        adjoint_source=array,
                        network="II",
                        station="AAK",
                        location="",
                        starttime=starttime)

    tr, meta = pa.convert_adj_to_trace(adj)
    npt.assert_allclose(tr.data, array)
    assert tr.stats.starttime == starttime
    npt.assert_almost_equal(tr.stats.delta, 1.0)
    assert tr.id == "II.AAK..BHZ"

    assert meta["adj_src_type"] == "cc_traveltime_misfit"
    npt.assert_almost_equal(meta["misfit"], 0.0)
    npt.assert_almost_equal(meta["min_period"], 17.0)
    npt.assert_almost_equal(meta["max_period"], 40.0)
Beispiel #5
0
def test_check_adj_consistency():
    array = np.zeros(10)
    adj1 = AdjointSource("cc_traveltime_misfit",
                         0,
                         1.0,
                         17,
                         40,
                         "BHZ",
                         adjoint_source=array,
                         network="II",
                         station="AAK",
                         location="",
                         starttime=UTCDateTime(1990, 1, 1))
    adj2 = deepcopy(adj1)
    sa.check_adj_consistency(adj1, adj2)

    adj2 = deepcopy(adj1)
    adj2.component = "HHZ"
    with pytest.raises(ValueError) as errmsg:
        sa.check_adj_consistency(adj1, adj2)

    adj2 = deepcopy(adj1)
    adj2.dt *= 2
    with pytest.raises(ValueError) as errmsg:
        sa.check_adj_consistency(adj1, adj2)
    print(str(errmsg))
    assert "DeltaT of current adjoint source" in str(errmsg.value)

    adj2 = deepcopy(adj1)
    adj2.starttime += 1
    with pytest.raises(ValueError) as errmsg:
        sa.check_adj_consistency(adj1, adj2)
    assert "Start time of current adjoint source" in str(errmsg.value)
Beispiel #6
0
def _convert_trace_to_adj(tr, adj_src_type, minp, maxp):
    """
    Convert Trace to AdjointSource, for internal use only
    """
    adj = AdjointSource(adj_src_type, 0.0, 0.0, minp, maxp, "")
    adj.dt = tr.stats.delta
    adj.adjoint_source = tr.data
    adj.station = tr.stats.station
    adj.network = tr.stats.network
    adj.location = tr.stats.location
    adj.component = tr.stats.channel
    adj.starttime = tr.stats.starttime

    return adj
Beispiel #7
0
def _convert_trace_to_adj(tr, adj_src_type, minp, maxp):
    """
    Convert Trace to AdjointSource, for internal use only
    """
    adj = AdjointSource(adj_src_type, 0.0, 0.0, minp, maxp, "")
    adj.dt = tr.stats.delta
    adj.adjoint_source = tr.data
    adj.station = tr.stats.station
    adj.network = tr.stats.network
    adj.location = tr.stats.location
    adj.component = tr.stats.channel
    adj.starttime = tr.stats.starttime

    return adj
def get_sample_adjsrcs(array, starttime):
    adjz = AdjointSource("cc_traveltime_misfit",
                         0,
                         1.0,
                         17,
                         40,
                         "BHZ",
                         adjoint_source=array,
                         network="II",
                         station="AAK",
                         location="",
                         starttime=starttime)
    adjr = deepcopy(adjz)
    adjr.component = "BHR"
    adjt = deepcopy(adjz)
    adjt.component = "BHT"
    return [adjz, adjr, adjt]
Beispiel #9
0
def test_sum_adj_to_base():
    array = np.array([1., 2., 3., 4., 5.])
    adjbase = AdjointSource("cc_traveltime_misfit",
                            2.0,
                            1.0,
                            17,
                            40,
                            "BHZ",
                            adjoint_source=array,
                            network="II",
                            station="AAK",
                            location="",
                            starttime=UTCDateTime(1990, 1, 1))

    adj1 = deepcopy(adjbase)
    adj2 = deepcopy(adjbase)
    sa.sum_adj_to_base(adj1, adj2, 0.0)
    adjoint_equal(adj1, adjbase)

    adj2 = AdjointSource("cc_traveltime_misfit",
                         2.0,
                         1.0,
                         40,
                         100,
                         "BHZ",
                         adjoint_source=array,
                         network="II",
                         station="AAK",
                         location="00",
                         starttime=UTCDateTime(1990, 1, 1))
    sa.sum_adj_to_base(adj1, adj2, 1.0)
    adj2.component = "BHR"
    with pytest.raises(ValueError):
        sa.sum_adj_to_base(adj1, adj2, 1.0)

    adj1 = deepcopy(adjbase)
    adj2 = AdjointSource("cc_traveltime_misfit",
                         10.0,
                         1.0,
                         40,
                         100,
                         "BHZ",
                         adjoint_source=array,
                         network="II",
                         station="AAK",
                         location="",
                         starttime=UTCDateTime(1990, 1, 1))

    sa.sum_adj_to_base(adj1, adj2, 0.1)
    npt.assert_almost_equal(adj1.adjoint_source, adjbase.adjoint_source * 1.1)
    npt.assert_almost_equal(adj1.misfit, 3.0)
    npt.assert_almost_equal(adj1.min_period, 17)
    npt.assert_almost_equal(adj1.max_period, 100)
def test_convert_trace_to_adj_2():
    array = np.array([1., 2., 3., 4., 5.])
    starttime = UTCDateTime(1990, 1, 1)
    adj = AdjointSource("cc_traveltime_misfit",
                        0,
                        1.0,
                        17,
                        40,
                        "BHZ",
                        adjoint_source=array,
                        network="II",
                        station="AAK",
                        location="",
                        starttime=starttime)

    tr, meta = pa.convert_adj_to_trace(adj)
    adj_new = pa.convert_trace_to_adj(tr, meta)
    assert_adj_same(adj, adj_new)
Beispiel #11
0
def ensemble_fake_adj(stream):
    """
    Ensemble fake adjoint sources from stream, for test purpose.
    """
    adjsrcs = list()
    comps = ["Z", "R", "T"]
    for comp in comps:
        tr = stream.select(channel="*%s" % comp)[0]
        adj = AdjointSource(
            "waveform_misfit", misfit=0.0, dt=tr.stats.delta,
            min_period=50.0, max_period=100.0,
            component=tr.stats.channel,
            adjoint_source=tr.data, network=tr.stats.network,
            station=tr.stats.station, location=tr.stats.location,
            starttime=tr.stats.starttime)
        adjsrcs.append(adj)

    return adjsrcs
Beispiel #12
0
def test_created_weighted_adj():
    array = np.array([1., 2., 3., 4., 5.])
    adj = AdjointSource("cc_traveltime_misfit",
                        2.0,
                        1.0,
                        17,
                        40,
                        "BHZ",
                        adjoint_source=array,
                        network="II",
                        station="AAK",
                        location="",
                        starttime=UTCDateTime(1990, 1, 1))

    new_adj = sa.create_weighted_adj(adj, 1.0)
    adjoint_equal(new_adj, adj)

    new_adj = sa.create_weighted_adj(adj, 0.5)
    npt.assert_array_almost_equal(new_adj.adjoint_source,
                                  adj.adjoint_source * 0.5)
    npt.assert_almost_equal(new_adj.misfit, adj.misfit * 0.5)
    assert new_adj.location == ""
Beispiel #13
0
def convert_trace_to_adj(tr, meta):
    """
    Convert Trace to AdjointSource, for internal use only, with
    meta data information
    """
    minp = meta["min_period"]
    maxp = meta["max_period"]
    adj_src_type = meta["adj_src_type"]
    misfit = meta["misfit"]

    dt = tr.stats.delta
    component = tr.stats.channel
    adj = AdjointSource(adj_src_type, misfit, dt, minp, maxp, component)

    adj.adjoint_source = deepcopy(tr.data)
    adj.station = tr.stats.station
    adj.network = tr.stats.network
    adj.location = tr.stats.location
    adj.starttime = tr.stats.starttime

    return adj
Beispiel #14
0
def ensemble_fake_adj(stream, time_offset=0.0):
    """
    Ensemble fake adjoint sources from stream, for test purpose.
    """

    adjsrc_dict = dict()
    comps = ["Z", "R", "T"]
    nwin_dict = dict()
    for comp in comps:
        tr = stream.select(channel="*%s" % comp)[0]
        adj = AdjointSource("waveform_misfit",
                            misfit=0.0,
                            dt=tr.stats.delta,
                            min_period=50.0,
                            max_period=100.0,
                            component=tr.stats.channel[-1],
                            adjoint_source=tr.data,
                            network=tr.stats.network,
                            station=tr.stats.station)
        adjsrc_dict[tr.id] = adj
        nwin_dict[tr.id] = 1

    return adjsrc_dict, nwin_dict
Beispiel #15
0
def construct_3_component_adjsrc(network="II", station="AAK", location=""):
    array = np.array([1., 2., 3., 4., 5.])
    adjz = AdjointSource("cc_traveltime_misfit",
                         2.0,
                         1.0,
                         40,
                         100,
                         "MXZ",
                         adjoint_source=array,
                         network=network,
                         station=station,
                         location=location,
                         starttime=UTCDateTime(1990, 1, 1))

    adjr = deepcopy(adjz)
    adjr.adjoint_source = 2 * array
    adjr.component = "MXR"

    adjt = deepcopy(adjz)
    adjt.adjoint_source = 3 * array
    adjt.component = "MXT"

    return [adjr, adjt, adjz]
Beispiel #16
0
def test_load_to_adjsrc():
    array = np.array([1., 2., 3., 4., 5.])
    adj = AdjointSource("cc_traveltime_misfit",
                        2.0,
                        1.0,
                        17,
                        40,
                        "BHZ",
                        adjoint_source=array,
                        network="II",
                        station="AAK",
                        location="",
                        starttime=UTCDateTime(1990, 1, 1))
    station_info = {
        "latitude": 1.0,
        "longitude": 2.0,
        "depth_in_m": 3.0,
        "elevation_in_m": 4.0
    }
    adj_array, adj_path, parameters = sa.dump_adjsrc(adj, station_info)

    # ensemble a faked adjoint source from hdf5
    hdf5_adj = namedtuple("HDF5Adj", ['data', 'parameters'])
    hdf5_adj.data = array
    hdf5_adj.parameters = parameters

    # load and check
    loaded_adj, loaded_station_info = sa.load_to_adjsrc(hdf5_adj)

    adjoint_equal(loaded_adj, adj)

    for k in station_info:
        npt.assert_almost_equal(station_info[k], loaded_station_info[k])
    assert loaded_station_info["station"] == "AAK"
    assert loaded_station_info["network"] == "II"
    assert loaded_station_info["location"] == ""
Beispiel #17
0
def _convert_trace_to_adj(tr, meta):
    """
    Convert Trace to AdjointSource, for internal use only, with
    meta data information
    """
    minp = meta["min_period"]
    maxp = meta["max_period"]
    adj_src_type = meta["adj_src_type"]
    misfit = meta["misfit"]

    dt = tr.stats.delta
    component = tr.stats.channel
    adj = AdjointSource(adj_src_type, misfit, dt, minp, maxp, component)

    adj.adjoint_source = tr.data
    adj.station = tr.stats.station
    adj.network = tr.stats.network
    adj.location = tr.stats.location
    adj.starttime = tr.stats.starttime

    return adj