Beispiel #1
0
def test_calculate_adjsrc_on_stream_raises_if_syn_is_not_stream():
    obs, _, windows = setup_calculate_adjsrc_on_stream_args()
    config = load_config_multitaper()
    syn = []
    with pytest.raises(ValueError):
        adj.calculate_adjsrc_on_stream(obs, syn, windows, config,
                                       adj_src_type="multitaper_misfit")
Beispiel #2
0
def test_calculate_adjsrc_on_stream_raises_if_windows_is_empty():
    obs, syn, _ = setup_calculate_adjsrc_on_stream_args()
    config = load_config_multitaper()
    windows = None
    ret = adj.calculate_adjsrc_on_stream(obs, syn, windows, config,
                                         adj_src_type="multitaper_misfit")
    assert ret is None
    windows = {}
    ret = adj.calculate_adjsrc_on_stream(obs, syn, windows, config,
                                         adj_src_type="multitaper_misfit")
    assert ret is None
Beispiel #3
0
def test_calculate_adjsrc_on_stream_traveltime_misfit_produces_adjsrc():
    obs, syn, windows = setup_calculate_adjsrc_on_stream_args()
    config = load_config_traveltime()

    adjsrc = adj.calculate_adjsrc_on_stream(
        obs, syn, windows, config, adj_src_type="cc_traveltime_misfit",
        adjoint_src_flag=True, figure_mode=False)
    assert adjsrc
Beispiel #4
0
def measure_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"):

    # 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)

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

    results = {}
    for adj in adjsrcs:
        results[adj.id] = adj.measurement

    return results
Beispiel #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
Beispiel #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
Beispiel #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