From cb7d4b0c80649326a0251243f09bfcab582000ba Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Fri, 15 Mar 2019 14:31:37 +0100 Subject: [PATCH 1/2] scan: add test for SEED ID selection in plotting with fnmatch wildcards --- obspy/imaging/tests/test_scan.py | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/obspy/imaging/tests/test_scan.py b/obspy/imaging/tests/test_scan.py index ad009ff698d..2e398a48fe1 100644 --- a/obspy/imaging/tests/test_scan.py +++ b/obspy/imaging/tests/test_scan.py @@ -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 @@ -73,6 +75,49 @@ 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 From ba61aad106d79b9982d44bc0e4b5583a2e6ec7da Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Fri, 5 Oct 2018 12:02:32 +0200 Subject: [PATCH 2/2] obspy-scan: allow fnmatch wildcards when selecting specific SEED IDs during plotting so one can e.g. select all "[EH]H*" channels for a plot of all scanned data --- CHANGELOG.txt | 3 +++ obspy/imaging/scripts/scan.py | 29 ++++++++++++++++++++++++----- obspy/imaging/tests/test_scan.py | 6 ++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 240aa55d312..f3548f3f6e1 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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) diff --git a/obspy/imaging/scripts/scan.py b/obspy/imaging/scripts/scan.py index 9155c6652f3..ba162e03cd6 100755 --- a/obspy/imaging/scripts/scan.py +++ b/obspy/imaging/scripts/scan.py @@ -34,6 +34,7 @@ unicode_literals) from future.builtins import * # NOQA +import fnmatch import os import sys import warnings @@ -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] @@ -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``). """ @@ -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 ' + diff --git a/obspy/imaging/tests/test_scan.py b/obspy/imaging/tests/test_scan.py index 2e398a48fe1..16958c2007c 100644 --- a/obspy/imaging/tests/test_scan.py +++ b/obspy/imaging/tests/test_scan.py @@ -100,16 +100,14 @@ def test_scan_plot_by_id_with_wildcard(self): ('*.UH[12]*', ['BW.UH2..SHZ\n100.0%', 'BW.UH1..SHZ\n100.0%', - 'BW.UH1..EHZ\n10.7%', - ]), + '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%', - ])] + 'BW.UH1..EHZ\n10.7%'])] for seed_id, expected_labels in expected: fig, ax = plt.subplots()