Skip to content

Commit fabae73

Browse files
committed
Refactor Figure.meca() to make the codes more readable and reusable
1 parent aeb4d4a commit fabae73

File tree

1 file changed

+80
-28
lines changed

1 file changed

+80
-28
lines changed

pygmt/src/meca.py

+80-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,84 @@
1616
)
1717

1818

19+
def data_format_code(convention, component="full"):
20+
"""
21+
Determine the data format code for meca -S option.
22+
23+
See the meca() method for explanations of the parameters.
24+
25+
Examples
26+
--------
27+
>>> data_format_code("aki")
28+
'a'
29+
>>> data_format_code("gcmt")
30+
'c'
31+
>>> data_format_code("partial")
32+
'p'
33+
34+
>>> data_format_code("mt", component="full")
35+
'm'
36+
>>> data_format_code("mt", component="deviatoric")
37+
'z'
38+
>>> data_format_code("mt", component="dc")
39+
'd'
40+
>>> data_format_code("principal_axis", component="full")
41+
'x'
42+
>>> data_format_code("principal_axis", component="deviatoric")
43+
't'
44+
>>> data_format_code("principal_axis", component="dc")
45+
'y'
46+
47+
>>> for code in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]:
48+
... assert data_format_code(code) == code
49+
...
50+
51+
>>> data_format_code("invalid")
52+
Traceback (most recent call last):
53+
...
54+
pygmt.exceptions.GMTInvalidInput: Invalid convention 'invalid'.
55+
56+
>>> data_format_code("mt", "invalid") # doctest: +NORMALIZE_WHITESPACE
57+
Traceback (most recent call last):
58+
...
59+
pygmt.exceptions.GMTInvalidInput:
60+
Invalid component 'invalid' for convention 'mt'.
61+
"""
62+
# Codes for focal mechanism formats determined by "convention"
63+
codes1 = {
64+
"aki": "a",
65+
"gcmt": "c",
66+
"partial": "p",
67+
}
68+
69+
# Codes for focal mechanism formats determined by both "convention" and
70+
# "component"
71+
codes2 = {
72+
"mt": {
73+
"deviatoric": "z",
74+
"dc": "d",
75+
"full": "m",
76+
},
77+
"principal_axis": {
78+
"deviatoric": "t",
79+
"dc": "y",
80+
"full": "x",
81+
},
82+
}
83+
84+
if convention in codes1:
85+
return codes1[convention]
86+
if convention in codes2:
87+
if component not in codes2[convention]:
88+
raise GMTInvalidInput(
89+
f"Invalid component '{component}' for convention '{convention}'."
90+
)
91+
return codes2[convention][component]
92+
if convention in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]:
93+
return convention
94+
raise GMTInvalidInput(f"Invalid convention '{convention}'.")
95+
96+
1997
@fmt_docstring
2098
@use_alias(
2199
R="region",
@@ -366,34 +444,8 @@ def update_pointers(data_pointers):
366444
else:
367445
raise GMTError("Parameter 'spec' contains values of an unsupported type.")
368446

369-
# Add condition and scale to kwargs
370-
if convention == "aki":
371-
data_format = "a"
372-
elif convention == "gcmt":
373-
data_format = "c"
374-
elif convention == "mt":
375-
# Check which component of mechanism the user wants plotted
376-
if component == "deviatoric":
377-
data_format = "z"
378-
elif component == "dc":
379-
data_format = "d"
380-
else: # component == 'full'
381-
data_format = "m"
382-
elif convention == "partial":
383-
data_format = "p"
384-
elif convention == "principal_axis":
385-
# Check which component of mechanism the user wants plotted
386-
if component == "deviatoric":
387-
data_format = "t"
388-
elif component == "dc":
389-
data_format = "y"
390-
else: # component == 'full'
391-
data_format = "x"
392-
# Support old-school GMT format options
393-
elif convention in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]:
394-
data_format = convention
395-
else:
396-
raise GMTError("Convention not recognized.")
447+
# determine data_foramt from convection and component
448+
data_format = data_format_code(convention=convention, component=component)
397449

398450
# Assemble -S flag
399451
kwargs["S"] = data_format + scale

0 commit comments

Comments
 (0)