def test_process_adjoint():
    array = np.array([1, 2, 3, 4, 5])
    starttime = UTCDateTime(1990, 1, 1)
    adjsrcs = get_sample_adjsrcs(array, starttime)
    final_adjsrcs = pa.process_adjoint(adjsrcs,
                                       interp_flag=False,
                                       sum_over_comp_flag=False,
                                       filter_flag=False,
                                       add_missing_comp_flag=False,
                                       rotate_flag=False)

    assert len(final_adjsrcs) == 3
    for adj1, adj2 in zip(adjsrcs, final_adjsrcs):
        npt.assert_allclose(adj1.adjoint_source, adj2.adjoint_source[::-1])
        assert adj1.id == adj2.id

    adjsrcs = [adjsrcs[0], adjsrcs[1]]
    final_adjsrcs = pa.process_adjoint(adjsrcs,
                                       interp_flag=False,
                                       sum_over_comp_flag=False,
                                       filter_flag=False,
                                       add_missing_comp_flag=True,
                                       rotate_flag=False)
    assert len(final_adjsrcs) == 3
    assert adjsrcs[0].id == final_adjsrcs[0].id
    npt.assert_allclose(adjsrcs[0].adjoint_source,
                        final_adjsrcs[0].adjoint_source[::-1])
    assert final_adjsrcs[1].id == "II.AAK..BHR"
    npt.assert_allclose(final_adjsrcs[1].adjoint_source,
                        adjsrcs[1].adjoint_source[::-1])
    assert final_adjsrcs[2].id == "II.AAK..BHT"
    npt.assert_allclose(final_adjsrcs[2].adjoint_source, np.zeros(len(array)))
Exemple #2
0
def test_process_adjoint():
    array = np.array([1, 2, 3, 4, 5])
    starttime = UTCDateTime(1990, 1, 1)
    adjsrcs = get_sample_adjsrcs(array, starttime)
    final_adjsrcs = pa.process_adjoint(
        adjsrcs, interp_flag=False, sum_over_comp_flag=False,
        filter_flag=False, add_missing_comp_flag=False,
        rotate_flag=False)

    assert len(final_adjsrcs) == 3
    for adj1, adj2 in zip(adjsrcs, final_adjsrcs):
        npt.assert_allclose(adj1.adjoint_source, adj2.adjoint_source[::-1])
        assert adj1.id == adj2.id

    adjsrcs = [adjsrcs[0], adjsrcs[1]]
    final_adjsrcs = pa.process_adjoint(
        adjsrcs, interp_flag=False, sum_over_comp_flag=False,
        filter_flag=False, add_missing_comp_flag=True,
        rotate_flag=False)
    assert len(final_adjsrcs) == 3
    assert adjsrcs[0].id == final_adjsrcs[0].id
    npt.assert_allclose(adjsrcs[0].adjoint_source,
                        final_adjsrcs[0].adjoint_source[::-1])
    assert final_adjsrcs[1].id == "II.AAK..BHR"
    npt.assert_allclose(final_adjsrcs[1].adjoint_source,
                        adjsrcs[1].adjoint_source[::-1])
    assert final_adjsrcs[2].id == "II.AAK..BHT"
    npt.assert_allclose(final_adjsrcs[2].adjoint_source,
                        np.zeros(len(array)))
def test_process_adjoint_2():
    st, meta = prepare_real_adj_data()
    inv = obspy.read_inventory()
    event = obspy.read_events()[0]

    adjs = pa.convert_stream_to_adjs(st, meta)
    starttime = adjs[0].starttime - 20
    delta = adjs[0].dt / 2.0
    npts = 40 + 2 * len(adjs[0].adjoint_source)
    new_adj = pa.process_adjoint(adjs,
                                 interp_flag=True,
                                 interp_starttime=starttime,
                                 interp_delta=delta,
                                 interp_npts=npts,
                                 sum_over_comp_flag=True,
                                 weight_flag=False,
                                 filter_flag=True,
                                 pre_filt=[0.02, 0.025, 0.059, 0.073],
                                 taper_percentage=0.05,
                                 taper_type="hann",
                                 add_missing_comp_flag=True,
                                 rotate_flag=True,
                                 inventory=inv,
                                 event=event)
    assert len(new_adj) == 3
Exemple #4
0
def test_process_adjoint_2():
    st, meta = prepare_real_adj_data()
    inv = obspy.read_inventory()
    event = obspy.read_events()[0]

    adjs = pa.convert_stream_to_adjs(st, meta)
    starttime = adjs[0].starttime - 20
    delta = adjs[0].dt / 2.0
    npts = 40 + 2 * len(adjs[0].adjoint_source)
    new_adj = pa.process_adjoint(
        adjs, interp_flag=True, interp_starttime=starttime,
        interp_delta=delta, interp_npts=npts, sum_over_comp_flag=True,
        weight_flag=False, filter_flag=True,
        pre_filt=[0.02, 0.025, 0.059, 0.073],
        taper_percentage=0.05, taper_type="hann",
        add_missing_comp_flag=True, rotate_flag=True,
        inventory=inv, event=event
    )
    assert len(new_adj) == 3
Exemple #5
0
def func_wrapper(obsd_station_group,
                 synt_station_group,
                 obsd_tag=None,
                 synt_tag=None,
                 event=None,
                 param=None,
                 _verbose=False,
                 figure_mode=False,
                 figure_dir=None):
    """
    combo function, including:
    1) observed data signal processing
    2) synthetic data signal processing
    3) window selection based on a pair of data
    4) adjoint source constructor
    """
    # Make sure everything thats required is there.
    _station_name = obsd_station_group._station_name
    if not hasattr(obsd_station_group, "StationXML"):
        raise ValueError("obsd station group '%s' missing 'StationXML'" %
                         _station_name)
    if not hasattr(synt_station_group, "StationXML"):
        raise ValueError("synt station group '%s' missing 'StationXML'" %
                         _station_name)
    if not hasattr(obsd_station_group, obsd_tag):
        raise ValueError("obsd station group '%s' missing '%s'" %
                         (_station_name, obsd_tag))
    if not hasattr(synt_station_group, synt_tag):
        raise ValueError("synt station group '%s' missing '%s'" %
                         (_station_name, synt_tag))

    param = copy.deepcopy(param)

    obsd_staxml = obsd_station_group.StationXML
    synt_staxml = synt_station_group.StationXML
    observed = getattr(obsd_station_group, obsd_tag)
    synthetic = getattr(synt_station_group, synt_tag)

    # keep a reference for construct adjoint source, which
    # should be same length and dt as raw synt
    _raw_synt_tr = synthetic[0].copy()

    obsd_param = param["proc_obsd_param"]
    new_obsd = process_stream(observed, inventory=obsd_staxml, **obsd_param)

    synt_param = param["proc_synt_param"]
    new_synt = process_stream(synthetic, inventory=synt_staxml, **synt_param)

    window_config = load_window_config(param["window_param"])
    windows = window_on_stream(new_obsd,
                               new_synt,
                               window_config,
                               station=synt_staxml,
                               event=event,
                               figure_mode=figure_mode,
                               figure_dir=figure_dir,
                               _verbose=_verbose)
    if len(windows) == 0:
        # No windows selected
        return

    windows = smart_transform_window(windows)

    adj_config, adj_src_type = load_adjoint_config(param["adjsrc_param"])
    adjsrcs = calculate_adjsrc_on_stream(new_obsd,
                                         new_synt,
                                         windows,
                                         adj_config,
                                         adj_src_type,
                                         figure_mode=figure_mode,
                                         figure_dir=figure_dir,
                                         adjoint_src_flag=True)

    chan_weight_dict = calculate_chan_weight(adjsrcs, windows)

    interp_starttime = _raw_synt_tr.stats.starttime
    interp_delta = _raw_synt_tr.stats.delta
    interp_npts = _raw_synt_tr.stats.npts
    pre_filt = obsd_param["pre_filt"]
    new_adjsrcs = process_adjoint(adjsrcs,
                                  interp_starttime,
                                  interp_delta,
                                  interp_npts,
                                  rotate_flag=True,
                                  inventory=synt_staxml,
                                  event=event,
                                  sum_over_comp_flag=True,
                                  weight_flag=True,
                                  weight_dict=chan_weight_dict,
                                  filter_flag=True,
                                  pre_filt=pre_filt)

    origin = event.preferred_origin() or event.origins[0]
    time_offset = interp_starttime - origin.time
    results = reshape_adj(new_adjsrcs, time_offset, synt_staxml)

    return results
