{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Simple cross correlation to enhance pick accuracy\n",
    "\n",
    "This part is taken from the ObsPy tutorial at https://docs.obspy.org/master/tutorial/code_snippets/xcorr_pick_correction.html.\n",
    "\n",
    "This example shows how to align the waveforms of phase onsets of two earthquakes in order to correct the original pick times that can never be set perfectly consistent in routine analysis. A parabola is fit to the concave part of the cross correlation function around its maximum, following the approach by [Deichmann 1992](https://docs.obspy.org/master/citations.html#deichmann1992).\n",
    "\n",
    "To adjust the parameters (i.e. the used time window around the pick and the filter settings) and to validate and check the results the options plot and filename can be used to open plot windows or save the figure to a file.\n",
    "\n",
    "See the documentation of [xcorr_pick_correction()](https://docs.obspy.org/master/packages/autogen/obspy.signal.cross_correlation.xcorr_pick_correction.html#obspy.signal.cross_correlation.xcorr_pick_correction) for more details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# show figures inline\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('ggplot')                            # Matplotlib style sheet - nicer plots!\n",
    "plt.rcParams['figure.figsize'] = 12, 8             # Slightly bigger plots by default\n",
    "\n",
    "# python imports\n",
    "from obspy import read, UTCDateTime\n",
    "from obspy.signal.cross_correlation import xcorr_pick_correction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "# read example data of two small earthquakes\n",
    "path = \"https://examples.obspy.org/BW.UH1..EHZ.D.2010.147.%s.slist.gz\"\n",
    "st1 = read(path % (\"a\", ))\n",
    "st2 = read(path % (\"b\", ))\n",
    "\n",
    "# display info and plot data\n",
    "print(st1)\n",
    "st1.plot();\n",
    "print(st2)\n",
    "st2.plot();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "# select the single traces to use in correlation.\n",
    "# to avoid artifacts from preprocessing there should be some data left and\n",
    "# right of the short time window actually used in the correlation.\n",
    "tr1 = st1.select(component=\"Z\")[0]\n",
    "tr2 = st2.select(component=\"Z\")[0]\n",
    "\n",
    "# these are the original pick times set during routine analysis\n",
    "t1 = UTCDateTime(\"2010-05-27T16:24:33.315000Z\")\n",
    "t2 = UTCDateTime(\"2010-05-27T16:27:30.585000Z\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# estimate the time correction for pick 2 without any preprocessing and open\n",
    "# a plot window to visually validate the results\n",
    "dt, coeff = xcorr_pick_correction(t1, tr1, t2, tr2, 0.05, 0.2, 0.25, plot=True)\n",
    "print(\"No preprocessing:\")\n",
    "print(\"  Time correction for pick 2: %.6f s\" % dt)\n",
    "print(\"  Correlation coefficient: %.2f\" % coeff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# estimate the time correction with bandpass prefiltering\n",
    "dt, coeff = xcorr_pick_correction(t1, tr1, t2, tr2, 0.05, 0.2, 0.25, plot=True,\n",
    "                                  filter=\"bandpass\",\n",
    "                                  filter_options={'freqmin': 1, 'freqmax': 10})\n",
    "print(\"Bandpass prefiltering:\")\n",
    "print(\"  Time correction for pick 2: %.6f s\" % dt)\n",
    "print(\"  Correlation coefficient: %.2f\" % coeff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
