Skip to content

Commit

Permalink
Merge pull request #2021 from obspy/patch-nordic-amps
Browse files Browse the repository at this point in the history
nordic: Bug-fix amplitude and wavefile write
  • Loading branch information
megies committed Apr 23, 2018
2 parents 7e30282 + cd47804 commit 3512c30
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.txt
Expand Up @@ -17,6 +17,9 @@
(see #1981, #2057).
* Fix util.get_start_and_end_time returning sample rate = 0 when
sample rate = 1 (see #2069)
- obspy.io.nordic
* Bug-fix for amplitudes without magnitude_hint (see #2021)
* Bug-fix for wavefiles with full path stripping (see #2021)
- obspy.io.reftek:
* Fix problems reading some Reftek 130 files, presumably due to floating
point accuracy issues in comparing timestamps. Internal representation of
Expand All @@ -35,7 +38,6 @@
npz file that was written with a newer version of the npz representation
than is used by current ObsPy version (see #2051)


1.1.0: (doi: 10.5281/zenodo.165135)
- General:
* Read support for Guralp Compressed Format (GCF) waveform data,
Expand Down
5 changes: 3 additions & 2 deletions obspy/io/nordic/core.py
Expand Up @@ -957,7 +957,7 @@ def _write_nordic(event, filename, userid='OBSP', evtype='L', outdir='.',
# Write line 3 of s-file
for wavefile in wavefiles:
sfile.write(' ' + os.path.basename(wavefile) +
'6'.rjust(79 - len(wavefile)) + '\n')
'6'.rjust(79 - len(os.path.basename(wavefile))) + '\n')
# Write final line of s-file
sfile.write(' STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU' +
' VELO AIN AR TRES W DIS CAZ7\n')
Expand Down Expand Up @@ -1109,7 +1109,8 @@ def nordpick(event):
else:
amp = None
coda = ' '
if amplitude.magnitude_hint.upper() == 'ML':
mag_hint = (amplitude.magnitude_hint or amplitude.type)
if mag_hint is not None and mag_hint.upper() in ['AML', 'ML']:
phase_hint = 'IAML'
impulsivity = ' '
else:
Expand Down
86 changes: 59 additions & 27 deletions obspy/io/nordic/tests/test_nordic.py
Expand Up @@ -105,6 +105,10 @@ def test_read_write(self):
test_ev.amplitudes[0].period)
self.assertEqual(read_ev.amplitudes[0].snr,
test_ev.amplitudes[0].snr)
self.assertEqual(read_ev.amplitudes[2].period,
test_ev.amplitudes[2].period)
self.assertEqual(read_ev.amplitudes[2].snr,
test_ev.amplitudes[2].snr)
# Check coda magnitude pick
# Resource ids get overwritten because you can't have two the same in
# memory
Expand Down Expand Up @@ -385,6 +389,25 @@ def test_read_wavename(self):
testing_path = os.path.join(self.testing_path, '01-0411-15L.S201309')
wavefiles = readwavename(testing_path)
self.assertEqual(len(wavefiles), 1)
# Test that full paths are handled
test_event = full_test_event()
# Add the event to a catalogue which can be used for QuakeML testing
test_cat = Catalog()
test_cat += test_event
# Check the read-write s-file functionality
with TemporaryWorkingDirectory():
sfile = _write_nordic(
test_cat[0], filename=None, userid='TEST', evtype='L',
outdir='.', wavefiles=['walrus/test'], explosion=True,
overwrite=True)
self.assertEqual(readwavename(sfile), ['test'])
# Check that multiple wavefiles are read properly
with TemporaryWorkingDirectory():
sfile = _write_nordic(
test_cat[0], filename=None, userid='TEST', evtype='L',
outdir='.', wavefiles=['walrus/test', 'albert'],
explosion=True, overwrite=True)
self.assertEqual(readwavename(sfile), ['test', 'albert'])

def test_read_event(self):
"""
Expand Down Expand Up @@ -641,11 +664,14 @@ def _test_similarity(event_1, event_2, verbose=False):
for amp_1, amp_2 in zip(event_1.amplitudes, event_2.amplitudes):
# Assuming same ordering of amplitudes
for key in amp_1.keys():
if key not in ["resource_id", "pick_id", "waveform_id", "snr"]:
if key not in ["resource_id", "pick_id", "waveform_id", "snr",
"magnitude_hint", 'type']:
if not amp_1[key] == amp_2[key]:
if verbose:
print('%s is not the same as %s for key %s' %
(amp_1[key], amp_2[key], key))
print(amp_1)
print(amp_2)
return False
elif key == "waveform_id":
if pick_1[key].station_code != pick_2[key].station_code:
Expand All @@ -661,6 +687,15 @@ def _test_similarity(event_1, event_2, verbose=False):
if verbose:
print('Channel codes do not match')
return False
elif key in ["magnitude_hint", "type"]:
# Reading back in will define both, but input event might have
# None
if amp_1[key] is not None:
if not amp_1[key] == amp_2[key]:
if verbose:
print('%s is not the same as %s for key %s' %
(amp_1[key], amp_2[key], key))
return False
return True


Expand Down Expand Up @@ -699,52 +734,49 @@ def full_test_event():
network_code='NZ')
_waveform_id_2 = WaveformStreamID(station_code='WTSZ', channel_code='BH1',
network_code=' ')
# Pick to associate with amplitude
test_event.picks.append(
# Pick to associate with amplitude - 0
test_event.picks = [
Pick(waveform_id=_waveform_id_1, phase_hint='IAML',
polarity='undecidable', time=UTCDateTime("2012-03-26") + 1.68,
evaluation_mode="manual"))
# Need a second pick for coda
test_event.picks.append(
evaluation_mode="manual"),
Pick(waveform_id=_waveform_id_1, onset='impulsive', phase_hint='PN',
polarity='positive', time=UTCDateTime("2012-03-26") + 1.68,
evaluation_mode="manual"))
# Unassociated pick
test_event.picks.append(
evaluation_mode="manual"),
Pick(waveform_id=_waveform_id_1, phase_hint='IAML',
polarity='undecidable', time=UTCDateTime("2012-03-26") + 1.68,
evaluation_mode="manual"),
Pick(waveform_id=_waveform_id_2, onset='impulsive', phase_hint='SG',
polarity='undecidable', time=UTCDateTime("2012-03-26") + 1.72,
evaluation_mode="manual"))
# Unassociated pick
test_event.picks.append(
evaluation_mode="manual"),
Pick(waveform_id=_waveform_id_2, onset='impulsive', phase_hint='PN',
polarity='undecidable', time=UTCDateTime("2012-03-26") + 1.62,
evaluation_mode="automatic"))
evaluation_mode="automatic")]
# Test a generic local magnitude amplitude pick
test_event.amplitudes.append(
test_event.amplitudes = [
Amplitude(generic_amplitude=2.0, period=0.4,
pick_id=test_event.picks[0].resource_id,
waveform_id=test_event.picks[0].waveform_id, unit='m',
magnitude_hint='ML', category='point', type='AML'))
# Test a coda magnitude pick
test_event.amplitudes.append(
magnitude_hint='ML', category='point', type='AML'),
Amplitude(generic_amplitude=10,
pick_id=test_event.picks[1].resource_id,
waveform_id=test_event.picks[1].waveform_id, type='END',
category='duration', unit='s', magnitude_hint='Mc',
snr=2.3))
test_event.origins[0].arrivals.append(
snr=2.3),
Amplitude(generic_amplitude=5.0, period=0.6,
pick_id=test_event.picks[2].resource_id,
waveform_id=test_event.picks[0].waveform_id, unit='m',
category='point', type='AML')]
test_event.origins[0].arrivals = [
Arrival(time_weight=0, phase=test_event.picks[1].phase_hint,
pick_id=test_event.picks[1].resource_id))
test_event.origins[0].arrivals.append(
Arrival(time_weight=2, phase=test_event.picks[2].phase_hint,
pick_id=test_event.picks[2].resource_id,
backazimuth_residual=5, time_residual=0.2, distance=15,
azimuth=25))
test_event.origins[0].arrivals.append(
pick_id=test_event.picks[1].resource_id),
Arrival(time_weight=2, phase=test_event.picks[3].phase_hint,
pick_id=test_event.picks[3].resource_id,
backazimuth_residual=5, time_residual=0.2, distance=15,
azimuth=25))
azimuth=25),
Arrival(time_weight=2, phase=test_event.picks[4].phase_hint,
pick_id=test_event.picks[4].resource_id,
backazimuth_residual=5, time_residual=0.2, distance=15,
azimuth=25)]
return test_event


Expand Down

0 comments on commit 3512c30

Please sign in to comment.