This allows seisplotjs in a web browser to be the seismogram display for obspy. A python http server is enabled to serve a stream, ie list of seismograms, to a browser. Load, process, share, then refresh.
First, of course, you must have obspy
installed.
Websockets must also be installed,
conda install -c conda-forge websockets
or pip install websockets
.
Start obspy
and then import serveobspy
and start the data server. The webdir
argument will allow the http server to serve the files needed and
so should be a directory that contains your index.html, the seisplotjs
standalone js file and viewObspy.js, tools.js and obspyconnection.js files.
In the example this is the
www directory. Note, some browser addons like NoScipt or Privacy Badger
may block requests and so if something breaks, try disabling any
of these that you have installed.
import serveobspy
serveSeis = serveobspy.ServeObsPy('www')
serveSeis.serveData()
This enables a http server on the default port of 8000 and a websocket on port 8001, listening only on localhost (127.0.0.1) for connections. Now you can go about your normal obspy processing steps. When you have data you wish to plot, pass it to the server to make it available.
import obspy
client = obspy.clients.fdsn.Client("IRIS")
start = obspy.UTCDateTime('2019-10-31T01:11:19')
st = client.get_waveforms("IU", "SNZO", "00", "BHZ", start, start + 20 * 60)
serveSeis.stream=st
serveSeis.title="SNZO Seismograms"
Now opening a web browser to http://127.0.0.1:8000 should show a plot of the seismograms loaded above. Additional calls to set stream will send a notification via the web socket to cause the browser to refresh the display. You can also trigger a refresh from the browser side as well. By default the display is just a column of seismograms, but as this is just a web page, anything you can do with html, css and javascript is possible to customize the display. Moreover, the browser also has the ability to load data and do calculations on the seismograms. The server can also serve earthquake information, in QuakeML, and station inventory information, in StationXML.
quake = client.get_events(starttime=start - 1*60, endtime=start + 20*60, minmagnitude=5)[0]
serveSeis.quake=quake
inventory = client.get_stations(network="IU", station="SNZO",
location="00", channel="BH?",
level="response",
starttime=start,
endtime=end)
serveSeis.inventory=inventory
Given this is just a web page, you can of course have multiple browsers open to the same page. If you serve with all hosts like
import serveobspy
serveSeis = serveobspy.ServeObsPy('www')
serveSeis.serveData(host='0.0.0.0')
then you can share the view of the data with browsers on other computers. Of course, this is a possible security issue, so be careful as the internal web server does no security checking.