Exemple #1
0
def iter_event_processed_data(events,
                              inventory,
                              pin,
                              format,
                              yield_traces=False,
                              pbar=None):
    """Iterator yielding streams or traces which are read from disc."""
    for meta in iter_event_metadata(events, inventory, pbar=pbar):
        meta['channel'] = '???'
        if 'event_time' not in meta and format != 'H5':
            meta['event_time'] = _DummyUTC()
        fname = FNAMES[format].format(root=pin, **meta)
        kwargs = {}
        if format == 'H5':
            meta.pop('channel')
            kwargs['readonly'] = meta
        try:
            stream = read_rf(fname, format, **kwargs)
        except:
            pass
        else:
            if yield_traces:
                for tr in stream:
                    yield tr
            else:
                yield stream
Exemple #2
0
def _read(stats, path, root, format):
    fname = os.path.join(path, FNAMES[format])
    if format == 'H5':
        stats.pop('channel')
        if isinstance(stats['event_time'], _DummyUTC):
            stats.pop('event_time')
    fname = fname.format(root=root, **stats)
    kwargs = {}
    if format == 'H5':
        set_index()
        kwargs['readonly'] = stats
    try:
        return read_rf(fname, format, **kwargs)
    except:
        pass
Exemple #3
0
def _read(stats, path, root, format):
    fname = os.path.join(path, FNAMES[format])
    if format == 'H5':
        stats.pop('channel')
        if isinstance(stats['event_time'], _DummyUTC):
            stats.pop('event_time')
    fname = fname.format(root=root, **stats)
    kwargs = {}
    if format == 'H5':
        set_index()
        kwargs['readonly'] = stats
    try:
        return read_rf(fname, format, **kwargs)
    except:
        pass
Exemple #4
0
 def test_deconvolution_of_stream_Qpeak_position(self):
     # S receiver deconvolution
     from pkg_resources import resource_filename
     fname = resource_filename('rf', 'example/minimal_example_S.tar.gz')
     stream = read_rf(fname)[:3]
     rfstats(stream, phase='S')
     stream.filter('bandpass', freqmin=0.2, freqmax=0.5)
     stream.trim2(10, 120, reftime='starttime')
     stream.rotate('ZNE->LQT')
     # check that maximum in Q component is at 0s (at S onset)
     test_deconvolve_Qpeak(self, stream, 'time')
     test_deconvolve_Qpeak(self, stream, 'freq')
     test_deconvolve_Qpeak(self, stream, 'time', winsrc=(-5, 18, 5))
     test_deconvolve_Qpeak(self, stream, 'freq', winsrc=(-5, 18, 5))
     test_deconvolve_Qpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Qpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
Exemple #5
0
 def test_deconvolution_of_stream_Qpeak_position(self):
     # S receiver deconvolution
     from pkg_resources import resource_filename
     fname = resource_filename('rf', 'example/minimal_example_S.tar.gz')
     stream = read_rf(fname)[:3]
     rfstats(stream, phase='S')
     stream.filter('bandpass', freqmin=0.2, freqmax=0.5)
     stream.trim2(10, 120, reftime='starttime')
     stream.rotate('ZNE->LQT')
     # check that maximum in Q component is at 0s (at S onset)
     test_deconvolve_Qpeak(self, stream, 'time')
     test_deconvolve_Qpeak(self, stream, 'freq')
     test_deconvolve_Qpeak(self, stream, 'time', winsrc=(-5, 18, 5))
     test_deconvolve_Qpeak(self, stream, 'freq', winsrc=(-5, 18, 5))
     test_deconvolve_Qpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Qpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
Exemple #6
0
 def test_deconvolution_of_stream_Lpeak_position(self):
     stream = read_rf()[:3]
     rfstats(stream)
     stream.filter('bandpass', freqmin=0.4, freqmax=1)
     stream.trim2(5, 95, reftime='starttime')
     stream.rotate('ZNE->LQT')
     # check that maximum in L component is at 0s (at P onset)
     test_deconvolve_Lpeak(self, stream, 'time')
     test_deconvolve_Lpeak(self, stream, 'freq')
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
     stream.trim2(5, 70, reftime='starttime')
     test_deconvolve_Lpeak(self, stream, 'time')
     test_deconvolve_Lpeak(self, stream, 'freq')
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-5, 18, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-5, 18, 5))
Exemple #7
0
def minimal_example_Srf():
    """
    Return S receiver functions calculated from example data.
    """
    cache_key = 'minimal_example_Srf'
    if cache_key in __CACHE:
        return __CACHE[cache_key].copy()
    from rf.rfstream import read_rf, rfstats
    fname = resource_filename('rf', 'example/minimal_example_S.tar.gz')
    stream = read_rf(fname)
    rfstats(stream, phase='S')
    stream.filter('bandpass', freqmin=0.2, freqmax=0.5)
    stream.trim2(10, 120, reftime='starttime')
    stream.rf(method='S', winsrc=(-5, 15, 5))
    stream.moveout(phase='Sp')
    stream.ppoints(50, pp_phase='P')
    __CACHE[cache_key] = stream
    return stream.copy()
