Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ All notable changes to the **AIMBAT** project will be documented in this file.
- Add asciinema
- Switch to zensical
- Re-arange api reference
- Restructure usage section into workflow-based pages and TUI improvements

### 📦 Miscellaneous

Expand Down Expand Up @@ -123,6 +124,7 @@ All notable changes to the **AIMBAT** project will be documented in this file.
- Add JSON datasource
- Add TUI and supporting changes
- Implement interactive shell and major documentation update for v2
- Store ICCS/MCCC quality metrics in database

### 🧪 Testing

Expand Down
40 changes: 23 additions & 17 deletions docs/usage/event-selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ one event at a time.
aimbat event list
```

The table shows each event's ID, time, location, and whether it is
currently the default. IDs are displayed in their shortest unambiguous
form — use any unique prefix when passing an ID to other commands.
The table shows each event's ID, time, and location. IDs are displayed in
their shortest unambiguous form — use any unique prefix when passing an
ID to other commands.

=== "TUI"

Expand All @@ -26,36 +26,42 @@ one event at a time.

---

## Setting the default event (CLI / Shell)
## Selecting an Event for CLI / Shell

The CLI and shell operate on a **default event** — a single event stored in
the database that all commands target unless overridden with `--event`. Set it
after import:
Most processing commands (like `aimbat align iccs` or `aimbat snapshot create`)
operate on a single event. You can specify the target event in two ways:

### 1. The `--event-id` flag (or `--event`)

Pass the ID directly to any command. You can use the full UUID or any unique
prefix:

```bash
aimbat event default <EVENT_ID>
aimbat align iccs --event-id 6a4a
```

From that point on, commands like `aimbat plot seismograms` or
`aimbat align iccs` automatically target this event without needing an
explicit ID.
### 2. The `DEFAULT_EVENT_ID` environment variable

To target a different event for a single command without changing the default:
If you are working on the same event for multiple commands, you can set the
`DEFAULT_EVENT_ID` environment variable in your shell. This tells AIMBAT to
use that event whenever the `--event-id` flag is omitted:

```bash
aimbat align iccs --event <EVENT_ID>
export DEFAULT_EVENT_ID=6a4a
aimbat align iccs
aimbat snapshot create "post-ICCS"
```

The default event is marked in `aimbat event list` and is also shown in the
shell prompt.
The shell prompt also reflects this ID when set. To clear it, simply unset the
variable: `unset DEFAULT_EVENT_ID`.

---

## Selecting an event for processing (TUI / GUI)

The TUI and GUI maintain their own event selection independently of the
database default — changing it here does not affect what the CLI uses, and
vice versa.
CLI / shell context — changing it here does not affect what the CLI uses,
and vice versa.

=== "TUI"

Expand Down
36 changes: 24 additions & 12 deletions docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ runs, prints its result, and exits. It is the natural choice for scripting,
batch jobs, and any task where you already know what you want to do.

Every command accepts `--help` for a full option listing. Most processing
commands operate on the [default event](index.md#default-event) unless you
pass an explicit `--event` flag:
commands require an event to operate on. You can pass an explicit `--event`
flag:

```bash
aimbat align iccs --event 6a4a
```

Alternatively, you can set the `DEFAULT_EVENT_ID` environment variable to
avoid passing the flag every time:

```bash
export DEFAULT_EVENT_ID=6a4a
aimbat align iccs
```

IDs can be supplied as the full UUID or any unique prefix.

All commands exit with a non-zero status on error, making them safe to chain
Expand Down Expand Up @@ -60,9 +68,9 @@ aimbat> event list
aimbat> align iccs
```

The shell maintains a local **event context** that is independent of the
database default event and is never written to the database. Switch it at any
time:
The shell maintains a local **event context** that can be pre-selected on
launch or switched at any time. When an event is selected, the shell
automatically injects it into all relevant commands:

```
aimbat [6a4a]> event switch <ID>
Expand Down Expand Up @@ -201,18 +209,22 @@ with an environment variable:
export AIMBAT_PROJECT=/path/to/my/project.db
```

### Default event
### Event selection

Projects can contain multiple seismic events. Most commands operate on a single
event at a time. You can choose the target event by passing the `--event-id`
(or `--event`) flag to any command.

Projects can contain multiple seismic events. The **default event** is a
database-level setting used by the CLI and, on startup, by the shell. Set it
with:
For convenience, you can also set the `DEFAULT_EVENT_ID` environment variable:

```bash
aimbat event default <EVENT_ID>
export DEFAULT_EVENT_ID=6a4a
```

The TUI and GUI maintain their own **event selection** independently of the
database default and never change it.
When this variable is set, the CLI and shell use it as the default target
whenever an explicit ID is omitted. The shell prompt also reflects this ID.
The TUI and GUI maintain their own event selection independently and never
change it.

### The ICCS instance

Expand Down
7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies = [
"textual-fspicker>=1.0.0",
"prompt-toolkit>=3.0.52",
"mplcursors>=0.7",
"typing-extensions>=4.15.0",
]

