Coverage for /opt/obspy/update-docs/src/obspy/obspy/imaging/beachball : 59%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- #------------------------------------------------------------------- # Filename: beachball.py # Purpose: Draws a beach ball diagram of an earthquake focal mechanism. # Author: Robert Barsch # Email: barsch@geophysik.uni-muenchen.de # # Copyright (C) 2008-2012 Robert Barsch #---------------------------------------------------------------------
Draws a beachball diagram of an earthquake focal mechanism
Most source code provided here are adopted from
1. MatLab script `bb.m`_ written by Andy Michael and Oliver Boyd. 2. ps_meca program from the `Generic Mapping Tools (GMT)`_.
:copyright: The ObsPy Development Team (devs@obspy.org) :license: GNU General Public License (GPL) (http://www.gnu.org/licenses/gpl.txt)
.. _`Generic Mapping Tools (GMT)`: http://gmt.soest.hawaii.edu .. _`bb.m`: http://www.ceri.memphis.edu/people/olboyd/Software/Software.html """
alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100): """ Return a beach ball as a collection which can be connected to an current matplotlib axes instance (ax.add_collection).
S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms.
:param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int :param width: Symbol size of beach ball. Defaults to ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. """ # check if one or two widths are specified (Circle or Ellipse) mt = fm np1 = MT2Plane(mt) np1 = fm else: raise TypeError("Wrong input value for 'fm'.")
# Only at least size 100, i.e. 100 points in the matrix are allowed size = 100
# Return as collection colors, p = plotDC(np1, size, xy=xy, width=width) else: plot_zerotrace=True, xy=xy, width=width) else:
# XXX: not tested with plotMT col = collections.PatchCollection([p[1]], match_original=False) col.set_facecolor('none') else: # Replace color dummies 'b' and 'w' by face and bgcolor
alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100, outfile=None, format=None, fig=None): """ Draws a beach ball diagram of an earthquake focal mechanism.
S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms.
:param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int :param width: Symbol size of beach ball. Defaults to ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. :param outfile: Output file string. Also used to automatically determine the output format. Supported file formats depend on your matplotlib backend. Most backends support png, pdf, ps, eps and svg. Defaults to ``None``. :param format: Format of the graph picture. If no format is given the outfile parameter will be used to try to automatically determine the output format. If no format is found it defaults to png output. If no outfile is specified but a format is, than a binary imagestring will be returned. Defaults to ``None``. :param fig: Give an existing figure instance to plot into. New Figure if set to ``None``. """
# plot the figure
# hide axes + ticks
# plot the collection edgecolor=edgecolor, bgcolor=bgcolor, alpha=alpha, nofill=nofill, xy=xy, width=plot_width, size=size, zorder=zorder)
# export if format: fig.savefig(outfile, dpi=100, transparent=True, format=format) else: fig.savefig(outfile, dpi=100, transparent=True) imgdata = StringIO.StringIO() fig.savefig(imgdata, format=format, dpi=100, transparent=True) imgdata.seek(0) return imgdata.read() else:
x0=0, y0=0, xy=(0, 0), width=200): """ Uses a principal axis T, N and P to draw a beach ball plot.
:param ax: axis object of a matplotlib figure :param T: :class:`~PrincipalAxis` :param N: :class:`~PrincipalAxis` :param P: :class:`~PrincipalAxis`
Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT)`_.
.. _`Generic Mapping Tools (GMT)`: http://gmt.soest.hawaii.edu """ # check if one or two widths are specified (Circle or Ellipse) except TypeError: width = (width, width)
# pure implosion-explosion if vi > 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') if vi < 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect
d = 0 m = 2 else:
# Cliff Frohlich, Seismological Research letters, # Vol 7, Number 1, January-February, 1996 # Unless the isotropic parameter lies in the range # between -1 and 1 - f there will be no nodes whatsoever
cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') return colors, collect
float(3. + (1. - 2. * f) * np.cos(2. * fir)) big_iso += 1 else:
san * cfi * cpm * cam san * cfi * cpm * sam
takeoff = 0. az = 0. else: xe * xe))) takeoff = np.pi - takeoff az += np.pi if az > np.pi * 2.: az -= np.pi * 2. else: azi[n][1] = azp n += 1 azi[n][0] = az azi[n][0] += np.pi * 2. else: elif n == 1: x2[j2] = x0 + radius_size * r * si y2[j2] = y0 + radius_size * r * co j2 += 1 elif n == 2: x3[j3] = x0 + radius_size * r * si y3[j3] = y0 + radius_size * r * co j3 += 1
rgb1 = 'b' rgb2 = 'w' else:
elif n == 1: for i in range(0, j): xp1[i] = x[i] yp1[i] = y[i] if azi[0][0] - azi[0][1] > np.pi: azi[0][0] -= np.pi * 2. elif azi[0][1] - azi[0][0] > np.pi: azi[0][0] += np.pi * 2. if azi[0][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az -= D2R else: az = azi[0][1] + D2R while az < azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect elif n == 2: for i in range(0, j3): xp1[i] = x3[i] yp1[i] = y3[i] for ii in range(0, j): xp1[i] = x[ii] i += 1 yp1[i] = y[ii] if big_iso: ii = j2 - 1 while ii >= 0: xp1[i] = x2[ii] i += 1 yp1[i] = y2[ii] ii -= 1 collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) return colors, collect
if azi[2][0] - azi[0][1] > np.pi: azi[2][0] -= np.pi * 2. elif azi[0][1] - azi[2][0] > np.pi: azi[2][0] += np.pi * 2. if azi[2][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az -= D2R else: az = azi[0][1] + D2R while az < azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1)
for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect
""" Uses one nodal plane of a double couple to draw a beach ball plot.
:param ax: axis object of a matplotlib figure :param np1: :class:`~NodalPlane`
Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ # check if one or two widths are specified (Circle or Ellipse) except TypeError: width = (width, width)
R1 -= 180 M = 1 R1 += 180 M = 1
# Get azimuth and dip of second plane
D1 = 89.9999 D2 = 89.9999
# arange checked for numerical stablility, np.pi is not multiple of 0.1 np.power(np.cos(phi), 2) * np.power(90 - D1, 2) / np.power(90, 2))) np.power(np.cos(phi), 2) * np.power(90 - D2, 2) / np.power(90, 2)))
lo = S1 - 180 hi = S2 if lo > hi: inc = -1 th1 = np.arange(S1 - 180, S2, inc) (Xs1, Ys1) = Pol2Cart(th1 * D2R, 90 * np.ones((1, len(th1)))) (X2, Y2) = Pol2Cart(phi + S2 * D2R, l2) th2 = np.arange(S2 + 180, S1, -inc) else: inc = -1
# calculate resolution
# construct the patches
# check if one or two resolutions are specified (Circle or Ellipse) except TypeError: res = (res, res) # transform into the Path coordinate system
""" """
""" Finds strike and dip of plane given normal vector having components n, e, and u.
Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """
strike = strike - 360
""" Get Strike and dip of second plane.
Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """
# slick vector in plane 1
# note h3=0 always so we leave it out # n3 = np.cos(z2)
""" Calculates a nodal plane of a given moment tensor.
:param mt: :class:`~MomentTensor` :return: :class:`~NodalPlane`
Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ [v[2, 1], -v[2, 0], -v[2, 2]], [-v[0, 1], v[0, 0], v[0, 2]]]) else:
""" Helper function for MT2Plane.
Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ FD = 90. AXN = np.fabs(XN) if AXN > 1.0: AXN = 1.0 FT = np.arcsin(AXN) * CON ST = -XN CT = YN if ST >= 0. and CT < 0: FT = 180. - FT if ST < 0. and CT <= 0: FT = 180. + FT if ST < 0. and CT > 0: FT = 360. - FT FL = np.arcsin(abs(ZE)) * CON SL = -ZE if np.fabs(XN) < AAA: CL = XE / YN else: CL = -YE / XN if SL >= 0. and CL < 0: FL = 180. - FL if SL < 0. and CL <= 0: FL = FL - 180. if SL < 0. and CL > 0: FL = -FL else: ZN = -1.0 return SX = 1.0 FT = 180. - FT FT = 360. - FT SX = 1.0 CL = XE / CT else: CL = YE / ST FL = 180. - FL FL = -FL
""" Calculates the principal axes of a given moment tensor.
:param mt: :class:`~MomentTensor` :return: tuple of :class:`~PrincipalAxis` T, N and P
Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT) <http://gmt.soest.hawaii.edu>`_. """ az[i] += 2 * np.pi az[i] -= 2 * np.pi
""" A principal axis.
Strike and dip values are in degrees.
>>> a = PrincipalAxis(1.3, 20, 50) >>> a.dip 50 >>> a.strike 20 >>> a.val 1.3 """
""" A nodal plane.
All values are in degrees.
>>> a = NodalPlane(13, 20, 50) >>> a.strike 13 >>> a.dip 20 >>> a.rake 50 """
""" A moment tensor.
>>> a = MomentTensor(1, 1, 0, 0, 0, -1, 26) >>> b = MomentTensor(np.array([1, 1, 0, 0, 0, -1]), 26) >>> c = MomentTensor(np.array([[1, 0, 0], [0, 1, -1], [0, -1, 0]]), 26) >>> a.mt array([[ 1, 0, 0], [ 0, 1, -1], [ 0, -1, 0]]) >>> b.yz -1 >>> a.expo 26 """ # six independent components [A[3], A[1], A[5]], [A[4], A[5], A[2]]]) # full matrix else: raise TypeError("Wrong size of input parameter.") # six independent components [args[3], args[1], args[5]], [args[4], args[5], args[2]]]) else: raise TypeError("Wrong size of input parameter.")
def xx(self): return self.mt[0][0]
def xy(self): return self.mt[0][1]
def xz(self): return self.mt[0][2]
def yz(self):
def yy(self): return self.mt[1][1]
def zz(self): return self.mt[2][2]
if __name__ == '__main__': import doctest doctest.testmod() |