Initial files to configure data querying and live websocket connections

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2025-05-20 15:24:00 -04:00
parent 96dfc06ee5
commit 6038fdef50
2 changed files with 392 additions and 0 deletions

338
datapolling.ipynb Normal file
View File

@ -0,0 +1,338 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"id": "e06a5cf6",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import obspy"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "434a13eb",
"metadata": {},
"outputs": [],
"source": [
"url = \"www.seismicportal.eu/fdsnws/event/1/query?limit=10&start=2020-01-01&end=2022-01-01&format=json\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e72fb8f7",
"metadata": {},
"outputs": [],
"source": [
"def geturl(url):\n",
" res = requests.get(\"https://\"+url, timeout=15)\n",
" return {'status': res.status_code,\n",
" 'content': res.text}"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "19355da2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"type\":\"FeatureCollection\",\"metadata\":{\"count\":10},\"features\":[{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" 122.38,\n",
" -8.11,\n",
" -10.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000155\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-02T06:11:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": 122.38,\n",
" \"auth\": \"DJA\",\n",
" \"lat\": -8.11,\n",
" \"depth\": 10.0,\n",
" \"unid\": \"20220101_0000155\",\n",
" \"mag\": 2.9,\n",
" \"time\": \"2022-01-01T23:50:06.0Z\",\n",
" \"source_id\": \"1083157\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"FLORES REGION, INDONESIA\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" 124.09,\n",
" -8.84,\n",
" -65.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000153\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-02T06:11:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": 124.09,\n",
" \"auth\": \"DJA\",\n",
" \"lat\": -8.84,\n",
" \"depth\": 65.0,\n",
" \"unid\": \"20220101_0000153\",\n",
" \"mag\": 3.3,\n",
" \"time\": \"2022-01-01T23:48:18.0Z\",\n",
" \"source_id\": \"1083154\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"KEPULAUAN ALOR, INDONESIA\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -173.92,\n",
" -21.37,\n",
" -10.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000137\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-02T08:20:00.0Z\",\n",
" \"magtype\": \"mb\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -173.92,\n",
" \"auth\": \"EMSC\",\n",
" \"lat\": -21.37,\n",
" \"depth\": 10.0,\n",
" \"unid\": \"20220101_0000137\",\n",
" \"mag\": 4.9,\n",
" \"time\": \"2022-01-01T23:47:39.0Z\",\n",
" \"source_id\": \"1083084\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"TONGA\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -66.64,\n",
" -23.73,\n",
" -233.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000136\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-01T23:56:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -66.64,\n",
" \"auth\": \"NSNA\",\n",
" \"lat\": -23.73,\n",
" \"depth\": 233.0,\n",
" \"unid\": \"20220101_0000136\",\n",
" \"mag\": 3.1,\n",
" \"time\": \"2022-01-01T23:45:36.0Z\",\n",
" \"source_id\": \"1083085\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"JUJUY, ARGENTINA\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -155.4,\n",
" 19.2,\n",
" -32.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000133\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-01T23:45:00.0Z\",\n",
" \"magtype\": \"md\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -155.4,\n",
" \"auth\": \"NEIR\",\n",
" \"lat\": 19.2,\n",
" \"depth\": 32.0,\n",
" \"unid\": \"20220101_0000133\",\n",
" \"mag\": 2.2,\n",
" \"time\": \"2022-01-01T23:42:34.8Z\",\n",
" \"source_id\": \"1083079\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"ISLAND OF HAWAII, HAWAII\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -16.21,\n",
" 28.09,\n",
" -8.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000202\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-02T20:45:00.0Z\",\n",
" \"magtype\": \"ml\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -16.21,\n",
" \"auth\": \"MDD\",\n",
" \"lat\": 28.09,\n",
" \"depth\": 8.0,\n",
" \"unid\": \"20220101_0000202\",\n",
" \"mag\": 1.8,\n",
" \"time\": \"2022-01-01T23:40:04.8Z\",\n",
" \"source_id\": \"1083360\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"CANARY ISLANDS, SPAIN REGION\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -69.31,\n",
" 18.08,\n",
" -10.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000134\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-01T23:51:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -69.31,\n",
" \"auth\": \"UASD\",\n",
" \"lat\": 18.08,\n",
" \"depth\": 10.0,\n",
" \"unid\": \"20220101_0000134\",\n",
" \"mag\": 3.1,\n",
" \"time\": \"2022-01-01T23:25:21.0Z\",\n",
" \"source_id\": \"1083082\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"DOMINICAN REPUBLIC REGION\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -74.06,\n",
" 18.95,\n",
" -10.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000132\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-01T23:36:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -74.06,\n",
" \"auth\": \"UASD\",\n",
" \"lat\": 18.95,\n",
" \"depth\": 10.0,\n",
" \"unid\": \"20220101_0000132\",\n",
" \"mag\": 3.1,\n",
" \"time\": \"2022-01-01T23:19:03.0Z\",\n",
" \"source_id\": \"1083078\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"HAITI REGION\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -98.05,\n",
" 16.31,\n",
" -8.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000141\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-02T02:01:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -98.05,\n",
" \"auth\": \"UNM\",\n",
" \"lat\": 16.31,\n",
" \"depth\": 8.0,\n",
" \"unid\": \"20220101_0000141\",\n",
" \"mag\": 3.3,\n",
" \"time\": \"2022-01-01T23:00:27.0Z\",\n",
" \"source_id\": \"1083098\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"OAXACA, MEXICO\"\n",
" }\n",
"},{\n",
" \"geometry\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [\n",
" -68.86,\n",
" -21.1,\n",
" -109.0\n",
" ]\n",
" },\n",
" \"type\": \"Feature\",\n",
" \"id\": \"20220101_0000131\",\n",
" \"properties\": {\n",
" \"lastupdate\": \"2022-01-01T23:08:00.0Z\",\n",
" \"magtype\": \"m\",\n",
" \"evtype\": \"ke\",\n",
" \"lon\": -68.86,\n",
" \"auth\": \"GUC\",\n",
" \"lat\": -21.1,\n",
" \"depth\": 109.0,\n",
" \"unid\": \"20220101_0000131\",\n",
" \"mag\": 2.7,\n",
" \"time\": \"2022-01-01T22:57:18.0Z\",\n",
" \"source_id\": \"1083075\",\n",
" \"source_catalog\": \"EMSC-RTS\",\n",
" \"flynn_region\": \"TARAPACA, CHILE\"\n",
" }\n",
"}]}\n"
]
}
],
"source": [
"res = geturl(url)\n",
"print(res['content'])\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sideprojects",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

54
datawebsocket.py Normal file
View File

@ -0,0 +1,54 @@
from __future__ import unicode_literals
from tornado.websocket import websocket_connect
from tornado.ioloop import IOLoop
from tornado import gen
import logging
import json
import sys
echo_uri = 'wss://www.seismicportal.eu/standing_order/websocket'
PING_INTERVAL = 15
#You can modify this function to run custom process on the message
def myprocessing(message):
try:
data = json.loads(message)
print(data)
info = data['data']['properties']
info['action'] = data['action']
logging.info('>>>> {action:7} event from {auth:7}, unid:{unid}, T0:{time}, Mag:{mag}, Region: {flynn_region}'.format(**info))
except Exception:
logging.exception("Unable to parse json message")
@gen.coroutine
def listen(ws):
while True:
msg = yield ws.read_message()
if msg is None:
logging.info("close")
ws = None
break
myprocessing(msg)
@gen.coroutine
def launch_client():
try:
logging.info("Open WebSocket connection to %s", echo_uri)
ws = yield websocket_connect(echo_uri, ping_interval=PING_INTERVAL)
except Exception:
logging.exception("connection error")
else:
logging.info("Waiting for messages...")
listen(ws)
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
ioloop = IOLoop.instance()
launch_client()
try:
ioloop.start()
except KeyboardInterrupt:
logging.info("Close WebSocket")
ioloop.stop()