def command_check(args): from grond.environment import Environment def setup(parser): parser.add_option( '--target-ids', dest='target_string_ids', metavar='TARGET_IDS', help='process only selected targets. TARGET_IDS is a ' 'comma-separated list of target IDs. Target IDs have the ' 'form SUPERGROUP.GROUP.NETWORK.STATION.LOCATION.CHANNEL.') parser.add_option( '--waveforms', dest='show_waveforms', action='store_true', help='show raw, restituted, projected, and processed waveforms') parser.add_option( '--nrandom', dest='n_random_synthetics', metavar='N', type=int, default=10, help='set number of random synthetics to forward model (default: ' '10). If set to zero, create synthetics for the reference ' 'solution.') parser.add_option( '--save-stations-used', dest='stations_used_path', metavar='FILENAME', help='aggregate all stations used by the setup into a file') parser, options, args = cl_parse('check', args, setup) if len(args) < 1: help_and_die(parser, 'missing arguments') try: env = Environment(args) config = env.get_config() target_string_ids = None if options.target_string_ids: target_string_ids = options.target_string_ids.split(',') grond.check(config, event_names=env.get_selected_event_names(), target_string_ids=target_string_ids, show_waveforms=options.show_waveforms, n_random_synthetics=options.n_random_synthetics, stations_used_path=options.stations_used_path) logger.info(CLIHints('check', config=env.get_config_path())) except grond.GrondError as e: die(str(e))
def make_movie(dirname, xpar_name, ypar_name, movie_filename): env = Environment([dirname]) optimiser = env.get_optimiser() problem = env.get_problem() history = env.get_history() movie_maker = optimiser.get_movie_maker(problem, history, xpar_name, ypar_name, movie_filename) movie_maker.render()
def command_tag(args): def setup(parser): parser.add_option( '-d', '--dir-names', dest='show_dirnames', action='store_true', help='show directory names instead of run names') parser, options, args = cl_parse('tag', args, setup) if len(args) < 2: help_and_die(parser, 'two or more arguments required') action = args.pop(0) if action not in ('add', 'remove', 'list'): help_and_die(parser, 'invalid action: %s' % action) if action in ('add', 'remove'): if len(args) < 2: help_and_die(parser, 'three or more arguments required') tag = args.pop(0) rundirs = args if action == 'list': rundirs = args from grond.environment import Environment errors = False for rundir in rundirs: try: env = Environment([rundir]) if options.show_dirnames: name = rundir else: name = env.get_problem().name info = env.get_run_info() if action == 'add': info.add_tag(tag) env.set_run_info(info) elif action == 'remove': info.remove_tag(tag) env.set_run_info(info) elif action == 'list': print('%-60s : %s' % ( name, ', '.join(info.tags))) except grond.GrondError as e: errors = True logger.error(e) if errors: die('Errors occurred, see log messages above.')
def command_go(args): from grond.environment import Environment def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing run directory') parser.add_option('--preserve', dest='preserve', action='store_true', help='preserve old rundir') parser.add_option( '--status', dest='status', default='state', type='choice', choices=['state', 'quiet'], help='status output selection (choices: state, quiet, default: ' 'state)') parser.add_option( '--parallel', dest='nparallel', type=int, default=1, help='set number of events to process in parallel, ' 'If set to more than one, --status=quiet is implied.') parser, options, args = cl_parse('go', args, setup) try: env = Environment(args) status = options.status if options.nparallel != 1: status = 'quiet' grond.go(env, force=options.force, preserve=options.preserve, status=status, nparallel=options.nparallel) if len(env.get_selected_event_names()) == 1: logger.info(CLIHints('go', rundir=env.get_rundir_path())) except grond.GrondError as e: die(str(e))
def run(self): logger.info('Waiting to follow environment %s...' % self.rundir) env = Environment.discover(self.rundir) if env is None: logger.error('Could not attach to Grond environment.') return self.environment = env self.history = self.environment.get_history() optimiser_fn = op.join(self.rundir, 'optimiser.yaml') self.optimiser = guts.load(filename=optimiser_fn) self.problem = self.history.problem self.niter = self.optimiser.niterations self.starttime = time.time() self.last_update = self.starttime self.history.add_listener(self) with TerminalMonitor(10) as tm: self._tm = tm ii = 0 while True: ii += 1 self.history.update() time.sleep(0.1) if self.sig_terminate.is_set(): break logger.debug('Monitor thread exiting.')
def command_plot(args): import matplotlib matplotlib.use('Agg') from grond.environment import Environment def setup(parser): pass details = '' parser, options, args = cl_parse('plot', args, setup, details) if len(args) not in (2, 3): help_and_die(parser, 'two or three arguments required') env = Environment(args[1:]) from grond import plot if args[0] == 'list': plot_names, plot_doc = zip(*[(pc.name, pc.__doc__) for pc in env.get_plot_classes()]) plot_descs = [doc.split('\n')[0].strip() for doc in plot_doc] left_spaces = max([len(pn) for pn in plot_names]) for name, desc in zip(plot_names, plot_descs): print('{name:<{ls}} - {desc}'.format(ls=left_spaces, name=name, desc=desc)) elif args[0] == 'config': plot_config_collection = plot.get_plot_config_collection(env) print(plot_config_collection) elif args[0] == 'all': plot_names = plot.get_plot_names(env) plot.make_plots(env, plot_names=plot_names) elif op.exists(args[0]): plots = plot.PlotConfigCollection.load(args[0]) plot.make_plots(env, plots) else: plot_names = [name.strip() for name in args[0].split(',')] plot.make_plots(env, plot_names=plot_names)
def make_report(env_args, event_name, conf, update_without_plotting): from grond.environment import Environment from grond.report import report try: env = Environment(env_args) if event_name: env.set_current_event_name(event_name) report(env, conf, update_without_plotting=update_without_plotting, make_index=False, make_archive=False) return True except grond.GrondError as e: logger.error(str(e)) return False
def command_forward(args): from grond.environment import Environment def setup(parser): parser.add_option( '--show', dest='show', metavar='WHAT', default='filtered', choices=('filtered', 'processed'), help='select whether to show only "filtered" or fully "processed" ' '(i.e. tapered) waveforms (default "%default").') parser, options, args = cl_parse('forward', args, setup) if len(args) < 1: help_and_die(parser, 'missing arguments') try: env = Environment(args) grond.forward(env, show=options.show) except grond.GrondError as e: die(str(e))
def command_plot(args): def setup(parser): parser.add_option( '--show', dest='show', action='store_true', help='show plot for interactive inspection') details = '' parser, options, args = cl_parse('plot', args, setup, details) if not options.show: import matplotlib matplotlib.use('Agg') from grond.environment import Environment if len(args) not in (1, 2, 3): help_and_die(parser, '1, 2 or 3 arguments required') if len(args) > 1: env = Environment(args[1:]) else: env = None from grond import plot if args[0] == 'list': def get_doc_title(doc): for ln in doc.split('\n'): ln = ln.strip() if ln != '': ln = ln.strip('.') return ln return 'Undocumented.' if env: plot_classes = env.get_plot_classes() else: plot_classes = plot.get_all_plot_classes() plot_names, plot_doc = zip(*[(pc.name, pc.__doc__) for pc in plot_classes]) plot_descs = [get_doc_title(doc) for doc in plot_doc] left_spaces = max([len(pn) for pn in plot_names]) for name, desc in zip(plot_names, plot_descs): print('{name:<{ls}} - {desc}'.format( ls=left_spaces, name=name, desc=desc)) elif args[0] == 'config': plot_config_collection = plot.get_plot_config_collection(env) print(plot_config_collection) elif args[0] == 'all': if env is None: help_and_die(parser, 'two or three arguments required') plot_names = plot.get_plot_names(env) plot.make_plots(env, plot_names=plot_names, show=options.show) elif op.exists(args[0]): if env is None: help_and_die(parser, 'two or three arguments required') plots = plot.PlotConfigCollection.load(args[0]) plot.make_plots(env, plots, show=options.show) else: if env is None: help_and_die(parser, 'two or three arguments required') plot_names = [name.strip() for name in args[0].split(',')] plot.make_plots(env, plot_names=plot_names, show=options.show)
def command_report(args): import matplotlib matplotlib.use('Agg') from pyrocko import parimap from grond.environment import Environment from grond.report import \ report_index, report_archive, serve_ip, serve_report, read_config, \ write_config, ReportConfig def setup(parser): parser.add_option( '--index-only', dest='index_only', action='store_true', help='create index only') parser.add_option( '--serve', '-s', dest='serve', action='store_true', help='start http service') parser.add_option( '--serve-external', '-S', dest='serve_external', action='store_true', help='shortcut for --serve --host=default --fixed-port') parser.add_option( '--host', dest='host', default='localhost', help='<ip> to start the http server on. Special values for ' '<ip>: "*" binds to all available interfaces, "default" ' 'to default external interface, "localhost" to "127.0.0.1".') parser.add_option( '--port', dest='port', type=int, default=8383, help='set default http server port. Will count up if port is ' 'already in use unless --fixed-port is given.') parser.add_option( '--fixed-port', dest='fixed_port', action='store_true', help='fail if port is already in use') parser.add_option( '--open', '-o', dest='open', action='store_true', help='open report in browser') parser.add_option( '--config', dest='config', metavar='FILE', help='report configuration file to use') parser.add_option( '--write-config', dest='write_config', metavar='FILE', help='write configuration (or default configuration) to FILE') parser.add_option( '--update-without-plotting', dest='update_without_plotting', action='store_true', help='quick-and-dirty update parameter files without plotting') parser.add_option( '--parallel', dest='nparallel', type=int, default=1, help='set number of runs to process in parallel, ' 'If set to more than one, --status=quiet is implied.') parser.add_option( '--threads', dest='nthreads', type=int, default=1, help='set number of threads per process (default: 1).' 'Set to 0 to use all available cores.') parser.add_option( '--no-archive', dest='no_archive', action='store_true', help='don\'t create archive file.') parser, options, args = cl_parse('report', args, setup) s_conf = '' if options.config: try: conf = read_config(options.config) except grond.GrondError as e: die(str(e)) s_conf = ' --config="%s"' % options.config else: from grond import plot conf = ReportConfig( plot_config_collection=plot.get_plot_config_collection()) conf.set_basepath('.') if options.write_config: try: write_config(conf, options.write_config) sys.exit(0) except grond.GrondError as e: die(str(e)) # commandline options that can override config values if options.no_archive: conf.make_archive = False if len(args) == 1 and op.exists(op.join(args[0], 'index.html')): conf.report_base_path = conf.rel_path(args[0]) s_conf = ' %s' % args[0] args = [] report_base_path = conf.expand_path(conf.report_base_path) if options.index_only: report_index(conf) report_archive(conf) args = [] entries_generated = False payload = [] if args and all(op.isdir(rundir) for rundir in args): rundirs = args all_failed = True for rundir in rundirs: payload.append(( [rundir], None, conf, options.update_without_plotting, options.nthreads)) elif args: try: env = Environment(args) for event_name in env.get_selected_event_names(): payload.append(( args, event_name, conf, options.update_without_plotting, options.nthreads)) except grond.GrondError as e: die(str(e)) if payload: entries_generated = [] for result in parimap.parimap( make_report, *zip(*payload), nprocs=options.nparallel): entries_generated.append(result) all_failed = not any(entries_generated) entries_generated = any(entries_generated) if all_failed: die('no report entries generated') report_index(conf) report_archive(conf) if options.serve or options.serve_external: if options.serve_external: host = 'default' else: host = options.host addr = serve_ip(host), options.port serve_report( addr, report_config=conf, fixed_port=options.fixed_port or options.serve_external, open=options.open) elif options.open: import webbrowser url = 'file://%s/index.html' % op.abspath(report_base_path) webbrowser.open(url) else: if not entries_generated and not options.index_only: logger.info('Nothing to do, see: grond report --help') if entries_generated and not (options.serve or options.serve_external): logger.info(CLIHints('report', config=s_conf))
def command_report(args): import matplotlib matplotlib.use('Agg') from grond.environment import Environment from grond.report import \ report, report_index, serve_ip, serve_report, read_config, \ write_config, ReportConfig def setup(parser): parser.add_option('--index-only', dest='index_only', action='store_true', help='create index only') parser.add_option('--serve', '-s', dest='serve', action='store_true', help='start http service') parser.add_option( '--serve-external', '-S', dest='serve_external', action='store_true', help='shortcut for --serve --host=default --fixed-port') parser.add_option( '--host', dest='host', default='localhost', help='<ip> to start the http server on. Special values for ' '<ip>: "*" binds to all available interfaces, "default" ' 'to default external interface, "localhost" to "127.0.0.1".') parser.add_option( '--port', dest='port', type=int, default=8383, help='set default http server port. Will count up if port is ' 'already in use unless --fixed-port is given.') parser.add_option('--fixed-port', dest='fixed_port', action='store_true', help='fail if port is already in use') parser.add_option('--open', '-o', dest='open', action='store_true', help='open report in browser') parser.add_option('--config', dest='config', help='report configuration file to use') parser.add_option( '--write-config', dest='write_config', metavar='FILE', help='write configuration (or default configuration) to FILE') parser.add_option( '--update-without-plotting', dest='update_without_plotting', action='store_true', help='quick-and-dirty update parameter files without plotting') parser, options, args = cl_parse('report', args, setup) s_conf = '' if options.config: try: conf = read_config(options.config) except grond.GrondError as e: die(str(e)) s_conf = ' --config="%s"' % options.config else: conf = ReportConfig() conf.set_basepath('.') if options.write_config: try: write_config(conf, options.write_config) sys.exit(0) except grond.GrondError as e: die(str(e)) if len(args) == 1 and op.exists(op.join(args[0], 'index.html')): conf.reports_base_path = conf.rel_path(args[0]) s_conf = ' %s' % args[0] args = [] reports_base_path = conf.expand_path(conf.reports_base_path) if options.index_only: report_index(conf) args = [] reports_generated = False if args and all(op.isdir(rundir) for rundir in args): rundirs = args all_failed = True for rundir in rundirs: try: env = Environment([rundir]) report(env, conf, update_without_plotting=options.update_without_plotting) all_failed = False reports_generated = True except grond.GrondError as e: logger.error(str(e)) if all_failed: die('no reports generated') elif args: try: env = Environment(args) for event_name in env.get_selected_event_names(): env.set_current_event_name(event_name) report(env, conf, update_without_plotting=options.update_without_plotting) reports_generated = True except grond.GrondError as e: die(str(e)) if options.serve or options.serve_external: if options.serve_external: host = 'default' else: host = options.host addr = serve_ip(host), options.port serve_report(addr, report_config=conf, fixed_port=options.fixed_port or options.serve_external, open=options.open) elif options.open: import webbrowser url = 'file://%s/index.html' % op.abspath(reports_base_path) webbrowser.open(url) else: if not reports_generated and not options.index_only: logger.info('nothing to do, see: grond report --help') if reports_generated and not (options.serve or options.serve_external): logger.info(CLIHints('report', config=s_conf))