Exemple #8
0
def minimal_example_rf():
    """
    Return receiver functions calculated from the data returned by read_rf().
    """
    cache_key = 'minimal_example_rf'
    if cache_key in __CACHE:
        return __CACHE[cache_key].copy()
    from rf.rfstream import read_rf, rfstats
    stream = read_rf()
    rfstats(stream)
    stream.filter('bandpass', freqmin=0.5, freqmax=2)
    stream.trim2(10, 110, reftime='starttime')
    stream.rf(winsrc=(-5, 25, 5))
    stream.moveout()
    stream.trim2(-10, 80, reftime='onset')
    stream.ppoints(50)
    __CACHE[cache_key] = stream
    return stream.copy()
Exemple #9
0
 def test_deconvolution_of_stream_Lpeak_position(self):
     stream = read_rf()[:3]
     rfstats(stream)
     stream.filter('bandpass', freqmin=0.4, freqmax=1)
     stream.trim2(5, 95, reftime='starttime')
     stream.rotate('ZNE->LQT')
     # check that maximum in L component is at 0s (at P onset)
     test_deconvolve_Lpeak(self, stream, 'time')
     test_deconvolve_Lpeak(self, stream, 'freq')
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
     stream.trim2(5, 70, reftime='starttime')
     test_deconvolve_Lpeak(self, stream, 'time')
     test_deconvolve_Lpeak(self, stream, 'freq')
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-20, 40, 5))
     test_deconvolve_Lpeak(self, stream, 'time', winsrc=(-5, 18, 5))
     test_deconvolve_Lpeak(self, stream, 'freq', winsrc=(-5, 18, 5))
Exemple #10
0
    def test_deconvolve_custom_function(self):
        def test_func_stream(rsp, src, **kw):
            for r_ in rsp:
                r_.data[:] = 1
                r_.stats.custom = 'X'
            src.data
            return rsp


        stream = read_rf()[:3]
        rfstats(stream)
        stream1 = stream.copy()
        stream1.rf(deconvolve='func', func=test_func_stream)
        for tr in stream1:
            np.testing.assert_equal(tr.data, 1)
            self.assertIn('custom', tr.stats)
        stream1 = stream
        stream1.deconvolve(method='func', func=test_func_stream)
        for tr in stream1:
            np.testing.assert_equal(tr.data, 1)
            self.assertIn('custom', tr.stats)
Exemple #11
0
def iter_event_processed_data(events, inventory, pin, format,
                              yield_traces=False, pbar=None):
    """Iterator yielding streams or traces which are read from disc."""
    for meta in iter_event_metadata(events, inventory, pbar=pbar):
        meta['channel'] = '???'
        if 'event_time' not in meta and format != 'H5':
            meta['event_time'] = _DummyUTC()
        fname = FNAMES[format].format(root=pin, **meta)
        kwargs = {}
        if format == 'H5':
            meta.pop('channel')
            kwargs['readonly'] = meta
        try:
            stream = read_rf(fname, format, **kwargs)
        except Exception:
            pass
        else:
            if yield_traces:
                for tr in stream:
                    yield tr
            else:
                yield stream
