ObsPy - geoscience-community-codes/GISMO GitHub Wiki

ObsPy is an excellent package for building seismic data analysis workflows and applications in Python. It has a team of full-time developers, good documentation, an active user group, training courses. While there is plenty of overlap between what GISMO and ObsPy can do, ObsPy can do many things GISMO cannot, and GISMO can do some things ObsPy cannot. So given all this, why not just migrate all the GISMO functionality from MATLAB to Python - rebuild it around ObsPy?

There are a few reasons. First, there are a lot of seismologists using MATLAB for their research and their teaching. For many seismology students, MATLAB is the language they learn first and use throughout their studies. Therefore, they often have a collection of MATLAB functions and scripts and these would also be time consuming to migrate to ObsPy, learn a new language etc. Just think of all the Fortran programs that were written for seismology back in the 1960s, 1970s and 1980s. Many are still being used today! So we can safely say we need a good seismology toolbox for MATLAB.

On the other hand, it would make little sense to migrate ObsPy functionality to GISMO. A better approach is to develop workflows that incorporate ObsPy and GISMO, incorporating the strengths of each.

<hr>

April 14th, 2016

Hi GISMO users,

Here is a simple workflow to use ObsPy to read stream objects and convert them to GISMO waveform objects.

You call the MATLAB function obspy_stream2waveform with 3 arguments - your path to your python binary, your path where you save the attached python script, and your path to your data (this has to be something obspy.read() can read).

>> [w,scnl] = obspy_stream2waveform( '/Users/glennthompson/anaconda/bin/python', '/Users/glennthompson/obspy_stream2matfile.py', 'https://examples.obspy.org/BW.BGLD..EH.D.2010.037')
>> plot(w)

I only spent about an hour on this, so would be great to hear if someone has a better solution, e.g. a direct way to call ObsPy commands from within MATLAB other than going to the system command like I did. I’ve also only tried it on a few examples, so I’m interested to hear about issues.

Not in GISMO yet, but building some rudimentary GISMO-ObsPy translation is on the agenda.


stream2matfile.py
def stream2matfile(st):
    '''
	stream2matfile
		Convert an ObsPy Stream object into a set of MATLAB *.mat files (one per trace)

	Example:
	     import obspy
	     st = obspy.read("https://examples.obspy.org/BW.BGLD..EH.D.2010.037")
             import sys
             sys.path.append('/Users/glennthompson/src/GISMO/contributed/+obspy')
             import stream2matfile
             stream2matfile.stream2matfile(st)
    '''

    from scipy.io import savemat
    for i, tr in enumerate(st):
        mdict = {k: str(v) for k, v in tr.stats.iteritems()}
        mdict['data'] = tr.data
        savemat("obspy.stream.%s.%s.%s.%s.mat" % (tr.stats.network, tr.stats.station, tr.stats.location, tr.stats.channel), mdict)

# These lines allow the file to be called as a script with a command line argument
if __name__ == "__main__":

    import obspy, sys, os
    if len(sys.argv) > 0:
        if os.path.exists(sys.argv[1]):
            st = obspy.read(sys.argv[1])
    else:
        st = obspy.read("https://examples.obspy.org/BW.BGLD..EH.D.2010.037")

    # This is where the function is called
    stream2matfile(st)

stream_matfile2waveform.m
function [w,scnl]=stream_matfile2waveform(matfilepath)
%OBSPY.STREAM2WAVEFORM Use ObsPy to read a stream object and then convert
% into a waveform object. Also return a scnlobject.
% Example:
%  [w,scnl]=obspy.stream_matfile2waveform(matfilepath)
    tr=load(matfilepath)
    scnl=scnlobject(tr.station, tr.channel, tr.network, tr.location);
    %snum=(tr.starttime.timestamp/86400)+datenum(1970,1,1);
    snum=datenum('2007-08-28T00:00:00','yyyy-mm-ddTHH:MM:SS');
    w=waveform(scnl, tr.sampling_rate, snum, tr.data);
end
⚠️ **GitHub.com Fallback** ⚠️