Metadata-Version: 2.4
Name: yellow-belt-imaging
Version: 0.2.0
Summary: A terminal application for driving an automatic MeerKAT imaging pipeline with Direction Dependent Calibration
Author-email: Tim Molteno <tim@elec.ac.nz>
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: textual>=1.0
Requires-Dist: textual-fspicker>=1.0

# yellow_belt_imaging

A terminal interface for the **yellow-belt MeerKAT imaging pipeline**: one
command runs the whole reduction — RFI flagging and averaging, 1GC calibration,
target flagging, 2GC, and the direction-dependent (3GC) calibration with
DDFacet imaging and a killMS solve — for one measurement set.  Everything runs
in containers ([apptainer](https://apptainer.org) or singularity); this tool
checks the environment, collects and validates the configuration, builds the
pipeline image, runs the pipeline with its output streaming into a log pane,
and cancels cleanly.

Built with [textual](https://textual.textualize.io/).

## Install

```bash
pip install yellow-belt-imaging
yellow_belt_imaging        # or: python -m yellow_belt_imaging
```

> **Install it into its own venv** (`python -m venv`): this package needs
> `rich >= 14.2` (via textual), while stimela 2.1.4 pins `rich < 14` — the two
> cannot share an environment, and pip will happily break one while installing
> the other.

Works on Linux (amd64 and arm64) and macOS (to drive a remote pipeline host —
the pipeline itself runs under apptainer/singularity on Linux).

## Command line

`yellow_belt_imaging` with a subcommand runs the same flow headless — no
terminal UI, scriptable:

```bash
yellow_belt_imaging check    --ms-path /astro/obs.ms --out-path out  # validate + summary
yellow_belt_imaging build    --tag latest                            # build the SIF
yellow_belt_imaging analyze  --ms-path /astro/obs.ms --out-path out \
                             --save-params                           # classify the MS
yellow_belt_imaging run      --params .yellow_belt_imaging_param     # the pipeline
yellow_belt_imaging show     --ms-path ... --out-path ...            # the exact command; runs nothing
```

* every configuration field is a flag (`--help` lists them all), plus
  repeatable `--exclude-target NAME` and `--set NAME=VALUE` (the extra
  entrypoint variables);
* precedence: a flag wins over the remembered parameters
  (`.yellow_belt_imaging_param` in the working directory — the same file the
  TUI writes), which win over the defaults — the reverse of the TUI, where
  the form is the authority.  `run` and `analyze` persist the merged
  configuration only with `--save-params`, so a scripted run never picks up a
  value remembered from an interactive session;
* `run` carries the same gates as the TUI — the observation must have been
  analysed for the MS, the SIF must not be stale — streams the output, keeps
  `pipeline-stages.log` in `OUT_PATH` as it goes, and Ctrl-C cancels (SIGTERM,
  then SIGKILL);
* exit codes: 0 success, 1 a launched step failed, 2 the configuration or a
  gate refused the launch;
* `show` prints the resolved environment and the exact `apptainer run`
  command `run` would execute — the thing to inspect before scripting a run.

With no subcommand the TUI opens.

## What you need

* **apptainer** (preferred) or **singularity** — no root, no special flags;
  user namespaces must be enabled (the default on most Linux systems).  The
  interface checks for this at launch and tells you if neither is installed.
* **The raw measurement set** (a `*.ms` directory) — it may live in a
  **read-only** directory (an archive mount): the averaged MS and the flag
  versions are written to `MS_OUTPUT_DIR`, which defaults to `OUT_PATH`.
* Disk space: a few GB for the pipeline image and cab images, plus the
  products (tens of GB for a real run).
* RAM — the amount you declare as `RAM_GB` becomes the compute budget the
  pipeline plans against (see the resolution note below).

## The walkthrough

1. **Launch** — the status line shows the detected runtime and version, and
   which release the SIF (the pipeline image file) carries.  The action
   buttons stay disabled until apptainer or singularity is found.
2. **Fill the form** (left column) — every directory field has a **Browse…**
   picker (navigate into the directory, press Select):
   * `MS_PATH` — the raw measurement set;
   * `OUT_PATH` — where the run works and the products land (relative paths
     are fine);
   * `MS_OUTPUT_DIR` — where the averaged MS is written (empty = `OUT_PATH`,
     so the MS's own directory may stay read-only);
   * the output directories are **created at launch** if they do not exist
     (they are apptainer bind sources, so the container cannot start without
     them);
   * `BEAMS_DIR` / `BEAM_CUBE_DIR` — optional; see the beam note below;
   * `RAM_GB` — the compute budget (see the resolution note below); `NCPU`
     (empty = all cores); `BAND` (L / UHF);
   * the image tag (default `latest`) and the SIF path (default
     `~/meerkat-pipeline.sif`).
3. **Build SIF** (once) — builds
   `docker://ghcr.io/tmolteno/meerkat-pipeline:<tag>` into the SIF path with
   `--force` (a switch: on by default, so a stale SIF can be refreshed).  The
   first build downloads a multi-GB image — the status line counts the elapsed
   time and the output streams as it converts.
4. **Analyse observation** — classifies the measurement set with the
   pipeline's own planner and fills the **Observation** group from what it
   decides: the bandpass calibrator, the targets, the phase calibrators (and
   which one brackets each target), and which scans to keep.  It takes seconds
   (only the MS's index columns are read) and prints the per-scan decode, its
   warnings, and the geometry numbers (resolution, recommended image size,
   averaging limits).  It also names the flux model stage 1 will apply to
   the bandpass calibrator (J0408-6545's spectrum, Stevens-Reynolds 2016 for
   J1939-6342, Perley-Butler 2017 for 3C286 / 3C138), matched by position. A
   bandpass calibrator with no known model is shown in red: the run would
   stop before stage 0.  The fields open for you to sanity-check; the geometry
   settings in the same group (`IMAGE_FOV`, `OVERSAMPLING`, `MS_UVMAX`,
   `MS_CHANBIN`) are yours to choose.  `Check` validates the form before
   anything runs.
5. **Run pipeline** — refuses to start on an invalid configuration, on a stale
   SIF (its version is compared against the selected tag on the registry, so
   you cannot accidentally run old pipeline code), and when the observation
   has not been analysed for the selected MS — that is how a run would
   otherwise quietly use another observation's calibrators and scans.  While
   it runs, the output streams into the right-hand log pane, the status line
   ticks, and **Cancel** stops everything (SIGTERM, then SIGKILL).
   **Targets — untick the ones this run skips** sits beside the Run button:
   every target of `TARGET_LIST`, ticked by default.  Unticking one removes
   it from this run only (the tick list comes back all-ticked next launch;
   the `TARGET_LIST` field itself is untouched), and the per-target phase
   calibrator map stays aligned — `J0159.0-3413` skipped means its
   `TARGET_PCAL_MAP` entry goes with it.
6. **Stage progress log** — as each stage completes, a line is appended to
   `pipeline-stages.log` in `OUT_PATH` (timestamp, stage name, OK/FAIL,
   duration), with run start/end markers around them — the same information
   stimela's end-of-run table carries, written as the run progresses.
7. **Copy output** (`ctrl+o` or the button) — the whole log pane onto the
   terminal clipboard.  For mouse selection, hold **shift** and drag — most
   terminals pass that through to their own selection even while a TUI claims
   the mouse.
8. Products land in `$OUT_PATH/out_<ms-name>/` (images, models, the plan
   reports, stimela logs under `logs/`); the averaged MS is written next to
   your raw MS; the analysis is kept in `$OUT_PATH/analysis_<ms-name>/`.

## RAM_GB sets the resolution

The uv cut — and with it the image resolution — is whatever `RAM_GB` affords,
up to the longest baseline: on the bundled mergA_tim example, 112 GB gives
~3980 m / ~10.9″ (NPix ~5265); give it less and the resolution drops (with
severe shortage, time averaging is added) rather than the run failing;
`RAM_GB=0` plans against what is free at the moment each stage starts.  Set
`UVMAX_USE_PROFILE_CAP` to pin the run to the observation's profile resolution
instead (uv ≤ `MS_UVMAX`, 2500 m / ~17.4″ for mergA_tim).

## Beam cubes are generated on demand

Stage 4 reads a scalar FITS beam cube derived from a full-Jones source cube.
If you don't supply one (`BEAMS_DIR`, or a pre-derived
`primary_beam_mbeams_m012_*` in `BEAM_CUBE_DIR`), the pipeline generates what
is missing: it downloads the band's holography dataset (~420 MB, cached under
`beam-cubes/mbeams-cache/`, ~5.4 GB, reused across runs) and renders the
cubes.  Hand a pre-rendered source cube to the run via `BEAMS_DIR` to skip the
download.  Both directories are bound into the run automatically (read-only
for `BEAMS_DIR`) — no manual `-B` flags — and wherever several bind paths
share a filesystem root, that root is mounted once instead of once per tree.

## Your parameters are remembered

Every Run or Build launch saves the full configuration to
`.yellow_belt_imaging_param` in the working directory; the next launch
pre-populates the form from it (a missing or corrupt file falls back to the
defaults).  The file is gitignored in the source repository.

