def __call__( self, time_arg: event_time_type, time_before: Optional[float] = None, time_after: Optional[float] = None, *args, **kwargs, ) -> obspy.Stream: """ Using a reference time, return a waveforms that encompasses that time. Parameters ---------- time_arg The argument that will indicate a start time. Can be a one length events, and event, a float, or a UTCDatetime object time_before The time before time_arg to include in waveforms time_after The time after time_arg to include in waveforms Returns ------- obspy.Stream """ tbefore = to_timedelta64(time_before, default=self.time_before) tafter = to_timedelta64(time_after, default=self.time_after) assert (tbefore is not None) and (tafter is not None) # get the reference time from the object time = to_datetime64(get_reference_time(time_arg)) t1 = time - tbefore t2 = time + tafter return self.get_waveforms(starttime=t1, endtime=t2, **kwargs)
def test_correct_stream(self, bingham_dataset, bingham_stream_dict): """ ensure the correct streams are given for ids """ cat = bingham_dataset.event_client.get_events() evs = {str(ev.resource_id): ev for ev in cat} for eve_id, st in bingham_stream_dict.items(): ev = evs[eve_id] time2 = get_reference_time(ev).timestamp tmin = min([tr.stats.starttime.timestamp for tr in st]) assert abs(tmin - time2) < 12
def bank_null_loc_codes(self, tmpdir): """ create a bank that has nullish location codes in its streams. """ st = obspy.read() path = Path(tmpdir) for tr in st: tr.stats.location = "--" time = str(get_reference_time(tr)) name = time.split(".")[0].replace(":", "-") + f"_{tr.id}" tr.write(str(path / name) + ".mseed", "mseed") bank = WaveBank(path) bank.update_index() return bank
def crandall_catalog(crandall_ds): """ Return the crandall catalog. Add one P amplitude first. """ cat = crandall_ds.event_client.get_events() # create dict of origin times/eids ot_id = {get_reference_time(eve).timestamp: eve for eve in cat} min_time = min(ot_id) event = ot_id[min_time] # add noise time amplitudes for a few channels (just so noise spectra path = Path(TEST_DATA_PATH) / "crandall_noise_picks.xml" noisey_cat = obspy.read_events(str(path), "quakeml") event.picks.extend(noisey_cat[0].picks) event.amplitudes.extend(noisey_cat[0].amplitudes) return cat
def get_event_path( eve: Union[Catalog, Event], base_directory: str = ".", create_directories: bool = True, ext=".xml", ) -> str: """ Get a path for the event (or len(1) catalog) to save to disk. This function is used in internal NIOSH systems and as such is very opinionated about how events should be named. It does the following: The comments will first be scanned for seisan comments, identified with the "ID:" tag. If one is found the date string used in the seisan comment will be used for the file name. If a comment with the substring "ID:" is not found the name will be generated based on the preferred origin time. The last 5 characters of the event ID will be included in the file name. Parameters ---------- eve : obspy.Event The event to get a path for base_directory : str The path to the base directory create_directories: bool If True create any directories needed for path that do not exist ext : str The extension for the file to save Returns ------- str : The path """ if isinstance(eve, Catalog): assert len(eve) == 1, "events must have only one event" eve = eve[0] utc = get_reference_time(eve) path = get_utc_path(utc, base_dir=base_directory, create=create_directories) fname = _get_comment_path(eve, ext) or _get_file_name_from_event(eve, ext) return os.path.join(path, fname)
def _get_file_name_from_event(eve: Event, ext: str = ".xml") -> str: """ Generate a file name for the given event. Parameters ---------- eve ext Returns ------- """ # get utc and formatted list from event utc = get_reference_time(eve) fmt = "year month day hour minute second".split() utc_list = [UTC_FORMATS[x] % getattr(utc, x) for x in fmt] # append last 5 of resource_id rid_str = str(eve.resource_id)[-5:] utc_list.append(rid_str) fn = "%s-%s-%sT%s-%s-%s-%s" % tuple(utc_list) if ext: fn = fn + ext return fn