Exemple #1
0
def read_catalog(hypFiles, fmt, phase_data=False, nprocs=1):
    """
    Read a set of earthquake hypocenter-phase files.

    :param hypFiles:
        Full or relative paths and names for hypocenter-phase files.
    :type hypFiles: str or list of strings
    :param str fmt:
        The seismic observation file format {'NLLOC_OBS', 'GFN'}.
    :param bool phase_data:
        If True, the phase block in the file is read as well and returned.
        Otherwise, only origin block (hypocenter information) is returned.
    :param int nprocs:
        Number of jobs to schedule for parallel processing. If -1 is given, all
        processors are used.
    """
    # Error handling
    if not hypFiles:
        # empty list?!
        raise InputError(hypFiles, "cannot access: Found no hypocenter file")

    if isinstance(hypFiles, basestring):
        hypFiles = list(hypFiles)

    logger = logging.getLogger('read_catalog')
    logger.setLevel(logging.INFO)

    if fmt == 'NLLOC_OBS':
        mod = importlib.import_module('nlloc_core')
        readf = mod.read_nlloc_hyp
    elif fmt == 'GFN':
        mod = importlib.import_module('gfn_core')
        readf = mod.read_geofon_hyp

    def _read(hf):
        try:
            ev = readf(hf, phase_data=phase_data)
            return ev
        except:
            msg = 'Skip reading file. '\
                  'It seem corrupt or not %s file: %s'
            logger.warning(str(msg % (fmt, hf)))
            return

    if nprocs == 1:
        Events = itertools.imap(_read, hypFiles)

    if nprocs == -1:
        nprocs = cpu_count()

    Events = parimap(_read, hypFiles, nprocs=nprocs)

    # Required for logger INFO
    Nf = len(hypFiles)
    D = set_percentage(Nf, 10)

    # Start reading hypocenter-phase files
    logger.info(" Started scanning input files")
    Catalog = []

    for iev, ev in enumerate(Events):
        if ev:
            Catalog.append(ev)
        else:
            continue

        if (iev+1) in D:
            logger.info("%4.0f%% scanned, %7i files" % (D[iev+1], iev+1))

    logger.info(" Finished scanning files")

    return Catalog