def read_index(self, **kwargs) -> pd.DataFrame: """ Read the index and return a dataframe containing the event info. Parameters ---------- {get_events_params} """ self.ensure_bank_path_exists() # Make sure all times are numpy datetime64 kwargs = _dict_times_to_npdatetimes(kwargs) # a simple switch to prevent infinite recursion allow_update = kwargs.pop("_allow_update", True) # Circular search requires work to be done on the dataframe - we need # to get the whole dataframe then calculate the distances and search in # that circular_kwargs, kwargs = _sanitize_circular_search(**kwargs) with sql_connection(self.index_path) as con: try: df = _read_table(self._index_node, con, **kwargs) except pd.io.sql.DatabaseError: # if this database has never been updated, update now if allow_update and self.last_updated_timestamp < 1: self.update_index() return self.read_index(_allow_update=False, **kwargs) # else return empty index df = pd.DataFrame(columns=list(EVENT_TYPES_OUTPUT)) df = _ints_to_time_columns(df, columns=INT_COLUMNS).pipe( self._prepare_dataframe, dtypes=EVENT_TYPES_OUTPUT) if len(circular_kwargs) >= 3: # Requires at least latitude, longitude and min or max radius circular_ids = _get_ids(df, circular_kwargs) df = df[df.event_id.isin(circular_ids)] return df
def get_events(cat: obspy.Catalog, **kwargs) -> obspy.Catalog: """ Return a subset of a events filtered on input parameters. Parameters ---------- {get_event_parameters} """ # If not kwargs are passed just return all events if not kwargs: return cat # Make sure all inputs are supported if not set(kwargs).issubset(SUPPORTED_PARAMS): bad_params = set(kwargs) - SUPPORTED_PARAMS msg = f"{bad_params} are not supported get_events parameters" raise TypeError(msg) # Ensure all times are numpy datetimes kwargs = _dict_times_to_npdatetimes(kwargs) event_ids = _get_ids(obsplus.events_to_df(cat), kwargs) events = [eve for eve in cat if str(eve.resource_id) in event_ids] return obspy.Catalog(events=events)