Exemplo n.º 1
0
def seismogram_as_stream(func, *args, **kwargs):
    """
    This decorator converts all the mspasspy Seismogram objects in user inputs (*args and **kargs)
    to stream objects (defined in Obspy), and execute the func with the converted user inputs. After the execution,
    the stream objects will be converted back by overriding the data and metadata of the origin mspasspy objects.

    :param func: target func
    :param args: extra arguments
    :param kwargs: extra kv arguments
    :return: the output of func
    """
    if is_input_dead(*args, **kwargs):
        return
    converted_args = []
    converted_kwargs = {}
    converted_args_ids = []
    converted_kwargs_keys = []
    converted = False
    for i in range(len(args)):
        if isinstance(args[i], Seismogram):
            converted_args.append(args[i].toStream())
            converted_args_ids.append(i)
            converted = True
        else:
            converted_args.append(args[i])
    for k in kwargs:
        if isinstance(kwargs[k], Seismogram):
            converted_kwargs[k] = kwargs[k].toStream()
            converted_kwargs_keys.append(k)
            converted = True
        else:
            converted_kwargs[k] = kwargs[k]
    res = func(*converted_args, **converted_kwargs)
    if converted:
        # todo save relative time attribute
        # fixme cardinal here
        for i in converted_args_ids:
            seis = Stream2Seismogram(converted_args[i], cardinal=True)
            args[i].data = seis.data
            # metadata copy
            for k in seis.keys():
                args[i][k] = seis[k]
        for k in converted_kwargs_keys:
            seis = Stream2Seismogram(converted_kwargs[k], cardinal=True)
            kwargs[k].data = seis.data
            # metadata copy
            for key in seis.keys():
                kwargs[k][key] = seis[key]
    return res
Exemplo n.º 2
0
def correlation_detector(stream,
                         templates,
                         heights,
                         distance,
                         object_history=False,
                         alg_name="correlation_detector",
                         alg_id=None,
                         dryrun=False,
                         template_times=None,
                         template_magnitudes=None,
                         template_names=None,
                         similarity_func=None,
                         details=None,
                         plot=None,
                         return_type="seismogram",
                         **kwargs):
    tem_list = []
    for template in templates:
        tem_list.append(template.toStream())
    detections, sims = obspy.signal.cross_correlation.correlation_detector(
        stream, tem_list, heights, distance, template_times,
        template_magnitudes, template_names, similarity_func, details, plot,
        **kwargs)
    converted_detections = []
    for detection in detections:
        if return_type == "seismogram":
            converted_detections.append(
                Stream2Seismogram(detection, cardinal=True))
        elif return_type == "timeseries_ensemble":
            converted_detections.append(Stream2TimeSeriesEnsemble(detection))
        else:
            raise TypeError(
                "Only seismogram and timeseries_ensemble types are supported")
    return converted_detections, sims
Exemplo n.º 3
0
def test_Stream2Seismogram():
    # TODO: need to refine the test as well as the behavior of the function.
    # Right now when cardinal is false, azimuth and dip needs to be defined.
    seis = Stream2Seismogram(test_Stream2Seismogram.stream, cardinal=True)
    assert np.isclose(
        np.array(seis.data)[0], test_Stream2Seismogram.stream[0].data).all()
    assert np.isclose(
        np.array(seis.data)[1], test_Stream2Seismogram.stream[1].data).all()
    assert np.isclose(
        np.array(seis.data)[2], test_Stream2Seismogram.stream[2].data).all()
Exemplo n.º 4
0
def correlate_stream_template(stream,
                              template,
                              preserve_history=False,
                              instance=None,
                              dryrun=False,
                              template_time=None,
                              return_type="seismogram",
                              **kwargs):
    res = obspy.signal.cross_correlation.correlate_stream_template(
        stream, template, template_time)
    if return_type == "seismogram":
        return Stream2Seismogram(res, cardinal=True)
    elif return_type == "timeseries_ensemble":
        return Stream2TimeSeriesEnsemble(res)
    else:
        raise TypeError(
            "Only seismogram and timeseries_ensemble types are supported")