-
Notifications
You must be signed in to change notification settings - Fork 225
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
Wrap meca #516
Changes from 75 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 0232450
Formatted and checked initial commit of add-meca
tjnewton 922af45
Merge branch 'master' into add-meca
tjnewton 28efc2d
Add meca to index.rst
liamtoney 2fe87af
Added all GMT meca data conventions to pygmt meca
tjnewton cff30cc
Remove trailing whitespace
liamtoney b6339a1
Remove extra parens
liamtoney 1239366
Remove convention warnings
liamtoney ffff36d
Use GMTError
liamtoney d9407e9
Import GMTError
liamtoney 8536864
Merge branch 'master' into add-meca
liamtoney ed383d0
Remove comment
liamtoney f9ac410
Minor comment edits
liamtoney 5f3bfae
Fix typo
liamtoney 910e0f3
Line length reductions
liamtoney 29c0738
Add alias autofill to docstring
liamtoney ae84de1
Docstring formatting
liamtoney 78cde02
Remove example
liamtoney 598efb6
Adjust line length in docstring
liamtoney cfef2d5
More line length stuff
liamtoney 31a0973
Better convention string formatting
liamtoney 100d4f1
Simplify docstring
liamtoney 52cd24f
Implemented scale, updated doc string.
tjnewton 97697aa
implemented plotting different moment tensor components and offset
tjnewton cfd1c21
More docstring edits
liamtoney da20e4f
Add full moment tensor test
liamtoney 6dfcbe6
Format with black; add os
liamtoney de80acc
Add fmt baseline image for test
liamtoney acc6bf6
Rename test function
liamtoney 8040b34
created meca example
tjnewton 5352f9c
formatted meca example
tjnewton 5782ee8
Update examples/gallery/plot/meca.py to style guidelines
tjnewton 393d1c8
Update examples/gallery/plot/meca.py
tjnewton f57ff25
better doc string in meca example
tjnewton 4314417
maybe fixed whitespace in meca.py?
tjnewton 1d89c6f
Add function for testing the component argument for mt convention
liamtoney cd3ebca
Change location of C flag logic
liamtoney 45b01de
Add text, which currently fails
liamtoney deaa1aa
Merge branch 'master' into add-meca
liamtoney ff164e6
Disable text kwarg for meca for now
liamtoney 64fa78c
added ability to read in dicts of lists as spec
tjnewton 641dd02
added pandas dataframe functionality
tjnewton a44e36a
deleted fancy MT tests
tjnewton d666290
update test_meca_spec_dictionary doc string
tjnewton 916269b
added test for dict of lists
tjnewton 9a5c390
added test for dataframe
tjnewton 9d0cb4a
pushing the test files would help
tjnewton 790864a
added test for 1d array and moment tensor convention
tjnewton b7a85bd
added 2d array test
tjnewton 770e6b6
change variable format to data_format for style checks
tjnewton f94c04e
fixed isinstance calls for style
tjnewton ed83d01
camel casing test names
tjnewton 5ea36ce
make format, make check
tjnewton c35bbbc
added test for file input
tjnewton 355379d
fix PARAMS storage to dict and fix random bugs
tjnewton dbd936f
fixed naming convention and loop bug
tjnewton 8defa26
make format & make check :)
tjnewton 80af68a
docstring formatting
tjnewton eb3ce79
moar docstring formatting
tjnewton 69f3919
rename param dict
tjnewton 0b8fdfe
didn't pull webpage commits first
tjnewton 6545cb1
an offering to the pylint gods
tjnewton e48c945
pylint dislikes capitalized constants in methods, hence now lowercase
tjnewton 6b83b16
silence refactor-related pylint warnings
tjnewton 2cad4a7
pylint: disable=too-many-statements
tjnewton 7548740
changed lat and lon variables to latitude and longitude
tjnewton b5ab932
updated syntax in meca exam example
tjnewton f44bc5d
clarification that plot_longitude and plot_latitude are no longer req…
tjnewton 3946493
added 1d array as input for latitude, longitude, depth, plot_longitud…
tjnewton 08830e9
test for event location as arrays and lists
tjnewton a4c65ac
Merge branch 'master' into add-meca
tjnewton 50f582c
changed test_meca_spec_dictionary to contain one focal mechanism
tjnewton b173b49
deleted focal_mechanisms.txt and changed test_meca to only use temp file
tjnewton b388ab2
more descriptive variable name in test_meca_spec_file
tjnewton bf47d8b
one more rename for good measure
tjnewton 6c3af68
fix bug in test_meca_spec_file
tjnewton 53424e9
pass object instead of file name string
tjnewton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Focal mechanisms | ||
---------------- | ||
|
||
The :meth:`pygmt.Figure.meca` method can plot focal mechanisms, or beachballs. | ||
We can specify the focal mechanism nodal planes or moment tensor components as | ||
a dict using the ``spec`` argument (or they can be specified as a 1d or 2d array, | ||
or within a specified file). The size of plotted beachballs can be specified | ||
using the ``scale`` argument. | ||
""" | ||
|
||
import pygmt | ||
|
||
fig = pygmt.Figure() | ||
|
||
# generate a basemap near Washington state showing coastlines, land, and water | ||
fig.coast( | ||
region=[-125, -122, 47, 49], | ||
projection="M6c", | ||
land="grey", | ||
water="lightblue", | ||
shorelines=True, | ||
resolution="f", | ||
frame="a", | ||
) | ||
|
||
# store focal mechanisms parameters in a dict | ||
focal_mechanism = dict(strike=330, dip=30, rake=90, magnitude=3) | ||
|
||
# pass the focal mechanism data to meca in addition to the scale and event location | ||
fig.meca(focal_mechanism, scale="1c", longitude=-124.3, latitude=48.1, depth=12.0) | ||
|
||
fig.show() |
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
""" | ||
Tests for meca | ||
""" | ||
import os | ||
import pandas as pd | ||
import numpy as np | ||
import pytest | ||
|
||
from .. import Figure | ||
|
||
|
||
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_dictionary(): | ||
""" | ||
Test supplying a dictionary containing a single focal mechanism to the | ||
`spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# Right lateral strike slip focal mechanism | ||
fig.meca( | ||
dict(strike=0, dip=90, rake=0, magnitude=5), | ||
longitude=0, | ||
latitude=5, | ||
depth=0, | ||
scale="2.5c", | ||
region=[-1, 1, 4, 6], | ||
projection="M14c", | ||
frame=2, | ||
) | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_dict_list(): | ||
""" | ||
Test supplying a dictionary containing a list of focal mechanism to the | ||
`spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# supply focal mechanisms as a dict of lists | ||
focal_mechanisms = dict( | ||
strike=[330, 350], dip=[30, 50], rake=[90, 90], magnitude=[3, 2] | ||
) | ||
|
||
fig.meca( | ||
focal_mechanisms, | ||
longitude=[-124.3, -124.4], | ||
latitude=[48.1, 48.2], | ||
depth=[12.0, 11.0], | ||
region=[-125, -122, 47, 49], | ||
scale="2c", | ||
projection="M14c", | ||
) | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_dataframe(): | ||
""" | ||
Test supplying a pandas DataFrame containing focal mechanisms and | ||
locations to the `spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# supply focal mechanisms to meca as a dataframe | ||
focal_mechanisms = dict( | ||
strike=[324, 353], | ||
dip=[20.6, 40], | ||
rake=[83, 90], | ||
magnitude=[3.4, 2.9], | ||
longitude=[-124, -124.4], | ||
latitude=[48.1, 48.2], | ||
depth=[12, 11.0], | ||
) | ||
spec_dataframe = pd.DataFrame(data=focal_mechanisms) | ||
|
||
fig.meca(spec_dataframe, region=[-125, -122, 47, 49], scale="2c", projection="M14c") | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_1d_array(): | ||
""" | ||
Test supplying a 1D numpy array containing focal mechanisms and | ||
locations to the `spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# supply focal mechanisms to meca as a 1D numpy array, here we are using | ||
# the Harvard CMT zero trace convention but the focal mechanism | ||
# parameters may be specified any of the available conventions. Since we | ||
# are not using a dict or dataframe the convention and component should | ||
# be specified. | ||
focal_mechanism = [ | ||
-127.40, # longitude | ||
40.87, # latitude | ||
12, # depth | ||
-3.19, # mrr | ||
0.16, # mtt | ||
3.03, # mff | ||
-1.02, # mrt | ||
-3.93, # mrf | ||
-0.02, # mtf | ||
23, # exponent | ||
0, # plot_lon, 0 to plot at event location | ||
0, # plot_lat, 0 to plot at event location | ||
] | ||
focal_mech_array = np.asarray(focal_mechanism) | ||
|
||
fig.meca( | ||
focal_mech_array, | ||
convention="mt", | ||
component="full", | ||
region=[-128, -127, 40, 41], | ||
scale="2c", | ||
projection="M14c", | ||
) | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_2d_array(): | ||
""" | ||
Test supplying a 2D numpy array containing focal mechanisms and | ||
locations to the `spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# supply focal mechanisms to meca as a 2D numpy array, here we are using | ||
# the GCMT convention but the focal mechanism parameters may be | ||
# specified any of the available conventions. Since we are not using a | ||
# dict or dataframe the convention and component should be specified. | ||
focal_mechanisms = [ | ||
[ | ||
-127.40, # longitude | ||
40.87, # latitude | ||
12, # depth | ||
170, # strike1 | ||
20, # dip1 | ||
-110, # rake1 | ||
11, # strike2 | ||
71, # dip2 | ||
-83, # rake2 | ||
5.1, # mantissa | ||
23, # exponent | ||
0, # plot_lon, 0 means we want to plot at the event location | ||
0, # plot_lat | ||
], | ||
[-127.50, 40.88, 12.0, 168, 40, -115, 20, 54, -70, 4.0, 23, 0, 0], | ||
] | ||
focal_mechs_array = np.asarray(focal_mechanisms) | ||
|
||
fig.meca( | ||
focal_mechs_array, | ||
convention="gcmt", | ||
region=[-128, -127, 40, 41], | ||
scale="2c", | ||
projection="M14c", | ||
) | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_spec_file(): | ||
""" | ||
Test supplying a file containing focal mechanisms and locations to the | ||
`spec` argument. | ||
""" | ||
|
||
fig = Figure() | ||
|
||
focal_mechanism = [-127.43, 40.81, 12, -3.19, 1.16, 3.93, -1.02, -3.93, -1.02, 23] | ||
|
||
# writes temp file to pass to gmt | ||
with open(os.path.join(TEST_DATA_DIR, "temp.test"), mode="w") as temp_file: | ||
temp_file.write(" ".join([str(x) for x in focal_mechanism])) | ||
|
||
# supply focal mechanisms to meca as a file | ||
fig.meca( | ||
os.path.join(TEST_DATA_DIR, "temp.test"), | ||
convention="mt", | ||
component="full", | ||
region=[-128, -127, 40, 41], | ||
scale="2c", | ||
projection="M14c", | ||
) | ||
|
||
# remove the temporary file | ||
os.remove("temp.test") | ||
|
||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_meca_loc_array(): | ||
""" | ||
Test supplying lists and np.ndarrays as the event location (longitude, | ||
latitude, and depth). | ||
""" | ||
|
||
fig = Figure() | ||
|
||
# specify focal mechanisms | ||
focal_mechanisms = dict( | ||
strike=[327, 350], dip=[41, 50], rake=[68, 90], magnitude=[3, 2] | ||
) | ||
|
||
# longitude, latitude, and depth may be specified as an int, float, | ||
# list, or 1d numpy array | ||
longitude = np.array([-123.3, -124.4]) | ||
latitude = np.array([48.4, 48.2]) | ||
depth = [12.0, 11.0] # to test mixed data types as inputs | ||
|
||
scale = "2c" | ||
|
||
fig.meca( | ||
focal_mechanisms, | ||
scale, | ||
longitude, | ||
latitude, | ||
depth, | ||
region=[-125, -122, 47, 49], | ||
projection="M14c", | ||
) | ||
|
||
return fig |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a really good application for the GMTTempFile, docstring w/ usage example below:
pygmt/pygmt/helpers/tempfile.py
Line 27 in 2884fd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the usage example would be:
Then you don't need to use the
os.remove()
since we have the context manager. LMK if that has issues!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooo thanks Liam, that's clever! Testing this test is a little slow on my end because
make test
on my machine freezes on../pygmt/tests/test_grdview.py::test_grdview_grid_dataarray
every time starting today -_-There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally passed after a restart but the grdview tests are very slow now. Looks like test_meca is passing now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the slow test (we'll need to streamline this somehow at some point). A workaround is to only run the meca tests locally. Use
pytest -v --mpl pygmt/tests/test_meca.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After using
with GMTTempFile() as temp:
, you should also remove the double quotes around"temp.name"
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so the temp.file object needed to be passed to meca rather than a temporary file name string. Should be fixed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think this PR is good to merge. Thanks for your contribution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temp.name
is a string with the system filename.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @liamtoney I apparently need some rest