{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Added Obspy Methods\n", "Obsplus adds methods to existing obspy data structures in order to \"unify\" the data-requesting interfaces (ie the `get_whatever` methods). This is done dynamically upon importing obsplus using a technique known as [monkeypatching](https://stackoverflow.com/a/6647776/3645626). You can then write source-agnostic code; any seismological data source, from in-memory obspy data structures, a directory of files, or a remote resource like IRIS can be treated the same way. If the data sources need to be changed it can be done seamlessly. \n", "\n", "The following figure demonstrates this concept with waveform data. In-memory (`Stream`), on disk (`WaveBank`), or remote (`Client`) all share the `get_waveforms` method. \n", "\n", "![get_waveforms_diagram](../../images/get_waveforms.png)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:58.184233Z", "iopub.status.busy": "2025-06-10T20:43:58.184050Z", "iopub.status.idle": "2025-06-10T20:43:58.485758Z", "shell.execute_reply": "2025-06-10T20:43:58.485208Z" } }, "outputs": [], "source": [ "# get a catalog, stream, and inventory to play with\n", "import obspy\n", "\n", "cat = obspy.read_events()\n", "st = obspy.read()\n", "inv = obspy.read_inventory()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Catalog " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inventory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stream" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:58.487966Z", "iopub.status.busy": "2025-06-10T20:43:58.487691Z", "iopub.status.idle": "2025-06-10T20:43:58.490382Z", "shell.execute_reply": "2025-06-10T20:43:58.489856Z" } }, "outputs": [], "source": [ "t1 = st[0].stats.starttime + 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting client_like objects\n", "Obsplus has a few convenience functions for getting client-like objects (meaning they implement the appropriate `get_whatever` methods) from various sources. For example, the object returned after calling `get_waveform_client` is guaranteed to have a `get_waveforms` method or the function will raise a `TypeError`. The input could be a single miniseed file, a directory of waveforms files, a `Stream` object, or a valid waveform client. \n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:58.491853Z", "iopub.status.busy": "2025-06-10T20:43:58.491690Z", "iopub.status.idle": "2025-06-10T20:43:59.896518Z", "shell.execute_reply": "2025-06-10T20:43:59.896010Z" } }, "outputs": [], "source": [ "import tempfile\n", "from pathlib import Path\n", "\n", "from obspy.clients.fdsn import Client\n", "\n", "from obsplus import get_waveform_client\n", "\n", "cl = Client()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:59.898752Z", "iopub.status.busy": "2025-06-10T20:43:59.898315Z", "iopub.status.idle": "2025-06-10T20:43:59.901010Z", "shell.execute_reply": "2025-06-10T20:43:59.900623Z" } }, "outputs": [], "source": [ "# get waveform client from various sources" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:59.902633Z", "iopub.status.busy": "2025-06-10T20:43:59.902374Z", "iopub.status.idle": "2025-06-10T20:43:59.908067Z", "shell.execute_reply": "2025-06-10T20:43:59.907588Z" } }, "outputs": [], "source": [ "# save a single file to disk\n", "p = Path(tempfile.mkdtemp()) / \"stream.mseed\"\n", "p.parent.mkdir(exist_ok=True, parents=True)\n", "st.write(str(p), \"mseed\")\n", "# get client from a single file" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:59.909874Z", "iopub.status.busy": "2025-06-10T20:43:59.909575Z", "iopub.status.idle": "2025-06-10T20:43:59.913503Z", "shell.execute_reply": "2025-06-10T20:43:59.912983Z" } }, "outputs": [], "source": [ "# create a directory of waveforms, get a client\n", "p = Path(tempfile.mkdtemp())\n", "p.mkdir(exist_ok=True, parents=True)\n", "for num, tr in enumerate(st):\n", " tr.write(str(p / f\"{num}.mseed\"), \"mseed\")\n", "# get client from waveform directory" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-06-10T20:43:59.915260Z", "iopub.status.busy": "2025-06-10T20:43:59.914920Z", "iopub.status.idle": "2025-06-10T20:43:59.918266Z", "shell.execute_reply": "2025-06-10T20:43:59.917812Z" } }, "outputs": [], "source": [ "# A TypeErorr is raised if get_waveform_client cannot get a waveform_client\n", "try:\n", " get_waveform_client(cat)\n", "except TypeError:\n", " pass" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 2 }