How to get the Antelope Python moment tensor code running

2012-02-07

ObsPy actually offers a complete package of NumPy, Matplotlib and SciPy (and multiple others) when you use their Mac OSX .dmg installer. This both eases and greatly improves the installation process.

2012-01-30

This project is in high flux. You have been warned! Also, it does not work against the latest Antelope 5.2 contributed code-base.

2011-07-29

We will be presenting the latest commit of the Antelope Moment Tensor Code at the AGU 2011 Fall Meeting in San Francisco on Friday, December 9th in the general Seismology section.

Contents

Pre-requisites

This how-to assumes that you have Python installed and running with Antelope. The recipe for that is beyond the scope of this how to. Please look at the Github website localmake FAQ provided by the Antelope Users Group or my own webpage for that...

top

Additional Python packages required

Getting all the pieces in place, you need to have the following:

  1. Numpy (pronounced NUM-pie)
  2. Matplotlib

First, to test you have numpy and matplotlib installed, write a simple Python script, here called tests.py:

  1. !/usr/bin/env python
  2. import numpy as np
  3. print np.__version__
  4. import matplotlib as mpl
  5. print mpl.__version__

This will either run or fail with an error:

  1. hostname {username} %1> ./tests.py
  2. Traceback (most recent call last):
  3. File "./tests.py", line 3, in <module>
  4. import numpy as np
  5. ImportError: No module named numpy

And a similar error will occur with Matplotlib.

top

Numpy & Matplotlib

So, we first need to get Numpy and Matplotlib. There are many ways of getting these libraries, including a DMG directly from SourceForge (Numpy & Matplotlib), however the easiest method is to install it via MacPorts. Installing MacPorts is beyond the scope of this tutorial, but it is a very nice way (read: easy!) of installing external (third-party) Python libraries. One word of warning – none of the official Python libraries like the Apple preinstalled Python. IMHO you are better off using MacPorts to install the latest and greatest Python too (2.7.2 at the time of writing). If you follow the directions below you can get the latest Python, the latest NumPy and the latest Matplotlib and Matplotlib toolkit for plotting data on map projections. Note that you need sudo privileges to run these commands!

  1. hostname {username} %2> sudo port install python27
  2. hostname {username} %4> sudo port install py27-numpy
  3. hostname {username} %5> sudo port install py27-matplotlib
  4. hostname {username} %5> sudo port install py27-matplotlib-basemap

Now running test.py again:

  1. hostname {username} %1> ./tests.py
  2. 1.6.1
  3. 1.1.0

top

Some warnings about Matplotlib

From the SourceForge page. I am not sure if this is relevant anymore – I didn't run into any problems with the MacPorts installer – but it may help some of you:

Building on OSX
The build situation on OSX is complicated by the various places one can get the png and freetype requirements from (darwinports, fink, /usr/X11R6) and the different architectures (x86, ppc, universal) and the different OSX version (10.4 and 10.5). We recommend that you build the way we do for the OSX release: by grabbing the tarbar or svn repository, cd-ing into the release/osx dir, and following the instruction in the README. This directory has a Makefile which will automatically grab the zlib, png and freetype dependencies from the web, build them with the right flags to make universal libraries, and then build the matplotlib source and binary installers.

Now expanding the script, and testing it:

  1. hostname {username} %16> cat tests.py
  2. #!!/usr/bin/env python
  3. import os
  4. import sys
  5. import numpy as np
  6. print np.__version__
  7. import matplotlib as mpl
  8. print mpl.__version__
  9. sys.path.append('%s/local/data/python' % os.environ['ANTELOPE'])
  10. from antelope.datascope import dbopen
  11. db = dbopen('/opt/antelope/data/db/demo/demo', 'r')
  12. print 'Lets print out the database pointer, with a database defined'
  13. print db

And running it......

  1. hostname {username} %17&gt; ./tests.py
  2. 1.6.1
  3. 1.1.0
  4.  
  5. Lets print out the database pointer, with a database defined
  6.  
  7. [Dbptr:
  8. database = 0
  9. table = -501
  10. field = -501
  11. record = -501
  12. ]

To test that your installs of numpy and matplotlib were successful, you might like to try copying, pasting and running some of the example code from the matplotlib website.

top

Generating the Green's functions

There are additional Python modules called fkrprog and fortran_fkrprog which can be used to generate Green's functions for your velocity model, using Python and Fortran respectively.

