Parse and serialize the top provenance comment line.
This helper targets lines such as:
#WRITTEN BY GEOTOOLS: kb0-s001 10/06/95 RAW RECS
It extracts the producer software name, an optional station
hint, a free-form date token, and an optional trailing note.
The writer defaults the software field to PYSCAMT when
none is provided.
Parameters:
top_lines (sequence of str, optional) – An initial slice of file lines. The constructor scans
these lines and parses the first matching banner.
software (str, optional) – Producer software name (e.g., 'GEOTOOLS'). When
omitted and the banner is not present, the writer uses
'PYSCAMT' as a default.
station (str, optional) – Station identifier hint found in the banner. This does
not replace the station found in the Head.
date (str, optional) – Free-form date token as found in the banner line.
note (str, optional) – Extra text following the date token on the same line.
verbose (int, default=0) – Verbosity for warnings during parsing.
Variables:
software (str or None) – Producer software parsed from the banner or provided by
the user.
station_hint (str or None) – Station token parsed from the banner; may differ from the
station found in the Head.
date (str or None) – Banner date token as text; not parsed into a datetime.
note (str or None) – Trailing free-form text after the date token.
The banner parser is intentionally liberal: it ignores
leading whitespace, is case-insensitive on the WRITTENBY
marker, and preserves the raw text of the trailing note. It
does not validate dates or enforce a specific date format.
Examples
>>> b=Banner().read([... '#WRITTEN BY GEOTOOLS: kb0-s001 10/06/95 RAW RECS'... ])>>> b.software,b.station_hint,b.date('GEOTOOLS', 'kb0-s001', '10/06/95')>>> Banner().write()[0].startswith('#WRITTEN BY PYSCAMT:')True
Parse and serialize a single J-format head triple.
This lightweight component represents one data-block header
consisting of the station line, the data-type line, and the
row count. It provides a consistent constructor and the
read / write round-trip expected by the package.
Parameters:
j_header_list (sequence of str, optional) – Three logical header lines. The sequence may be a clean
triple [station,dtype,n] or a longer slice from a
file; the parser will extract the first valid triple.
verbose (int, default=0) – Verbosity for warnings during parsing.
**kwargs – Accepted for API forwards-compatibility; ignored here.
Variables:
station (str or None) – Upper-cased station identifier. Trailing azimuth tokens
on the same line are supported and ignored here (but see
az_hint).
dtype (DataType or None) – Parsed data-type token (e.g., ZXY, RTE) with
optional normalized units and a TE/TM tensor hint.
n (int or None) – Number of data rows that follow this header.
az_hint (float or None) – Optional azimuth extracted from the station line when
present (e.g., KB0001-30). This is not the same
as site AZIMUTH from the info block.
The station regex accepts common field variants, including
hyphens and underscores, and an optional numeric azimuth
token after the station id. The data-type parser is liberal
with whitespace and recognizes SI (S.I.) and
FIELD units.
The class provides convenient accessors for station and
site-level properties, and a small banner recorder for the
top provenance line (#WRITTENBY...). It is intended as
the lightest useful representation of a single J header
section.
Parameters:
head (Head, optional) – Existing head to attach. If omitted, an empty
Head is created.
info (Info, optional) – Existing info to attach. If omitted, an empty
Info is created.
verbose (int, default=0) – Verbosity for warnings during parsing.
azimuth (latitude, longitude, elevation,) – Shortcuts to values provided by info. azimuth
falls back to head.az_hint when the info block does
not contain AZIMUTH.
This class does not parse the subsequent data rows. It is
focused on fast, dependency-free header discovery to support
scanning tasks and metadata extraction.
Parse and serialize the J-format information block.
This component collects >KEY=VALUE records along with
leading comment lines. It exposes a small set of convenience
properties derived from JSiteProperty.
Parameters:
j_info_list (sequence of str, optional) – A sequence containing the comment and information
records. The parser stops at the first non-info,
non-comment, non-blank line.
verbose (int, default=0) – Verbosity for warnings during parsing.
**kwargs – Accepted for API forwards-compatibility; ignored here.
Variables:
items (dict of (str -> str)) – Mapping of upper-cased keys to unmodified string values.
comments (list of str) – Preserved leading comment lines (starting with #).
site (JSiteProperty) – Lazily parsed view providing normalized latitude,
longitude, azimuth and elevation (see properties below).
latitude (float or None) – Decimal degrees; hemisphere and DMS tolerated.
longitude (float or None) – Decimal degrees in [-180,180).
azimuth (float or None) – Site X-axis azimuth (degrees, true north).
Container for site properties parsed from J-format info.
This class stores site-level metadata such as latitude,
longitude, azimuth, and elevation that are declared in the
J-format information block as >KEY=VALUE lines.
Parameters:
azimuth (float or None, optional) – Site X-axis azimuth in degrees, referenced to true
north. Values are wrapped to [0,360) when parsed.
latitude (float or None, optional) – Latitude in decimal degrees. Inputs may include DMS or
hemisphere suffixes and are normalized to [-90,90].
longitude (float or None, optional) – Longitude in decimal degrees. Inputs may include DMS or
hemisphere suffixes and are normalized to [-180,180).
elevation (float or None, optional) – Elevation in metres. Missing sentinels are mapped to
None.
extra (dict of (str -> str), optional) – Unrecognized >KEY=VALUE pairs preserved verbatim.
verbose (int or bool, default 0) – Verbosity for non-fatal parsing warnings.
strict (bool, default False) – If True, out-of-range coordinates raise errors
instead of being coerced to valid bounds.
Variables:
location (tuple of (float, float) or None) – Convenience property returning (lat,lon) when both
are available, else None.
azimuth_rad (float or None) – Azimuth converted to radians when available.
Notes
Parsing leverages robust coordinate helpers that accept DMS,
hemisphere letters, and free whitespace. Longitudes are
normalized into the half-open interval [-180,180).
The parser reads sequentially and collects >KEY=VALUE
pairs as long as lines are comments, blanks, or info
records. It stops at the first non-conforming line.
Parameters:
lines (iterable of str) – Lines that include the information block content.
strict (bool, default False) – If True, invalid coordinates raise. If False,
out-of-range values are coerced with a warning.
verbose (int or bool, default 0) – Verbosity for non-fatal parsing warnings.
Build a JSiteProperty from a dict of key-value
pairs.
Keys can be any case and will be normalized to the upper
case tokens used by J files. Values can be strings or
numbers. Unknown keys are preserved in extra.
Parameters:
mapping (Mapping[str, Any]) – Pairs like {\"latitude\":41.9,\"longitude\":140.8}.
strict (bool, default False) – If True, invalid coordinates raise. If False,
out-of-range values are coerced with a warning.
verbose (int or bool, default 0) – Verbosity for non-fatal parsing warnings.
One parsed resistivity/phase row of a J R/S block.
The row stores period (s), apparent resistivity (Ω·m), phase
(deg), their 1σ bounds, and per-column weights. A derived
boolean flag rej marks a row as rejected according to the
J-format rules.
Parameters:
period (float) – Period in seconds. If input stored frequency (Hz) as a
negative number, it is normalized to a positive period.
rho (float) – Apparent resistivity. Values <0 mark the estimate as
rejected.
A block ties a parsed header (Head) to a homogeneous
sequence of rows (RRow or TFRow). Subclasses
implement the row parser, normalization, serialization, and
simple QA summaries.
Parameters:
head (Head) – Parsed header triple (station, data-type, count).
rows (sequence of rows) – Concrete row records for this block family.
verbose (int, optional) – Verbosity for warnings during parsing and writing.
Variables:
head (Head) – Block header with station, kind, comp, n.
Header discovery is tolerant to real-world quirks, including
optional azimuth on the station line and the
count-before-dtype pattern seen in some files.
The mixin groups small, allocation-friendly utilities
that many parsers need. It keeps math, alignment and
token-handling logic out of higher-level classes.
Notes
The helpers are intentionally tiny and avoid importing
heavy libraries beyond numpy. They favor pure
functions that accept and return arrays.
Return a tuple (p_common,i0,i1) where
p_common are periods present in both sequences,
ordered like p0; i0 and i1 are indices
to select the matching rows in the original arrays.
This mixin concentrates the I/O-oriented parts for J
files: scanning blocks, aligning periods, normalizing
rows, and assembling higher-level objects (Z,
Tipper, and ResPhase).
It aims to be robust against non-canonical header
orders and minor format quirks.
Notes
Data rows are normalized before assembly. Period
sign conventions (negative means Hz) are corrected.
Missing sentinels (e.g., -999) are mapped to
nan for numeric arrays, but objects are pre-
allocated with zeros to keep shapes consistent.
When only rho/phi are present, impedance is rebuilt
using \(|Z|=\sqrt{\mu_0 \,\omega\, \rho}\) and
\(\phi\) for the phase. The vacuum permeability
\(\mu_0\) is imported from
pycsamt.constants.
Parse the file into a component dictionary indexed
by tokens like 'ZXY', 'RXX' or 'TZX'.
Values include period, real/imag/error (TF) or rho/
phi (+ auxiliary columns for R blocks).
Assemble Z,
Tipper, and
ResPhase from the
scanned components. Returns a triple
(Z|None,Tipper|None,ResPhase|None).
Examples
>>> mix=JIOMixin()>>> # (typically used via JFile; direct use shown here)>>> # comp = mix._scan_blocks(Path("data/j/site.j"))>>> # z, tip, rp = mix._build_from_comp(comp)
The class reads a J file, extracts headers and blocks,
and builds analysis-ready objects for impedance (Z),
resistivity/phase (Res), and tipper (Tip). It
also writes new J files from the in-memory state.
Parameters:
path (str or Path, optional) – Input J file. If omitted, call read() later.
verbose (int, default=0) – Verbosity level. Non-zero emits informational
messages during parsing and writing.
Variables:
path (Path or None) – Source path when set via __init__ or
from_file().
This convenience constructor mirrors __init__ +
read(). It resolves path to a filesystem
location, parses headers and data blocks, and builds
analysis-ready objects (Z, Tip, Res) when
present or derivable.
Parameters:
path (str or Path) – Path to a J-format text file (Jones v2.0 style).
verbose (int, default=0) – Verbosity flag. When non-zero, progress/info
messages may be emitted during parsing.
The method reads the banner, info block and the first
head triple, then scans all following data blocks.
Transfer functions (Zxx, Zxy, …) and tipper
(Tzx, Tzy) are assembled when present. If only
resistivity/phase blocks exist, a synthetic impedance
is rebuilt from \(\rho\) and \(\phi\).
Parameters:
path (str or Path, optional) – If given, set as the source and read from it.
If omitted, reuse self.path set at construction.
start (int, optional) – Line index hint to start scanning blocks. Most
users can leave this as None.
The writer renders a banner, info lines and one or more
data blocks selected via datatype. When uncertainties
are unavailable, zero-filled error columns are emitted to
preserve column layout. Periods are derived from the
active frequency vector.
Parameters:
j_fn (str, optional) – Base filename to use. If omitted, derive from
:pyattr:`path` or default to 'out.j'.
new_jfn (str, optional) – Replacement filename. Takes precedence over
j_fn when provided.
datatype ({'Z','R','T','ZR','RT','ZT','ZRT','ALL'}, optional) – Select families to emit. If None, the writer
auto-detects from available objects on the instance.
savepath (str or Path, optional) – Folder where to save. Defaults to the parent of
:pyattr:`path` or the current directory.
Abstract base for A.G. Jones J-format validation helpers.
Subclasses implement :pyattr:`is_valid`. The static method
_assert_j() provides a robust, file-level validator
that accepts either a path or an existing IsJ
instance. The check is heuristic, fast and tolerant of
minor formatting issues (e.g., non-canonical head order).
Lightweight helpers for scanning Jones J-format text.
The mixin implements tolerant, file-level utilities used by
higher-level parsers and collections. It focuses on small,
dependency-free pieces such as path coercion, candidate file
discovery, banner and header probing, and quick content
reads.
The mixin does not keep state. Methods are small and
side-effect free so they can be reused by different classes
(e.g., JCoreParser, JCBBase,
JCollection).
Notes
Utilities are designed to be permissive. They handle odd
encodings, mixed line endings, and common filename patterns.
They also accept both pathlib.Path and strings.
The intent is to keep the heavy parsing in dedicated
readers, while providing robust file discovery and quick
checks here.
Core scanner for J files that extracts light metadata.
Extends JParseMixin with small, read-only probes
that inspect the top header and first data head triple.
This class can reveal the station code, presence of info
keys, encountered data kinds, and the declared row count of
the first block. It avoids loading the whole file.
The implementation aims to be fast and safe for directory
crawls, where many files are filtered before full parse.
Variables:
encoding (str, default 'utf-8') – Encoding used when reading text. Implementations may
choose a tolerant variant such as 'utf-8-sig' with
errors='replace'.
The class manages a list of parsed items (usually
JFile), keeps light metadata for
indexing and filtering, and offers a stable surface for
higher-level collection types. It is intentionally small so
projects can extend it without tight coupling.
Parameters:
verbose (int, default 0) – Verbosity for logs and warnings. Implementations are
free to ignore it or to pass it to child readers.
Subclasses are encouraged to keep the API stable while
augmenting with domain-specific helpers, like saving index
CSV files or computing per-site quality metrics.
The base class uses tolerant discovery, so it can be used on
mixed folders where some files are not Jones J-format.
Mixin that provides folder/glob expansion and robust
parsing orchestration for Jones J-format collections.
The mixin augments a stateful base (e.g.,
JCBBase) with helpers to normalize user input
paths, expand folders and wildcards, deduplicate
candidates, and parse items using a tolerant strategy.
Implementations typically use
JFile as the per-item reader,
but the mixin is agnostic to the concrete class as long
as it exposes a compatible from_file constructor.
Notes
The design keeps discovery and parsing separate. First
the mixin expands and filters path candidates; then the
owning class decides how to instantiate items and how to
record failures. This separation avoids tight coupling
and keeps error handling clear.
The mixin favors permissive heuristics while still
catching obvious mistakes (non-files, unreadable paths,
extensions that are unlikely to be J-format, etc.).
Combines JCBBase (state container) with
JCollectionMixin (discovery/orchestration) to
offer a convenient interface for batch loading, quick
metadata browsing, and simple filtering over many J files.
Optional, serialize a CSV/TSV inventory for the
collection.
Notes
The collection is intentionally lightweight. It does not
enforce a database schema, and it avoids implicitly
reading large arrays until needed. This makes it suitable
for quick exploration and CI tests.
Error handling is conservative: unreadable paths or parse
errors are usually skipped with a warning (unless
strict=True is requested).
Exports all JFile items to a directory with advanced options.
This method orchestrates the writing of each JFile in the collection
to a specified directory, with flexible naming, error handling, and
an optional summary CSV file.
Parameters:
output_dir (Pathish) – The path to the directory where files will be saved. It will
be created if it does not exist.
file_pattern (str, default="{station}.j") – A format string for output filenames. Can use attributes of
JFile like {station}, {name}, {site}.
export_summary (bool, default=False) – If True, a summary of the collection will also be saved as a
CSV file in the output directory.
summary_filename (str, default="summary.csv") – The name of the summary file if export_summary is True.
**jfile_write_kwargs – Keyword arguments to be passed directly to each JFile.write()
call (e.g., datatype=”ZRT”, overwrite=True).
Returns:
A dictionary with two keys:
- ‘successful’: A list of paths to successfully written files.
- ‘failed’: A list of tuples, where each tuple contains the
Fetches JFile objects from the collection based on specified criteria.
This method provides a flexible way to search for J-format files
by site name, geographic coordinates, or any other attribute of
the JFile object or its nested Heads sections.
Parameters:
site (str, optional) – The site or station name to search for. The comparison is
case-insensitive.
lat (float, optional) – The latitude to search for, in decimal degrees.
lon (float, optional) – The longitude to search for, in decimal degrees.
tol (float, default=0.001) – The tolerance in decimal degrees for geographic coordinate
searches. A match is found if the absolute difference is
within this tolerance.
first (bool, default=False) – If True, returns only the first matching JFile object found,
or None if no match is found. If False, returns a list of
all matching objects.
**kwargs (Any) – Additional keyword arguments to match against attributes of
the JFile object or its nested Heads object (e.g.,
acqby=’Contractor’). The attribute name is case-insensitive.
Returns:
If first=True, returns the first matching JFile or None.
If first=False, returns a list of all matching JFile objects.
An empty list is returned if no matches are found.
Lightweight facade to manage Jones (J-format) components on a
host object. The host is expected to expose a mapping-like
container (self.sections or self.components). Keys are
normalized to lowercase for stable lookups.
The mixin centralizes small CRUD helpers (cget/cset/chas/cdrop/snapshot) and offers typed accessors for frequent J
parts such as banner, info, head, heads,
blocks, and derived data objects (z, tip, res).
It also provides compose_headers which serializes the
header portion of a J file by calling write() on available
components. If a combined heads component is present, it
takes precedence over separate banner/head/info
parts to avoid duplication.
Serialize header lines by querying available components.
If prefer_heads is True and a heads component is
present, it is used exclusively. Otherwise the mixin
attempts banner + head + info in that order.
When join is True, returns a single string joined
with newlines. Otherwise returns a list of lines.