예제 #1
0
파일: grond.py 프로젝트: zhanfeng1986/grond
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))
예제 #2
0
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))
예제 #3
0
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))
예제 #4
0
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))