SCRIPT RUN ORDER — squealer / jrs-code
======================================

RUN EVERYTHING
  ./run-all.sh
    - Runs run-all.R, then scripts/reproduce_gp_samples.py, then the CMB
      example in cmb/.
    - run-all.R sources the R scripts below and writes every R fit and figure.
      It skips the one-time cmdstanr/CmdStan install in squealer_golf.R.
    - The Gaussian-process script uses the uv environment in this directory.
      It writes squealer_gp_samples0.png, _1.png, and _2.png, and prints the
      hyperparameter table.
    - The CMB section builds the Planck likelihood the first time (see below),
      then writes the spectrum figure, the three paper fits, and the
      multipanel figure. Each fit takes a few minutes.

STEP 0 (one-time setup, only if cmdstanr/CmdStan aren't installed yet)
  squealer_golf.R
    - When run by itself, its first lines install cmdstanr and CmdStan,
      which dilution.R and dilution-plots.R also need. run-all.R skips
      those lines.

STEP 1  fit-dilution-model.R
    - Fits models/Bayesian_contamination_model.stan
    - WRITES: data/model_fit.rds

STEP 2  fit-dilution-squealer-model.R
    - Fits models/Bayesian_contamination_model_squealer.stan with pseudo-data
    - WRITES: data/model_fit_squealer.rds

STEP 3  Figure3.R
    - READS: data/model_fit.rds (from Step 1)
    - WRITES: Figure3.png

STEP 4  dilution-fig-9.R
    - READS: data/model_fit.rds (Step 1) AND data/model_fit_squealer.rds (Step 2)
    - WRITES: Figure3_squealer.png, Scatterplot_squealer.pdf

Steps 3 and 4 can be run in either order, as long as Steps 1 and 2 are done.

INDEPENDENT (no dependencies on other scripts; run any time)
  dilution.R          - cmdstanr fit of models/contamination_model.stan
  dilution-plots.R    - simulated-data squealer dashboards (dilution_1-4.pdf)
  squealer_golf.R     - golf example (squealer_golf_0-3.pdf)


CMB (cmb/; included in ./run-all.sh)
  One-time, and only if cmb/planck_2018 is not already built. Needs a C
  compiler, gfortran, and CFITSIO (macOS: brew install gcc cfitsio). The
  Python packages are already in the uv environment here. run-all.sh runs
  this itself when the build is missing.
    ./cmb/setup_planck.sh
      Downloads and builds the Planck 2018 likelihood into cmb/planck_2018/
      (about 190 MB). Uses .venv/bin/python when PYTHON is unset.
      Leave cmb/planck_2018/ out of the submitted zip: it is ESA code and
      data, built for the local machine.
      Should print:
        plik_lite TT  logL(best fit) = -103.29
        Commander TT  logL(best fit) = -11.84

  run-all.sh then runs these, still from this directory. Run any one of them
  on its own the same way.
    uv run python cmb/cmb_spectrum.py
      WRITES: cmb/figures/cmb_spectrum.pdf and .png
    uv run python cmb/cmb_squealer.py --ell 22  --dl 600
    uv run python cmb/cmb_squealer.py --ell 464 --dl 2225
    uv run python cmb/cmb_squealer.py --ell 220 --dl 6000
      Each fit takes a few minutes.
      WRITES: cmb/figures/cmb_squealer_ell{ℓ}_Dl{D}.pdf and .png
               cmb/results/cmb_squealer_ell{ℓ}_Dl{D}.json
    uv run python cmb/cmb_squealer_multipanel.py --from-results
      Four-panel figure from those three results.
      WRITES: cmb/figures/cmb_squealer_multipanel.pdf and .png

  Without --from-results, cmb_squealer_multipanel.py draws the paper figure
  from values stored in the script (about 10 s) and does not need the three
  fits. cmb/README.md also describes the --test check.


WORKING DIRECTORY
-----------------
The R scripts call setwd("~/squealer/jrs-code") and then use paths relative
to that directory (data/, models/, and output files in the project root).
They can be run from any working directory.

The CMB scripts live in cmb/ and write cmb/figures and cmb/results next to
themselves. Run them with uv run from this directory, as in the CMB section.