Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap meca #516

Merged
merged 77 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
e26ecd3
Initial commit to add meca for focal mechanism plotting
tjnewton Jul 11, 2020
0232450
Formatted and checked initial commit of add-meca
tjnewton Jul 11, 2020
922af45
Merge branch 'master' into add-meca
tjnewton Jul 11, 2020
28efc2d
Add meca to index.rst
liamtoney Jul 12, 2020
2fe87af
Added all GMT meca data conventions to pygmt meca
tjnewton Jul 12, 2020
cff30cc
Remove trailing whitespace
liamtoney Jul 12, 2020
b6339a1
Remove extra parens
liamtoney Jul 12, 2020
1239366
Remove convention warnings
liamtoney Jul 12, 2020
ffff36d
Use GMTError
liamtoney Jul 12, 2020
d9407e9
Import GMTError
liamtoney Jul 12, 2020
8536864
Merge branch 'master' into add-meca
liamtoney Jul 12, 2020
ed383d0
Remove comment
liamtoney Jul 12, 2020
f9ac410
Minor comment edits
liamtoney Jul 12, 2020
5f3bfae
Fix typo
liamtoney Jul 12, 2020
910e0f3
Line length reductions
liamtoney Jul 12, 2020
29c0738
Add alias autofill to docstring
liamtoney Jul 12, 2020
ae84de1
Docstring formatting
liamtoney Jul 12, 2020
78cde02
Remove example
liamtoney Jul 12, 2020
598efb6
Adjust line length in docstring
liamtoney Jul 12, 2020
cfef2d5
More line length stuff
liamtoney Jul 12, 2020
31a0973
Better convention string formatting
liamtoney Jul 12, 2020
100d4f1
Simplify docstring
liamtoney Jul 12, 2020
52cd24f
Implemented scale, updated doc string.
tjnewton Jul 12, 2020
97697aa
implemented plotting different moment tensor components and offset
tjnewton Jul 12, 2020
cfd1c21
More docstring edits
liamtoney Jul 12, 2020
da20e4f
Add full moment tensor test
liamtoney Jul 12, 2020
6dfcbe6
Format with black; add os
liamtoney Jul 12, 2020
de80acc
Add fmt baseline image for test
liamtoney Jul 12, 2020
acc6bf6
Rename test function
liamtoney Jul 12, 2020
8040b34
created meca example
tjnewton Jul 12, 2020
5352f9c
formatted meca example
tjnewton Jul 12, 2020
5782ee8
Update examples/gallery/plot/meca.py to style guidelines
tjnewton Jul 12, 2020
393d1c8
Update examples/gallery/plot/meca.py
tjnewton Jul 12, 2020
f57ff25
better doc string in meca example
tjnewton Jul 12, 2020
4314417
maybe fixed whitespace in meca.py?
tjnewton Jul 12, 2020
1d89c6f
Add function for testing the component argument for mt convention
liamtoney Jul 13, 2020
cd3ebca
Change location of C flag logic
liamtoney Jul 13, 2020
45b01de
Add text, which currently fails
liamtoney Jul 13, 2020
deaa1aa
Merge branch 'master' into add-meca
liamtoney Jul 13, 2020
ff164e6
Disable text kwarg for meca for now
liamtoney Jul 13, 2020
64fa78c
added ability to read in dicts of lists as spec
tjnewton Jul 19, 2020
641dd02
added pandas dataframe functionality
tjnewton Jul 19, 2020
a44e36a
deleted fancy MT tests
tjnewton Jul 19, 2020
d666290
update test_meca_spec_dictionary doc string
tjnewton Jul 19, 2020
916269b
added test for dict of lists
tjnewton Jul 19, 2020
9a5c390
added test for dataframe
tjnewton Jul 19, 2020
9d0cb4a
pushing the test files would help
tjnewton Jul 19, 2020
790864a
added test for 1d array and moment tensor convention
tjnewton Jul 21, 2020
b7a85bd
added 2d array test
tjnewton Jul 21, 2020
770e6b6
change variable format to data_format for style checks
tjnewton Jul 21, 2020
f94c04e
fixed isinstance calls for style
tjnewton Jul 21, 2020
ed83d01
camel casing test names
tjnewton Jul 21, 2020
5ea36ce
make format, make check
tjnewton Jul 21, 2020
c35bbbc
added test for file input
tjnewton Jul 21, 2020
355379d
fix PARAMS storage to dict and fix random bugs
tjnewton Jul 21, 2020
dbd936f
fixed naming convention and loop bug
tjnewton Jul 21, 2020
8defa26
make format & make check :)
tjnewton Jul 21, 2020
80af68a
docstring formatting
tjnewton Jul 24, 2020
eb3ce79
moar docstring formatting
tjnewton Jul 24, 2020
69f3919
rename param dict
tjnewton Jul 24, 2020
0b8fdfe
didn't pull webpage commits first
tjnewton Jul 24, 2020
6545cb1
an offering to the pylint gods
tjnewton Jul 24, 2020
e48c945
pylint dislikes capitalized constants in methods, hence now lowercase
tjnewton Jul 24, 2020
6b83b16
silence refactor-related pylint warnings
tjnewton Jul 24, 2020
2cad4a7
pylint: disable=too-many-statements
tjnewton Jul 24, 2020
7548740
changed lat and lon variables to latitude and longitude
tjnewton Jul 27, 2020
b5ab932
updated syntax in meca exam example
tjnewton Jul 27, 2020
f44bc5d
clarification that plot_longitude and plot_latitude are no longer req…
tjnewton Jul 27, 2020
3946493
added 1d array as input for latitude, longitude, depth, plot_longitud…
tjnewton Jul 27, 2020
08830e9
test for event location as arrays and lists
tjnewton Jul 27, 2020
a4c65ac
Merge branch 'master' into add-meca
tjnewton Jul 31, 2020
50f582c
changed test_meca_spec_dictionary to contain one focal mechanism
tjnewton Aug 3, 2020
b173b49
deleted focal_mechanisms.txt and changed test_meca to only use temp file
tjnewton Aug 3, 2020
b388ab2
more descriptive variable name in test_meca_spec_file
tjnewton Aug 3, 2020
bf47d8b
one more rename for good measure
tjnewton Aug 3, 2020
6c3af68
fix bug in test_meca_spec_file
tjnewton Aug 3, 2020
53424e9
pass object instead of file name string
tjnewton Aug 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Plotting data and laying out the map:
Figure.image
Figure.shift_origin
Figure.text
Figure.meca

