def write(self, filename, format, **kwargs): """ Save stream to file including format specific headers. See `Stream.write() <obspy.core.stream.Stream.write>` in ObsPy. """ if len(self) == 0: return for tr in self: tr._write_format_specific_header(format) if format.upper() == 'Q': tr.stats.station = tr.id if format.upper() == 'H5': index = self.type if index is None and 'event_time' in self[0].stats: index = 'rf' if index: import obspyh5 old_index = obspyh5._INDEX obspyh5.set_index(_H5INDEX[index]) super(RFStream, self).write(filename, format, **kwargs) if format.upper() == 'H5' and index: obspyh5.set_index(old_index) if format.upper() == 'Q': for tr in self: tr.stats.station = tr.stats.station.split('.')[1]
def run_xcorr(stream=None, tw='Scoda', timenorm=None, write=False, write_all=False, **kw): if stream is None: stream = load_stream() name = f"{YEAR}_mag>{MAG}_q{QU}_{FILTER['freqmin']:02.0f}Hz-{FILTER['freqmax']:02.0f}Hz_{tw}_{timenorm}_dist<{DIST}km_{STACK[0]}{STACK[1]}" ccs = correlate_stream2(stream, tw=tw, timenorm=timenorm, max_dist=DIST, tw_len=TW_LEN, **kw) add_distbin(ccs, DIST) if STACK != 'max': if 'coda' in tw: ccs_stack1 = tw_stack(ccs.copy(), '{evid1}-{evid2}', stack_type=STACK) else: ccs_stack1 = copy(ccs).stack('{evid1}-{evid2}', stack_type=STACK) else: ccs_stack1 = _select_max(copy(ccs)) if write: obspyh5.set_index('waveforms/{stack.group}') ccs_stack1.write(f'{OUT}ccs_stack_{name}.h5', 'H5') if write_all: obspyh5.set_index( 'waveforms/{evid1}-{evid2}/{network}.{station}.{location}.{channel}' ) ccs.write(f'{OUT}ccs_{name}.h5', 'H5', ignore=('processing', )) return ccs, ccs_stack1
def set_index(index='rf'): import obspyh5 if index == 'rf': index = H5INDEX elif index == 'rf_stack': index = H5INDEX_STACK obspyh5.set_index(index)
def test_stored_index(self): stream = self.stream with NamedTemporaryFile(suffix='.h5') as ft: fname = ft.name stream.write(fname, 'H5') set_index('nonesens') stream.write(fname, 'H5', mode='a', override='ignore') set_index()
def test_trc_num(self): stream = self.stream.copy() set_index('waveforms/{trc_num:03d}') with NamedTemporaryFile(suffix='.h5') as ft: fname = ft.name stream.write(fname, 'H5') stream.write(fname, 'H5', mode='a', offset_trc_num=3) stream2 = read(fname, 'H5') for tr in stream2: del tr.stats._format set_index() self.assertEqual(len(stream2), 6) self.assertEqual(stream2[::2], stream) self.assertEqual(stream2[1::2], stream)
def test_read_files_saved_prior_version_0_3(self): stream = self.stream index_v_0_2 = ('{network}.{station}/{location}.{channel}/' '{starttime.datetime:%Y-%m-%dT%H:%M:%S}_' '{endtime.datetime:%Y-%m-%dT%H:%M:%S}') set_index(index_v_0_2) with NamedTemporaryFile(suffix='.h5') as ft: fname = ft.name stream.write(fname, 'H5', group='waveforms') stream2 = read(fname, 'H5', group='waveforms') stream3 = read(fname, 'H5') set_index() for tr in stream2: del tr.stats._format for tr in stream3: del tr.stats._format self.assertEqual(stream, stream2) self.assertEqual(stream, stream3)
def test_read_files_saved_prior_version_0_3(self): stream = self.stream index_v_0_2 = ('{network}.{station}/{location}.{channel}/' '{starttime.datetime:%Y-%m-%dT%H:%M:%S}_' '{endtime.datetime:%Y-%m-%dT%H:%M:%S}') with NamedTemporaryFile(suffix='.h5') as ft: fname = ft.name set_index(index_v_0_2) stream.write(fname, 'H5', group='waveforms') stream2 = read(fname, 'H5', group='waveforms') stream3 = read(fname, 'H5') for tr in stream2: del tr.stats._format for tr in stream3: del tr.stats._format self.assertEqual(stream, stream2) self.assertEqual(stream, stream3) set_index()
def write_h5_event_stream(dest_h5_file, stream, mode='a', ignore=()): """ Write stream to HDF5 file in event indexed format using obspy. :param dest_h5_file: File in which to write the stream. :type dest_h5_file: str or pathlib.Path :param stream: The stream to write :type stream: obspy.Stream :param mode: Write mode, such as 'w' or 'a'. Use 'a' to iteratively write multiple streams to one file. :type mode: str :param ignore: List of headers to ignore when writing attributes to group. Passed on directly to obspyh5.writeh5 :type ignore: Any iterable of str """ # Lock down format of obspyh5 node layout in HDF5 to ensure compatibility with # custom iterators. assert mode.lower() != 'r', 'Write mode cannot be \'r\'' prior_index = obspyh5._INDEX obspyh5.set_index(EVENTIO_H5INDEX) stream.write(dest_h5_file, 'H5', mode=mode, ignore=ignore) obspyh5.set_index(prior_index)
def test_hdf5_plugin_and_xcorr_index(self): set_index('xcorr') stream = self.stream.copy() for i, tr in enumerate(stream): # manipulate stats object station1, station2 = 'ST1', 'ST%d' % i channel1, channel2 = 'HHZ', u'HHN' s = tr.stats # we manipulate seed id so that important information gets # printed by obspy s.network, s.station = s.station1, s.channel1 = station1, channel1 s.location, s.channel = s.station2, s.channel2 = station2, channel2 s.network1 = s.network2 = 'BW' s.location1 = s.location2 = '' stream.sort() with NamedTemporaryFile(suffix='.h5') as ft: fname = ft.name stream.write(fname, 'H5') stream2 = read(fname).sort() for tr in stream2: del tr.stats._format set_index() self.assertEqual(stream, stream2)
def load(io, key, seedid=None, day=None, do='return', prep_kw={}, fname=None, format=None): """ Load object and do something with it :param io: io :param key: key of object to load (key inside HDF5 file, or one of data, prepdata, stations) :param seedid: seed id of a channel (for data or prepdata) :param day: |UTC| object with day (for data or prepdata) :param do: specifies what to do with the object, default is ``'return'`` which simply returns the object, other possible values are ``'print'`` -- print object (used by print command), ``'load'`` -- load object in IPython session (used by load command), ``'export'`` -- export correlations to different file format (used by export command) :param dict prep_kw: options passed to preprocess (for prepdata only) :param fname: file name (for export command) :param format: target format (for export command) """ if key == 'stations': obj = io['inventory'] elif key in ('data', 'prepdata'): if seedid is None or day is None: msg = 'seedid and day must be given for data or prepdata' raise ParseError(msg) if key == 'prepdata': prep_keys = ('remove_response', 'remove_response_options', 'demean', 'filter', 'normalization', 'time_norm_options', 'spectral_whitening_options', 'tolerance_shift', 'downsample') prep_kw = {k: prep_kw.get(k) for k in prep_keys} obj = _load_data(seedid, day, io['data'], io.get('data_format'), key, inventory=io['inventory'], **prep_kw) else: is_stretch = 't' in _analyze_key(key) fname_in = _get_fname(io, key) if is_stretch: obj = read_dicts(fname_in, key) if do == 'print': obj = '\n\n'.join(str(o) for o in obj) else: obj = obspy.read(fname_in, 'H5', group=key, headonly=do == 'print') if do == 'print': obj = obj.__str__(extended=True) if do == 'print': print(obj) elif do == 'load': _start_ipy(obj) elif do == 'return': return obj elif do == 'export': print('export', obj) if format == 'H5': obspyh5.set_index() obj.write(fname, format) if format == 'H5': from yam.io import INDEX obspyh5.set_index(INDEX) else: raise
import h5py import obspyh5 import yam import os.path import obspy from yam.util import _analyze_key, _get_fname INDEX = ('{key}/{network1}.{station1}-{network2}.{station2}/' '{location1}.{channel1}-{location2}.{channel2}/' '{starttime.datetime:%Y-%m-%dT%H:%M}') INDEX_STRETCH = ('{key}/{network1}.{station1}-{network2}.{station2}/' '{location1}.{channel1}-{location2}.{channel2}') obspyh5.set_index(INDEX) def write_dict(dict_, fname, mode='a', libver='latest', dtype='float16'): """ Write similarity matrix into HDF5 file :param dict_: Dictionary with stretching results (output from `~yam.stretch.stretch()`) :param fname: file name :param mode: file mode (default ``'a'`` -- write into file) :param libver: use latest version of HDF5 file format :param dtype: type for similarity matrix """ with h5py.File(fname, mode=mode, libver=libver) as f: f.attrs['file_format_stretch'] = 'yam'