Template utilities for AVG/J -> EDI transformations.
This module defines TransformerMixin, a light
template that standardizes the steps needed to convert
foreign transfer-function containers (e.g. Zonge AVG or
Jones J) into EDI files or collections.
The mixin focuses on finalization of a neutral payload
(pycsamt.core.base.TFBundle): validating the
station name, ordering and de-duplicating frequencies, and
optionally filling missing pieces (Z vs rho/phase)
according to user config.
>>> frompycsamt.core._transformersimportTransformerMixin>>> frompycsamt.core.baseimportTFBundle>>>>>> classMyX(TransformerMixin):... defextract(self,source):... # pull TF data from source into a bundle... returnTFBundle(freq=[1.0],rho=[100.],phase=[45.])... defemit_edi(self,bundle):... # return an EDI-like stub for illustration... return{'freq':bundle.freq,'rho':bundle.rho}...>>> out=MyX().transform(object())>>> isinstance(out,dict)True
This transformer orchestrates extraction of transfer
functions from a Zonge AVG
object (or file path), finalizes the neutral payload
(TFBundle), and emits EDI
objects. The result is always an
EDICollection, even for
single-site inputs.
The pipeline is:
Parse TFs from the AVG using to_tensor. When Z is
not present, fall back to (rho,phase).
Finalize each bundle using
TransformerMixin:
validate the station name, order and de-duplicate
frequencies, and fill missing parts if enabled by the
global config.
Materialize EDIFile objects and
attach site metadata (e.g., coordinates) if available
from avg.topo.frame.
Reconstruct Z from apparent resistivity and phase.
If a bundle lacks Z but holds (rho,phase) and the
global config enables compute_z_from_res, this method
builds a complex tensor whose magnitude and angle satisfy
the standard MT relations:
|Z|=sqrt(μ0*ω*ρa)
phase is interpreted as milliradians and converted to
radians before forming Z=|Z|(cosφ+isinφ).
Parameters:
b (TFBundle) – Input bundle with rho and phase set.
Ensure phase units are consistent. Zonge workflows often
store milliradians; the implementation converts by dividing
by 1000. If units differ, override this method.
Populates Z fields (_freq, _z, _z_err). If
Z is missing but (rho,phase) are present, uses the
backend method to set resistivity and phase. If tipper data
exist, populates tipper arrays and derived attributes.
Convert a Jones J source to EDI or an EDI collection.
This transformer ingests a JFile
instance or a path to a .j file, extracts transfer
functions (Z, tipper, or fallback rho/phase),
finalizes a neutral payload
(TFBundle), and emits concrete
EDIFile objects. When given a
JCollection, it produces
an EDICollection.
The pipeline mirrors the AVG workflow:
Extract TF arrays and metadata from the J structure,
tolerating variant attribute names often found in legacy
files.
Finalize each bundle using the mixin logic: ensure a
valid station name, order and de-duplicate frequencies,
and fill missing TF parts if configured.
Emit EDI and optionally attach site coordinates if a
header-like structure is present on the J side.
Notes
Naming, frequency sorting, and de-duplication follow the
global configuration in pycsamt.core.config.
If only (rho,phase) are present, Z can be
reconstructed when allowed by
compute_z_from_res (via the mixin hook).
A Head or head object on JFile with attributes
lat, long, and elev is used to populate the EDI
>HEAD section when available.
Populates impedance arrays when present. If Z is absent
but (rho,phase) are available, the backend call is used
to set resistivity/phase onto the EDI structure. Tipper data
are also populated when provided.
>>> frompycsamt.core.transformersimportJtoEDI>>> # Single file>>> # edi = JtoEDI().transform(\"/data/site.j\")>>> # Collection>>> # out = JtoEDI().transform(j_collection)
Transform SEG spectra-EDI files to MT-impedance EDI files.
The transformer calls from_file()
on each input file, then to_edi()
to recover Z and, when a vertical-magnetic channel is present,
the tipper. All successfully converted files are collected into a
single EDICollection.
Parameters:
e_labels (tuple of str, default ("EX", "EY")) – Electric-channel type labels used to identify E-field channels
in the spectra block.
h_labels (tuple of str, default ("HX", "HY")) – Horizontal magnetic-channel type labels.
ridge (float, optional) – Tikhonov regularisation added to the magnetic block before
inversion. Useful for numerically ill-conditioned spectra.
None (default) applies no regularisation.
dof (float or ndarray, optional) – Effective degrees of freedom per frequency, forwarded to
to_Z(). When None,
the transformer tries to infer DoF from spectra metadata
(segnum, avgt, bw).
use_remote (bool, default False) – When both local and remote electric channels are present,
set True to select the remote reference.
station_suffix (str, default "") – Appended to the station name of every converted EDI. Use
"_IMP" to reproduce the HBH03→HBH03_IMP convention.
skip_errors (bool, default True) – If True, per-file conversion failures are caught, logged,
and included in TransformResult.failures without
aborting the batch. Set to False to raise on first error.
Transform raw MT field time series to impedance EDI files.
Each source record is processed with
pycsamt.ts.ts_to_edifile(): band-averaged cross-power
spectra (Hann-tapered overlapping segments, Huber-weighted
robust stacking), impedance recovery Z=S_EH·S_HH⁻¹
(or remote reference Z=S_ER·S_HR⁻¹), and EDI header
synthesis from the recorded site metadata (coordinates,
channel azimuths, dipole lengths). All successfully
converted records are gathered into a single
EDICollection.
Parameters:
estimator ({'ls', 'rr'}, default 'ls') – Single-site least squares, or remote reference (needs
remote).
remote (TSData, optional) – Simultaneous remote-reference record whose horizontal
magnetic channels are appended as RHX/RHY.
nfft (int, optional) – FFT segment length. Defaults to an automatic choice
targeting ~60+ segments at 50 % overlap.