Rotate impedance tensor Z (and tipper T, if present) by an
azimuthal angle in degrees.
The rotation is applied in the horizontal plane using the
similarity transform \(Z' = R Z R^{-1}\), where \(R\)
is the 2x2 rotation matrix built from angle_deg. When a
tipper is available (either on the EDI object as T /
TIP / Tip or attached to Z), the 2-component
vector is rotated consistently.
Parameters:
site (Any) – An EDI-like object (e.g., pycsamt.seg.edi.EDIFile) or
wrapper exposing a Z section compatible with a
complex 2x2 impedance array and, optionally, a tipper.
angle_deg (float) – Rotation angle in degrees. Positive values rotate the
measurement axes according to the internal convention of
this package. If your acquisition system defines the sign
oppositely, use the negative of your desired angle.
inplace (bool, optional) – If True, mutate site in place. Otherwise, work on
a shallow copy and return that copy. Default is
False.
Returns:
The rotated object. If inplace is True, this is
the same object as site; otherwise a new object.
Error arrays (z_error or aliases) are rotated with a
magnitude-only scheme (using absolute values of the
rotation matrices) as a pragmatic best-effort. This is a
common, but approximate, practice.
Only arrays with shapes consistent with MT tensors
((n,2,2) for Z, (n,2) for T) are rotated. Other
shapes are ignored silently.
The function is no-throw by design. If a section is not
present or incompatible, it is skipped.
Examples
>>> frompycsamt.seg.ediimportEDIFile>>> frompycsamt.site.editimportrotate>>> ed=EDIFile("path/to/station.edi")>>> ed_rot=rotate(ed,30.0)# copy by default>>> ed_rot2=rotate(ed,-45.0,inplace=True)
Subset the dataset along frequency by range or explicit
indices, keeping all affected arrays aligned.
This applies the selection to every frequency-indexed array
found on the object, including Z, Z errors, derived
resistivity/phase, and any tipper arrays resident on either
the root EDI object or under its Z section.
Parameters:
site (Any) – An EDI-like object (e.g., pycsamt.seg.edi.EDIFile)
with a discoverable frequency vector.
fmin (float, optional) – Keep rows with freq>=fmin. Ignored if None.
fmax (float, optional) – Keep rows with freq<=fmax. Ignored if None.
keep (Iterable[int] or numpy.ndarray, optional) – Explicit indices or a boolean mask to keep. If provided,
fmin and fmax are ignored. Use integer indices
for exact row picks, or a boolean mask of the same length
as the frequency vector.
inplace (bool, optional) – If True, mutate site in place. Otherwise, operate
on a shallow copy. Default is False.
Returns:
The object after selection. If inplace is True,
this is the same object as site; otherwise a new
object.
Rename a station using an explicit name or a policy function.
This updates common station identifiers across the EDI header
and attempts to keep them in sync so that downstream code
resolves the new name consistently.
Parameters:
site (Any) – An EDI-like object (e.g., pycsamt.seg.edi.EDIFile) or
wrapper with a modifiable HEAD section.
name (str, optional) – Explicit new station name to set. If provided, this takes
precedence over policy.
policy (Callable[[str], str], optional) – A function mapping the current station name to a new one.
Ignored when name is provided.
inplace (bool, optional) – If True, mutate site in place. Otherwise, operate
on a shallow copy. Default is False.
Returns:
The object with updated identifiers. If inplace is
True, this is the same object as site; otherwise a
new object.
The function writes multiple header fields when present
(e.g., dataid, station, and other common aliases)
and also mirrors the name to edi.name. This increases
the chance that name resolution remains stable across
different readers.
The rename operation does not touch on-disk filenames. File
paths remain unchanged unless you later write out using a
template that depends on the station name.
Only the values explicitly provided are updated. The call
delegates to the same coordinate writer used by the Site
API, so downstream tools see consistent lat, lon, and elev
fields.
Parameters:
site (Any) – An EDI-like object (e.g., pycsamt.seg.edi.EDIFile)
or wrapper exposing a mutable HEAD section.
lat (float, optional) – Latitude in degrees. If None, the field is left
unchanged.
lon (float, optional) – Longitude in degrees. If None, the field is left
unchanged.
elev (float, optional) – Elevation in meters. If None, the field is left
unchanged.
inplace (bool, optional) – If True, mutate site in place. Otherwise, work
on a shallow copy and return that copy. Default is
False.
Returns:
The updated object. If inplace is True, this is
the same object as site; otherwise a new object.
Replace missing or non-finite values in Z and/or tipper
arrays with zeros or NaNs.
The operation preserves shapes and alignment across arrays.
If an array is absent, a new one is allocated with the
correct shape inferred from the frequency vector.
Parameters:
site (Any) – An EDI-like object with a discoverable frequency vector
and optionally Z and tipper sections.
how ({"zero", "nan"}, optional) – Replacement policy. Use "zero" to fill with numeric
zeros. Use "nan" to fill with NaN for all non-finite
entries. Default is "zero".
components (Iterable[str], optional) – Which components to process. Accepts items like
"Z" or "Tip" (case-insensitive). Default is
("Z","Tip").
inplace (bool, optional) – If True, mutate in place. Otherwise, operate on a
shallow copy and return that copy. Default is False.
Returns:
The object after filling. If inplace is True,
this is the same object as site; otherwise a new
object.
Z arrays are expected as shape (n,2,2) and tipper as
(n,2). Only arrays with the expected shapes are
modified or allocated.
When Z exists, the function also fills common aliases for
errors and derived quantities, such as z_error,
rho (resistivity), and phase arrays, if present.
The frequency vector length n defines the number of
rows used for any new arrays.
This function is no-throw by design. Incompatible or absent
pieces are skipped silently.
Examples
Fill Z and tipper with zeros where values are non-finite:
Recompute apparent resistivity and phase from the impedance
tensor Z for a single site.
The function looks for a Z section and, if present, calls
its compute_resistivity_phase() method. The operation is
best-effort and suppresses exceptions.
Parameters:
site (Any) – An EDI-like object (EDIFile) or a wrapper exposing a
Z section with the expected API.
inplace (bool, optional) – If True, mutate the given object in place. Otherwise
work on a shallow copy and return it. Default is
False.
Returns:
The mutated object (in place) or a new object (copy).
Rotate every site in a collection by an azimuthal angle in
degrees.
This is the broadcast variant of rotate(). It accepts
either a Sites wrapper or any iterable of EDI-like
objects and returns a new Sites unless inplace is
requested.
Parameters:
sites (Any) – A pycsamt.site.base.Sites instance or any iterable
of EDI-like objects.
angle_deg (float) – Rotation angle in degrees, passed through to
rotate().
inplace (bool, optional) – If True, attempt to apply the rotation in place on
the given container. Otherwise, return a new Sites.
Default is False.
Returns:
A sites collection holding the rotated items, or the
original container when mutated in place.
Subset all sites in a collection along frequency, keeping
arrays aligned.
This is the broadcast variant of select_freq(). It
accepts a Sites wrapper or any iterable of EDI-like
objects and returns a new Sites unless inplace is
requested.
Parameters:
sites (Any) – A pycsamt.site.base.Sites instance or any iterable
of EDI-like objects.
fmin (float, optional) – Keep rows with freq>=fmin. Ignored if None.
fmax (float, optional) – Keep rows with freq<=fmax. Ignored if None.
keep (Iterable[int] or numpy.ndarray, optional) – Explicit indices or a boolean mask to keep. If provided,
fmin and fmax are ignored.
inplace (bool, optional) – If True, attempt to modify the given container in
place. Otherwise, return a new Sites. Default is
False.
Returns:
A sites collection with selection applied, or the
original container when mutated in place.
This is the broadcast variant of rename(). It accepts a
Sites wrapper or any iterable of EDI-like objects and
produces a new Sites (unless inplace is True).
policy (Callable[[str], str], optional) – Function mapping the current station name to a new one,
e.g. lambdan:f"X_{n}". Ignored if name_fn is
provided.
name_fn (Callable[[Any], str], optional) – Function mapping each EDI object to a new name, e.g. from
the file stem. This takes precedence over policy and
is useful to guarantee uniqueness across sites.
inplace (bool, optional) – If True, attempt to modify the given container in
place. Otherwise, return a new Sites. Default is
False.
Returns:
A collection holding the renamed items, or the original
container when mutated in place.
The rename updates common header identifiers and mirrors
the name to edi.name for robust downstream resolution.
If multiple sites share the same original name, a simple
policy like lambdan:"X_"+n may produce duplicate
outputs. Prefer name_fn that uses a unique attribute
(e.g., the file stem) to avoid collisions.
The function preserves input order and is no-throw; items
that cannot be renamed are skipped.
Set site coordinates for many EDI files from a table.
This high-level helper accepts a wide range of table-like
objects (CSV path, pandas DataFrame, numpy structured array,
or list of dicts / tuples). It normalizes column names,
optionally projects easting/northing to lon/lat, builds a
mapping {station:(lat,lon,elev)}, and delegates to
set_coords_all().
Parameters:
sites (Any) – A Sites instance, an iterable
of EDIFile objects, or anything accepted by
set_coords_all().
columns (dict, optional) – Explicit column mapping. Keys are canonical names and
values are the actual column names present in table.
Supported canonical keys are:
'station', 'lat', 'lon', 'elev',
'easting', 'northing'. Matching is case-insensitive.
crs_from (str, optional) – Source CRS used when the table provides easting and
northing instead of lat and lon. Required in
that case. Example: 'EPSG:32631' for UTM 31N.
to_crs (str, default "EPSG:4326") – Target CRS for output coordinates. The default is WGS84
lon/lat.
inplace (bool, default False) – If True, mutate the given collection and return it.
Otherwise return a new Sites
with updated EDI objects.
Returns:
The same semantics as set_coords_all(): either the
mutated input (inplace=True) or a new
Sites instance.
ValueError – If the station column cannot be resolved, or if neither
(lat, lon) nor (easting, northing) can be
resolved, or if crs_from is required but missing.
ImportError – If projection is needed (easting/northing present)
but pyproj is not installed.
Notes
Column detection is case-insensitive and understands common
aliases:
station: station, name, site, id.
lat: lat, latitude.
lon: lon, long, longitude.
elev: elev, elevation, z.
easting: easting, x.
northing: northing, y.
When both geographic and projected fields are present, the
geographic pair (lat, lon) is preferred. If only
projected fields are present, a valid crs_from must be
provided and pyproj will be used for projection.
Examples
Load from a CSV path with standard columns:
>>> frompycsamt.site.editimportset_coords_from_table>>> frompycsamt.site.baseimportSites>>> edis=Sites([...])# your EDI files>>> out=set_coords_from_table(... edis,"coords.csv",inplace=False... )>>> isinstance(out,Sites)True
Pass a DataFrame with aliases and an explicit mapping:
Project (easting, northing) to lon/lat and set a site’s coords.
This convenience wraps projection and assignment for a single
EDI site. It projects the provided easting/northing from
crs_from to to_crs (default WGS84 lon/lat), then calls
set_coords().
Parameters:
site (Any) – A single EDI-like object compatible with
set_coords().
easting (float) – Easting value in meters for the source CRS.
northing (float) – Northing value in meters for the source CRS.
crs_from (str) – The EPSG or PROJ string that identifies the source CRS,
for example 'EPSG:32631' for UTM 31N.
elev (float, optional) – Elevation in meters to store in the EDI header. If not
provided, the previous elevation is kept (if any).
to_crs (str, default "EPSG:4326") – Target CRS for output coordinates. The default is WGS84
lon/lat.
inplace (bool, default False) – If True, mutate the given object and return it.
Otherwise work on a copy and return the copy.
Returns:
The mutated site (inplace=True) or a new site object
with updated coordinates.
Exception – Any errors raised by the underlying projection engine or
by set_coords() may propagate.
Notes
Projection uses pyproj with always_xy=True so that
the axis order is interpreted as (lon, lat). Units are assumed
to be meters for the easting and northing values.