2.23.2.3. pycsamt.format.text#
Reading and writing .pcsm, the text encoding of PCSF.
The implementation lives in the standalone pcsf package
(pcsf.core.text), the reference implementation of the format.
This module re-exports it under its pyCSAMT name, so that existing
imports keep working and pyCSAMT objects are the pcsf objects
themselves.
See also
pcsf.core.textImplementation.
- pycsamt.format.text.pcsf_to_pcsm(pcsf_path, pcsm_path, *, log10_view=False)
Convert a
.pcsf(HDF5) file to.pcsmtext.- Parameters:
pcsf_path (path-like) – Source file.
pcsm_path (path-like) – Destination; a
.gzsuffix compresses it.log10_view (bool, default False) – Passed to
write_pcsm().
- Returns:
The path written to.
- Return type:
See also
pcsm_to_pcsfInverse conversion.
pcsf.convertConversion with content detection.
Examples
>>> import os, tempfile >>> import pcsf >>> from pcsf.core.text import pcsf_to_pcsm, pcsm_to_pcsf >>> model = pcsf.PCSFModel( ... geometry=pcsf.Grid2DGeometry(x=[0.0], z=[5.0]), ... resistivity=[[100.0]], ... ) >>> with tempfile.TemporaryDirectory() as tmp: ... a = pcsf.write(model, os.path.join(tmp, "a.pcsf")) ... b = pcsf_to_pcsm(a, os.path.join(tmp, "b.pcsm")) ... c = pcsm_to_pcsf(b, os.path.join(tmp, "c.pcsf")) ... pcsf.read(c).resistivity.tolist() [[100.0]]
- pycsamt.format.text.pcsm_to_pcsf(pcsm_path, pcsf_path)
Convert a
.pcsmtext file to.pcsf(HDF5).- Parameters:
pcsm_path (path-like) – Source file, plain or gzip-compressed.
pcsf_path (path-like) – Destination.
- Returns:
The path written to.
- Return type:
See also
pcsf_to_pcsmInverse conversion, with an example.
- pycsamt.format.text.peek_kind(path)
Geometry kind of a file, without reading its arrays.
Only the geometry attribute of a
.pcsffile or theGEOMETRY_KINDheader of a.pcsmfile is read, so that a file picker can label many files cheaply.- Parameters:
path (path-like) – A
.pcsf,.pcsmor.pcsm.gzfile.- Returns:
Geometry kind, such as
"grid2d".- Return type:
- Raises:
PCSMSyntaxError – If the extension is not recognised or the kind cannot be found (for instance in a truncated file).
Examples
>>> import os, tempfile >>> import pcsf >>> from pcsf.core.text import peek_kind >>> model = pcsf.PCSFModel( ... geometry=pcsf.Grid2DGeometry(x=[0.0], z=[5.0]), ... resistivity=[[100.0]], ... ) >>> with tempfile.TemporaryDirectory() as tmp: ... path = pcsf.write(model, os.path.join(tmp, "m.pcsm")) ... peek_kind(path) 'grid2d'
- pycsamt.format.text.read_pcsf_or_pcsm(path)
Read a
PCSFModelfrom either encoding, by extension.The extension is the only signal used;
pcsf.read()detects the encoding from the content instead and is preferred in new code.- Parameters:
path (path-like) – A
.pcsf(binary HDF5),.pcsm(ASCII), or.pcsm.gz(gzip-compressed ASCII) file.- Returns:
The model read.
- Return type:
- Raises:
PCSMSyntaxError – If the extension is none of the three recognised forms.
See also
pcsf.readContent-based detection.
Examples
>>> from pcsf.core.text import read_pcsf_or_pcsm >>> read_pcsf_or_pcsm("model.txt") Traceback (most recent call last): ... pcsf.exceptions.PCSMSyntaxError: model.txt: unrecognised ...
- pycsamt.format.text.read_pcsm(path)
Read a
PCSFModelback from a.pcsm(ASCII) file.- Parameters:
path (path-like) – Source file.
- Returns:
Fully reconstructed and re-validated model.
- Return type:
- Raises:
PCSMSyntaxError – If the text does not follow the grammar.
VersionError – If
PCSM_VERSIONis malformed or of another major version (specification, section 5).GeometryError, EncodingError – If the reconstructed model fails
PCSFModel.validate().
See also
write_pcsmInverse operation.
pcsf.readRead either encoding.
Examples
See
write_pcsm().
- pycsamt.format.text.write_pcsm(model, path, *, log10_view=False)
Write a
PCSFModelto a.pcsm(ASCII) file.The same model
write_pcsf()would encode as HDF5, encoded instead as human-readable, hand-editable text. See the module docstring for the grammar.Every resistivity block carries its own encoding as an inline comment right next to the data, not only in a separate header line – e.g.
RESISTIVITY # linear ohm.m (canonical, ...)– so a reader never has to guess or scroll elsewhere to tell linear from log10. The canonicalRESISTIVITYfield is always linear ohm.m (specification, section 2): this cannot be changed by an option, since a reader/consumer relies on that invariant unconditionally, and reversing a10**log10(x)round-trip is not guaranteed bit-exact the way the rest of this format’srepr()-based float round-trip is.Pass
log10_view=Trueto additionally write aRESISTIVITY_LOG10block (and, for amultilinefile, one per line) alongside it – a convenience for reading resistivity in the log space many EM inversions (Occam2D among them) actually work in. This is a write-only, clearly-labelled derived view:read_pcsm()discards it, it never becomes part of the returnedPCSFModel, and it never influences the canonicalRESISTIVITYblock. (An Occam2D-sourced model already carries its own real log10 array asRESISTIVITY_NATIVEwheneverresistivity_nativeis set – that block is unaffected by this option and is written either way.)A path ending in
.gz(e.g.model.pcsm.gz) is written gzip-compressed. Large volumes (a nativegrid3dor a densemesh_unstructuredmesh) produce large plain-text files – the same trade-off ModEM’s own ASCII.rhoformat already accepts – and gzip substantially reduces that in practice (measured on the 590,400-cell ModEM volume of the accompanying paper: 19.7 MB plain, 3.9 MB gzipped, against 4.1 MB for the equivalent.pcsf; real inverted resistivity compresses far better than random test data). A gzipped file is no longer something to casually open in a text editor, so this stays opt-in rather than the default: PCSM’s purpose is hand-editability, which a compressed file gives up.- Parameters:
model (PCSFModel) – The model to serialize. Validated before anything is written.
path (path-like) – Destination file. Parent directories are created if missing. A
.gzsuffix writes a gzip-compressed file.log10_view (bool, default False) – Also write a
RESISTIVITY_LOG10convenience block (see above). Off by default so existing files/output size are unaffected unless explicitly requested.
- Returns:
The path written to.
- Return type:
- Raises:
TypeError – If model is not a
PCSFModel.GeometryError, EncodingError – If model fails
PCSFModel.validate().
See also
read_pcsmInverse operation.
pcsf.core.hdf5.write_pcsfBinary encoding.
Examples
>>> import os, tempfile >>> from pcsf import Grid2DGeometry, PCSFModel >>> from pcsf.core.text import read_pcsm, write_pcsm >>> model = PCSFModel( ... geometry=Grid2DGeometry(x=[0.0, 100.0], z=[10.0, 50.0]), ... resistivity=[[100.0, 120.0], [50.0, 60.0]], ... source_backend="occam2d", ... ) >>> with tempfile.TemporaryDirectory() as tmp: ... path = os.path.join(tmp, "m.pcsm.gz") ... _ = write_pcsm(model, path, log10_view=True) ... back = read_pcsm(path) >>> print(back.resistivity) [[100. 120.] [ 50. 60.]]