[project.urls]
Expand All @@ -49,11 +50,7 @@ test = [
"pytest-sugar>=1.1.1",
"ruff>=0.13.0",
]
dev = [
"ruff>=0.15.5",
"textual-dev>=1.8.0",
"vulture>=2.15",
]
dev = ["ruff>=0.15.5", "textual-dev>=1.8.0", "vulture>=2.15"]
docs = [
"git-cliff>=2.12.0",
"griffe-fieldz>=0.5.0",
Expand Down
25 changes: 25 additions & 0 deletions src/aimbat/_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .align import app as align
from .data import app as data
from .event import app as event
from .pick import app as pick
from .plot import app as plot
from .project import app as project
from .seismogram import app as seismogram
from .shell import app as shell
from .snapshot import app as snapshot
from .station import app as station
from .utils import app as utils

__all__ = [
"utils",
"align",
"data",
"event",
"pick",
"plot",
"project",
"seismogram",
"shell",
"snapshot",
"station",
]
72 changes: 30 additions & 42 deletions src/aimbat/_cli/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
starting point instead, with the resulting pick stored in `t1`.
"""

from dataclasses import dataclass
from typing import Annotated
from uuid import UUID

from cyclopts import App, Parameter

from .common import GlobalParameters, _DebugTrait, _EventContextTrait, simple_exception
from .common import (
DebugParameter,
event_parameter,
simple_exception,
)

__all__ = ["cli_iccs_run", "cli_mccc_run"]

app = App(name="align", help=__doc__, help_format="markdown")


@Parameter(name="*")
@dataclass
class _IccsParametersTrait:
autoflip: Annotated[
bool,
Parameter(
name="autoflip",
help="Whether to automatically flip seismograms (multiply data"
" by -1) when the cross-correlation is negative.",
),
] = False
@app.command(name="iccs")
@simple_exception
def cli_iccs_run(
event_id: Annotated[UUID, event_parameter()],
*,
autoselect: Annotated[
bool,
Parameter(
Expand All @@ -36,60 +34,50 @@ class _IccsParametersTrait:
" cross-correlation with the stack falls below `min_cc`, and"
" re-select them if the cross-correlation later exceeds `min_cc`.",
),
] = False


@dataclass
class _IccsParameters(_IccsParametersTrait, _EventContextTrait, _DebugTrait):
pass


@app.command(name="iccs")
@simple_exception
def cli_iccs_run(
*,
iccs_parameters: _IccsParameters = _IccsParameters(),
] = False,
autoflip: Annotated[
bool,
Parameter(
name="autoflip",
help="Whether to automatically flip seismograms (multiply data"
" by -1) when the cross-correlation is negative.",
),
] = False,
_: DebugParameter = DebugParameter(),
) -> None:
"""Run the ICCS algorithm to align seismograms for the default event.
"""Run the ICCS algorithm to align seismograms for an event.

Iteratively cross-correlates seismograms against a running stack to refine
arrival time picks (`t1`). If `t1` is not yet set, `t0` is used as the
starting point.

Args:
autoflip: Whether to automatically flip seismograms (multiply data by -1)
when the cross-correlation is negative.
autoselect: Whether to automatically de-select seismograms whose
cross-correlation with the stack falls below `min_cc`.
"""
from sqlmodel import Session

from aimbat.core import create_iccs_instance, resolve_event, run_iccs
from aimbat.db import engine

with Session(engine) as session:
event = resolve_event(session, iccs_parameters.event_id)
event = resolve_event(session, event_id)
iccs = create_iccs_instance(session, event).iccs
run_iccs(
session, event, iccs, iccs_parameters.autoflip, iccs_parameters.autoselect
)
run_iccs(session, event, iccs, autoflip, autoselect)


@app.command(name="mccc")
@simple_exception
def cli_mccc_run(
event_id: Annotated[UUID, event_parameter()],
*,
all_seismograms: Annotated[
bool,
Parameter(
name="all",
help="Include all seismograms in MCCC processing, not just the "
"currently selected ones",
help="Include all seismograms of an event in MCCC processing, "
"not just the currently selected ones.",
),
] = False,
global_parameters: GlobalParameters = GlobalParameters(),
_: DebugParameter = DebugParameter(),
) -> None:
"""Run the MCCC algorithm to refine arrival time picks for the default event.
"""Run the MCCC algorithm to refine arrival time picks for an event.

Multi-channel cross-correlation simultaneously determines the optimal time
shifts for all seismograms. Results are stored in `t1`.
Expand All @@ -100,7 +88,7 @@ def cli_mccc_run(
from aimbat.db import engine

with Session(engine) as session:
event = resolve_event(session, global_parameters.event_id)
event = resolve_event(session, event_id)
iccs = create_iccs_instance(session, event).iccs
run_mccc(session, event, iccs, all_seismograms)

Expand Down
Loading
Loading