10
10
from pygmt .src ._common import _FocalMechanismConvention
11
11
12
12
13
+ def _get_focal_convention (spec , convention , component ) -> _FocalMechanismConvention :
14
+ """
15
+ Determine the focal mechanism convention from the input data or parameters.
16
+ """
17
+ # Determine the convention from dictionary keys or pandas.DataFrame column names.
18
+ if hasattr (spec , "keys" ): # Dictionary or pandas.DataFrame
19
+ return _FocalMechanismConvention .from_params (spec .keys (), component = component )
20
+
21
+ # Determine the convention from the 'convention' parameter.
22
+ if convention is None :
23
+ msg = "Parameter 'convention' must be specified."
24
+ raise GMTInvalidInput (msg )
25
+ return _FocalMechanismConvention (convention = convention , component = component )
26
+
27
+
13
28
@fmt_docstring
14
29
@use_alias (
15
30
A = "offset" ,
@@ -234,13 +249,11 @@ def meca( # noqa: PLR0912, PLR0913
234
249
"""
235
250
kwargs = self ._preprocess (** kwargs )
236
251
252
+ # Determine the focal mechanism convention from the input data or parameters.
253
+ _convention = _get_focal_convention (spec , convention , component )
254
+
237
255
# Convert spec to pandas.DataFrame unless it's a file
238
256
if isinstance (spec , dict | pd .DataFrame ): # spec is a dict or pd.DataFrame
239
- # Determine convention from dict keys or pd.DataFrame column names
240
- _convention = _FocalMechanismConvention .from_params (
241
- spec .keys (), component = component
242
- )
243
-
244
257
# convert dict to pd.DataFrame so columns can be reordered
245
258
if isinstance (spec , dict ):
246
259
# convert values to ndarray so pandas doesn't complain about "all
@@ -250,14 +263,6 @@ def meca( # noqa: PLR0912, PLR0913
250
263
{key : np .atleast_1d (value ) for key , value in spec .items ()}
251
264
)
252
265
elif isinstance (spec , np .ndarray ): # spec is a numpy array
253
- if convention is None :
254
- msg = "'convention' must be specified for an array input."
255
- raise GMTInvalidInput (msg )
256
-
257
- _convention = _FocalMechanismConvention (
258
- convention = convention , component = component
259
- )
260
-
261
266
# Convert array to pd.DataFrame and assign column names
262
267
spec = pd .DataFrame (np .atleast_2d (spec ))
263
268
colnames = ["longitude" , "latitude" , "depth" , * _convention .params ]
@@ -277,10 +282,6 @@ def meca( # noqa: PLR0912, PLR0913
277
282
)
278
283
raise GMTInvalidInput (msg )
279
284
spec .columns = colnames
280
- else :
281
- _convention = _FocalMechanismConvention (
282
- convention = convention , component = component
283
- )
284
285
285
286
# Now spec is a pd.DataFrame or a file
286
287
if isinstance (spec , pd .DataFrame ):
0 commit comments