Color palette table generation:

Expand Down
150 changes: 150 additions & 0 deletions pygmt/base_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,153 @@ def text(

arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("text", arg_str)

@fmt_docstring
liamtoney marked this conversation as resolved.
Show resolved Hide resolved
@use_alias(R="region", J="projection", B="frame", S="convention")
@kwargs_to_strings(R="sequence",)
def meca(
self,
lon=None,
lat=None,
tjnewton marked this conversation as resolved.
Show resolved Hide resolved
depth=None,
spec=None,
convention=None,
plot_lon=None, # FIXME: implement -C flag if plot location is specified
plot_lat=None,
text=None,
scale=None, # FIXME: implement
**kwargs,
):
"""
Plot focal mechanisms.

Parameters
----------
lon: int or float
Longitude of event location.
lat: int or float
Latitude of event location.
depth: int or float
Depth of event location in kilometers.
spec: dict, 1D array, 2D array, or str
Object specifying event parameters that are consistent with the specified convention.
If a string specifying a filename is assigned to spec, no other parameters are required,
as those parameters are assumed to exist in the file.
List of required spec parameters for different conventions:
a (Aki & Rickards): strike, dip, rake, magnitude
c (global CMT): strike1, dip1, rake1, strike2, dip2, rake2, mantissa, exponent
m (seismic moment tensor): mrr, mtt, mff, mrt, mrf, mtf, exponent
p (partial focal mechanism): strike1, dip1, strike2, fault_type, magnitude
x (principal axis): t_exponent, t_azimuth, t_plunge, n_exponent, n_azimuth, n_plunge, p_exponent, p_azimuth, p_plunge, exponent
convention: str
a (Aki & Richards), c (global CMT), m (seismic moment tensor), p (partial focal mechanism), x (principal axis)
Optional (auto-detected) if spec is defined as a dict.
plot_lon: (optional) int or float
Longitude at which to place beachball
plot_lat: (optional) int or float
Latitude at which to place beachball
text: (optional) str
Text string to appear near the beachball

Examples
--------
fig = pygmt.Figure()
fig.meca(lon=239.384, lat=34.556, depth=12.0,
spec=dict(strike1=180, dip1=18, rake1=-88, strike2=0,
dip2=72, rake2=-90, mantissa=5.5, exponent=0),
region=[239, 240, 34, 35.2], projection='m4c')
fig.show()

fig = pygmt.Figure()
fig.meca(spec='focal_mechanisms.psmeca')
fig.show()
"""

# Check the spec and parse the data according to the specified convention
if type(spec) is dict:
if lon is None or lat is None or depth is None: # if no specified location
raise GMTError("Location not fully specified.")
liamtoney marked this conversation as resolved.
Show resolved Hide resolved

# These are constants related to GMT and could be defined elsewhere (top of file?)
AKI_PARAMS = ["strike", "dip", "rake", "magnitude"]
GCMT_PARAMS = [
"strike1",
"dip1",
"rake1",
"strike2",
"dip2",
"rake2",
"mantissa",
"exponent",
]
# FIXME: allow specification of moment instead of mantissa and exponent.
MT_PARAMS = ["mrr", "mtt", "mff", "mrt", "mrf", "mtf", "exponent"]
PFM_PARAMS = ["strike1", "dip1", "strike2", "fault_type", "magnitude"]
PA_PARAMS = [
"t_exponent",
"t_azimuth",
"t_plunge",
"n_exponent",
"n_azimuth",
"n_plunge",
"p_exponent",
"p_azimuth",
"p_plunge",
"exponent",
]

# Aki and Richards convention: -Sa in GMT
if set(spec.keys()) == set(AKI_PARAMS):
convention = "a"
foc_params = AKI_PARAMS

# Global CMT convention: -Sc in GMT
elif set(spec.keys()) == set(GCMT_PARAMS):
convention = "c"
foc_params = GCMT_PARAMS

# Seismic moment tensor convention: -Sm in GMT
elif set(spec.keys()) == set(MT_PARAMS):
convention = "m"
foc_params = MT_PARAMS

# Partial focal mechanism convention: -Sp in GMT
elif set(spec.keys()) == set(PFM_PARAMS):
convention = "p"
foc_params = PFM_PARAMS

# Principal axis convention: -Sx in GMT
elif set(spec.keys()) == set(PFM_PARAMS):
convention = "x"
foc_params = PA_PARAMS

else:
raise GMTError("Parameters in spec dict do not match known conventions.")
liamtoney marked this conversation as resolved.
Show resolved Hide resolved

# Construct the vector (note that order matters here, hence the list comprehension!)
spec = [lon, lat, depth] + [spec[key] for key in foc_params]

# Add in plotting options, if given, otherwise add 0s as required by GMT
for arg in plot_lon, plot_lat, text:
if arg is None:
spec.append(0)
else:
spec.append(arg)

elif convention is None:
raise GMTError("We need a convention to know how to interpret the input!")
liamtoney marked this conversation as resolved.
Show resolved Hide resolved

# if spec is not a dict it is handled here
kind = data_kind(spec)
with Session() as lib:
if kind == "matrix":
file_context = lib.virtualfile_from_matrix(
np.atleast_2d(spec)
) # np.atleast_2d allows 1D and 2D arrays
elif kind == "file":
file_context = dummy_context(spec)
else:
raise GMTInvalidInput("Unrecognized data type: {}".format(type(spec)))
with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("meca", arg_str)