If you decide to use the fortran version you will need to make sure you have the latest GCC port installed that contains gfortran otherwise you won't be able to run it.

When you create the Green's functions, they are stored in their own wfdisc table, so it is important that in the dbmoment parameter file you specify a unique name for the dbpath for the Green's functions. An example of the wfdisc table is shown below:

They are created internally to the dbmoment script and automatically saved to the wfdisc. On subsequent runs of dbmoment, if your station distance and depth have a Green's function already calculated for your velocity model you can reuse it, rather than generating a new one each time.

top

Schema extensions for moment tensors

Note: This has been updated and the following fields have been removed: azimuth dip

moment_tensor_images table CSS3.0 schema extension

top

Plotting moment tensors: using ObsPy

Now, moving onto the moment tensor code. For this to work, you need to get ObsPy, an open-source package for plotting seismological data. This is available from the ObsPy website.

top

Install easy_install

To simply install many of the packages that are core to ObsPy, you need to get easy_install. Depending on your platform, you might already have this (test by typing 'which easy_install' on your command line). If you don't have it, this is available as part of the Python distribute package from http://pypi.python.org/pypi/distribute.

Use curl as follows:

  1. curl -0 http://python-distribute.org/distribute_setup.py > distribute_setup.py

This will make a local copy of the file distribute_setup.py. Now change the Python path on the first line of distribute_setup.py so it compiles against your Antelope Python. So change from:

  1. #!/python

To (where ANTVERSION is your version of Antelope, such as 5.1-64, 4.11p, etc):

  1. #!/opt/antelope/ANTVERSION/local/bin/python

Now executing distribute_setup.py (you will need sudo to do this):

  1. hostname rnewman$ sudo python distribute_setup.py
  2. Password:
  3. <snip>
  4. Adding distribute 0.6.14 to easy-install.pth file
  5. Installing easy_install script to /usr/local/bin
  6. Installing easy_install-2.6 script to /usr/local/bin
  7.  
  8. Installed /Library/Python/2.6/site-packages/distribute-0.6.14-py2.6.egg
  9. Processing dependencies for distribute==0.6.14
  10. Finished processing dependencies for distribute==0.6.14
  11. After install bootstrap.
  12. Creating /Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
  13. Creating /Library/Python/2.6/site-packages/setuptools.pth

top

Add ObsPy modules

