API

fio

File IO utilities.

pymodule.fio.file_is_empty(path)[source]

https://stackoverflow.com/a/52259169/8877268

pymodule.fio.flen(fnm)[source]

Return # of lines in file.

https://stackoverflow.com/a/845081/8877268

pymodule.fio.rcol(fname, col, rtype='list', delimiter=None, comments='#')[source]

Read columns from file.

Parameters:
  • fname – input file
  • col (int or sequence) – columns to get
  • rtype – return list or set
pymodule.fio.rpk(fnm)[source]

Read pickle.

pymodule.fio.ryml(fin)[source]

Load YAML.

pymodule.fio.wpk(fnm, data, protocol=3)[source]

Save into pickle.

hdf5

Provide custom functions to HDF5 file.

class pymodule.hdf5.MyH5(name, mode=None, driver=None, libver=None, userblock_size=None, swmr=False, rdcc_nslots=None, rdcc_nbytes=None, rdcc_w0=None, track_order=None, fs_strategy=None, fs_persist=False, fs_threshold=1, **kwds)[source]
static dict2attr(obj, d)[source]
Parameters:obj – dataset or group
static new_dset(grp, name, data, ow=False, **kwargs)[source]

Create a dataset in group allowing overwriting.

static new_grp(parent, name)[source]

Create a group in parent considering it already exists.

seis

Seismology related utilities.

pymodule.seis.dist_one2many(lon0, lat0, lons, lats)[source]

Distance between one point and many points.

pymodule.seis.geod_inv(lon1, lat1, lon2, lat2, radians=False, km=True)[source]

Can take array.

https://pyproj4.github.io/pyproj/html/api/geod.html#pyproj.Geod.inv

Return az:azimuth 1->2
Return baz:azimuth 2->1 (backazimuth)
pymodule.seis.get_spectrum(tr)[source]

Return frequencies, amplitude and phase of FFT.

Parameters:tr (Trace) –
pymodule.seis.hdr_cor(reftime='2000-01-01', **kwargs)[source]

Create a new header for correlatoin.

Parameters:
  • kuser1 – rec network
  • user1 – 1 for symmetric & 2 for double-sided
pymodule.seis.interp2d(x, y, z, xi, yi, **kwargs)[source]
Parameters:isgrid – Use RegularGridInterpolator if True otherwise griddata
pymodule.seis.interp_transect(lon1, lat1, lon2, lat2, x, y, z, kwargs_interp={}, **kwargs)[source]

Interpolate values along a transect.

pymodule.seis.sachd(reftime='2019-01-01', **kwargs)[source]
Parameters:kwargs – for SAC
pymodule.seis.sliced(tr, b=None, e=None, data_only=False)[source]

Return sliced trace given begin/end relative to starttime.

pymodule.seis.slowness2velocity(sn, fill_value=0, dtype='float64')[source]

Slowness to velocity.

pymodule.seis.snr(tr, **kwargs)[source]

Return SNR defined as (peak of signal amplitude) / (RMS of trailing noise)

Parameters:
  • dsn – differential time between signal and noise
  • nlen – length of noise window
  • Tmax
  • [be]fact – [tau - bfact*Tmax, tau + efact*Tmax]
  • fill – fill value if failing to measure SNR
pymodule.seis.stack(st, stats=None, kw_stack={}, kw_snr={})[source]

Stack traces in stream.

pymodule.seis.sym_xc(xc, noeven=False)[source]

Symmetrize a cross-correlation.

pymodule.seis.transect(lon1, lat1, lon2, lat2, step=None, npts=None, **kwargs)[source]

Return coordinates along a great circle.

Parameters:
  • ends – If include initial and terminus points
  • npts – Number of points in the transect.
  • step – Transect interval in km.
pymodule.seis.trim(tr, b=None, e=None)[source]

Trim trace given begin/end relative to starttime.

pymodule.seis.txt2inv(fin, **kwargs)[source]

Return Inventory from text file.

pymodule.seis.velocity2slowness(vel, fill_value=0, dtype='float64')[source]

Velocity to slowness.

pymodule.seis.wrap_lon(lon, wrap_lon=True)[source]

https://github.com/mathause/regionmask/blob/791b3d510b687d9dbb045db8ca353f1cbb627919/regionmask/core/mask.py#L36

Parameters:wrap_lon – 180 or 360
pymodule.seis.x_cor(tr1, tr2, **kwargs)[source]

