Пример #1
0
def load(io, key, seedid=None, day=None, do='return', prep_kw={},
         fname=None, format=None):
    """
    Load object and do something with it

    :param io: io
    :param key: key of object to load
        (key inside HDF5 file, or one of data, prepdata, stations)
    :param seedid: seed id of a  channel (for data or prepdata)
    :param day: |UTC| object with day (for data or prepdata)
    :param do: specifies what to do with the object, default is ``'return'``
        which simply returns the object, other possible values are
        ``'print'`` -- print object (used by print command),
        ``'load'`` -- load object in IPython session (used by load command),
        ``'export'`` -- export correlations to different file format
        (used by export command)
    :param dict prep_kw: options passed to preprocess (for prepdata only)
    :param fname: file name (for export command)
    :param format: target format (for export command)
    """
    if key == 'stations':
        obj = io['inventory']
    elif key in ('data', 'prepdata'):
        if seedid is None or day is None:
            msg = 'seedid and day must be given for data or prepdata'
            raise ParseError(msg)
        if key == 'prepdata':
            prep_keys = ('remove_response', 'remove_response_options',
                         'demean', 'filter', 'normalization',
                         'time_norm_options', 'spectral_whitening_options',
                         'tolerance_shift',
                         'downsample')
            prep_kw = {k: prep_kw.get(k) for k in prep_keys}
        obj = _load_data(seedid, day, io['data'], io.get('data_format'),
                         key, inventory=io['inventory'], **prep_kw)
    else:
        is_stretch = 't' in _analyze_key(key)
        fname_in = _get_fname(io, key)
        if is_stretch:
            obj = read_dicts(fname_in, key)
            if do == 'print':
                obj = '\n\n'.join(str(o) for o in obj)
        else:
            obj = obspy.read(fname_in, 'H5', group=key, headonly=do == 'print')
            if do == 'print':
                obj = obj.__str__(extended=True)
    if do == 'print':
        print(obj)
    elif do == 'load':
        _start_ipy(obj)
    elif do == 'return':
        return obj
    elif do == 'export':
        print('export', obj)
        if format == 'H5':
            obspyh5.set_index()
        obj.write(fname, format)
        if format == 'H5':
            from yam.io import INDEX
            obspyh5.set_index(INDEX)
    else:
        raise
Пример #2
0
def plot(io, key, plottype=None, seedid=None, day=None, prep_kw={},
         corrid=None, show=False,
         **kwargs):
    """
    Plot everything

    :param io: |io|
    :param key: key of objects to plot, or one of stations, data, prepdata
    :param plottype: plot type to use
        (non default values are ``'vs_dist'`` and ``'wiggle'`` for
        correlation plots, ``'velocity'`` for plots of stretching results)
    :param seedid: seed id of a  channel (for data or prepdata)
    :param day: |UTC| object with day (for data or prepdata)
    :param dict prep_kw: options passed to preprocess (for prepdata only)
    :param corrid: correlation configuration (for prepdata only)
    :param show: show interactive plot
    :param \*\*kwargs: all other kwargs are passed to
        the corresponding plot function in `~yam.imaging` module
    """
    import yam.imaging
    path = io['plot']
    if not os.path.exists(path):
        os.mkdir(path)
    if key in ('stations', 'data', 'prepdata'):
        pt = key
    else:
        is_corr = 't' not in _analyze_key(key)
        if is_corr and plottype == 'vs_dist':
            pt = 'corr_vs_dist'
        elif is_corr and plottype == 'wiggle':
            pt = 'corr_vs_time_wiggle'
        elif is_corr and plottype is None:
            pt = 'corr_vs_time'
        elif not is_corr and plottype is None:
            pt = 'sim_mat'
        elif not is_corr and plottype == 'velocity':
            pt = 'velocity_change'
        else:
            raise ParseError('Combination of key and plottype not supported')

    kw = kwargs.get('plot_%s_options' % pt, {})
    kw.update(kwargs.get('plot_options', {}))
    bname = os.path.join(path, pt)
    if key == 'stations':
        yam.imaging.plot_stations(io['inventory'], bname, **kw)
    elif key in ('data', 'prepdata'):
        data = load(io, key, do='return', seedid=seedid, day=day,
                    prep_kw=prep_kw)
        fname = bname + '_%s_%s' % (seedid, day)
        if key == 'prepdata':
            fname = fname + '_c' + corrid
        yam.imaging.plot_data(data, fname, show=show, **kw)
    else:
        plot_ = getattr(yam.imaging, 'plot_' + pt)
        if pt == 'corr_vs_dist':
            fname2 = _get_fname(io, key)
            stream = obspy.read(fname2, 'H5', group=key)
            fname = bname + '_' + key.replace('/', '_')
            plot_(stream, fname, **kw)
        elif pt == 'velocity_change':
            results = [res for task, res in _iter_h5(io, key)]
            fname = bname + '_' + key.replace('/', '_')
            plot_(results, fname, **kw)
        else:
            for task, res in _iter_h5(io, key):
                fname = bname + task.replace('/', '_')
                plot_(res, fname, **kw)
    if show:
        from matplotlib.pyplot import show
        show()