Now use the newly minted easy_install to install the core ObsPy packages (as per http://www.obspy.org/wiki/InstallationInstructions#a2.1Prepackedmodulesrecommendedforendusers ). Going through them, one at a time:

  1. hostname rnewman$ easy_install -N obspy.core
  2. <snip>
  3. Using /Library/Python/2.6/site-packages/obspy.core-0.4.5-py2.6.egg

 

  1. hostname rnewman$ sudo easy_install -N obspy.mseed
  2. <snip>
  3. Installed /Library/Python/2.6/site-packages/obspy.mseed-0.4.5-py2.6-macosx-10.6-universal.egg

 

  1. hostname rnewman$ sudo easy_install -N obspy.sac
  2. <snip>
  3. Installed /Library/Python/2.6/site-packages/obspy.sac-0.4.5-py2.6.egg

 

  1. hostname rnewman$ sudo easy_install -N obspy.gse2
  2. <snip>
  3. Installed /Library/Python/2.6/site-packages/obspy.gse2-0.4.5-py2.6-macosx-10.6-universal.egg

 

  1. hostname rnewman$ sudo easy_install -N obspy.imaging
  2. <snip>
  3. Installed /Library/Python/2.6/site-packages/obspy.imaging-0.4.5-py2.6.egg

top

Testing ObsPy

Now you are ready to test the moment tensor creation code by importing the beachball class from the obspy.imaging library:

[Note: The beachball creation code taken from: http://www.obspy.org/wiki/ObspyTutorial#Beachballplot]

  1. #!/opt/antelope/4.11/local/bin/python
  2. import numpy as np
  3. import matplotlib as mpl
  4. from obspy.imaging.beachball import Beachball
  5. # Focal mechanism can be given by 3 (strike, dip and rake) components
  6. # or 6 independent components of the moment tensor (Mxx, Myy, Mzz, Mxy, Mxz, Myz)
  7. mt = [0.91, -0.89, -0.02, 1.78, -1.55, 0.47]
  8. Beachball(mt, size=200, linewidth=2, facecolor='b')
  9. mt2 = [150, 87, 1]
  10. Beachball(mt2, size=200, linewidth=2, facecolor='r')
  11. mt3 = [-2.39, 1.04, 1.35, 0.57, -2.94, -0.94]
  12. Beachball(mt3, size=200, linewidth=2, facecolor='g')

This should return you something like the following figures:

Voila! You now have moment tensors!

top

Running dbmoment

Now you have all the component pieces in place to run dbmoment. To get the latest release of dbmoment, you will need to add as a remote, clone, or otherwise get the my branch of contrib with the moment tensor codebase. The simplest way of doing this is to add my github account as a remote, and pull the branch.

top

Git instructions

The following code should be executed where you have the Antelope contributed code installed (typically $ANTELOPE/src/contrib). This will add my Github account as a remote repo, pull my repository to your localhost, then create a new local branch on your machine called moment_tensor_rob that tracks my remote branch (meaning that when I do updates you can pull them so you have the most up-to-date codebase). Then you checkout your new local branch moment_tensor_rob.

  1. git remote add github_rob git://github.com/robnewman/antelope_contrib.git
  2. git pull github_rob
  3. git branch --track moment_tensor_rob github_rob/moment_tensor
  4. git checkout moment_tensor_rob

top

Build it

Now you just need to go into the moment tensor directory and run make, make install! (assuming your current directory is $ANTELOPE/src/contrib).

  1.  
  2. > cd bin/export/moment_tensor
  3. > ls -la
  4. -rw-r--r-- 1 rnewman staff 414B Feb 6 11:46 Makefile2
  5. -rw-r--r-- 1 rnewman staff 683B Feb 6 11:46 PDS1_MODEL.pf
  6. -rw-r--r-- 1 rnewman staff 2.5K Feb 3 15:41 README
  7. -rw-r--r-- 1 rnewman staff 3.0K Feb 3 15:41 README.markdown
  8. -rw-r--r-- 1 rnewman staff 668B Feb 6 11:46 SOCAL_MODEL.pf
  9. -rw-r--r-- 1 rnewman staff 0B Jan 30 11:38 __init__.py
  10. -rwxr-xr-x 1 rnewman staff 75K Feb 7 10:06 dbmoment
  11. -rw-r--r-- 1 rnewman staff 3.5K Jan 30 11:38 dbmoment.1
  12. -rw-r--r-- 1 rnewman staff 2.6K Feb 2 11:49 dbmoment.pf
  13. -rwxr-xr-x 1 rnewman staff 75K Feb 7 10:15 dbmoment.xpy
  14. -rwxr-xr-x 1 rnewman staff 170K Feb 6 11:46 fkrprog.py
  15. -rw-r--r-- 1 rnewman staff 97K Dec 1 13:06 fkrprog.pyc
  16. -rwxr-xr-x 1 rnewman staff 143K Jan 31 13:53 fortran_fkrprog
  17. drwxr-xr-x 3 rnewman staff 102B Jan 31 13:53 fortran_fkrprog.dSYM
  18. -rw-r--r-- 1 rnewman staff 44K Jan 30 15:01 fortran_fkrprog.f
  19. -rw-r--r-- 1 rnewman staff 200B Jan 30 11:38 makefile
  20. -rwxr-xr-x 1 rnewman staff 1.2K Feb 6 11:46 run_fkrprog_wrapper
  21. -rwxr-xr-x 1 rnewman staff 1.0K Feb 6 11:46 run_fkrprog_wrapper.xpy
  22. > make
  23. rm -f dbmoment
  24. cat /opt/antelope/5.1-64/local/data/templates/xpy dbmoment.xpy > dbmoment
  25. chmod a+x dbmoment
  26. > make install
  27. deposit run_fkrprog_wrapper /opt/antelope/5.1-64/local/bin
  28. deposit fortran_fkrprog /opt/antelope/5.1-64/local/bin
  29. deposit dbmoment /opt/antelope/5.1-64/local/bin
  30. deposit dbmoment.pf /opt/antelope/5.1-64/local/data/pf
  31. deposit SOCAL_MODEL.pf /opt/antelope/5.1-64/local/data/pf
  32. deposit PDS1_MODEL.pf /opt/antelope/5.1-64/local/data/pf

top

Define your parameters

Now you have to copy the relevant parameter files to your real-time system and config them...

  1. > pwd
  2. /Users/rnewman/rtsystems/mt
  3. > ls -la
  4. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 bin
  5. drwxr-xr-x 17 rnewman staff 578B Feb 3 13:37 db
  6. drwxr-xr-x 14 rnewman staff 476B Nov 22 11:33 db_old
  7. drwxr-xr-x 5 rnewman staff 170B Nov 2 14:52 dbmaster
  8. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 idserver
  9. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 logs
  10. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 orb
  11. drwxr-xr-x 25 rnewman staff 850B Feb 6 11:51 pf
  12. -rw-r--r-- 1 rnewman staff 10K Dec 1 13:24 rtexec.pf
  13. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 rtlogs
  14. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 rtsys
  15. drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 state
  16. > cp $ANTELOPE/local/data/pf/dbmoment.pf pf
  17. > cp $ANTELOPE/local/data/pf/SOCAL_MODEL.pf pf
  18. > cp $ANTELOPE/local/data/pf/PDS1_MODEL.pf pf
  19. > pfecho pf/dbmoment.pf
  20. chan_to_use LH.*
  21. clip_values &Arr{
  22. max 1.6**23
  23. min -1.6**23
  24. }
  25. distance_weighting off
  26. event_db /Users/rnewman/rtsystems/mt/db/mt
  27. green_db /Users/rnewman/rtsystems/mt/greens_db
  28. isoflag 1
  29. mag_filters &Tbl{
  30. 2.0 4.0 120 BW_0.02_5_0.10_5
  31. 4.0 5.0 120 BW_0.02_5_0.05_5
  32. 5.0 6.0 120 BW_0.01_5_0.05_5
  33. 6.0 7.0 120 BW_0.01_5_0.07_5
  34. 7.0 120 BW_0.01_5_0.07_5
  35. }
  36. model_name SOCAL_MODEL
  37. model_type v
  38. mt_images_dir /Users/rnewman/rtsystems/mt/mtimages
  39. obspy_beachball &Arr{
  40. alpha
  41. bgcolor w
  42. edgecolor
  43. facecolor b
  44. fig
  45. format png
  46. linewidth 2
  47. nofill
  48. size 300
  49. width 200
  50. xy
  51. }
  52. pf_revision_time 1328212187
  53. resp_db demo/resp
  54. statmax 24
  55. trim_value 0.2
  56. ttfont /Library/Fonts/Verdana.ttf
  57. use_inc 0
  58. wave_db

You need to make sure you have the relevant velocity model for your region. We have included two models in the distribution: one for Southern California (SOCAL_MODEL.pf) and one for Canada (PDS1_MODEL.pf).

top

Execute dbmoment

Once everything is defined, you just need to run dbmoment with a valid origin number from your origin table!

  1. > rtrun dbmoment -v 12345
  2. Used PFPATH to determine which parameter file to use and found 'pf/dbmoment.pf'
  3. 2/07/2012 19:48:54.190 Get canned view from database
  4. 2/07/2012 19:48:54.265 Processing origin #: 96527
  5. 2/07/2012 19:48:54.291 There are 334 records between 1 & 10 deg that match this origin arrival and iphase
  6. 2/07/2012 19:48:54.304 Iterate over groups of stations in each azimuthal range. Requesting 3 per group
  7. 2/07/2012 19:48:54.304 - Working on group (NNW)
  8. 2/07/2012 19:48:54.304 - Get channel data (trace objects) for station (L11A)
  9. 2/07/2012 19:48:54.331 - Retrieve L11A data with time range (1203603264.91, 1203603984.91)
  10. 2/07/2012 19:48:54.475 - Trace extracted for R samples:[480], wanted:[600]
  11. 2/07/2012 19:48:54.508 - Trace extracted for T samples:[480], wanted:[600]
  12. 2/07/2012 19:48:54.541 - Trace extracted for Z samples:[480], wanted:[600]
  13. <snip>

If you want to learn what other command line arguments you have available or the format of the parameter file, you can type man dbmoment.

top

Disclaimer

This information is freely provided as–is. Messing around with the command line and creating files is a serious business, and I accept no liability for errors created, systems corrupted, or hard–disk damage by you following these instructions. They worked for me but may not work for you. Remember to back–up EVERYTHING before you try any of this stuff — it is not simple OR easy!!!

If you have any questions about this please email me at rlnewman@ucsd.edu and I will try my best to help you out.

Last modified: 2012-03-15 19:37:58