Skip to content

Commit

Permalink
Merge pull request #1695 from obspy/various
Browse files Browse the repository at this point in the history
Fixes various small issues, typos, UserWarnings, deprecations, etc.
  • Loading branch information
megies committed Mar 6, 2017
2 parents 8bce327 + 4363cb6 commit d5ba2eb
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 74 deletions.
6 changes: 3 additions & 3 deletions obspy/core/tests/test_response.py
Expand Up @@ -161,7 +161,7 @@ def test_segfault_after_error_handling(self):
As long as it does not segfault the test is doing alright.
"""
filename = os.path.join(self.data_dir,
"TM.SKLT..BHZ_faulty_response.xml")
"TM.SKLT.__.BHZ_faulty_response.xml")
inv = read_inventory(filename)

t_samp = 0.05
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_response_list_stage(self):

sampling_rate = 40.0
t_samp = 1.0 / sampling_rate
nfft = 100.0
nfft = 100

cpx_response, freq = inv[0][0][0].response.get_evalresp_response(
t_samp=t_samp, nfft=nfft, output="VEL", start_stage=None,
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_response_list_raises_error_if_out_of_range(self):
# list.
sampling_rate = 45.0
t_samp = 1.0 / sampling_rate
nfft = 100.0
nfft = 100

with self.assertRaises(ValueError) as e:
inv[0][0][0].response.get_evalresp_response(
Expand Down
22 changes: 17 additions & 5 deletions obspy/core/tests/test_trace.py
Expand Up @@ -1333,13 +1333,19 @@ def test_taper_onesided(self):
"""
data = np.ones(11)
tr = Trace(data=data)
tr.taper(max_percentage=None, side="left")
# overlong taper - raises UserWarning - ignoring
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore", UserWarning)
tr.taper(max_percentage=None, side="left")
self.assertTrue(tr.data[:5].sum() < 5.)
self.assertEqual(tr.data[6:].sum(), 5.)

data = np.ones(11)
tr = Trace(data=data)
tr.taper(max_percentage=None, side="right")
# overlong taper - raises UserWarning - ignoring
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore", UserWarning)
tr.taper(max_percentage=None, side="right")
self.assertEqual(tr.data[:5].sum(), 5.)
self.assertTrue(tr.data[6:].sum() < 5.)

Expand All @@ -1349,8 +1355,11 @@ def test_taper_length(self):

data = np.ones(npts)
tr = Trace(data=data, header={'sampling': 1.})
# test an overlong taper request, should still work
tr.taper(max_percentage=0.7, max_length=int(npts / 2) + 1)
# test an overlong taper request, still works but raises UserWarning
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", UserWarning)
tr.taper(max_percentage=0.7, max_length=int(npts / 2) + 1)
self.assertEqual(w[0].category, UserWarning)

data = np.ones(npts)
tr = Trace(data=data, header={'sampling': 1.})
Expand Down Expand Up @@ -1724,7 +1733,10 @@ def test_remove_polynomial_response(self):
filename = os.path.join(path, 'data', 'stationxml_BK.CMB.__.LKS.xml')
inv = read_inventory(filename, format='StationXML')
tr.attach_response(inv)
tr.remove_response()
# raises UserWarning: Stage gain not defined - ignoring
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore", UserWarning)
tr.remove_response()

def test_processing_info_remove_response_and_sensitivity(self):
"""
Expand Down
16 changes: 9 additions & 7 deletions obspy/imaging/tests/test_scan.py
Expand Up @@ -10,6 +10,7 @@
import shutil
import unittest
from os.path import abspath, dirname, join, pardir
import warnings

from obspy import read
from obspy.core.util.base import NamedTemporaryFile
Expand Down Expand Up @@ -51,7 +52,7 @@ def test_scan_main_method(self):
shutil.copy(filename, os.curdir)

with ImageComparison(self.path, 'scan.png') as ic:
obspy_scan([os.curdir] + ['--output', ic.name, '--quiet'])
obspy_scan([os.curdir] + ['--output', ic.name])

def test_scan_function_and_scanner_class(self):
"""
Expand Down Expand Up @@ -107,15 +108,17 @@ def test_scan_save_load_npz(self):
for filename in self.all_files:
shutil.copy(filename, os.curdir)

obspy_scan([os.curdir, '--write', 'scan.npz', '--quiet'])
obspy_scan([os.curdir, '--write', 'scan.npz'])
scanner.parse(os.curdir)
scanner.save_npz('scanner.npz')
scanner = Scanner()
scanner.load_npz('scanner.npz')
# version string of '0.0.0+archive' raises UserWarning - ignore
with warnings.catch_warnings(record=True):
warnings.simplefilter('ignore', UserWarning)
scanner.load_npz('scanner.npz')

with ImageComparison(self.path, 'scan.png') as ic:
obspy_scan(['--load', 'scan.npz', '--output', ic.name,
'--quiet'])
obspy_scan(['--load', 'scan.npz', '--output', ic.name])
with ImageComparison(self.path, 'scan.png') as ic:
scanner.plot(ic.name)

