11. Format Commands#
pycsamt format is the command group for the PCSF / PCSM
inversion-result format. PCSF (.pcsf, an HDF5 container) is
pyCSAMT’s backend-neutral representation of a finished resistivity
model; PCSM (.pcsm, or gzip-compressed .pcsm.gz) is its
lossless, hand-editable ASCII sibling.
Every classical solver (Occam2D, ModEM 3-D, MARE2DEM) and any AI/DL
inversion converts to PCSF. The group has one job: take whatever an
inversion produced — a solver working directory, a .npz of
predicted arrays, or an existing .pcsf / .pcsm — and turn it
into a single self-describing file that every downstream viewer
(pycsamt map, the desktop 3-D panel, the web map view, a plain
analysis script) can open without knowing which backend produced it.
11.1. Command Map#
Command |
Purpose |
Main output |
|---|---|---|
|
Convert any inversion result to |
One PCSF/PCSM file plus a written-file summary. |
|
Report what a file or folder is, and how |
A classification table (or JSON). |
|
Full summary of an existing |
Geometry, resistivity statistics, stations, topography, provenance, history. |
|
Structural + round-trip check of a |
Per-step pass/fail; non-zero exit on any failure. |
11.2. Source Detection#
convert and detect share one detector
(pycsamt.format.detect_source()). It never loads heavy array
data — it looks at file names, extensions, and a handful of solver
signatures, then reports a category:
|
Recognised from |
Converted through |
|---|---|---|
|
a |
direct transcode to |
|
a |
direct transcode to |
|
an Occam2D / ModEM / MARE2DEM working directory (or a signature file inside one) |
|
|
a |
|
|
anything else |
nothing — |
Solver directory signatures:
Occam2D — any
*.iterfile, or names such asOccam2DMesh,Occam2DModel,OccamStartup,OccamDataFile.dat.ModEM —
Modular_NLCG.log,ModEM.inv, or a*.rhomodel file next to a*.datdata file.MARE2DEM — a
*.polyPSLG next to a*.resistivityfile, ormare2dem.settings.
When a directory carries more than one signature, the detector reports
medium confidence and picks one; pass --solver to force the
choice. Pointing convert at a single file inside a run (for
example run/ITER17.iter or demo.poly) resolves to that file’s
folder automatically.
11.2.1. AI array bundles#
For a .npz, the detector matches keys case-insensitively:
resistivity —
resistivity,rho,model,prediction,pred,output… (linear ohm-m). Alog10_rho/ln_rhokey is also recognised and sets the encoding automatically.grid2d — coordinate arrays
xandz(plus optionalx_nodes/z_nodes), or a 2-D resistivity array.grid3d —
x,y,z, or a 3-D resistivity array.mesh —
nodes(orvertices/points) together withconnectivity(ortriangles/elements), plus optionalregion_ids.optional
uncertaintyandsensitivityarrays are carried through when present.
A bare .npy is accepted as linear resistivity on a unit-spaced
grid (low confidence) — prefer an .npz with real coordinates.
11.3. Detect#
Usage:
pycsamt format detect PATH [--solver {occam2d,modem,mare2dem}] [-f {text,json}]
detect performs no conversion and writes nothing. It exits non-zero
when the source is not convertible, which makes it a cheap pre-flight
check in scripts.
pycsamt format detect data/occam2D
pycsamt format detect data/mare2dem/demo_mt_inversion
pycsamt format detect unet_prediction.npz -f json
11.4. Convert#
Usage:
pycsamt format convert SOURCE [TARGET] [OPTIONS]
If TARGET is omitted, the output name is derived from the source
and written into --output-dir (default: the current directory).
The output format comes from TARGET’s extension, or from --to
(default: pcsf).
Option |
Default |
Meaning |
|---|---|---|
|
|
Output format when |
|
auto |
Force the source backend instead of fingerprinting it. |
|
final |
Occam2D iteration index to convert. |
|
none |
Topography source ( |
|
none |
Projection for |
|
auto |
Encoding of the AI resistivity array. Overrides key-name detection. |
|
auto |
MARE2DEM |
|
none |
Real-world grid origin for AI |
|
none |
Profile azimuth ( |
|
CLI defaults |
Values written into the PCSF attributes. |
|
false |
For |
|
false |
Detect and print the plan without writing anything. |
|
false |
Replace an existing output file. |
|
|
Console output format for the result summary. |
Canonical resistivity in a PCSF file is always linear ohm-m,
regardless of the source. A backend’s native encoding
(log10 for Occam2D, ln for ModEM, a --encoding for an AI
array) is preserved separately as provenance.
11.4.1. Examples#
Occam2D working directory to PCSF:
pycsamt format convert data/occam2D occam.pcsf
ModEM 3-D directory to a compressed PCSM, with topography from the EDI survey:
pycsamt format convert data/modem/run01/ run01.pcsm.gz \
--topo data/AMT/WILLY_DATA --epsg 32648
MARE2DEM run (the mesh is rebuilt from the run’s .poly PSLG — this
needs the triangle package):
pycsamt format convert data/mare2dem/demo_mt_inversion mare.pcsf
An AI/DL prediction saved as resistivity / x / z arrays:
pycsamt format convert unet_prediction.npz unet.pcsf --encoding log10
Transcode between the two encodings (lossless both ways):
pycsamt format convert occam.pcsf occam.pcsm --log10-view
pycsamt format convert occam.pcsm roundtrip.pcsf
Preview without writing:
pycsamt format convert data/mare2dem/demo_mt_inversion --dry-run
11.5. Info#
Usage:
pycsamt format info FILE [-f {text,json}]
info loads the file and prints geometry (axis extents, mesh sizes,
origin/rotation), resistivity statistics for every stored array
(resistivity, resistivity_native, resistivity_by_region,
resistivity_by_node, uncertainty, sensitivity), the station
table, topography kind, inversion history keys, and — for AI results —
the embedded model_provenance block.
pycsamt format info model.pcsf
pycsamt format info model.pcsm.gz -f json
11.6. Validate#
Usage:
pycsamt format validate FILE [--no-roundtrip] [-f {text,json}]
validate runs four checks and exits non-zero if any fail:
header — the geometry kind can be peeked;
load — the file parses into a
PCSFModel;schema —
PCSFModel.validate()passes;roundtrip — re-writing the model to a temp file of the same encoding and reading it back reproduces the resistivity array (skip with
--no-roundtrip).
pycsamt format validate model.pcsf
pycsamt format validate model.pcsm --no-roundtrip -f json
11.7. Common Failures#
Nothing to convert — … looks like: …The detector returned
unknown. Runpycsamt format detecton the same path to see why, and pass--solverif it is a non-standard solver directory.No .poly PSLG foundA MARE2DEM directory has no polygon mesh file. Pass
--poly PATH.MARE2DEM → PCSF needs the 'triangle' packageInstall it with
pip install triangle. It rebuilds the run’s triangulation from the.polyPSLG.… exists — pass --overwrite to replace itThe target file is already there.
… is already PCSF; give a different TARGET or --to the other formatconvertwill not transcode a file onto itself.
11.8. Python Equivalents#
The CLI is a thin layer over pycsamt.format:
from pycsamt.format import detect_source, write_pcsf
from pycsamt.format.adapters import occam2d_to_pcsf
from pycsamt.models.occam2d.results import InversionResult
sk = detect_source("data/occam2D") # -> SourceKind(category="solver", ...)
result = InversionResult(workdir=sk.path)
model = occam2d_to_pcsf(result, created_by="me")
write_pcsf(model, "occam.pcsf")
For an AI result, skip the solver classes entirely:
import numpy as np
from pycsamt.format import write_pcsm
from pycsamt.format.adapters.generic import grid2d_to_pcsf
d = np.load("unet_prediction.npz")
model = grid2d_to_pcsf(d["resistivity"], d["x"], d["z"], encoding="log10")
write_pcsm(model, "unet.pcsm")