Uh oh!
There was an error while loading. Please reload this page.
Wrap meca - #516
Conversation
💖 Thanks for opening this pull request! 💖 Please make sure you read our contributing guidelines and abide by our code of conduct. A few things to keep in mind:
|
Uh oh!
There was an error while loading. Please reload this page.
liamtoney
commented
Jul 11, 2020
@tjnewton and I will work on this more tomorrow, adding tests and whatnot. |
My question for you guys, as end-users, is: In what formats would you want to provide MT/FMT info? As a filename? List of params? Dictionary? Something with Pandas? Hi Liam, It sounds like you are looking to replace the classic psmeca command syntax, in which values are read from an ascii table, with something more Pythonic? Then perhaps one (or both) of the following conventions from obspy:
|
liamtoney
commented
Jul 12, 2020
Hi Ryan, thanks for reminding us that we should try to align with existing libraries! Tyler and I were discussing two options — the original file input as well as an array input with the same info for non-file usage — but I think your suggestion is much more Pythonic. So we can add a third option for a keyword (i.e., dict) based option. @tjnewton I propose the following sketched-out method definition, similar to what we had but with a third option that involves providing a dictionary for 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],
**other_kwargs
)All names/details are drafts of course. defmeca(
self,
lon=None,
lat=None,
depth=None,
spec=None,
convention=None,
plot_lon=None,
plot_lat=None,
text=None,
**kwargs
):
iftype(spec) isdict: # The user opted for the nice keyword-based approachif (
latisNoneorlonisNoneordepthisNone
): # They need to give the location!raiseError('Location not fully specified.')
# 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',
]
# Aki and Richardsifset(spec.keys()) ==set(AKI_PARAMS):
convention='a'foc_params=AKI_PARAMS# GCMTelifset(spec.keys()) ==set(GCMT_PARAMS):
convention='c'foc_params=GCMT_PARAMS# Other cases....elif ...:
pass# Same strategy as aboveelse:
raiseError('Focal mech params not understood')
# Construct the vector (note that order matters here, hence the list comprehension!)spec= [lon, lat, depth] + [spec[key] forkeyinfoc_params]
# Add in plotting options, if given, otherwise add 0s as required by GMTforarginplot_lon, plot_lat, text:
ifargisNone:
spec.append(0)
else:
spec.append(arg)
elifconventionisNone:
raiseError('We need a convention to know how to interpret the input!')
kind=data_kind(spec)
ifkind=='file':
pass# Use file as-is (dummy file context)...elifkind=='matrix':
pass# Convert to virtual file and then use as-is...else:
raiseError('File type not understood')
# Build arg str and call meca... |
liamtoney
commented
Jul 12, 2020
I also wonder if we could replace the conventions that require moment=5000mantissa=10# Hard-codedexponent=np.log10(moment)
mantissa**exponent# == 4999.999999999999 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| 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: |
There was a problem hiding this comment.
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.
I think the usage example would be:
frompygmt.helpersimportGMTTempFilewithGMTTempFile() astemp:
withopen(temp.name, mode="w") astemp_file:
temp_file.write(...)
some_gmt_module(temp.name, ...)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.
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.
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.
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.
Including the
as tempthat Liam mentioned in the second line will throw a pylint warning for an unused variable.
After using with GMTTempFile() as temp:, you should also remove the double quotes around "temp.name".
There was a problem hiding this comment.
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.
Yes, I think this PR is good to merge. Thanks for your contribution!
There was a problem hiding this comment.
temp.name is a string with the system filename.
There was a problem hiding this comment.
thanks @liamtoney I apparently need some rest
@tjnewton Again, thanks for your contributions to PyGMT. Feel free to open another PR to add yourself to the AUTHORS list after this PR is merged. |
tjnewton
commented
Aug 3, 2020
Will do, thanks! I plan to continue contributing to the project. |
weiji14
commented
Aug 3, 2020
Thanks @tjnewton, really appreciate the work you've done here! 😄 @liamtoney, could you squash and merge in this PR since you've worked on it too? Will need to shorten the commit message to capture the gist of what was achieved in this PR. |
🎉🎉🎉 Congrats on merging your first pull request and welcome to the team! 🎉🎉🎉 Please open a new pull request to add yourself to the |
liamtoney
commented
Aug 3, 2020
Congrats @tjnewton for your first contribution to PyGMT! Definitely let us know how we could improve this process and make it more accessible. I think a lot of seismologists will be excited about this implementation! |
Description of proposed changes
Add meca functionality to plot focal mechanisms. Started during SciPy2020 sprint.
Adds meca function to base_plotting.py to add focal mechanism plotting to pygmt.
Live documentation preview at https://pygmt-git-fork-tjnewton-add-meca.gmt.now.sh/api/generated/pygmt.Figure.meca.html
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.