# Mtpy

> |

- **Type:** Skill
- **Install:** `agentstack add skill-steadfastasart-geoscience-skills-mtpy`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [SteadfastAsArt](https://agentstack.voostack.com/s/steadfastasart)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [SteadfastAsArt](https://github.com/SteadfastAsArt)
- **Source:** https://github.com/SteadfastAsArt/geoscience-skills/tree/main/mtpy

## Install

```sh
agentstack add skill-steadfastasart-geoscience-skills-mtpy
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# mtpy - Magnetotelluric Analysis

## Quick Reference

```python
from mtpy import MT, MTCollection

# Read single station
mt = MT('station001.edi')

# Access data
Z = mt.Z                         # Complex impedance tensor
freq = mt.frequency              # Frequency array
rho_xy = mt.apparent_resistivity[:, 0, 1]  # Apparent resistivity

# Station info
print(mt.station, mt.latitude, mt.longitude)

# Write EDI
mt.write_edi('output.edi')
```

## Key Classes

| Class | Purpose |
|-------|---------|
| `MT` | Single station MT data container |
| `MTCollection` | Multiple stations management |
| `PlotMTResponse` | Plot impedance, resistivity, phase |
| `PlotPhaseTensor` | Phase tensor ellipse visualization |
| `PlotPseudoSection` | Profile pseudosection display |
| `PlotStrike` | Strike direction analysis |

## Essential Operations

### Load and Inspect EDI
```python
from mtpy import MT

mt = MT('station001.edi')
print(f"Station: {mt.station}")
print(f"Location: ({mt.latitude}, {mt.longitude})")
print(f"Frequencies: {len(mt.frequency)} points")
print(f"Period range: {1/mt.frequency.max():.2f} - {1/mt.frequency.min():.0f} s")
```

### Load Multiple Stations
```python
from mtpy import MTCollection

mc = MTCollection()
mc.from_edis('survey_data/*.edi')
print(f"Loaded {len(mc)} stations")

for station in mc:
    print(f"  {station.station}: ({station.latitude:.4f}, {station.longitude:.4f})")
```

### Plot MT Response
```python
from mtpy import MT
from mtpy.imaging import PlotMTResponse

mt = MT('station001.edi')
plot = PlotMTResponse(mt)
plot.plot()  # Apparent resistivity and phase
```

### Phase Tensor Analysis
```python
from mtpy import MT
from mtpy.imaging import PlotPhaseTensor

mt = MT('station001.edi')

# Get phase tensor parameters
phi_min = mt.phase_tensor.phimin
phi_max = mt.phase_tensor.phimax
skew = mt.phase_tensor.skew       # 3D indicator

# Plot
pt = PlotPhaseTensor(mt)
pt.plot()
```

### Rotate Impedance Tensor
```python
from mtpy import MT

mt = MT('station001.edi')
mt_rotated = mt.rotate(30)        # 30 degrees clockwise
mt.rotate_to_strike()             # Auto-rotate to geoelectric strike
```

### Create Pseudosection
```python
from mtpy import MTCollection
from mtpy.imaging import PlotPseudoSection

mc = MTCollection()
mc.from_edis('profile/*.edi')

ps = PlotPseudoSection(mc)
ps.plot(plot_type='apparent_resistivity', mode='te')  # or 'tm', 'det'
```

### Export Data
```python
from mtpy import MT
import pandas as pd

mt = MT('station001.edi')

# Export to CSV
df = pd.DataFrame({
    'frequency': mt.frequency,
    'rho_xy': mt.apparent_resistivity[:, 0, 1],
    'rho_yx': mt.apparent_resistivity[:, 1, 0],
    'phase_xy': mt.phase[:, 0, 1],
    'phase_yx': mt.phase[:, 1, 0]
})
df.to_csv('mt_data.csv', index=False)

# Export for ModEM
mt.write_modem('station001.dat')
```

## Impedance Tensor Components

| Component | Description | Mode |
|-----------|-------------|------|
| Zxx | Ex/Bx response | Diagonal (usually small) |
| Zxy | Ex/By response | TE mode |
| Zyx | Ey/Bx response | TM mode |
| Zyy | Ey/By response | Diagonal (usually small) |

## Phase Tensor Parameters

| Parameter | Description | Interpretation |
|-----------|-------------|----------------|
| phi_min | Minimum phase | Relates to resistivity gradient |
| phi_max | Maximum phase | Relates to resistivity gradient |
| skew | Skew angle | >5 suggests 3D structure |
| ellipticity | (phi_max-phi_min)/(phi_max+phi_min) | 2D/3D indicator |

## When to Use vs Alternatives

| Tool | Best For | Limitations |
|------|----------|-------------|
| **mtpy** | Full MT workflow in Python, EDI I/O, visualization, modelling prep | Complex API, evolving between v1 and v2 |
| **EMTF** | USGS time-series to impedance processing | Fortran-based, processing only |
| **WinGLink** | Commercial integrated MT processing and inversion | Expensive commercial license |

**Use mtpy when** you need end-to-end MT analysis in Python: reading EDI files,
QC, phase tensor analysis, pseudosections, and preparing data for ModEM or other
inversion codes.

**Consider alternatives when** you need time-series to impedance processing from raw
field data (use EMTF), or a fully integrated commercial inversion package with GUI
(use WinGLink).

## Common Workflows

### Load, QC, and analyze MT station data
- [ ] Load EDI file(s) with `MT()` or `MTCollection()`
- [ ] Inspect station metadata (location, frequency range)
- [ ] Plot apparent resistivity and phase with `PlotMTResponse`
- [ ] Check phase tensor parameters for dimensionality (skew > 5 = 3D)
- [ ] Identify and mask noisy data points using error thresholds
- [ ] Rotate impedance tensor to geoelectric strike if needed
- [ ] Create pseudosection for profile data
- [ ] Export cleaned data for inversion (ModEM format)

## Common Issues

| Issue | Solution |
|-------|----------|
| No tipper data | Check `mt.has_tipper` before accessing |
| Bad data points | Use `mt.Z_err / np.abs(mt.Z) > threshold` to mask |
| Static shift | Apply correction before interpretation |
| Wrong rotation | Verify coordinate system (N vs E convention) |

## References

- **[EDI Format](references/edi_format.md)** - EDI file structure and sections
- **[Plotting Options](references/plotting.md)** - Visualization parameters and styles

## Scripts

- **[scripts/mt_analysis.py](scripts/mt_analysis.py)** - MT data analysis and QC

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [SteadfastAsArt](https://github.com/SteadfastAsArt)
- **Source:** [SteadfastAsArt/geoscience-skills](https://github.com/SteadfastAsArt/geoscience-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-steadfastasart-geoscience-skills-mtpy
- Seller: https://agentstack.voostack.com/s/steadfastasart
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
