from obspy.core.inventory import Channel import obsplus from obsplus.constants import STATION_COLUMNS, NSLC, STATION_DTYPES from obsplus.interfaces import BankType, EventClient from obsplus.structures.dfextractor import ( DataFrameExtractor, standard_column_transforms, ) from obsplus.utils import apply_to_files_or_skip, get_instances # attributes from channel to extract stations_to_df = DataFrameExtractor( Channel, STATION_COLUMNS, column_funcs=standard_column_transforms, dtypes=STATION_DTYPES, ) @stations_to_df.extractor() def _extract_from_channels(channel): """ extract info from channels. """ return {x: getattr(channel, x) for x in STATION_COLUMNS[5:]} @stations_to_df.register(obspy.Inventory) def _extract_channel(inventory: obspy.Inventory): """ Get a summary dataframe from the stations object """
from obsplus import load_dataset from obsplus.structures.dfextractor import DataFrameExtractor # create a dataframe extractor for magnitudes # note: because we are on python > 3.6 dicts are ordered. dtypes = { "magnitude": float, "azimuthal_gap": float, "station_count": int, "resource_id": str, "origin_id": str, "event_id": str, } ml_to_df = DataFrameExtractor(ev.Magnitude, required_columns=list(dtypes), dtypes=dtypes) # first extractor, get basic info from the magnitude object @ml_to_df.extractor def _get_basic(obj: ev.Magnitude): # check mag type, if not ML return None to not add a row for this object if obj.magnitude_type != "ML": return None out = dict( magnitude=obj.mag, resource_id=str(obj.resource_id), azimuthal_gap=obj.azimuthal_gap, origin_id=obj.origin_id, )
MAGNITUDE_COLUMN_TYPES, ) from obsplus.interfaces import BankType, EventClient from obsplus.structures.dfextractor import ( DataFrameExtractor, standard_column_transforms, ) from obsplus.utils.events import get_preferred from obsplus.utils.events import get_seed_id from obsplus.utils.misc import get_instances_from_tree, read_file, getattrs from obsplus.utils.time import get_reference_time # -------------------- init extractors events_to_df = DataFrameExtractor( ev.Event, required_columns=EVENT_COLUMNS, column_funcs=standard_column_transforms, dtypes=EVENT_DTYPES, ) picks_to_df = DataFrameExtractor(ev.Pick, PICK_COLUMNS, column_funcs=standard_column_transforms) arrivals_to_df = DataFrameExtractor(ev.Arrival, ARRIVAL_COLUMNS, column_funcs=standard_column_transforms) amplitudes_to_df = DataFrameExtractor(ev.Amplitude, AMPLITUDE_COLUMNS, column_funcs=standard_column_transforms)
import numpy as np import obspy import pandas as pd from obspy.core.event import Event, Catalog, WaveformStreamID from obspy.core.inventory import Channel import obsplus from obsplus.constants import STATION_COLUMNS, NSLC, STATION_DTYPES from obsplus.interfaces import BankType, EventClient from obsplus.structures.dfextractor import DataFrameExtractor from obsplus.utils import apply_to_files_or_skip, get_instances # attributes from channel to extract stations_to_df = DataFrameExtractor(Channel, STATION_COLUMNS, utc_columns=("start_date", "end_date")) @stations_to_df.extractor(dtypes=STATION_DTYPES) def _extract_from_channels(channel): """ extract info from channels. """ return {x: getattr(channel, x) for x in STATION_COLUMNS[5:]} @stations_to_df.register(obspy.Inventory) def _extract_channel(inventory: obspy.Inventory): """ Get a summary dataframe from the stations object """ extras = {}
MAGNITUDE_COLUMNS, MAGNITUDE_DTYPES, ARRIVAL_COLUMNS, ARRIVAL_DTYPES, NSLC, ) from obsplus.events.utils import get_reference_time, get_seed_id from obsplus.interfaces import BankType, EventClient from obsplus.structures.dfextractor import DataFrameExtractor from obsplus.utils import read_file, apply_to_files_or_skip, get_instances, getattrs from obsplus import get_preferred # -------------------- event extractors events_to_df = DataFrameExtractor(ev.Event, required_columns=EVENT_COLUMNS, utc_columns=("time", )) @events_to_df.extractor def _get_event_description(event): """ return a string of the first event description. """ try: return event.event_descriptions[0].text except (AttributeError, IndexError, TypeError): return None @events_to_df.extractor def _get_event_id(event): return str(event.resource_id)