The class parses and serializes the spectra section
header that precedes one or more >SPECTRA data
blocks. It collects the option key/values and the
ordered set of measurement IDs that the spectra apply
to, as described by the SEG EDI convention [1]_.
Parameters:
verbose (int or bool, optional) – Verbosity flag propagated from Base.
logger (object, optional) – Logger instance to use. If None, a default
null-safe logger is attached.
**kws – Additional field overrides. Keys may include
any attribute listed below.
>>> sect.nfreq=3>>> sect.meas_ids=["HX","HY","EX","EY"]>>> lines=sect.write()>>> print("".join(lines).strip())>=SPECTRASECT SECTID=... NCHAN=... NFREQ=3 MAXBLKS=... // 4 HX HY EX EY
A spectra section contains one block per frequency.
Each block begins with a >SPECTRA line that holds
options such as frequency and bandwidth, optionally
followed by a comment with the number of values,
then one or more lines of numeric values.
Known options are normalized:
FREQ : float
ROTSPEC : int
BW : float
AVGT : float
Unrecognized options are preserved in a free-form
mapping so that vendor-specific metadata is not lost.
Parameters:
verbose (int or bool, optional) – Verbosity flag propagated from Base.
logger (object, optional) – Logger instance to use. If None, a default
null-safe logger is attached.
blocks (list of _SpectraBlock) – Parsed spectra blocks, one per frequency. Each
block stores header options, the optional value
count hint, and the numeric values.
Notes
from_file() reads successive >SPECTRA
blocks starting from a given line or from the
first match in the file.
Values are parsed as floats; non-numeric tokens
in data lines are ignored rather than raising.
The writer orders known options first in the
header line, then appends extra options sorted
by key. Both option keys and values are written
in upper case.
Line formatting uses the per-line and float
format defaults from Base unless you
provide explicit overrides.
Use this mixin in higher-level readers so spectra
handling remains consistent and centralized. The
method pair mirrors the design used for MT/EMAP
headers and for time series sections.
Container for >SPECTRA blocks grouped per frequency.
The class gathers one spectra record per frequency and
exposes typed header fields (frequency, rotation flag,
bandwidth, and averaging time) together with the numeric
values stored in each block. It is a compact, array-
oriented view on top of SpectraSECT and SpectraIO.
Parameters:
name (str, optional) – Display name forwarded to BaseEM.
verbose (int, default 0) – Verbosity level, forwarded to BaseEM.
Variables:
freq (ndarray, shape (n_blk,)) – Frequency (Hz) per block. Missing values are set to
np.nan.
rotspec (ndarray of int, shape (n_blk,)) – Rotation specifier per block. Missing values are set
to -1.
bw (ndarray, shape (n_blk,)) – Nominal bandwidth (Hz) per block or np.nan.
avgt (ndarray, shape (n_blk,)) – Averaging time (s) per block or np.nan.
values (list of ndarray) – Numeric payload for each block. Lengths may differ
across blocks, as allowed by the SEG format.
n_values (ndarray of int, shape (n_blk,)) – Number of values in each block (as parsed or counted).
Notes
Blocks may contain vendor-specific options beyond the
canonical FREQ, ROTSPEC, BW, and AVGT.
Those options are preserved when round-tripping via
to_io(). The class does not impose a common
length across spectra vectors; if you require a 2-D array,
pad the values list explicitly.
The constructor itself does not read files. Use
from_io() or from_file() to populate an
instance from sections and data blocks.
frompycsamt.seg.spectraimportSpectrasp=Spectra.from_file("site.edi")f=sp.freqfirst=sp.values[0]sect2,io2=sp.to_io()# writer can now combine sect2.write() and io2.write()
Calls to_Z() and assembles a complete
>=MTSECT / >FREQ / >ZXXR / >ZXYR / … EDI
ready to be saved with write().
The structural sections (>HEAD, >INFO, >=DEFINEMEAS)
are re-used from source_edi when provided, preserving all
acquisition metadata; otherwise a minimal header is synthesised
from the Spectra metadata.
According to the SEG EDI standard (§ 7.53, 12.1), an MT data
section requires:
The measurement IDs for HX, HY, … in >=MTSECT are
resolved from id_to_chtype (populated by
SpectraSECT from >HMEAS / >EMEAS lines).
Parameters:
source_edi (str, Path, or EDIFile, optional) – Spectra EDI file whose >HEAD, >INFO, and
>=DEFINEMEAS sections are copied into the output.
Pass the same path used with from_file() to produce
a fully metadata-rich result. When None, a minimal
header is synthesised.
station_name (str, optional) – Override for the DATAID in >HEAD and SECTID in
>=MTSECT. Defaults to name.
e_labels (tuple of str) – Electric channel type labels forwarded to to_Z().
h_labels (tuple of str) – Horizontal magnetic channel type labels forwarded to to_Z().
ridge (float, optional) – Tikhonov regularisation forwarded to to_Z().
estimate_error (bool) – If True, propagate 1-σ errors into >ZXX.VAR … blocks.
dof (float or ndarray, optional) – Effective degrees of freedom forwarded to to_Z().
Returns:
Fully populated MT-impedance container. Call
write() to save.
Recover an impedance tensor Z and, if available, the
tipper from cross-spectra stored in this Spectra.
The method resolves channel types, extracts the electric
and magnetic sub-blocks, and computes per-frequency
Z=S_EH@inv(S_HH). If a vertical magnetic channel
is present it also computes the tipper
T=S_ZH@inv(S_HH). Optional ridge regularization
can be applied to stabilize the magnetic block.
Parameters:
id_to_chtype (dict of str to str, optional) – Mapping from measurement IDs (as recorded in
>=SPECTRASECT or DefineMeas) to channel
types ("HX", "HY", "HZ", "EX",
"EY"). If omitted, the method uses
self.id_to_chtype when available, otherwise it
interprets self.chan_ids directly as labels.
e_labels (tuple of str, default ("EX", "EY")) – Labels that identify the two electric channels used
for the E block.
h_labels (tuple of str, default ("HX", "HY")) – Labels that identify the two horizontal magnetic
channels used for the H block.
use_remote (bool, default False) – When duplicate electric channels exist (e.g., local
and remote), choose the second occurrence for the
E block if True; otherwise choose the first.
ridge (float, optional) – Non-negative Tikhonov regularization added to
S_HH prior to inversion, S_HH+ridge*I.
estimate_error (bool, default True) – If True, estimate per-component 1-sigma standard
errors for Z (and tipper when available) using
compute_errors_from_S and the degrees of freedom
given by dof (or inferred; see Notes).
dof (float or ndarray, optional) – Effective degrees of freedom per frequency. If an
array is provided it must broadcast to n_freq.
If None and estimate_error is True, the
method tries to infer DoF from metadata via
effective_dof_from_meta using segnum, or
avgt*bw as a fallback.
Returns:
z_obj (pycsamt.z.z.Z) – Impedance object on the spectra frequency grid with
z populated and, when estimated, z_err set.
tip (pycsamt.z.tipper.Tipper or None) – Tipper on the same grid when HZ is available.
When errors are estimated, tipper uncertainties are
attached.
Raises:
EdIDataError – If spectra are empty, channel types cannot be
resolved, or the stabilized magnetic block is
singular.
Notes
Per frequency, Z is formed as Z=S_EH@inv(S_HH),
where S_EH is the cross-spectra between E and H, and
S_HH is the magnetic auto/cross block. If a vertical
magnetic channel is available, the tipper is computed as
T=S_ZH@inv(S_HH).
Channel type resolution proceeds in this order:
explicit id_to_chtype argument,
self.id_to_chtype from the section header or
DefineMeas,
direct interpretation of self.chan_ids.
When both local and remote electric channels are present,
setting use_remote=True chooses the second occurrence
as a simple heuristic. Frequency ordering is preserved.
Uncertainties are computed by first-order propagation
under a complex-Wishart model and scale as
1/sqrt(DoF). If DoF cannot be determined, errors
are left as NaN.
This class method is a thin, convenience wrapper
around spectra_from_Z(). It synthesizes a full
Hermitian cross–spectral density tensor from the
impedance tensor Z(f) and optional inputs that
control magnetic power and tipper usage.
Parameters:
z_obj (Z) – Input impedance object. The attributes
z_obj.z (shape (n,2,2)) and
z_obj.freq (shape (n,)) must be set.
**kws (Any) – Forwarded to spectra_from_Z(). See that
function for the complete set of options such as
S_HH, H_psd, tipper, include_hz,
and chan_order.
Returns:
A spectra container on the same frequency grid as
z_obj. Channel order follows the requested
chan_order (default: HX,HY,EX,EY).
EdIDataError – If z_obj is incomplete (missing z or
freq).
Notes
Absolute spectral levels are not carried by the
impedance tensor. To obtain physically scaled
spectra, provide magnetic spectra via S_HH or
H_psd. If neither is given, a unit–power
assumption is used (S_HH=I), which is suitable
for tests but not for quantitative analysis.
This method does not infer per–frequency metadata
such as bandwidth or averaging time; those fields
are initialized with zeros/NaNs.