Skip to content

Instantly share code, notes, and snippets.

@d-chambers
Created February 22, 2018 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-chambers/a1ef831a4bb207d9cfaca5a1d4b1dc74 to your computer and use it in GitHub Desktop.
Save d-chambers/a1ef831a4bb207d9cfaca5a1d4b1dc74 to your computer and use it in GitHub Desktop.
wrangling output of obspy coincidence trigger
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Processing output of obspy [coincidence trigger](https://docs.obspy.org/tutorial/code_snippets/trigger_tutorial.html#network-coincidence-trigger-example)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import obspy\n",
"import pandas as pd\n",
"from obspy.signal.trigger import coincidence_trigger"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# get data (same as from tutorial)\n",
"st = obspy.Stream()\n",
"\n",
"files = [\"BW.UH1..SHZ.D.2010.147.cut.slist.gz\",\n",
" \"BW.UH2..SHZ.D.2010.147.cut.slist.gz\",\n",
" \"BW.UH3..SHZ.D.2010.147.cut.slist.gz\",\n",
" \"BW.UH4..SHZ.D.2010.147.cut.slist.gz\"]\n",
"\n",
"for filename in files:\n",
" st += obspy.read(\"https://examples.obspy.org/\" + filename)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"st.filter('bandpass', freqmin=10, freqmax=20) # optional prefiltering\n",
"st2 = st.copy()\n",
"trig = coincidence_trigger(\"recstalta\", 3.5, 1, st2, 3, sta=0.5, lta=10)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'time': UTCDateTime(2010, 5, 27, 16, 24, 33, 209999), 'stations': ['UH3', 'UH2', 'UH1', 'UH4'], 'trace_ids': ['BW.UH3..SHZ', 'BW.UH2..SHZ', 'BW.UH1..SHZ', 'BW.UH4..SHZ'], 'coincidence_sum': 4.0, 'similarity': {}, 'duration': 4.510000228881836}, {'time': UTCDateTime(2010, 5, 27, 16, 27, 1, 260000), 'stations': ['UH2', 'UH3', 'UH1'], 'trace_ids': ['BW.UH2..SHZ', 'BW.UH3..SHZ', 'BW.UH1..SHZ'], 'coincidence_sum': 3.0, 'similarity': {}, 'duration': 3.440000057220459}, {'time': UTCDateTime(2010, 5, 27, 16, 27, 30, 510000), 'stations': ['UH3', 'UH2', 'UH1', 'UH4'], 'trace_ids': ['BW.UH3..SHZ', 'BW.UH2..SHZ', 'BW.UH1..SHZ', 'BW.UH4..SHZ'], 'coincidence_sum': 4.0, 'similarity': {}, 'duration': 4.769999742507935}]\n"
]
}
],
"source": [
"print(trig)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'time': UTCDateTime(2010, 5, 27, 16, 24, 33, 209999), 'duration': 4.510000228881836}, {'time': UTCDateTime(2010, 5, 27, 16, 27, 1, 260000), 'duration': 3.440000057220459}, {'time': UTCDateTime(2010, 5, 27, 16, 27, 30, 510000), 'duration': 4.769999742507935}]\n"
]
}
],
"source": [
"# get data using a list comprehension (only keep trigger time and duration)\n",
"desired_columns = ('time', 'duration')\n",
"\n",
"new = [{x: sub_trig[x] for x in desired_columns} \n",
" for sub_trig in trig]\n",
"\n",
"print(new)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" coincidence_sum duration similarity stations \\\n",
"0 4.0 4.51 {} [UH3, UH2, UH1, UH4] \n",
"1 3.0 3.44 {} [UH2, UH3, UH1] \n",
"2 4.0 4.77 {} [UH3, UH2, UH1, UH4] \n",
"\n",
" time \\\n",
"0 2010-05-27T16:24:33.210000Z \n",
"1 2010-05-27T16:27:01.260000Z \n",
"2 2010-05-27T16:27:30.510000Z \n",
"\n",
" trace_ids \n",
"0 [BW.UH3..SHZ, BW.UH2..SHZ, BW.UH1..SHZ, BW.UH4... \n",
"1 [BW.UH2..SHZ, BW.UH3..SHZ, BW.UH1..SHZ] \n",
"2 [BW.UH3..SHZ, BW.UH2..SHZ, BW.UH1..SHZ, BW.UH4... \n"
]
}
],
"source": [
"# use pandas\n",
"df = pd.DataFrame(trig)\n",
"print(df)"
]
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment