2.9.1.18. pycsamt.seg.validation#
SEG-EDI validators (no base-class deps).
Module Attributes
|
Encodings tried, in order, by |
Functions
|
Read an EDI file as text lines, tolerating legacy encodings. |
Classes
|
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:
- Raises:
FileNotFoundError – If
pathdoes not exist.UnicodeDecodeError – If no codec in
encodingscan decode the file (only possible with a custom list that omits'latin-1').
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:
ABCAbstract 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 existingIsEdiinstance. The check is heuristic, fast, and tolerant of minor formatting issues.The validator recognizes three EDI families:
Impedance-style files that contain a
>FREQblock and a measurement section such as>=MTSECT,>=DEFINEMEAS,>=EMAPSECTor>=OTHERSECT.Spectra-style files that include
>=SPECTRASECTand or at least one>SPECTRAblock.Time-series files that include
>=TSERIESSECTand or at least one>TSERIESblock.
In addition, the first top-level tag must be
>HEADand the last must be>END. On failure the method raises a descriptiveEdIDataError.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-sigwitherrors="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_fileParse
>=MTSECT/>=EMAPSECTheaders.pycsamt.seg.spectra.SpectraSECT.from_fileParse
>=SPECTRASECTheaders.pycsamt.seg.time_series.TSect.from_fileParse
>=TSERIESSECTheaders.
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 concreteIsEdiinstance is provided instead of a file path.- Returns:
Truewhen the instance is valid. Subclasses decide the exact criteria.- Return type:
Notes
Implementations should be lightweight and side-effect free. Heavy validation belongs to dedicated readers (e.g., MTEMAP, Spectra, Time-Series parsers).