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.text

Implementation.

pycsamt.format.text.pcsf_to_pcsm(pcsf_path, pcsm_path, *, log10_view=False)

Convert a .pcsf (HDF5) file to .pcsm text.

Parameters:
  • pcsf_path (path-like) – Source file.

  • pcsm_path (path-like) – Destination; a .gz suffix compresses it.

  • log10_view (bool, default False) – Passed to write_pcsm().

Returns:

The path written to.

Return type:

pathlib.Path

See also

pcsm_to_pcsf

Inverse conversion.

pcsf.convert

Conversion 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 .pcsm text 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:

pathlib.Path

See also

pcsf_to_pcsm

Inverse 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 .pcsf file or the GEOMETRY_KIND header of a .pcsm file is read, so that a file picker can label many files cheaply.

Parameters:

path (path-like) – A .pcsf, .pcsm or .pcsm.gz file.

Returns:

Geometry kind, such as "grid2d".

Return type:

str

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 PCSFModel from 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:

PCSFModel

Raises:

PCSMSyntaxError – If the extension is none of the three recognised forms.

See also

pcsf.read

Content-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 PCSFModel back from a .pcsm (ASCII) file.

Parameters:

path (path-like) – Source file.

Returns:

Fully reconstructed and re-validated model.

Return type:

PCSFModel

Raises:
  • PCSMSyntaxError – If the text does not follow the grammar.

  • VersionError – If PCSM_VERSION is malformed or of another major version (specification, section 5).

  • GeometryError, EncodingError – If the reconstructed model fails PCSFModel.validate().

See also

write_pcsm

Inverse operation.

pcsf.read

Read either encoding.

Examples

See write_pcsm().

pycsamt.format.text.write_pcsm(model, path, *, log10_view=False)

Write a PCSFModel to 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 canonical RESISTIVITY field 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 a 10**log10(x) round-trip is not guaranteed bit-exact the way the rest of this format’s repr()-based float round-trip is.

Pass log10_view=True to additionally write a RESISTIVITY_LOG10 block (and, for a multiline file, 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 returned PCSFModel, and it never influences the canonical RESISTIVITY block. (An Occam2D-sourced model already carries its own real log10 array as RESISTIVITY_NATIVE whenever resistivity_native is 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 native grid3d or a dense mesh_unstructured mesh) produce large plain-text files – the same trade-off ModEM’s own ASCII .rho format 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 .gz suffix writes a gzip-compressed file.

  • log10_view (bool, default False) – Also write a RESISTIVITY_LOG10 convenience block (see above). Off by default so existing files/output size are unaffected unless explicitly requested.

Returns:

The path written to.

Return type:

pathlib.Path

Raises:
  • TypeError – If model is not a PCSFModel.

  • GeometryError, EncodingError – If model fails PCSFModel.validate().

See also

read_pcsm

Inverse operation.

pcsf.core.hdf5.write_pcsf

Binary 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.]]