Exemple #12
0
def run_commands(command,
                 commands=(),
                 events=None,
                 inventory=None,
                 objects=None,
                 get_waveforms=None,
                 data=None,
                 plugin=None,
                 phase=None,
                 moveout_phase=None,
                 path_in=None,
                 path_out=None,
                 format='Q',
                 newformat=None,
                 **kw):
    """Load files, apply commands and write result files."""
    for opt in kw:
        if opt not in DICT_OPTIONS:
            raise ParseError('Unknown config option: %s' % opt)
    for opt in DICT_OPTIONS:
        default = None if opt == 'boxbins' else {}
        d = kw.setdefault(opt, default)
        if isinstance(d, basestring):
            kw[opt] = json.loads(d)
    if phase is not None:
        kw['options']['phase'] = phase
    if moveout_phase is not None:
        kw['moveout']['phase'] = moveout_phase
    if kw['boxbins'] is not None:
        kw['boxes']['bins'] = np.linspace(*kw['boxbins'])

    try:
        if command == 'calc':
            assert len(commands) < 3
            if len(commands) == 2:
                assert commands[0] != commands[1]
        elif command == 'calc':
            assert len(commands) < 2
    except:
        raise ParseError('calc or moveout command given more than once')

    # Read events and inventory
    try:
        if command in ('stack', 'plot'):
            events = None
        elif command != 'print' or objects[0] == 'events':
            if (not isinstance(events, obspy.Catalog)
                    or not isinstance(events, list) or
                (len(events) == 2 and isinstance(events[0], basestring))):
                if isinstance(events, basestring):
                    format_ = None
                else:
                    events, format_ = events
                events = obspy.read_events(events, format_)
        if command != 'print' or objects[0] == 'stations':
            if not isinstance(inventory, obspy.Inventory):
                if isinstance(inventory, basestring):
                    format_ = None
                else:
                    inventory, format_ = inventory
                inventory = obspy.read_inventory(inventory, format_)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print('cannot read events or stations')
        return
    # Initialize get_waveforms
    if command == 'data':
        try:
            # Initialize get_waveforms
            if get_waveforms is None:
                get_waveforms = init_data(data,
                                          client_options=kw['client_options'],
                                          plugin=plugin)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            print('cannot initalize data')
            return
    # Print command
    if command == 'print':
        if objects[0] == 'events':
            print(events.__str__(True))
        elif objects[0] == 'stations':
            print(inventory)
        else:
            from rf.rfstream import RFStream
            stream = sum((read_rf(fname) for fname in objects), RFStream())
            print(stream.__str__(True))
        return
    # Select appropriate iterator
    if command == 'data':
        iter_ = iter_event_data(events,
                                inventory,
                                get_waveforms,
                                pbar=tqdm(),
                                **kw['options'])
    elif command == 'plot-profile':
        iter_ = _iter_profile(path_in, format)
    else:
        yt = command == 'profile'
        iter_ = iter_event_processed_data(events,
                                          inventory,
                                          path_in,
                                          format,
                                          pbar=tqdm(),
                                          yield_traces=yt)
    # Run all commands
    if command == 'convert':
        for stream in iter_:
            write(stream, path_out, newformat)
    elif command == 'plot':
        for stream in iter_:
            channels = set(tr.stats.channel for tr in stream)
            for ch in channels:
                st2 = stream.select(channel=ch)
                fname = PLOT_FNAMES.format(root=path_out, **st2[0].stats)
                _create_dir(fname)
                st2.sort(['back_azimuth'])
                st2.plot_rf(fname, **kw['plot'])
    elif command == 'plot-profile':
        for stream in iter_:
            channels = set(tr.stats.channel for tr in stream)
            for ch in channels:
                st2 = stream.select(channel=ch)
                fname = PLOT_PROFILE_FNAMES.format(root=path_out,
                                                   **st2[0].stats)
                _create_dir(fname)
                st2.plot_profile(fname, **kw['plot_profile'])
    elif command == 'stack':
        for stream in iter_:
            stack = stream.stack()
            write(stack, path_out, format, type='stack')
    elif command == 'profile':
        from rf.profile import get_profile_boxes, profile
        boxx = get_profile_boxes(**kw['boxes'])
        prof = profile(iter_, boxx, **kw['profile'])
        write(prof, path_out, format, type='profile')
    else:
        commands = [command] + list(commands)
        for stream in iter_:
            for command in commands:
                if command == 'data':
                    pass
                elif command == 'calc':
                    stream.rf(**kw['rf'])
                elif command == 'moveout':
                    stream.moveout(**kw['moveout'])
                else:
                    raise NotImplementedError
            write(stream, path_out, format)
Exemple #13
0
def _iter_profile(pin, format):
    fname = PROFILE_FNAMES[format].format(root=pin, box_pos='*', channel='???')
    yield read_rf(fname)
