Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #536 from usgs/oscillators_fix2
Browse files Browse the repository at this point in the history
Oscillators fix2
  • Loading branch information
emthompson-usgs authored Aug 30, 2020
2 parents 581014e + 159296f commit e5c9831
Show file tree
Hide file tree
Showing 23 changed files with 28,004 additions and 102 deletions.
3,066 changes: 3,066 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10590_ComalTX11-10-20_IU.CCM.BH1.00.AT2

Large diffs are not rendered by default.

3,066 changes: 3,066 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10590_ComalTX11-10-20_IU.CCM.BH2.00.AT2

Large diffs are not rendered by default.

3,066 changes: 3,066 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10590_ComalTX11-10-20_IU.CCM.BHZ.00.AT2

Large diffs are not rendered by default.

6,163 changes: 6,163 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10591_ComalTX11-10-20_IU.CCM.BH1.10.AT2

Large diffs are not rendered by default.

6,163 changes: 6,163 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10591_ComalTX11-10-20_IU.CCM.BH2.10.AT2

Large diffs are not rendered by default.

6,163 changes: 6,163 additions & 0 deletions gmprocess/data/testdata/high_freq_sa/RSN10591_ComalTX11-10-20_IU.CCM.BHZ.10.AT2

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gmprocess/io/asdf/stream_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def summarizeLabels(self):
if label in ptag:
labeldict[label] = ptag
for label, ptag in labeldict.items():
row = pd.Series(index=cols)
row = pd.Series(index=cols, dtype=object)
row['Label'] = label
provdoc = self.dataset.provenance[ptag]
user, software = _get_agents(provdoc)
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def getProvenance(self, eventid, stations=None, labels=None):
else:
pass
attrkey = key.replace('seis_prov:', '')
row = pd.Series(index=cols)
row = pd.Series(index=cols, dtype=object)
row['Record'] = provname
row['Processing Step'] = pstep
row['Step Attribute'] = attrkey
Expand Down
7 changes: 4 additions & 3 deletions gmprocess/io/cosmos/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,11 @@ def _get_header_info(int_data, flt_data, lines, cmt_data, location=''):
for key in coordinates:
if coordinates[key] == unknown:
if key != 'elevation':
warnings.warn('Missing %r. Setting to np.nan.' % key, Warning)
warnings.warning(
'Missing %r. Setting to np.nan.' % key, Warning)
coordinates[key] = np.nan
else:
warnings.warn('Missing %r. Setting to 0.0.' % key, Warning)
warnings.warning('Missing %r. Setting to 0.0.' % key, Warning)
coordinates[key] = 0.0

hdr['coordinates'] = coordinates
Expand Down Expand Up @@ -763,7 +764,7 @@ def _read_lines(skip_rows, filename):
# parse out the format of the data
# sometimes header has newline characters in it...
header = header.replace('\n', '')
format_data = re.findall('\d+', header[header.find('format=') + 8:])
format_data = re.findall(r"\d+", header[header.find('format=') + 8:])
cols = int(format_data[0])
fmt = int(format_data[1])
num_lines = int(np.ceil(npts / cols))
Expand Down
2 changes: 1 addition & 1 deletion gmprocess/io/cwb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _get_header_info(file, data):
timedelta(seconds=secs, microseconds=microsecs)

# Set defaults
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coordinates['elevation'] = 0.0
if 'longitude' not in coordinates:
coordinates['longitude'] = np.nan
Expand Down
4 changes: 2 additions & 2 deletions gmprocess/io/dmg/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def _get_header_info_v1(int_data, flt_data, lines, level, location=''):
latitude, longitude = _get_coords(latitude_str, longitude_str)
coordinates['latitude'] = latitude
coordinates['longitude'] = longitude
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coordinates['elevation'] = 0.0

# Standard metadata
Expand Down Expand Up @@ -679,7 +679,7 @@ def _get_header_info(int_data, flt_data, lines, level, location=''):
latitude, longitude = _get_coords(latitude_str, longitude_str)
coordinates['latitude'] = latitude
coordinates['longitude'] = longitude
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coordinates['elevation'] = 0.0

# Standard metadata
Expand Down
4 changes: 2 additions & 2 deletions gmprocess/io/geonet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _read_channel(filename, line_offset):

# parse the sensor resolution from the text header
resolution_str = lines[4].split()[1]
resolution = int(re.search('\d+', resolution_str).group())
resolution = int(re.search(r"\d+", resolution_str).group())

# read floating point header array
skip_header = line_offset + TEXT_HDR_ROWS
Expand Down Expand Up @@ -323,7 +323,7 @@ def _read_header(hdr_data, station, name, component, data_format,
lonmn = hdr_data[2, 4]
lonsc = hdr_data[2, 5]
coordinates['longitude'] = _dms_to_dd(londg, lonmn, lonsc)
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coordinates['elevation'] = 0.0

# get other standard metadata
Expand Down
4 changes: 2 additions & 2 deletions gmprocess/io/renadic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read_renadic(filename):
data_dir = pkg_resources.resource_filename('gmprocess',
'data')
tablefile = os.path.join(data_dir, 'station_coordinates.xlsx')
table = pd.read_excel(tablefile)
table = pd.read_excel(tablefile, engine="openpyxl")

with open(filename, 'rt', encoding=ENCODING) as f:
lines1 = [next(f) for x in range(TEXT_HDR_ROWS)]
Expand Down Expand Up @@ -248,7 +248,7 @@ def _read_header(lines, filename, table):
lon = row['Lon']

altitude = 0.0
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coords = {'latitude': lat,
'longitude': lon,
'elevation': altitude}
Expand Down
2 changes: 1 addition & 1 deletion gmprocess/io/smc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def _get_header_info(filename, any_structure=False, accept_flagged=False,
if floatheader[2, 2] != missing_data:
coordinates['elevation'] = floatheader[2, 2]
else:
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')

# figure out the channel code
if format_specific['vertical_orientation'] in [0, 180]:
Expand Down
2 changes: 1 addition & 1 deletion gmprocess/io/usc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def _get_header_info(int_data, flt_data, lines, volume, location=''):
longitude = -longitude
coordinates['latitude'] = latitude
coordinates['longitude'] = longitude
logging.warn('Setting elevation to 0.0')
logging.warning('Setting elevation to 0.0')
coordinates['elevation'] = 0.0
# Get standard paramaters
standard['units_type'] = get_units_type(hdr['channel'])
Expand Down
34 changes: 16 additions & 18 deletions gmprocess/metrics/cfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
* This code is based on Dave Boore's fortran function icmpmx, which in turn
* was based on ucmpmx, written by Bob Youngs and I. Idriss). It was re-
* written in python/cython by Heather Schovanec (8/2018) and rewritten in
* C by Bruce Worden (8/2018). This version does not do the full upsampling
* that Boore's code implements, but only produces the first sub-sample. If
* data with coarse sampling is used, or very high frequencies are required,
* this function should be rewritten to do the full sub-sampling. Note that
* this will require increasing the size of the output arrays sacc, svel,
* and sdis, and returning the new sample rate.
* C by Bruce Worden (8/2018).
* Note that this code does no resampling. It is assumed that the data
* provided to this function has been sufficiently resampled. See, for
* example, Boore and Goulet (2014), Bull Earthquake Eng, 12:203-216,
* DOI 10.1007/s10518-013-9574-9.
*/
void calculate_spectrals_c(double *times, double *acc, int np, double period,
void calculate_spectrals_c(double *acc, int np, double dt, double period,
double damping, double *sacc, double *svel,
double *sdis) {
double *sdis)
{
double w = 2 * M_PI / period;
double d = damping;
double wd = sqrt(1. - d * d) * w;
double dt_in = times[1] - times[0];
double ns = (int)(10. * dt_in / period - 0.01) + 1.0;
double dt = dt_in / ns;
double e = exp( -1 * d * w * dt);
double e = exp(-1 * d * w * dt);
double sine = e * sin(wd * dt);
double cosine = e * cos(wd * dt);

Expand All @@ -43,7 +40,7 @@ void calculate_spectrals_c(double *times, double *acc, int np, double period,
int k;

g = acc[0];
dug = (acc[1] - g) / ns;
dug = acc[1] - g;
gw2i = g * w2i;
dugw2i = dug * w2i;
dugw2idt = dugw2i / dt;
Expand All @@ -54,15 +51,16 @@ void calculate_spectrals_c(double *times, double *acc, int np, double period,
b * (wd * sine + dw * cosine) -
dugw2idt;
sacc[0] = -2. * dw * svel[0] - w2 * sdis[0];
for (k = 1; k < np-1; k++) {
for (k = 1; k < np - 1; k++)
{
g = acc[k];
dug = (acc[k+1] - g) / ns;
dug = acc[k + 1] - g;
gw2i = g * w2i;
dugw2i = dug * w2i;
dugw2idt = dugw2i / dt;
b = sdis[k-1] + gw2i - ddtw3 * dug;
a = wdi * svel[k-1] + dw * wdi * b + wdi * dugw2idt;
sdis[k] = a * sine + b * cosine + ddtw3 * dug -
b = sdis[k - 1] + gw2i - ddtw3 * dug;
a = wdi * svel[k - 1] + dw * wdi * b + wdi * dugw2idt;
sdis[k] = a * sine + b * cosine + ddtw3 * dug -
gw2i - dugw2i;
svel[k] = a * (wd * cosine - dw * sine) -
b * (wd * sine + dw * cosine) -
Expand Down
2 changes: 1 addition & 1 deletion gmprocess/metrics/cfuncs.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
void calculate_spectrals_c(double *times, double *acc, int np, double period,
void calculate_spectrals_c(double *acc, int np, double dt, double period,
double damping, double *sacc, double *svel,
double *sdis);
71 changes: 51 additions & 20 deletions gmprocess/metrics/oscillators.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,59 @@ from obspy import read
from gmprocess.constants import GAL_TO_PCTG

cdef extern from "cfuncs.h":
void calculate_spectrals_c(double *times, double *acc, int np,
void calculate_spectrals_c(double *acc, int np, double dt,
double period, double damping, double *sacc,
double *svel, double *sdis);

cpdef list calculate_spectrals(np.ndarray[double, ndim=1, mode='c']times,
np.ndarray[double, ndim=1, mode='c']acc,
period, damping):
cpdef list calculate_spectrals(trace, period, damping):
"""
Returns a list of spectral responses for acceleration, velocity,
and displacement.
Args:
times (np.ndarray): Times corresponding to the acceleration values.
acceleration (np.ndarray): Acceleration values.
trace (obspy Trace object): The trace to be acted upon
period (float): Period in seconds.
damping (float): Fraction of critical damping.
Returns:
list: List of spectral responses (np.ndarray).
"""
cdef int kg = len(acc)
cdef ndarray[double, ndim=1] spectral_acc = np.zeros(kg)
cdef ndarray[double, ndim=1] spectral_vel = np.zeros(kg)
cdef ndarray[double, ndim=1] spectral_dis = np.zeros(kg)
cdef int new_np = trace.stats.npts
cdef double new_dt = trace.stats.delta
cdef double new_sample_rate = trace.stats.sampling_rate
# The time length of the trace in seconds
cdef double tlen = (new_np - 1) * new_dt
cdef int ns

calculate_spectrals_c(<double *>times.data, <double *>acc.data, kg,
# This is the resample factor for low-sample-rate/high-frequency
ns = (int)(10. * new_dt / period - 0.01) + 1
if ns > 1:
# Increase the number of samples as necessary
new_np = new_np * ns
# Make the new number of samples a power of two
# leaving this out for now; it slows things down but doesn't
# appear to affect the results. YMMV.
# new_np = 1 if new_np == 0 else 2**(new_np - 1).bit_length()
# The new sample interval
new_dt = tlen / (new_np - 1)
# The new sample rate
new_sample_rate = 1.0 / new_dt
# Make a copy because resampling happens in place
trace = trace.copy()
# Resample the trace
trace.resample(new_sample_rate, window=None)

cdef ndarray[double, ndim=1] spectral_acc = np.zeros(new_np)
cdef ndarray[double, ndim=1] spectral_vel = np.zeros(new_np)
cdef ndarray[double, ndim=1] spectral_dis = np.zeros(new_np)
cdef ndarray[double, ndim=1] acc = trace.data

calculate_spectrals_c(<double *>acc.data, new_np, new_dt,
period, damping,
<double *>spectral_acc.data,
<double *>spectral_vel.data,
<double *>spectral_dis.data)
return [spectral_acc, spectral_vel, spectral_dis]
return [spectral_acc, spectral_vel, spectral_dis, new_np, new_dt,
new_sample_rate]


def get_acceleration(stream, units='%%g'):
Expand Down Expand Up @@ -97,11 +120,14 @@ def get_spectral(period, stream, damping=0.05, times=None):
if isinstance(stream, (StationStream, Stream)):
for idx in num_trace_range:
trace = stream[idx]
acc_sa = calculate_spectrals(trace.times(), trace.data,
period, damping)[0]
sa_list = calculate_spectrals(trace, period, damping)
acc_sa = sa_list[0]
acc_sa *= GAL_TO_PCTG
stats = trace.stats.copy()
stats.npts = sa_list[3]
stats.delta = sa_list[4]
stats.sampling_rate = sa_list[5]
stats['units'] = '%%g'
acc_sa = np.array(acc_sa) * GAL_TO_PCTG
spect_trace = StationTrace(data=acc_sa, header=stats)
traces += [spect_trace]
spect_stream = StationStream(traces)
Expand All @@ -110,12 +136,17 @@ def get_spectral(period, stream, damping=0.05, times=None):
rotated = []
for idx in range(0, len(stream)):
rot_matrix = stream[idx]
rotated_spectrals = np.zeros(rot_matrix.shape)
rotated_spectrals = []
for idy in range(0, len(rot_matrix)):
acc_sa = np.asarray(calculate_spectrals(times, rot_matrix[idy],
period, damping)[0])
acc_sa = acc_sa * GAL_TO_PCTG
rotated_spectrals[idy] = acc_sa
stats = {'npts': len(rot_matrix[idy]),
'delta': times[1] - times[0],
'sampling_rate': 1.0 / (times[1] - times[0])
}
new_trace = Trace(data=rot_matrix[idy], header=stats)
sa_list = calculate_spectrals(new_trace, period, damping)
acc_sa = sa_list[0]
acc_sa *= GAL_TO_PCTG
rotated_spectrals.append(acc_sa)
rotated += [rotated_spectrals]
return rotated

Expand Down
Loading

0 comments on commit e5c9831

Please sign in to comment.