Exemple #6
0
def adjoint_wrapper(obsd_station_group,
                    synt_station_group,
                    config=None,
                    obsd_tag=None,
                    synt_tag=None,
                    windows=None,
                    event=None,
                    adj_src_type="multitaper_misfit",
                    postproc_param=None,
                    figure_mode=False,
                    figure_dir=False,
                    adjoint_src_flag=True):
    """
    Function wrapper for pyasdf.

    :param obsd_station_group: observed station group, which contains
        seismogram(stream) and station information(inventory)
    :param synt_station_group: synthetic station group. Same as
        obsd_station_group
    :param config: config object for adjoint source
    :type config: pyadjoint.Config
    :param obsd_tag: observed seismogram tag, used for extracting the
        seismogram in observed asdf file
    :type obsd_tag: str
    :param synt_tag: synthetic seismogram tag, used for extracting the
        seismogram in synthetic asdf file
    :type synt_tag: str
    :param windows: windows for this station group. Two dimension list.
        The first dimension is different channels, the second dimension
        is windows for this channel, like [[chan1_win1, chan1_win2],
        [chan2_win1,], ...]
    :type windows: list
    :param event: event information
    :type event: obspy.Inventory
    :param adj_src_type: adjoint source type, currently support:
        1) "cc_traveltime_misfit"
        2) "multitaper_misfit"
        3) "waveform_misfit"
    :type adj_src_type: st
    :param adjoint_src_flag: calcualte adjoint source, put this to true.
        If false, only make measurements but no adjoint sources.
    :type adjoint_src_flag: bool
    :param figure_mode: plot figures for adjoint source or not
    :type figure_mode: bool
    :param figure_dir: output figure directory
    :type figure_dir: str
    :return: adjoint sources for pyasdf write out(reshaped)
    """
    # Make sure everything thats required is there.
    if not hasattr(obsd_station_group, obsd_tag):
        print("Missing tag '%s' from obsd_station_group %s. Skipped." %
              (obsd_tag, obsd_station_group._station_name))
        return
    if not hasattr(synt_station_group, synt_tag):
        print("Missing tag '%s' from synt_station_group %s. Skipped." %
              (synt_tag, synt_station_group._station_name))
        return
    if not hasattr(obsd_station_group, "StationXML"):
        print("Missing tag 'STATIONXML' from obsd_station_group %s. Skipped" %
              (obsd_tag, obsd_station_group._station_name))

    try:
        window_sta = windows[obsd_station_group._station_name]
    except:
        return
    # check total number of windows. If total number of
    # window is 0, return None
    nwin_total = 0
    for value in window_sta.itervalues():
        nwin_total += len(value)
    if nwin_total == 0:
        return

    observed = getattr(obsd_station_group, obsd_tag)
    synthetic = getattr(synt_station_group, synt_tag)
    obsd_staxml = getattr(obsd_station_group, "StationXML")

    adjsrcs = calculate_adjsrc_on_stream(observed,
                                         synthetic,
                                         window_sta,
                                         config,
                                         adj_src_type,
                                         figure_mode=figure_mode,
                                         figure_dir=figure_dir,
                                         adjoint_src_flag=adjoint_src_flag)

    if postproc_param["weight_flag"]:
        chan_weight_dict = calculate_chan_weight(adjsrcs, window_sta)
    else:
        chan_weight_dict = None

    origin = event.preferred_origin() or event.origins[0]
    focal = event.preferred_focal_mechanism()
    hdr = focal.moment_tensor.source_time_function.duration / 2.0
    # according to SPECFEM starttime convention
    time_offset = -1.5 * hdr
    starttime = origin.time + time_offset

    new_adjsrcs = process_adjoint(adjsrcs,
                                  interp_starttime=starttime,
                                  inventory=obsd_staxml,
                                  event=event,
                                  weight_dict=chan_weight_dict,
                                  **postproc_param)

    _final = reshape_adj(new_adjsrcs, time_offset, obsd_staxml)

    if _final is None:
        return
    else:
        return _final