Do correlation/convolution between two Trace objects.

ObsPy:

\[\int f(\tau) g(t+\tau) d\tau\]

The definition in SAC is different:

\[\int f(t+\tau) g(\tau) d\tau\]

Results agree except at ends (~100 s for one lag of ~1400 s). Not sure about the reason.

Parameters:
  • demean – demean data beforehand
  • normalize – for correlation coefficient
  • domain – “time” or “freq”
  • Welch – apply Welch’s method for correlation
  • subwin – length of subwindow [s]
  • lap – lap fraction of subwindows
  • op – correlation, convolution

signal

Signal processing.

pymodule.signal.conv_spc(in1, in2)[source]

Return spectra of convolution (spectral multiplication) of two 1-D signals.

https://github.com/scipy/scipy/blob/v1.6.0/scipy/signal/signaltools.py#L551-L663

pymodule.signal.corr2(a, b)[source]

2-D correlation coefficient.

https://www.mathworks.com/help/images/ref/corr2.html#f1-227958

pymodule.signal.corr_coef(reference, current, shift=None, demean=True, abs_max=True, domain='freq')[source]

Return shift and value of maximum of cross-correlation.

pymodule.signal.ddot(x, y, interp=True)[source]

Return 2nd order derivative.

pymodule.signal.fit_dist(y, dist='normal', **kwargs)[source]

Fit histogram to a distribution.

pymodule.signal.gaussian_filter(a, x_stddev, y_stddev=None, **kwargs)[source]

http://docs.astropy.org/en/stable/convolution/index.html

pymodule.signal.gradient_2d(z, dx, dy, order=2)[source]

Not use Numpy because np.gradient dx or dy must be 1D.

https://github.com/numpy/numpy/issues/9401 https://github.com/Unidata/MetPy/issues/174

pymodule.signal.inst_ph(a)[source]

Return instantaneous phase.

pymodule.signal.intersect2d(a, b, assume_unique=False, return_indices=False)[source]

Find the intersection of two 2-D arrays.

https://stackoverflow.com/a/8317403/8877268

Parameters:
  • assume_unique
  • return_indices
pymodule.signal.linrange(start, stop, step=1)[source]

Mix of arange & linspace.

pymodule.signal.ma_corr2(a, b)[source]

Similar to corr2() but for masked array.

pymodule.signal.nextpow2(n)[source]

Exponent of next higher power of 2.

https://www.mathworks.com/help/matlab/ref/nextpow2.html

pymodule.signal.norm_weight(w, axis=0)[source]

Normalize weights.

pymodule.signal.normalized(a, norm=None)[source]

Normalize 1D array to its absolute maximum.

https://docs.obspy.org/_modules/obspy/core/trace.html#Trace.normalize

pymodule.signal.nsig(x, n=2)[source]

Round to n signifcant figures.

https://stackoverflow.com/a/3411435/8877268

pymodule.signal.rms(a)[source]

Root mean square.

pymodule.signal.spectrum(a, delta=1, **kwargs)[source]

Return frequencies, amplitude and phase of FFT.

pymodule.signal.weighted_mean(x, w, axis=0)[source]

https://www.wikiwand.com/en/Weighted_arithmetic_mean#/Mathematical_definition

pymodule.signal.weighted_sd(x, w, axis=0)[source]

https://www.wikiwand.com/en/Weighted_arithmetic_mean#/Weighted_sample_variance

pymodule.signal.weighted_sem(x, w, axis=0)[source]

No n/(n-1) factor.

https://www.wikiwand.com/en/Weighted_arithmetic_mean#/Bootstrapping_validation

sys_tool

System related tools.

pymodule.sys_tool.log_cmd(cmd, logger)[source]

Pass stdout to logger. https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python

pymodule.sys_tool.mkdir(path)[source]

Meke intermediate directories and OK if exists. https://stackoverflow.com/a/14364249/8877268

pymodule.sys_tool.rm_flst(flst)[source]

Remove files in a lst.

pymodule.sys_tool.setup_logging(fpar='logging.yml', env_key='LOG_CFG', default_level=20)[source]

Setup logging configuration. https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/

Parameters:
  • fpar
  • env_key – environment variable e.g. LOG_CFG=my_logging.yml python my_server.py

timing

Show time elapsed.

https://stackoverflow.com/a/12344609/8877268

pymodule.timing.timing()[source]

Show time elapsed.