def test_profile(self): dx = np.linspace(-50, 50, 41) boxes = get_profile_boxes((-21, -69.5), 85, dx, width=300) stream = minimal_example_rf() stream.extend(stream[:3]) # to actually stack something profile = stream.select(component='Q').profile(boxes) self.assertEqual(len(profile), 3) str_ = ('Prf profile (Q) | -10.0s - 80.0s | 5.0 Hz, 451 samples | ' 'pos:-6.25km slow:6.40 (Ps moveout)') self.assertEqual(str(profile[0]), str_) test_io_header(self, profile[:1]) self.assertIn('profile(', ' '.join(profile[0].stats.processing)) # test plots profile.plot_profile(top='hist') from rf.imaging import plot_profile_map plot_profile_map(boxes)
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)
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)