Exemple #14
0
def run_commands(command, commands=(), events=None, inventory=None,
                 objects=None, get_waveforms=None, data=None, plugin=None,
                 phase=None, moveout_phase=None,
                 path_in=None, path_out=None, format='Q',
                 newformat=None, **kw):
    """Load files, apply commands and write result files."""
    for opt in kw:
        if opt not in DICT_OPTIONS:
            raise ParseError('Unknown config option: %s' % opt)
    for opt in DICT_OPTIONS:
        default = None if opt == 'boxbins' else {}
        d = kw.setdefault(opt, default)
        if isinstance(d, basestring):
            kw[opt] = json.loads(d)
    if phase is not None:
        kw['options']['phase'] = phase
    if moveout_phase is not None:
        kw['moveout']['phase'] = moveout_phase
    if kw['boxbins'] is not None:
        kw['boxes']['bins'] = np.linspace(*kw['boxbins'])

    try:
        if command == 'calc':
            assert len(commands) < 3
            if len(commands) == 2:
                assert commands[0] != commands[1]
        elif command == 'calc':
            assert len(commands) < 2
    except Exception:
        raise ParseError('calc or moveout command given more than once')

    # Read events and inventory
    try:
        if command in ('stack', 'plot'):
            events = None
        elif command != 'print' or objects[0] == 'events':
            if (not isinstance(events, obspy.Catalog) or
                    not isinstance(events, list) or
                    (len(events) == 2 and isinstance(events[0], basestring))):
                if isinstance(events, basestring):
                    format_ = None
                else:
                    events, format_ = events
                events = obspy.read_events(events, format_)
        if command != 'print' or objects[0] == 'stations':
            if not isinstance(inventory, obspy.Inventory):
                if isinstance(inventory, basestring):
                    format_ = None
                else:
                    inventory, format_ = inventory
                inventory = obspy.read_inventory(inventory, format_)
    except Exception:
        print('cannot read events or stations')
        return
    # Initialize get_waveforms
    if command == 'data':
        try:
            # Initialize get_waveforms
            if get_waveforms is None:
                get_waveforms = init_data(
                    data, client_options=kw['client_options'], plugin=plugin)
        except Exception:
            print('cannot initalize data')
            return
    # Print command
    if command == 'print':
        if objects[0] == 'events':
            print(events.__str__(True))
        elif objects[0] == 'stations':
            print(inventory)
        else:
            from rf.rfstream import RFStream
            stream = sum((read_rf(fname) for fname in objects), RFStream())
            print(stream.__str__(True))
        return
    # Select appropriate iterator
    if command == 'data':
        iter_ = iter_event_data(events, inventory, get_waveforms, pbar=tqdm(),
                                **kw['options'])
    elif command == 'plot-profile':
        iter_ = _iter_profile(path_in, format)
    else:
        yt = command == 'profile'
        iter_ = iter_event_processed_data(
            events, inventory, path_in, format, pbar=tqdm(), yield_traces=yt)
    # Run all commands
    if command == 'convert':
        for stream in iter_:
            write(stream, path_out, newformat)
    elif command == 'plot':
        for stream in iter_:
            channels = set(tr.stats.channel for tr in stream)
            for ch in channels:
                st2 = stream.select(channel=ch)
                fname = PLOT_FNAMES.format(root=path_out, **st2[0].stats)
                _create_dir(fname)
                st2.sort(['back_azimuth'])
                st2.plot_rf(fname, **kw['plot'])
    elif command == 'plot-profile':
        for stream in iter_:
            channels = set(tr.stats.channel for tr in stream)
            for ch in channels:
                st2 = stream.select(channel=ch)
                fname = PLOT_PROFILE_FNAMES.format(root=path_out,
                                                   **st2[0].stats)
                _create_dir(fname)
                st2.plot_profile(fname, **kw['plot_profile'])
    elif command == 'stack':
        for stream in iter_:
            stack = stream.stack()
            write(stack, path_out, format, type='stack')
    elif command == 'profile':
        from rf.profile import get_profile_boxes, profile
        boxx = get_profile_boxes(**kw['boxes'])
        prof = profile(iter_, boxx, **kw['profile'])
        write(prof, path_out, format, type='profile')
    else:
        commands = [command] + list(commands)
        for stream in iter_:
            for command in commands:
                if command == 'data':
                    pass
                elif command == 'calc':
                    stream.rf(**kw['rf'])
                elif command == 'moveout':
                    stream.moveout(**kw['moveout'])
                else:
                    raise NotImplementedError
            write(stream, path_out, format)
Exemple #15
0
def _iter_profile(pin, format):
    fname = PROFILE_FNAMES[format].format(root=pin, box_pos='*', channel='???')
    yield read_rf(fname)