Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obspy-scan: allow fnmatch wildcards when selecting specific SEED IDs during plotting #2227

Merged
merged 2 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -49,6 +49,9 @@ master:
clients (see #2342 and #2344)
* make it possible to use signed EIDA tokens and also skip token validation
completely (see #2297)
- obspy.imaging:
* obspy-scan can now be used with wildcarded SEED IDs when specifying what
to plot after scanning data (see #2227)
- obspy.io.nordic:
* Add ability to read and write focal mechanisms and moment tensor
information. (see #1924)
Expand Down
29 changes: 24 additions & 5 deletions obspy/imaging/scripts/scan.py
Expand Up @@ -34,6 +34,7 @@
unicode_literals)
from future.builtins import * # NOQA

import fnmatch
import os
import sys
import warnings
Expand Down Expand Up @@ -309,10 +310,26 @@ def plot(self, outfile=None, show=True, fig=None, plot_x=True,
:type seed_ids: list of str
:param seed_ids: Whether to consider only a specific set of SEED IDs
(e.g. ``seed_ids=["GR.FUR..BHZ", "GR.WET..BHZ"]``) or just all SEED
IDs encountered in data (if left ``None``).
IDs encountered in data (if left ``None``). Given SEED IDs may
contain ``fnmatch``-style wildcards (e.g. ``"BW.UH?..[EH]H*"``).
"""
import matplotlib.pyplot as plt

data_keys = list(self.data.keys())
if seed_ids is not None:
ids = []
for id_ in seed_ids:
# allow fnmatch type wildcards in given seed ids
if any(special in id_ for special in '*?[]!'):
ids.extend(fnmatch.filter(data_keys, id_))
else:
ids.append(id_)
# make sure we don't have duplicates in case multiple wildcard
# patterns were given and some ids were matched by more than one
# pattern
ids = list(set(ids))
seed_ids = ids

if fig:
if fig.axes:
ax = fig.axes[0]
Expand Down Expand Up @@ -434,7 +451,7 @@ def analyze_parsed_data(self, print_gaps=False, starttime=None,
:param endtime: Whether to use a fixed end time for the plot and
data percentage calculation.
:type seed_ids: list of str
:param endtime: Whether to consider only a specific set of SEED IDs
:param seed_ids: Whether to consider only a specific set of SEED IDs
(e.g. ``seed_ids=["GR.FUR..BHZ", "GR.WET..BHZ"]``) or just all SEED
IDs encountered in data (if left ``None``).
"""
Expand Down Expand Up @@ -712,9 +729,11 @@ def main(argv=None):
'time-axis axis accordingly.')
parser.add_argument('--id', action='append',
help='Optional, a SEED channel identifier '
"(e.g. 'GR.FUR..HHZ'). You may provide this " +
'option multiple times. Only these ' +
'channels will be plotted.')
"(e.g. 'GR.FUR..HHZ'). You may provide this "
'option multiple times. Only these '
'channels will be plotted. Given SEED IDs may '
'contain fnmatch-style wildcards (e.g. '
"'BW.UH?..[EH]H*').")
parser.add_argument('-t', '--event-time', default=None, type=UTCDateTime,
action='append',
help='Optional, a UTCDateTime compatible string ' +
Expand Down
45 changes: 44 additions & 1 deletion obspy/imaging/tests/test_scan.py
Expand Up @@ -12,8 +12,10 @@
from os.path import abspath, dirname, join, pardir
import warnings

import matplotlib.pyplot as plt

from obspy import read, UTCDateTime
from obspy.core.util.base import NamedTemporaryFile
from obspy.core.util.base import NamedTemporaryFile, get_example_file
from obspy.core.util.misc import TemporaryWorkingDirectory, CatchOutput
from obspy.core.util.testing import ImageComparison
from obspy.imaging.scripts.scan import main as obspy_scan
Expand Down Expand Up @@ -73,6 +75,47 @@ def test_scan_function_and_scanner_class(self):
with ImageComparison(self.path, 'scan.png') as ic:
scanner.plot(ic.name)

def test_scan_plot_by_id_with_wildcard(self):
"""
Test selecting what to plot after scanning with wildcards in selected
SEED IDs
"""
files = [
"BW.UH1._.EHZ.D.2010.147.a.slist.gz",
"BW.UH1._.EHZ.D.2010.147.b.slist.gz",
"BW.UH1._.SHZ.D.2010.147.cut.slist.gz",
"BW.UH2._.SHZ.D.2010.147.cut.slist.gz",
"BW.UH3._.SHE.D.2010.147.cut.slist.gz",
"BW.UH3._.SHN.D.2010.147.cut.slist.gz",
"BW.UH3._.SHZ.D.2010.147.cut.slist.gz",
"BW.UH4._.EHZ.D.2010.147.cut.slist.gz",
"IUANMO.seed"]

scanner = Scanner()

for filename in files:
scanner.parse(get_example_file(filename))

expected = [
('*.UH[12]*',
['BW.UH2..SHZ\n100.0%',
'BW.UH1..SHZ\n100.0%',
'BW.UH1..EHZ\n10.7%']),
('*Z',
['IU.ANMO.00.LHZ\n100.0%',
'BW.UH4..EHZ\n100.0%',
'BW.UH3..SHZ\n100.0%',
'BW.UH2..SHZ\n100.0%',
'BW.UH1..SHZ\n100.0%',
'BW.UH1..EHZ\n10.7%'])]

for seed_id, expected_labels in expected:
fig, ax = plt.subplots()
fig = scanner.plot(fig=fig, show=False, seed_ids=[seed_id])
got = [label.get_text() for label in ax.get_yticklabels()]
self.assertEqual(got, expected_labels)
plt.close(fig)

def test_scanner_manually_add_streams(self):
"""
Test Scanner class, manually adding streams of read data files
Expand Down