def get_data_coverage_figure(station_ids, channel_codes, startdate, enddate, datafilepathfunc): """ :type station_ids: list :param station_ids: station ids as a list of strings :type channel_codes: list :param channel_codes: data channel codes as a list of strings :type startdate: :class:`~obspy.core.utcdatetime.UTCDateTime` :param startdate: scanning startdate :type enddate: :class:`~obspy.core.utcdatetime.UTCDateTime` :param enddate: scanning enddate :type filepathfunc: func :param filepathfunc: A dynamic filepath, wildcards can be used since :func:`~glob.glob` is use to process filepaths after the arguments are filled in. :rtype: :class:`~matplotlib.figure.Figure` :return: scanner plot """ scanner = Scanner() paths = [] pathkwargs = {} pathkwargs["location_code"] = "*" network_and_station_codes = (s.split(".") for s in station_ids) combinations = \ list(itertools.product(network_and_station_codes, channel_codes)) date = startdate added_channels = set() while date <= enddate: pathkwargs["year"] = date.year pathkwargs["julday"] = date.julday for (network_code, station_code), channel_code in combinations: pathkwargs["network_code"] = network_code pathkwargs["station_code"] = station_code pathkwargs["channel_code"] = channel_code trace_key = network_code + station_code + channel_code paths = datafilepathfunc(**pathkwargs) for path in glob.glob(paths): scanner.parse(path) added_channels.add(trace_key) date += 86400 count = len(added_channels) if count: height = 1.0 + count * 0.5 height = min(height, 655.35) fig = scanner.plot(show=False, starttime=startdate, endtime=enddate) fig.set_size_inches(20.0, height) fig.set_facecolor(colors.WHITE) fig.tight_layout() return fig return null_figure()
def test_obspy_scanner(self): scanner = Scanner("MSEED") scanner.parse(TestMseed.root_path) print(scanner.data.keys()) print(scanner.data) scanner.analyze_parsed_data(True) scanner.plot()
def test_scanner_manually_add_streams(self): """ Test Scanner class, manually adding streams of read data files """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in self.all_files: shutil.copy(filename, os.curdir) for file_ in os.listdir(os.curdir): # some files used in the test cases actually can not # be read with obspy.. if file_ in ('STA2.testlines_out', 'STA2.testlines', 'seism-shorter.sac', 'seism-longer.sac'): continue st = read(file_, headonly=True) scanner.add_stream(st) 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_scan_save_load_npz(self): """ Run obspy-scan on selected tests/data directories, saving/loading to/from npz. Tests both the command line script and the Scanner class. """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in self.all_files: shutil.copy(filename, os.curdir) obspy_scan([os.curdir, '--write', 'scan.npz', '--quiet']) scanner.parse(os.curdir) scanner.save_npz('scanner.npz') scanner = Scanner() scanner.load_npz('scanner.npz') with ImageComparison(self.path, 'scan.png') as ic: obspy_scan(['--load', 'scan.npz', '--output', ic.name, '--quiet']) with ImageComparison(self.path, 'scan.png') as ic: scanner.plot(ic.name)
def data_coverage(paths=[], starttime=None, endtime=None, scale=lambda x: x, invalid_value=0.0): """ Calculates the datacoverage percentage between ``starttime`` and ``endtime``. See :class:`~obspy.imaging.scripts.scan.Scanner` and :meth:`~obspy.imaging.scripts.scan.Scanner.analyze_parsed_data` for more information. :type paths: list :param paths: list of filepaths (as string values) to the datefiles to be scanned :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime` :param starttime: :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime` :param endtime: :type scale: func, optional :param scale: defaults to identity function, scaling function applied to data values :type invalid_value: float, optional :param invalid_value: default to ``0.0`` :rtype: list or None :return: list of lists containing timestamp (as :class:`~obspy.core.utcdatetime.UTCDateTime` instance), data value, and additional z value as string. """ if starttime is None or endtime is None: return None if starttime > endtime: starttime, endtime = endtime, starttime st0 = Stream() for path in paths: st = fileutils.get_stream(path) if st is None: continue st0 += st percentage = invalid_value # Arbitrary z_value as string following python dictionary syntax z_value = "{{'starttime':'{}'}}".format(str(starttime)) if len(st0) > 0: scanner = Scanner() scanner.add_stream(st0) scanner.analyze_parsed_data(starttime=starttime, endtime=endtime) percentage = scanner._info[st0[0].id]['percentage'] percentage = invalid_value if percentage is None else percentage percentage = scale(percentage) return [[endtime, percentage, z_value]]
def test_scan_function_and_scanner_class(self, all_files, image_path): """ Test scan function and Scanner class (in one test to keep overhead of copying files down) """ scanner = Scanner() path1 = image_path.parent / 'scan1.png' path2 = image_path.parent / 'scan2.png' # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in all_files: shutil.copy(filename, os.curdir) scanner.parse(os.curdir) scan(paths=os.curdir, plot=path1) scanner.plot(path2)
def test_scan_function_and_scanner_class(self): """ Test scan function and Scanner class (in one test to keep overhead of copying files down) """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in self.all_files: shutil.copy(filename, os.curdir) scanner.parse(os.curdir) with ImageComparison(self.path, 'scan.png') as ic: scan(paths=os.curdir, plot=ic.name) with ImageComparison(self.path, 'scan.png') as ic: scanner.plot(ic.name)
def test_scanner_manually_add_streams(self, all_files, image_path): """ Test Scanner class, manually adding streams of read data files """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in all_files: shutil.copy(filename, os.curdir) for file_ in os.listdir(os.curdir): # some files used in the test cases actually can not # be read with obspy.. if file_ in ('STA2.testlines_out', 'STA2.testlines', 'seism-shorter.sac', 'seism-longer.sac'): continue st = read(file_, headonly=True) scanner.add_stream(st) scanner.plot(str(image_path))
def test_scan_save_load_npz(self): """ Run obspy-scan on selected tests/data directories, saving/loading to/from npz. Tests both the command line script and the Scanner class. """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in self.all_files: shutil.copy(filename, os.curdir) # save via command line obspy_scan([os.curdir, '--write', 'scan.npz']) # save via Python scanner.parse(os.curdir) scanner.save_npz('scanner.npz') scanner = Scanner() # version string of '0.0.0+archive' raises UserWarning - ignore with warnings.catch_warnings(record=True): warnings.simplefilter('ignore', UserWarning) # load via Python scanner.load_npz('scanner.npz') with ImageComparison(self.path, 'scan.png') as ic: scanner.plot(ic.name) # load via command line with ImageComparison(self.path, 'scan.png') as ic: obspy_scan(['--load', 'scan.npz', '--output', ic.name])
def data_coverage_image(flags, starttime, endtime, network_code, station_codes, channel_codes, datafilepathfunc, outfilepathfunc): """ Creates data coverage images using :class:`~obspy.imaging.scripts.scan.Scanner`. The available seismic data is scanned between times ``starttime`` and ``endtime``. All channel and station combinations are included in the scan (``channel_codes`` and ``station_codes``). :type flags: dict :param flags: Flag variables from :class:`~polyfemos.back.interpreter.Interpreter` :type startime: :class:`~polyfemos.almanac.ordinal.Ordinal` :param startime: :type endtime: :class:`~polyfemos.almanac.ordinal.Ordinal` :param endtime: :type network_code: str :param network_code: Network code as a string, e.g. "FN" :type station_codes: list :param station_codes: list of string consisting of station codes, e.g. ``["MSF", "SGF", ...]``. :type channel_codes: list :param channel_codes: list of string consisting of channel codes, e.g. ``["HHZ", "HHE", ...]``. :type datafilepathfunc: func :param datafilepathfunc: A function returning filepaths to be included in the scan. :type outfilepathfunc: func :param outfilepathfunc: If the ``outfilepathfunc`` returns different filepaths with changing stations or channels etc., separate scanner plots are created with each different filepath. """ # scanners dict stores 'outfilepath' and their corresponding scanner # instances scanners = {} pathkwargs = {} added_traces = set() _product = itertools.product(station_codes, channel_codes) for station_code, channel_code in _product: tr_id = "{}{}{}".format(network_code, station_code, channel_code) pathkwargs["network_code"] = network_code pathkwargs["station_code"] = station_code pathkwargs["channel_code"] = channel_code # Location in the seismic datafile paths is set to be anything pathkwargs["location_code"] = "*" for time_ in Ordinal.range(starttime, endtime): pathkwargs["year"] = time_.year pathkwargs["julday"] = time_.julday datafilepath = datafilepathfunc(**pathkwargs) outfilepath = outfilepathfunc(**pathkwargs) if outfilepath not in scanners: scanners[outfilepath] = Scanner() file_tr_key = outfilepath + tr_id # glob allows the usage of wildcards in the filepath for path in glob.glob(datafilepath): added_traces.add(file_tr_key) scanners[outfilepath].parse(path) if file_tr_key not in added_traces: tr = Trace() tr.stats.network = network_code tr.stats.station = station_code tr.stats.channel = channel_code tr_id = tr.get_id() st = Stream(traces=[tr]) scanners[outfilepath].add_stream(st) added_traces.add(file_tr_key) for outfile, scanner in scanners.items(): scanner.plot(outfile=outfile, starttime=starttime, endtime=endtime)
def test_scan_save_load_npz(self): """ Run obspy-scan on selected tests/data directories, saving/loading to/from npz. Tests both the command line script and the Scanner class. """ scanner = Scanner() # Copy files to a temp folder to avoid wildcard scans. with TemporaryWorkingDirectory(): for filename in self.all_files: shutil.copy(filename, os.curdir) obspy_scan([os.curdir, '--write', 'scan.npz', '--quiet']) scanner.parse(os.curdir) scanner.save_npz('scanner.npz') scanner = Scanner() scanner.load_npz('scanner.npz') with ImageComparison(self.path, 'scan.png') as ic: obspy_scan( ['--load', 'scan.npz', '--output', ic.name, '--quiet']) with ImageComparison(self.path, 'scan.png') as ic: scanner.plot(ic.name)