Exemple #7
0
def func_wrapper(obsd_station_group, synt_station_group, obsd_tag=None,
                 synt_tag=None, event=None, param=None, _verbose=False,
                 figure_mode=False, figure_dir=None):
    """
    combo function, including:
    1) observed data signal processing
    2) synthetic data signal processing
    3) window selection based on a pair of data
    4) adjoint source constructor
    """
    # Make sure everything thats required is there.
    _station_name = obsd_station_group._station_name
    if not hasattr(obsd_station_group, "StationXML"):
        raise ValueError("obsd station group '%s' missing 'StationXML'"
                         % _station_name)
    if not hasattr(synt_station_group, "StationXML"):
        raise ValueError("synt station group '%s' missing 'StationXML'"
                         % _station_name)
    if not hasattr(obsd_station_group, obsd_tag):
        raise ValueError("obsd station group '%s' missing '%s'"
                         % (_station_name, obsd_tag))
    if not hasattr(synt_station_group, synt_tag):
        raise ValueError("synt station group '%s' missing '%s'"
                         % (_station_name, synt_tag))

    param = copy.deepcopy(param)

    obsd_staxml = obsd_station_group.StationXML
    synt_staxml = synt_station_group.StationXML
    observed = getattr(obsd_station_group, obsd_tag)
    synthetic = getattr(synt_station_group, synt_tag)

    # keep a reference for construct adjoint source, which
    # should be same length and dt as raw synt
    _raw_synt_tr = synthetic[0].copy()

    obsd_param = param["proc_obsd_param"]
    new_obsd = process_stream(observed, inventory=obsd_staxml, **obsd_param)

    synt_param = param["proc_synt_param"]
    new_synt = process_stream(synthetic, inventory=synt_staxml, **synt_param)

    window_config = load_window_config(param["window_param"])
    windows = window_on_stream(new_obsd, new_synt, window_config,
                               station=synt_staxml, event=event,
                               figure_mode=figure_mode, figure_dir=figure_dir,
                               _verbose=_verbose)
    if len(windows) == 0:
        # No windows selected
        return

    windows = smart_transform_window(windows)

    adj_config, adj_src_type = load_adjoint_config(param["adjsrc_param"])
    adjsrcs = calculate_adjsrc_on_stream(
        new_obsd, new_synt, windows, adj_config, adj_src_type,
        figure_mode=figure_mode, figure_dir=figure_dir,
        adjoint_src_flag=True)

    chan_weight_dict = calculate_chan_weight(adjsrcs, windows)

    interp_starttime = _raw_synt_tr.stats.starttime
    interp_delta = _raw_synt_tr.stats.delta
    interp_npts = _raw_synt_tr.stats.npts
    pre_filt = obsd_param["pre_filt"]
    new_adjsrcs = process_adjoint(
        adjsrcs, interp_starttime, interp_delta, interp_npts,
        rotate_flag=True, inventory=synt_staxml, event=event,
        sum_over_comp_flag=True, weight_flag=True,
        weight_dict=chan_weight_dict, filter_flag=True,
        pre_filt=pre_filt)

    origin = event.preferred_origin() or event.origins[0]
    time_offset = interp_starttime - origin.time
    results = reshape_adj(new_adjsrcs, time_offset, synt_staxml)

    return results