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

Add private _FocalMechanismConvention class for handling focal mechanism conventions and refactor Figure.meca #3551

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 23, 2024

Functions convention_code, convention_name, convention_params are defined in meca.py and they can also be used when wrapping coupe (#2019). So it's better to move these shared functions into pygmt/src/_common.py as proposed in #3357.

Instead of simply moving the functions, this PR adds the private _FocalMechanismConvention class which does the same thing as the three functions. With this new class, the Figure.meca source codes can be simplified.

It's likely the Figure.meca method can be further simplified, but will leave it to future PRs to make this PR small for reviewing.

@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Oct 23, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 23, 2024
@seisman seisman added the run/benchmark Trigger the benchmark workflow in PRs label Oct 23, 2024
Comment on lines +196 to +215
if convention in self._conventions:
# Convention is given via 'convention' and 'component' parameters.
if component not in {"full", "deviatoric", "dc"}:
msg = (
f"Invalid component '{component}' for focal mechanism convention "
f"'{convention}'."
)
raise GMTInvalidInput(msg)

self.convention = convention
self.code = self._conventions[convention]
if isinstance(self.code, dict):
self.code = self.code[component]
elif convention in self._codes:
# Convention is given as a single-letter code.
self.code = convention
self.convention = self._codes[convention]
else:
msg = f"Invalid focal mechanism convention '{convention}'."
raise GMTInvalidInput(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if some of the pattern matching on 'convention' long names (e.g. akl) or single-letter aliases (e.g. a) could be simplified using StrEnum, but not sure how to handle the extra logic around 'component's. Haven't looked into it too closely, but there's supposedly ways to extend Enums with extra methods - https://realpython.com/python-enum/#adding-and-tweaking-member-methods.

@seisman seisman removed the needs review This PR has higher priority and needs review. label Oct 25, 2024
@seisman seisman marked this pull request as draft October 25, 2024 03:16
@seisman seisman self-assigned this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature run/benchmark Trigger the benchmark workflow in PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants