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
- Additional Python packages required
- Generating the Green's functions
- Schema extensions for moment tensors
- Plotting moment tensors: using ObsPy
- Running dbmoment
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...
Additional Python packages required
Getting all the pieces in place, you need to have the following:
- Numpy (pronounced NUM-pie)
- Matplotlib
First, to test you have numpy and matplotlib installed, write a simple Python script, here called tests.py:
!/usr/bin/env python import numpy as np print np.__version__ import matplotlib as mpl print mpl.__version__
This will either run or fail with an error:
hostname {username} %1> ./tests.py Traceback (most recent call last): File "./tests.py", line 3, in <module> import numpy as np ImportError: No module named numpy
And a similar error will occur with Matplotlib.
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!
hostname {username} %2> sudo port install python27 hostname {username} %4> sudo port install py27-numpy hostname {username} %5> sudo port install py27-matplotlib hostname {username} %5> sudo port install py27-matplotlib-basemap
Now running test.py again:
hostname {username} %1> ./tests.py 1.6.1 1.1.0
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:
hostname {username} %16> cat tests.py #!!/usr/bin/env python import os import sys import numpy as np print np.__version__ import matplotlib as mpl print mpl.__version__ sys.path.append('%s/local/data/python' % os.environ['ANTELOPE']) from antelope.datascope import dbopen db = dbopen('/opt/antelope/data/db/demo/demo', 'r') print 'Lets print out the database pointer, with a database defined' print db
And running it......
hostname {username} %17> ./tests.py 1.6.1 1.1.0 Lets print out the database pointer, with a database defined [Dbptr: database = 0 table = -501 field = -501 record = -501 ]
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.
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.
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
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.
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:
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:
#!/python
To (where ANTVERSION is your version of Antelope, such as 5.1-64, 4.11p, etc):
#!/opt/antelope/ANTVERSION/local/bin/python
Now executing distribute_setup.py (you will need sudo to do this):
hostname rnewman$ sudo python distribute_setup.py Password: <snip> Adding distribute 0.6.14 to easy-install.pth file Installing easy_install script to /usr/local/bin Installing easy_install-2.6 script to /usr/local/bin Installed /Library/Python/2.6/site-packages/distribute-0.6.14-py2.6.egg Processing dependencies for distribute==0.6.14 Finished processing dependencies for distribute==0.6.14 After install bootstrap. Creating /Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg-info Creating /Library/Python/2.6/site-packages/setuptools.pth
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:
hostname rnewman$ easy_install -N obspy.core <snip> Using /Library/Python/2.6/site-packages/obspy.core-0.4.5-py2.6.egg
hostname rnewman$ sudo easy_install -N obspy.mseed <snip> Installed /Library/Python/2.6/site-packages/obspy.mseed-0.4.5-py2.6-macosx-10.6-universal.egg
hostname rnewman$ sudo easy_install -N obspy.sac <snip> Installed /Library/Python/2.6/site-packages/obspy.sac-0.4.5-py2.6.egg
hostname rnewman$ sudo easy_install -N obspy.gse2 <snip> Installed /Library/Python/2.6/site-packages/obspy.gse2-0.4.5-py2.6-macosx-10.6-universal.egg
hostname rnewman$ sudo easy_install -N obspy.imaging <snip> Installed /Library/Python/2.6/site-packages/obspy.imaging-0.4.5-py2.6.egg
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]
#!/opt/antelope/4.11/local/bin/python import numpy as np import matplotlib as mpl from obspy.imaging.beachball import Beachball # Focal mechanism can be given by 3 (strike, dip and rake) components # or 6 independent components of the moment tensor (Mxx, Myy, Mzz, Mxy, Mxz, Myz) mt = [0.91, -0.89, -0.02, 1.78, -1.55, 0.47] Beachball(mt, size=200, linewidth=2, facecolor='b') mt2 = [150, 87, 1] Beachball(mt2, size=200, linewidth=2, facecolor='r') mt3 = [-2.39, 1.04, 1.35, 0.57, -2.94, -0.94] Beachball(mt3, size=200, linewidth=2, facecolor='g')
This should return you something like the following figures:
Voila! You now have moment tensors!
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.
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.
git remote add github_rob git://github.com/robnewman/antelope_contrib.git git pull github_rob git branch --track moment_tensor_rob github_rob/moment_tensor git checkout moment_tensor_rob
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).
> cd bin/export/moment_tensor > ls -la -rw-r--r-- 1 rnewman staff 414B Feb 6 11:46 Makefile2 -rw-r--r-- 1 rnewman staff 683B Feb 6 11:46 PDS1_MODEL.pf -rw-r--r-- 1 rnewman staff 2.5K Feb 3 15:41 README -rw-r--r-- 1 rnewman staff 3.0K Feb 3 15:41 README.markdown -rw-r--r-- 1 rnewman staff 668B Feb 6 11:46 SOCAL_MODEL.pf -rw-r--r-- 1 rnewman staff 0B Jan 30 11:38 __init__.py -rwxr-xr-x 1 rnewman staff 75K Feb 7 10:06 dbmoment -rw-r--r-- 1 rnewman staff 3.5K Jan 30 11:38 dbmoment.1 -rw-r--r-- 1 rnewman staff 2.6K Feb 2 11:49 dbmoment.pf -rwxr-xr-x 1 rnewman staff 75K Feb 7 10:15 dbmoment.xpy -rwxr-xr-x 1 rnewman staff 170K Feb 6 11:46 fkrprog.py -rw-r--r-- 1 rnewman staff 97K Dec 1 13:06 fkrprog.pyc -rwxr-xr-x 1 rnewman staff 143K Jan 31 13:53 fortran_fkrprog drwxr-xr-x 3 rnewman staff 102B Jan 31 13:53 fortran_fkrprog.dSYM -rw-r--r-- 1 rnewman staff 44K Jan 30 15:01 fortran_fkrprog.f -rw-r--r-- 1 rnewman staff 200B Jan 30 11:38 makefile -rwxr-xr-x 1 rnewman staff 1.2K Feb 6 11:46 run_fkrprog_wrapper -rwxr-xr-x 1 rnewman staff 1.0K Feb 6 11:46 run_fkrprog_wrapper.xpy > make rm -f dbmoment cat /opt/antelope/5.1-64/local/data/templates/xpy dbmoment.xpy > dbmoment chmod a+x dbmoment > make install deposit run_fkrprog_wrapper /opt/antelope/5.1-64/local/bin deposit fortran_fkrprog /opt/antelope/5.1-64/local/bin deposit dbmoment /opt/antelope/5.1-64/local/bin deposit dbmoment.pf /opt/antelope/5.1-64/local/data/pf deposit SOCAL_MODEL.pf /opt/antelope/5.1-64/local/data/pf deposit PDS1_MODEL.pf /opt/antelope/5.1-64/local/data/pf
Define your parameters
Now you have to copy the relevant parameter files to your real-time system and config them...
> pwd /Users/rnewman/rtsystems/mt > ls -la drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 bin drwxr-xr-x 17 rnewman staff 578B Feb 3 13:37 db drwxr-xr-x 14 rnewman staff 476B Nov 22 11:33 db_old drwxr-xr-x 5 rnewman staff 170B Nov 2 14:52 dbmaster drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 idserver drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 logs drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 orb drwxr-xr-x 25 rnewman staff 850B Feb 6 11:51 pf -rw-r--r-- 1 rnewman staff 10K Dec 1 13:24 rtexec.pf drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 rtlogs drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 rtsys drwxr-xr-x 2 rnewman staff 68B Nov 2 14:52 state > cp $ANTELOPE/local/data/pf/dbmoment.pf pf > cp $ANTELOPE/local/data/pf/SOCAL_MODEL.pf pf > cp $ANTELOPE/local/data/pf/PDS1_MODEL.pf pf > pfecho pf/dbmoment.pf chan_to_use LH.* clip_values &Arr{ max 1.6**23 min -1.6**23 } distance_weighting off event_db /Users/rnewman/rtsystems/mt/db/mt green_db /Users/rnewman/rtsystems/mt/greens_db isoflag 1 mag_filters &Tbl{ 2.0 4.0 120 BW_0.02_5_0.10_5 4.0 5.0 120 BW_0.02_5_0.05_5 5.0 6.0 120 BW_0.01_5_0.05_5 6.0 7.0 120 BW_0.01_5_0.07_5 7.0 120 BW_0.01_5_0.07_5 } model_name SOCAL_MODEL model_type v mt_images_dir /Users/rnewman/rtsystems/mt/mtimages obspy_beachball &Arr{ alpha bgcolor w edgecolor facecolor b fig format png linewidth 2 nofill size 300 width 200 xy } pf_revision_time 1328212187 resp_db demo/resp statmax 24 trim_value 0.2 ttfont /Library/Fonts/Verdana.ttf use_inc 0 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).
Execute dbmoment
Once everything is defined, you just need to run dbmoment with a valid origin number from your origin table!
> rtrun dbmoment -v 12345 Used PFPATH to determine which parameter file to use and found 'pf/dbmoment.pf' 2/07/2012 19:48:54.190 Get canned view from database 2/07/2012 19:48:54.265 Processing origin #: 96527 2/07/2012 19:48:54.291 There are 334 records between 1 & 10 deg that match this origin arrival and iphase 2/07/2012 19:48:54.304 Iterate over groups of stations in each azimuthal range. Requesting 3 per group 2/07/2012 19:48:54.304 - Working on group (NNW) 2/07/2012 19:48:54.304 - Get channel data (trace objects) for station (L11A) 2/07/2012 19:48:54.331 - Retrieve L11A data with time range (1203603264.91, 1203603984.91) 2/07/2012 19:48:54.475 - Trace extracted for R samples:[480], wanted:[600] 2/07/2012 19:48:54.508 - Trace extracted for T samples:[480], wanted:[600] 2/07/2012 19:48:54.541 - Trace extracted for Z samples:[480], wanted:[600] <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.
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