91
91
np .datetime64 : "GMT_DATETIME" ,
92
92
np .timedelta64 : "GMT_LONG" ,
93
93
}
94
+ # Dictionary for storing the values of GMT constants.
95
+ GMT_CONSTANTS = {}
94
96
95
97
# Load the GMT library outside the Session class to avoid repeated loading.
96
98
_libgmt = load_libgmt ()
@@ -239,23 +241,41 @@ def __exit__(self, exc_type, exc_value, traceback):
239
241
"""
240
242
self .destroy ()
241
243
242
- def __getitem__ (self , name ):
244
+ def __getitem__ (self , name : str ) -> int :
245
+ """
246
+ Get the value of a GMT constant.
247
+
248
+ Parameters
249
+ ----------
250
+ name
251
+ The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``).
252
+
253
+ Returns
254
+ -------
255
+ value
256
+ Integer value of the constant. Do not rely on this value because it might
257
+ change.
258
+ """
259
+ if name not in GMT_CONSTANTS :
260
+ GMT_CONSTANTS [name ] = self .get_enum (name )
261
+ return GMT_CONSTANTS [name ]
262
+
263
+ def get_enum (self , name : str ) -> int :
243
264
"""
244
265
Get the value of a GMT constant (C enum) from gmt_resources.h.
245
266
246
- Used to set configuration values for other API calls. Wraps
247
- ``GMT_Get_Enum``.
267
+ Used to set configuration values for other API calls. Wraps ``GMT_Get_Enum``.
248
268
249
269
Parameters
250
270
----------
251
- name : str
252
- The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``)
271
+ name
272
+ The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``).
253
273
254
274
Returns
255
275
-------
256
- constant : int
257
- Integer value of the constant. Do not rely on this value because it
258
- might change.
276
+ value
277
+ Integer value of the constant. Do not rely on this value because it might
278
+ change.
259
279
260
280
Raises
261
281
------
@@ -266,18 +286,15 @@ def __getitem__(self, name):
266
286
"GMT_Get_Enum" , argtypes = [ctp .c_void_p , ctp .c_char_p ], restype = ctp .c_int
267
287
)
268
288
269
- # The C lib introduced the void API pointer to GMT_Get_Enum so that
270
- # it's consistent with other functions. It doesn't use the pointer so
271
- # we can pass in None (NULL pointer). We can't give it the actual
272
- # pointer because we need to call GMT_Get_Enum when creating a new API
273
- # session pointer (chicken-and-egg type of thing).
289
+ # The C library introduced the void API pointer to GMT_Get_Enum so that it's
290
+ # consistent with other functions. It doesn't use the pointer so we can pass
291
+ # in None (NULL pointer). We can't give it the actual pointer because we need
292
+ # to call GMT_Get_Enum when creating a new API session pointer (chicken-and-egg
293
+ # type of thing).
274
294
session = None
275
-
276
295
value = c_get_enum (session , name .encode ())
277
-
278
296
if value is None or value == - 99999 :
279
297
raise GMTCLibError (f"Constant '{ name } ' doesn't exist in libgmt." )
280
-
281
298
return value
282
299
283
300
def get_libgmt_func (self , name , argtypes = None , restype = None ):
0 commit comments