Beispiel #1
0
def multiLoc(obsFiles, obsFileType, ctrlFile, ttFileRoot, outPath,
             iSwapBytes=1, locmode='single_event', delayFile=None, nprocs=1):
    # Error handling
    if not isinstance(obsFiles, list):
        raise InputError(obsFiles, "'obsFiles' must be a list of strings")

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

    # Build the task list
    Ne = len(obsFiles)
    task = [obsFiles]   # The first in the task list
    for x in (obsFileType, ctrlFile, ttFileRoot, outPath, iSwapBytes):
        task.append(repeat(x, Ne))

    if locmode == 'static':
        if not delayFile or not isinstance(delayFile, basestring):
            err = "'delayFile' must be a str for %s mode" % locmode
            raise InputError(None, err)
        task.append(repeat(delayFile, Ne))   # The last in the task list
    elif locmode == 'source_specific':
        if not delayFile or not isinstance(delayFile, list):
            err = "'delayFile' must be a list of strings for %s mode" % locmode
            raise InputError(None, err)
        # Match each observation file to its corresponding delay file
        T = []
        for delay in delayFile:
            fname = op.basename(delay).split(".")[0]
            obs = [x for x in obsFiles if fname in x][0]
            if obs:
                T.append((obs, delay))
        OBS, DEL = zip(*T)
        task[0] = OBS      # Replacing ordered obsFiles
        task.append(DEL)   # Appending Ordered delayFiles

    # Run (parallel) location
    if nprocs == 1:
        runs = imap(PyNLLoc, *task)
    else:
        runs = parimap(PyNLLoc, *task, nprocs=nprocs)
    ii = 0
    for run in runs:
        ii += 1
Beispiel #2
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