Skip to content

Commit b39c47a

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

File tree

1 file changed

+79
-28
lines changed

1 file changed

+79
-28
lines changed

pygmt/src/meca.py

+79-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,83 @@
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")
57+
Traceback (most recent call last):
58+
...
59+
pygmt.exceptions.GMTInvalidInput: Invalid component 'invalid' for convention 'mt'.
60+
"""
61+
# Codes for focal mechanism formats determined by "convention"
62+
codes1 = {
63+
"aki": "a",
64+
"gcmt": "c",
65+
"partial": "p",
66+
}
67+
68+
# Codes for focal mechanim formats determined by both "convention" and "component"
69+
codes2 = {
70+
"mt": {
71+
"deviatoric": "z",
72+
"dc": "d",
73+
"full": "m",
74+
},
75+
"principal_axis": {
76+
"deviatoric": "t",
77+
"dc": "y",
78+
"full": "x",
79+
},
80+
}
81+
82+
if convention in codes1:
83+
return codes1[convention]
84+
elif convention in codes2:
85+
if component not in codes2[convention]:
86+
raise GMTInvalidInput(
87+
f"Invalid component '{component}' for convention '{convention}'."
88+
)
89+
return codes2[convention][component]
90+
elif convention in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]:
91+
return convention
92+
else:
93+
raise GMTInvalidInput(f"Invalid convention '{convention}'.")
94+
95+
1996
@fmt_docstring
2097
@use_alias(
2198
R="region",
@@ -366,34 +443,8 @@ def update_pointers(data_pointers):
366443
else:
367444
raise GMTError("Parameter 'spec' contains values of an unsupported type.")
368445

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.")
446+
# determine data_foramt from convection and component
447+
data_format = data_format_code(convention=convention, component=component)
397448

398449
# Assemble -S flag
399450
kwargs["S"] = data_format + scale

0 commit comments

Comments
 (0)