Expand All @@ -133,8 +136,7 @@ def test_scan_times(self):
['--start-time', '2004-01-01'] +
['--end-time', '2004-12-31'] +
['--event-time', '2004-03-14T15:09:26'] +
['--event-time', '2004-02-07T18:28:18'] +
['--quiet'])
['--event-time', '2004-02-07T18:28:18'])

def test_multiple_sampling_rates(self):
"""
Expand Down
14 changes: 11 additions & 3 deletions obspy/io/mseed/tests/test_mseed_reading_and_writing.py
Expand Up @@ -136,7 +136,11 @@ def test_read_and_write_traces(self):
temp_file = tf.name
_write_mseed(this_stream, temp_file, encoding=encoding,
byteorder=byteorder, reclen=reclen)
new_stream = _read_mseed(temp_file)
# some files raise "UserWarning: Record contains a
# fractional seconds" - ignore
with warnings.catch_warnings(record=True):
warnings.simplefilter('ignore', UserWarning)
new_stream = _read_mseed(temp_file)
# Assert the new stream still has the chosen attributes.
# This should mean that writing as well as reading them
# works.
Expand Down Expand Up @@ -733,7 +737,7 @@ def test_all_data_types_and_endians_in_multiple_files(self):
of the read method. Only the data part is verified.
"""
file = os.path.join(self.path, "data",
"BW.BGLD.__.EHE.D.2008.001.first_record")
"BW.BGLD.__.EHE.D.2008.001.second_record")
# Read the data and copy them
st = read(file)
data_copy = st[0].data.copy()
Expand Down Expand Up @@ -1313,8 +1317,12 @@ def test_libmseed_test_cases(self):
"""
def assert_valid(filename, reference, test_type):
if test_type == "data":
# some files raise "UserWarning: Record contains a fractional
# seconds" - ignore
with warnings.catch_warnings(record=True):
warnings.simplefilter('ignore', UserWarning)
st = read(filename)

st = read(filename)
self.assertEqual(len(st), 1, msg=filename)
tr = st[0]

Expand Down
15 changes: 8 additions & 7 deletions obspy/io/mseed/tests/test_mseed_util.py
Expand Up @@ -1121,19 +1121,20 @@ def test_set_flags_in_fixed_header_with_blockette_100(self):
tf.close()
shutil.copy(os.path.join(self.path, 'data', 'test.mseed'),
tf.name)
# No flags set.
flags = util.get_timing_and_data_quality(tf.name)
self.assertEqual(flags["data_quality_flags"], [0] * 8)
# Set flags.
# No data quality flags set.
flags = util.get_flags(tf.name)['data_quality_flags_counts']
self.assertEqual(max(flags.values()), 0)
# Set data quality flags.
util.set_flags_in_fixed_headers(tf.name, {
"NL.HGN.00.BHZ": {"data_qual_flags": {
'glitches_detected': True,
'time_tag_questionable': True}}})
# Flags are set now.
flags = util.get_timing_and_data_quality(tf.name)
flags = util.get_flags(tf.name)['data_quality_flags_counts']
# 2 because file contains two records.
self.assertEqual(flags["data_quality_flags"],
[0, 0, 0, 2, 0, 0, 0, 2])
self.assertEqual(sum(flags.values()), 4)
self.assertEqual(flags['glitches'], 2)
self.assertEqual(flags['suspect_time_tag'], 2)

def _check_values(self, file_bfr, trace_id, record_numbers, expected_bytes,
reclen):
Expand Down
14 changes: 9 additions & 5 deletions obspy/io/nordic/core.py
Expand Up @@ -287,8 +287,12 @@ def _readheader(f):
ksta = Comment(text='Number of stations=' + topline[49:51].strip())
new_event.origins[0].comments.append(ksta)
if _float_conv(topline[51:55]) is not None:
new_event.origins[0].time_errors['Time_Residual_RMS'] = \
_float_conv(topline[51:55])
# raises "UserWarning: Setting attribute "Time_Residual_RMS" which is
# not a default attribute"
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
new_event.origins[0].time_errors['Time_Residual_RMS'] = \
_float_conv(topline[51:55])
# Read in magnitudes if they are there.
for index in [59, 67, 75]:
if not topline[index].isspace():
Expand Down Expand Up @@ -488,11 +492,11 @@ def _read_picks(f, new_event):
pickline = []
# Set a default, ignored later unless overwritten
snr = None
for lineno, line in enumerate(f):
for _lineno, line in enumerate(f):
if line[79] == '7':
header = line
break
for lineno, line in enumerate(f):
for _lineno, line in enumerate(f):
if len(line.rstrip('\n').rstrip('\r')) in [80, 79] and \
line[79] in ' 4\n':
pickline += [line]
Expand Down Expand Up @@ -756,7 +760,7 @@ def write_select(catalog, filename, userid='OBSP', evtype='L',
way as the events in the catalog.
"""
if not wavefiles:
wavefiles = ['DUMMY' for i in range(len(catalog))]
wavefiles = ['DUMMY' for _i in range(len(catalog))]
with open(filename, 'w') as fout:
for event, wavfile in zip(catalog, wavefiles):
select = io.StringIO()
Expand Down

0 comments on commit d5ba2eb

Please sign in to comment.