def _zip(keys, vals): """ Zip together keys and vals :type keys: dict_keys :param keys: keys :type vals: dict_values :param vals: values """ return zip(iterable(keys), iterable(vals))
def rm(path): """ Remove files or directories """ for name in iterable(path): if os.path.isfile(name) or os.path.islink(name): os.remove(name) elif os.path.isdir(name): shutil.rmtree(name)
def rename(old, new, names): """ Rename multiple files :type old: str :param old: expression to replace :type new: str :param new: replacement expression :type names: list :param names: files to replace expressions in """ for name in iterable(names): if name.find(old) >= 0: os.rename(name, name.replace(old, new))
def read_slice(path, parameters, iproc): """ Reads SPECFEM model slice(s) :type path: str :param path: path to the database files :type parameters: str :param parameters: parameters to read, e.g. 'vs', 'vp' :type iproc: int :param iproc: processor/slice number to read """ vals = [] for key in iterable(parameters): filename = os.path.join(path, f"proc{int(iproc):06d}_{key}.bin") vals += [_read(filename)] return vals
def write_slice(data, path, parameters, iproc): """ Writes SPECFEM model slice :type data: seisflows.Container :param data: data to be written to a slice :type path: str :param path: path to the database files :type parameters: str :param parameters: parameters to write, e.g. 'vs', 'vp' :type iproc: int :param iproc: processor/slice number to write """ for key in iterable(parameters): filename = os.path.join(path, f"proc{int(iproc):06d}_{key}.bin") _write(data, filename)
def mkdir(dirs): """ Make directory or directories .. note:: Random wait times to prevent overloading disk when multiple subprocesses are running mkdir :type dirs: str or list :param dirs: pathnames to make """ time.sleep(2 * random.random()) # interval [0, 2]s for dir_ in iterable(dirs): if not os.path.isdir(dir_): os.makedirs(dir_)
def write_slice(data, path, parameters, iproc): """ Writes SPECFEM model slice !!! This won't work because we need access to the spatial components that !!! are only the model :type data: seisflows.Container :param data: data to be written to a slice :type path: str :param path: path to the database files :type parameters: str :param parameters: parameters to write, e.g. 'vs', 'vp' :type iproc: int :param iproc: processor/slice number to write """ for key in iterable(parameters): filename = os.path.join(path, f"proc{int(iproc):06d}_{key}.bin") _write(data, filename)
def read_slice(path, parameters, iproc): """ Reads SPECFEM model slice(s) based on .dat ASCII files :type path: str :param path: path to the database files :type parameters: str :param parameters: parameters to read, e.g. 'vs', 'vp' :type iproc: int :param iproc: processor/slice number to read :rtype: list of np.array :return: list of arrays corresponding to model parameters in given order """ filename = _get_filename(path, iproc) available_parameters = _get_available_parameters(filename) model = np.loadtxt(filename).T vals = [] for key in iterable(parameters): vals += [model[available_parameters.index(key)]] return vals