From ddfb118027ba57948bfa31c37210fa49077a0c6e Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Thu, 6 Sep 2018 14:23:14 +0200 Subject: [PATCH] save raw waveforms and metadata in tmp dir save raw waveform data and station metadata in current obspyck instance's temporary directory for easy reuse (see #79) --- CHANGELOG.txt | 2 ++ obspyck/obspyck.py | 10 +++++++--- obspyck/util.py | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 11193fc..851c12d 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -15,6 +15,8 @@ master - make reference time given on command line the zero-time on time axis always (so far if an offset was given the zero time was equal to reference time plus offset, see #78) + - save raw waveform data and station metadata in current obspyck instance's + temporary directory for easy reuse (see #79) 0.5.1 - fix getting metadata via arclink (see #65) diff --git a/obspyck/obspyck.py b/obspyck/obspyck.py index 78e28a6..9c4341f 100755 --- a/obspyck/obspyck.py +++ b/obspyck/obspyck.py @@ -76,7 +76,7 @@ class ObsPyck(QtGui.QMainWindow): """ Main Window with the design loaded from the Qt Designer. """ - def __init__(self, clients, streams, options, keys, config): + def __init__(self, clients, streams, options, keys, config, inventories): """ Standard init. """ @@ -272,8 +272,12 @@ def __init__(self, clients, streams, options, keys, config): else: self.test_event_server = None + # save input raw data and metadata for eventual reuse + _save_input_data(streams, inventories, self.tmp_dir) + (warn_msg, merge_msg, streams) = \ merge_check_and_cleanup_streams(streams, options, config) + # if it's not empty show the merge info message now if merge_msg: self.info(merge_msg) @@ -4782,10 +4786,10 @@ def main(): # TODO: remove KEYS variable and lookup from config directly KEYS = {key: config.get('keys', key) for key in config.options('keys')} check_keybinding_conflicts(KEYS) - (clients, streams) = fetch_waveforms_with_metadata(options, args, config) + (clients, streams, inventories) = fetch_waveforms_with_metadata(options, args, config) # Create the GUI application qApp = QtGui.QApplication(sys.argv) - obspyck = ObsPyck(clients, streams, options, KEYS, config) + obspyck = ObsPyck(clients, streams, options, KEYS, config, inventories) qApp.connect(qApp, QtCore.SIGNAL("aboutToQuit()"), obspyck.cleanup) os._exit(qApp.exec_()) diff --git a/obspyck/util.py b/obspyck/util.py index 137debe..8b4796a 100644 --- a/obspyck/util.py +++ b/obspyck/util.py @@ -336,7 +336,8 @@ def fetch_waveforms_with_metadata(options, args, config): "-m overwrite" option. :returns: (dictionary with clients, - list(:class:`obspy.core.stream.Stream`s)) + list(:class:`obspy.core.stream.Stream`s), + list of Inventory) """ if not options.station_combinations and not options.seed_ids and not args: msg = ('No data to use specified. At least use one of option "-s", ' @@ -395,6 +396,7 @@ def fetch_waveforms_with_metadata(options, args, config): streams = [] sta_fetched = set() # Local files: + all_inventories = [] inventories = [] if args: print "=" * 80 @@ -444,6 +446,7 @@ def fetch_waveforms_with_metadata(options, args, config): if net_sta_loc in config.options("rotate_channels"): rotate_channels(stream_tmp_, net, sta, loc, config) streams.append(stream_tmp_) + all_inventories += inventories print "=" * 80 print "Fetching waveforms and metadata from servers:" @@ -490,6 +493,7 @@ def fetch_waveforms_with_metadata(options, args, config): inv = read_inventory(bio, format='XSEED') inventories.append(inv) _attach_metadata(st, inventories) + all_inventories += inventories # ArcLink elif server_type == "arclink": st = client.get_waveforms( @@ -506,6 +510,7 @@ def fetch_waveforms_with_metadata(options, args, config): inventories.append( read_inventory(bio, format='SEED')) _attach_metadata(st, inventories) + all_inventories += inventories # FDSN (or JANE) elif server_type in ("fdsn", "jane"): st = client.get_waveforms( @@ -516,6 +521,7 @@ def fetch_waveforms_with_metadata(options, args, config): network=net, station=sta, location=loc, level="response") _attach_metadata(st, inventory) + all_inventories += [inventory] # Seedlink elif server_type == "seedlink": # XXX I think the wild card checks for net/sta/loc can be @@ -547,6 +553,7 @@ def fetch_waveforms_with_metadata(options, args, config): network=net, station=sta, location=loc, level="response") _attach_metadata(st, inventory) + all_inventories += [inventory] else: raise NotImplementedError() # SDS @@ -580,6 +587,7 @@ def fetch_waveforms_with_metadata(options, args, config): network=net, station=sta, location=loc, level="response") _attach_metadata(st, inventory) + all_inventories += [inventory] else: raise NotImplementedError() sta_fetched.add(net_sta_loc) @@ -621,7 +629,7 @@ def fetch_waveforms_with_metadata(options, args, config): tr.stats['_format'] = "SDS" streams.append(st) print "=" * 80 - return (clients, streams) + return (clients, streams, all_inventories) def rotate_channels(st, net, sta, loc, config): @@ -1283,3 +1291,29 @@ def set_matplotlib_defaults(config): if config.has_section('matplotlibrc'): for key, value in config.items('matplotlibrc'): mpl.rcParams[key] = value + + +def _save_input_data(streams, inventories, directory): + """ + :type streams: list of Stream + :type inventories: list of Inventory + :type directory: str + """ + if not os.path.isdir(directory): + raise OSError('Not a directory: ' + str(directory)) + if isinstance(streams, (list, tuple)): + pass + elif isinstance(streams, Stream): + streams = [streams] + else: + raise TypeError + # write raw waveforms + st = Stream() + for st_ in streams: + st += st_ + st.write(os.path.join(directory, 'waveforms.mseed'), format='MSEED') + # write inventories + inv = Inventory(networks=[], source='') + for inv_ in inventories: + inv += inv_ + inv.write(os.path.join(directory, 'inventory.xml'), format='STATIONXML')