Пример #3
0
def run2(command,
         io,
         logging=None,
         verbose=0,
         loglevel=3,
         logfile=None,
         key=None,
         keys=None,
         corrid=None,
         stackid=None,
         stretchid=None,
         correlate=None,
         stack=None,
         stretch=None,
         **args):
    """
    Second main function for unpacking arguments

    Initialize logging, load inventory if necessary, load options from
    configuration dictionary into args
    (for correlate, stack and stretch commands) and run the corresponding
    command in `~yam.commands` module. If ``"based_on"`` key is set the
    configuration dictionary will be preloaded with the specified
    configuration.

    :param command: specified subcommand, will call one of
        `~yam.commands.start_correlate()`,
        `~yam.commands.start_stack()`,
        `~yam.commands.start_stretch()`,
        `~yam.commands.info()`,
        `~yam.commands.load()`,
        `~yam.commands.plot()`,
        `~yam.commands.remove()`
    :param logging,verbose,loglevel,logfile: logging configuration
    :param key: the key to work with
    :param keys: keys to remove (only remove command)
    :param correlate,stack,stretch: corresponding configuration dictionaries
    :param \*id: the configuration id to load from the config dictionaries
    :param \*\*args: all other arguments are passed to next called function
    """
    time_start = time.time()
    # Configure logging
    if command in ('correlate', 'stack', 'stretch'):
        configure_logging(loggingc=logging,
                          verbose=verbose,
                          loglevel=loglevel,
                          logfile=logfile)
    log.info('Yam version %s', yam.__version__)
    if key is not None and '/' in key:
        key, subkey = key.split('/', 1)
        subkey = '/' + subkey
    else:
        subkey = ''
    # load data plugin, discard unnecessary io kwargs
    data_plugin = io.get('data_plugin')
    if command == 'correlate' or (command in ('print', 'load', 'plot')
                                  and key in ('data', 'prepdata')):
        if data_plugin is not None:
            modulename, funcname = data_plugin.split(':')
            io['data'] = _load_func(modulename.strip(), funcname.strip())
    # pop plotting options
    if command != 'plot':
        for k in list(args.keys()):
            if k.startswith('plot_'):
                args.pop(k)
    # Start main routine
    if command == 'correlate':
        kw = _get_kwargs(correlate, corrid)
        kw['outkey'] = 'c' + corrid
        args.update(kw)
        io['inventory'] = load_inventory(
            kw.get('inventory') or io.get('inventory'))
        yam.commands.start_correlate(io, **args)
    elif command == 'stack':
        if stack is not None and stackid in stack:
            kw = _get_kwargs(stack, stackid)
        else:
            kw = {}
            if 'm' in stackid:
                kw['length'], kw['move'] = stackid.split('m')
            elif stackid in ('', 'all', 'None', 'none', 'null'):
                stackid = ''
                kw['length'] = None
            else:
                kw['length'] = stackid
        kw['outkey'] = key + '_s' + stackid
        args.update(kw)
        yam.commands.start_stack(io, key, subkey=subkey, **args)
    elif command == 'stretch':
        kw = _get_kwargs(stretch, stretchid)
        kw['outkey'] = key + '_t' + stretchid
        args.update(kw)
        yam.commands.start_stretch(io, key, subkey=subkey, **args)
    elif command == 'remove':
        yam.commands.remove(io, keys)
    elif command in ('info', 'print', 'load', 'plot', 'export'):
        if key == 'stations' or command == 'info' and key is None:
            io['inventory'] = load_inventory(io['inventory'])
        if key == 'prepdata':
            if corrid is None:
                msg = 'seed id, day and corrid need to be set for prepdata'
                raise ParseError(msg)
            kw = _get_kwargs(correlate, corrid)
            io['inventory'] = load_inventory(
                kw.get('inventory') or io['inventory'])
            args['prep_kw'] = kw
        if command == 'info':
            yam.commands.info(io,
                              key=key,
                              subkey=subkey,
                              config=(correlate, stack, stretch),
                              **args)
        elif command == 'print':
            yam.commands.load(io, key=key + subkey, do='print', **args)
        elif command == 'load':
            yam.commands.load(io, key=key + subkey, do='load', **args)
        elif command == 'export':
            yam.commands.load(io, key=key + subkey, do='export', **args)
        else:
            yam.commands.plot(io, key + subkey, corrid=corrid, **args)
    elif command == 'scan':
        data_glob = yam.commands._get_data_glob(io['data'])
        print('Suggested call to obspy-scan:')
        if io.get('data_format') is not None:
            print('obspy-scan -f %s %s' % (io['data_format'], data_glob))
        else:
            print('obspy-scan %s' % data_glob)
        print('This will probably not work if you use the data_plugin '
              'configuration. '
              'For more options check obspy-scan -h.')
    else:
        raise ValueError('Unknown command')
    time_end = time.time()
    log.debug('used time: %.1fs', time_end - time_start)