2.9.1.18. pycsamt.seg.validation#

SEG-EDI validators (no base-class deps).

Module Attributes

EDI_ENCODINGS

Encodings tried, in order, by read_edi_lines().

Functions

read_edi_lines(path[, encodings])

Read an EDI file as text lines, tolerating legacy encodings.

Classes

IsEdi()

Abstract base for SEG-EDI validation helpers.

pycsamt.seg.validation.EDI_ENCODINGS: tuple[str, ...] = ('utf-8-sig', 'gb18030', 'latin-1')

Encodings tried, in order, by read_edi_lines().

pycsamt.seg.validation.read_edi_lines(path, encodings=('utf-8-sig', 'gb18030', 'latin-1'))[source]

Read an EDI file as text lines, tolerating legacy encodings.

SEG EDI data blocks are pure ASCII, but free-text header fields (ACQBY, PROSPECT, >INFO …) are often written by vendor software in a local code page, e.g. GBK for Chinese instruments. A strict UTF-8 read then fails on the first non-ASCII byte although every number in the file is readable.

Parameters:
  • path (str or os.PathLike) – EDI file to read.

  • encodings (sequence of str, default EDI_ENCODINGS) – Codecs tried in order with strict error handling. The default ends with 'latin-1', which maps every byte, so the read never fails on decoding.

Returns:

Lines with their line terminators, as from io.TextIOBase.readlines().

Return type:

list of str

Raises:

Notes

UTF-8 is tried first, so valid UTF-8 files (including ASCII and BOM-prefixed files) are read exactly as before. GB18030 is a superset of GBK and GB2312.

Examples

>>> import tempfile, os
>>> from pycsamt.seg.validation import read_edi_lines
>>> fd, p = tempfile.mkstemp(suffix=".edi")
>>> with os.fdopen(fd, "wb") as f:
...     _ = f.write('>HEAD\n ACQBY="中南大学"\n'.encode("gbk"))
>>> read_edi_lines(p)[1].strip()
'ACQBY="中南大学"'
>>> os.remove(p)
class pycsamt.seg.validation.IsEdi[source]

Bases: ABC

Abstract base for SEG-EDI validation helpers.

Subclasses implement IsEdi.is_valid. The static method _assert_edi() provides a robust, file-level validator that accepts either a path or an existing IsEdi instance. The check is heuristic, fast, and tolerant of minor formatting issues.

The validator recognizes three EDI families:

  1. Impedance-style files that contain a >FREQ block and a measurement section such as >=MTSECT, >=DEFINEMEAS, >=EMAPSECT or >=OTHERSECT.

  2. Spectra-style files that include >=SPECTRASECT and or at least one >SPECTRA block.

  3. Time-series files that include >=TSERIESSECT and or at least one >TSERIES block.

In addition, the first top-level tag must be >HEAD and the last must be >END. On failure the method raises a descriptive EdIDataError.

Notes

The check is structural rather than semantic. It does not validate numerical values or cross-block consistency. Files are read as text using utf-8-sig with errors="replace" to gracefully handle odd encodings.

Examples

Validate an EDI file path:

>>> from pycsamt.seg.validation import IsEdi
>>> IsEdi._assert_edi("path/to/site.edi")
True

Use with an object that implements is_valid:

>>> class MyEdi(IsEdi):
...     @property
...     def is_valid(self):
...         return True
>>> IsEdi._assert_edi(MyEdi())
True

See also

pycsamt.seg.mtemap.MTEMAP.from_file

Parse >=MTSECT / >=EMAPSECT headers.

pycsamt.seg.spectra.SpectraSECT.from_file

Parse >=SPECTRASECT headers.

pycsamt.seg.time_series.TSect.from_file

Parse >=TSERIESSECT headers.

References

[IsEdi-1]

SEG (1987). MT/EMAP EDI Format Standard. Society of Exploration Geophysicists. Available online: https://www.mtnet.info/docs/seg_mt_emap_1987.pdf

abstract property is_valid: bool[source]

Indicate whether the current instance represents a structurally valid EDI object.

This property is used by IsEdi._assert_edi() when a concrete IsEdi instance is provided instead of a file path.

Returns:

True when the instance is valid. Subclasses decide the exact criteria.

Return type:

bool

Notes

Implementations should be lightweight and side-effect free. Heavy validation belongs to dedicated readers (e.g., MTEMAP, Spectra, Time-Series parsers).