pycsamt.core.base#
Foundational objects and transfer-function containers for pyCSAMT.
Functions
|
Return a valid station name using policy rules. |
|
Infer an adapter key ( |
|
Dispatch |
Classes
Minimal base class for PyCSAMT objects and mixins. |
|
|
Common electromagnetic and MT utilities. |
|
Protocol for classes that can be built from a |
|
Protocol for objects that can export a |
|
Lightweight, neutral payload for transfer functions. |
- class pycsamt.core.base.TFBundle(freq=None, z=None, z_err=None, tipper=None, tipper_err=None, rho=None, phase=None, station=None, station_id=None, lat=None, lon=None, elev=None, azimuth=None, attrs=<factory>)[source]#
Bases:
CoreObjectLightweight, neutral payload for transfer functions.
The bundle holds frequency-indexed arrays and optional site metadata. It is intentionally permissive about array types (e.g., lists or NumPy arrays) so that backends can populate and consume it without hard dependencies.
- Parameters:
freq (array_like or None) – Frequency vector, length
n.z (array_like or None) – Impedance tensor, shape
(n, 2, 2)(complex).z_err (array_like or None) – Errors for
z, shape(n, 2, 2)(float).tipper (array_like or None) – Magnetic tipper. Common shapes are
(n, 2)or(n, 1, 2); callers may normalize as needed.tipper_err (array_like or None) – Errors for the tipper, shape like
tipper.rho (array_like or None) – Apparent resistivity, length
n(float).phase (array_like or None) – Phase, length
n. Units are backend-specific; the Zonge path often stores milliradians.station (str or None) – Preferred station/site name.
station_id (str, int or None) – Identifier used to synthesize a name when missing.
lat (float or None) – Optional geographic location and elevation.
lon (float or None) – Optional geographic location and elevation.
elev (float or None) – Optional geographic location and elevation.
azimuth (float or None) – Sensor azimuth or site orientation (degrees).
attrs (dict) – Free-form attributes that accompany the data.
Notes
Only one of
zor(rho, phase)is required for most workflows. Transformers may fill the missing part using package settings (seepycsamt.core.config).Examples
>>> from pycsamt.core.base import TFBundle >>> TFBundle(freq=[1.0], z=[[ [0+1j, 0], [0, 0+1j] ]], ... station=\"S001\") TFBundle(...)
- class pycsamt.core.base.SupportsToBundle(*args, **kwargs)[source]#
Bases:
ProtocolProtocol for objects that can export a
TFBundle.Any class implementing:
to_bundle(self) -> TFBundleis considered compatible. This keeps interop light while avoiding a hard dependency on specific backends.
- class pycsamt.core.base.SupportsFromBundle(*args, **kwargs)[source]#
Bases:
ProtocolProtocol for classes that can be built from a
TFBundle.Any class implementing the classmethod:
from_bundle(cls, bundle: TFBundle) -> Anyis considered compatible. This is commonly used by test doubles or thin wrappers in frontends.
- pycsamt.core.base.ensure_station(name, station_id, *, policy=None)[source]#
Return a valid station name using policy rules.
Validation is performed by the active
StationNamePolicy. If the givennameis invalid or missing, a synthetic one is derived fromstation_id.- Parameters:
name (str or None) – Preferred station/site name.
station_id (str, int or None) – Identifier used to synthesize a fallback name.
policy (StationNamePolicy, optional) – Custom policy. Defaults to the global policy from
get_config().
- Returns:
Validated or synthesized station name.
- Return type:
See also
pycsamt.core.config.StationNamePolicyNormalization, synthesis and limits.
Examples
>>> ensure_station(None, 7) 'S007'
- pycsamt.core.base.pick_adapter_key(obj, *, hint=None)[source]#
Infer an adapter key (
'avg','j','edi') from an object.Heuristics look at the object’s module and class name. If a
hintis provided, it is returned verbatim (lower-cased).- Parameters:
- Returns:
Inferred key or
Noneif it cannot be decided.- Return type:
str or None
Notes
This function does not validate that an adapter exists for the returned key. Use
to_edi()to dispatch safely.Examples
>>> class X: pass >>> X.__module__ = 'pycsamt.zonge.avg' >>> pick_adapter_key(X()) # zonge path 'avg'
- pycsamt.core.base.to_edi(source, *, key=None, **kwargs)[source]#
Dispatch
sourceto a registered adapter and return EDI.This function consults the adapter registry managed by
pycsamt.core.config. It attempts to infer the adapter key when not provided and calls the associated factory.- Parameters:
source (Any) – The object to convert (AVG, Jones, etc.).
key (str, optional) – Adapter key (e.g.,
'avg','j','edi'). If omitted,pick_adapter_key()is used.**kwargs (Any) – Extra keyword arguments forwarded to the adapter.
- Returns:
An EDI object or an EDI collection, depending on the adapter.
- Return type:
- Raises:
RuntimeError – If the key cannot be inferred or no adapter is registered for it.
See also
pycsamt.core.config.register_adapterRegister new adapters at runtime.
pick_adapter_keyLightweight key inference from object metadata.
Examples
Register a trivial adapter and convert an object:
>>> from pycsamt.core.config import register_adapter >>> class Dummy: pass >>> Dummy.__module__ = 'pycsamt.zonge.avg' >>> register_adapter('avg', lambda src, **k: {'edi': True}) >>> to_edi(Dummy()) {'edi': True}