Lightweight wrapper exposing station-centric accessors
and utilities for a single EDIFile.
The mixin normalizes common site operations around the
underlying EDI content, including coordinate handling,
impedance arrays, tipper, derived resistivity/phase, and
convenient export to pandas.DataFrame.
Notes
The station name is resolved from EDI HEAD fields in the
following order: dataid, station, sitename,
name, STATION. If none is present, the file stem
is used. See _station_name().
The Z tensor is treated as an array of shape (n,2,2)
or flattened to (n,4) as needed. The order is:
Zxx,Zxy,Zyx,Zyy.
The method writes to EDI HEAD fields dataid,
station, sitename, name (best effort) and
also updates edi.name. Downstream containers that
derive station IDs from the file stem may be configured
to prefer edi.name to preserve the rename.
High-level wrapper for a single MT/CSAMT site backed by an
EDIFile. The class enforces a
stable, file-stem-based station identifier and exposes
convenient accessors for coordinates, impedance Z, tipper,
and derived quantities.
The constructor normalizes EDI HEAD fields so that
dataid (and, if absent, station) matches the site
stem resolved by _stem_from_edi(). If an in-memory
edi.name exists, the stem may prefer it so that a prior
rename is preserved. This normalization improves name-based
indexing, deterministic selection, and downstream joins in
collections.
Parameters:
edi (pycsamt.seg.edi.EDIFile) – Parsed SEG-EDI container holding one station. The file
may be constructed from disk or synthesized in memory.
name (str) – Normalized station identifier. By default this equals
the file stem, unless a previous explicit rename is in
effect.
coords (tuple of float) – Geographic location as (lat,lon,elev). Latitude
and longitude are in degrees; elevation is in meters.
Notes
Identity policy
The site identity is derived from header fields in the
following order: dataid, station, sitename,
name, STATION. If none are present, the file
stem is used. The constructor ensures that dataid is
set to the resolved stem to stabilize lookups.
Array conventions
The impedance tensor \(Z\) may be represented as a
3D array with shape (n,2,2) or flattened to
(n,4) in the component order
Zxx,Zxy,Zyx,Zyy. Frequencies are 1D of length
n. The tipper has two columns Tx and Ty.
Derived fields
Apparent resistivity and phase are derived from
\(Z\) and the frequency vector. When Z or frequency
slices are applied, recomputation is triggered after
arrays are aligned to avoid inconsistent shapes.
Robustness
Accessors are defensive. Missing arrays produce empty
frames or None. Coordinates are validated for range
using utility checks.
Container for multiple Site
objects with convenient indexing, selection, and bulk edit
operations.
Sites wraps each provided EDIFile
into a Site, ensuring that station
identity is normalized consistently. You can iterate, index by
integer, or look up by case-insensitive station name. Bulk
operations such as renaming, frequency slicing, and masking
are provided via edit_all().
Parameters:
edic (pycsamt.seg.collection.EDICollection or sequence of) – pycsamt.seg.edi.EDIFile
Parsed EDI collection or any sequence of EDI objects.
Items are wrapped into Site instances in the
order provided.
Variables:
_items (list of Site) – Internal sequence of sites. This is considered private.
Iterate over Sites instead of accessing it directly.
Notes
Identity and lookup
Each site inside the container uses the same naming
policy as Site. Name-based lookups with
sites["E01"] are case-insensitive and match the
normalized station name. If duplicates exist, the first
match is returned.
Order preservation
Ordering of input items is preserved. Integer indexing
with sites[i] retrieves the i-th Site.
Bulk edits
edit_all() supports three common operations:
- rename: compute a new name from the old name.
- freq_slice: apply a frequency slice (slice)
consistently across Z, freq, errors, and derived fields.
mask: apply a boolean mask to the Z tensor rows.
Use inplace=True to modify the container, otherwise a
new Sites is returned.
Geospatial helpers
closest() uses geodetic distance to find the nearest
site to a given latitude and longitude. Sites with missing
or non-finite coordinates are skipped.
Topography integration
with_topography() aligns site coordinates and
elevation with a user-provided frame, returning a new
container unless inplace=True is requested.
Profile conversion
to_profile() attempts to build a
Profile when that optional
dependency is available. Otherwise, a lightweight dict
describing a chainage-ordered sequence is returned.
Persistence
write() emits one EDI file per site into a target
directory. If an EDI object provides to_file, it is
used; otherwise a placeholder is written.
Robustness and errors
__getitem__ raises KeyError when a name is not
found. Prefer get() to obtain None instead.
Bulk operations ignore missing arrays on a best-effort
basis so that other arrays can still be processed.
>>> sl=slice(1,None)# drop the first frequency>>> out2=sites.edit_all(freq_slice=sl,inplace=False)>>> f0=sites["E01"].freq>>> f1=out2["E01"].freq>>> len(f1)==len(f0)-1True
Bulk-edit all sites with optional rename, frequency slicing,
and tensor masking.
Parameters:
rename (callable, optional) – Function rename(old_name)->new_name. If provided,
each site is renamed accordingly.
freq_slice (slice, optional) – Row-wise slice applied consistently to frequency,
impedance Z, errors, and derived arrays.
mask (callable, optional) – Function mask(df)->bool_series where df is the
output of site.to_dataframe("z"). Rows where the
mask is False are set to NaN in Z.
inplace (bool, optional) – If True, modify this container and return it. If
False, return a new Sites. Default is
False.
Returns:
The edited container (possibly the same instance).
Align site coordinates and elevation from a tabular frame.
Parameters:
frame (Any) – A table-like object (e.g., pandas.DataFrame)
with station identifiers and columns for latitude,
longitude, and elevation. Column names are resolved by
the topography utility.
inplace (bool, optional) – If True, modify this container and return it. If
False, return a new Sites. Default is
False.
Construct a container from heterogeneous inputs by using a
normalized loading session.
Parameters:
source (Any) – Input that can be understood by the loader. Supported
cases include:
- an EDICollection,
- a list of EDIFile,
- a single EDIFile,
- or an iterable that yields EDIs.
topo_src (Any, optional) – Optional topography source passed to the session for
use during loading. The default is None.
Returns:
Parsed container. Returns an empty container if the
input cannot be interpreted.
Use this utility at API boundaries to conveniently accept
multiple input forms while providing a consistent downstream
interface. If you need independent copies of the underlying
data, perform your own cloning before calling to_sites.
This is the inverse boundary of to_sites(). It accepts a
single Site, a Sites collection, an
EDICollection, raw EDI objects, path-like inputs, or mixed
iterables containing those forms. The returned objects are the
underlying EDI containers used by low-level writers, exporters,
and EM processing functions.
Parameters:
x (Any) – Site-like input to unwrap. Supported values include Site,
Sites, EDIFile, EDICollection, path-like inputs,
or iterables containing site/EDI-like objects.
as_collection (bool, default False) – If True, return an EDICollection.
Otherwise a single input returns one EDI object and multi-item
inputs return a list.
copy (bool, default False) – If True, return best-effort deep copies of the EDI objects.
If copying fails for an item, that item is returned unchanged.
recursive (bool, default True) – Forwarded to path-like discovery through EDICollection.
on_dup ({'replace', 'keep', 'keep_first', 'keep_last', 'raise'}, default 'replace') – Duplicate station policy. replace and keep are forwarded
to path loading. keep_first, keep_last, and raise are
enforced after collection construction.
strict (bool, default False) – If True, raise when an object cannot be unwrapped to EDI.
If False, invalid items are skipped.
verbose (int, default 0) – Verbosity forwarded to collection construction and duplicate
policy diagnostics.
progress (bool or {'auto'}, default False) – Enable progress display while unwrapping iterable inputs.
Returns:
Raw EDI object(s), depending on the input shape and
as_collection.
The operation is shallow by default. It returns the same EDI objects
wrapped by Site or Sites. Pass copy=True when the caller
should be able to mutate the returned objects independently.