# Recipe: sweeps, and staying inside the limits

A design sweep (altitudes, inclinations, masks, planes) is a program: one `program_run`, the loop next to the Engine. Two habits make it fast and keep it inside the limits.

## Screen short, confirm long

A Walker's coverage of a latitude band repeats with each orbit (about 95.6 min at 550 km, 109 min at 1,200 km). So:

1. **Screen** every candidate on one or two orbital periods, at a 60 s step.
2. **Confirm** the best two or three over 24 h before you report a winner.

The screen ranks; only the confirmation is a result. Say which numbers come from which.

Orbital period: T = 2 pi sqrt(a^3 / mu), with a = 6,378.137 km + altitude and mu = 398,600.4418 km^3/s^2.

## Size the work before you send it

Each function has a size limit; `describe {topic:"limits"}` gives today's values and your plan's per-run limits. The products that matter:

| Function | Work | Limit today |
|---|---|---|
| `propagate` | satellites x instants | 5,000,000 samples |
| `visibility` passes, pairs and the sky view (counts allow up to 50 million tests) | stations x satellites x instants | 2,000,000 samples |
| `grid_coverage`, `grid_revisit` | cells x satellites x instants | 3 billion elevations |
| `links` (ground) | sites x satellites x instants | 50,000,000 tests |
| A grid | cells | 200,000; a whole-Earth grid stops at H3 resolution 3 (41,162 cells) |
| One Engine call from a program, or `compute {keep: false}` | its time | 30 s: about 100 million coverage or visibility elevations; `links` calls past about 5 million tests slow down sharply |
| A program run | Engine CPU over all its calls, and the run's wall time | `engine_s` and `wall_s` in `describe {topic:"limits"}`: 300 s each on Pro |

Past a limit, `compute` answers what fits. A propagation too large is refused (`invalid_action`, `details: ["E_TOO_LARGE"]`) and nothing is written; its hints name the smallest step, the longest window and the most satellites that fit. A visibility past the pair limit still answers its counts and summary, and lists `passes` and `pairs` under `withheld`, each with `fits: {step, duration, stations?, satellites?}`. Pick one and call again: the step is never coarsened for you.

`compute {keep: false}` writes nothing. Each output answers its rows, columns, up to 100 rows as `sample` and an `export` link to its Arrow file: 10 minutes, one download at a time (per link, per account and across Dynamik), each within 2 minutes. Read a larger result from a kept computation with `data_read`. When the sample or the link is missing, `note` says why. The call holds its share of your daily CPU seconds before it runs: calls made together wait (`rate_limited`) rather than pass the day's limit.

Worked examples:
- 1,584 satellites over 24 h at 60 s: 1,441 instants, 2.28 million samples. Fits. 9,000 satellites over 24 h needs a step of 156 s or more (554 instants, 4.99 million samples), or shorter windows. In a program, compute the step from the layer's `rows` (`examples/best_sites.py`).
- Whole-Earth coverage at resolution 3 for 1,584 satellites over one orbit at 60 s (96 instants): 41,162 x 1,584 x 96 = 6.3 billion. Too big. Resolution 2 (5,882 cells) is 0.9 billion. That is under the function's limit, but too long inside a program. Each Engine call a program makes, and each `compute {keep: false}`, is stopped after 30 s. At about 4 million elevations a CPU second (measured on one core, radio refraction), a call there holds about 100 million elevations. Run a whole-Earth resolution 2 coverage as its own kept `compute`, one design per call. Inside a program, screen on fewer cells or fewer instants, for example a latitude band at resolution 2 over a few cycles. Then confirm the winner on a regional grid at a finer resolution.
- 200 terminals x 9,000 satellites x 721 instants (6 h at 30 s) is 1.3 billion tests: far more than one program run's Engine time holds (about 300 million `links` tests on Pro). Use a 60 s step over 2 h (218 million) in calls of about 5 million tests each (`terminals.md`).

The step decides what you can see: a 60 s step cannot claim a gap shorter than 60 s, nor continuous access between samples. State the step with every number.

## Emit what the person will read

Each output slot holds at most 200,000 rows (`describe {topic:"program"}`). Emit intervals and summaries, not one row per step:
- passes as `start, end, max_elevation` per station and satellite, not every sample;
- per cell `coverage_fraction` and `longest_gap`, not the time series;
- one row per candidate design with its score, plus a chart.

Read big results in pages with `data_read {limit, offset}` or as one `export` link, never row by row through the model.

## Sweep skeleton

```python
p = dk.params(altitudes=[500, 550, 600], inclinations=[50, 53, 56], planes=72, per_plane=22)
grid = dk.layer("grid")  # an H3 cells layer bound to data (data_put rows or CSV with a cell column), declared in inputs; a drawn H3 grid entity is refused
rows = []
for alt in p["altitudes"]:
    for inc in p["inclinations"]:
        fleet = dk.engine.walker(total=p["planes"] * p["per_plane"], planes=p["planes"], phasing=1,
                                 altitude=alt, inclination=inc)
        period_s = 2 * 3.141592653589793 * ((6378.137 + alt) ** 3 / 398600.4418) ** 0.5
        cov = dk.engine.grid_coverage(fleet, grid=grid, min_elevation=25,
                                      start=dk.clock.start, duration=2 * period_s, step=60)
        c = cov.cells.df()
        rows.append({"altitude": alt, "inclination": inc,
                     "min_coverage": c.coverage_fraction.min(), "mean_coverage": c.coverage_fraction.mean()})
table = __import__("pandas").DataFrame(rows)
dk.emit.table(table, slot="table", title="Screen: two orbits, 25 deg mask",
              units={"altitude": "km", "inclination": "deg", "min_coverage": "1", "mean_coverage": "1"})
dk.emit.chart(slot="table", x="inclination", y=["min_coverage"], kind="line")
dk.result({"best": table.sort_values("min_coverage").tail(3).to_dict("records")})
```

Full version: `examples/walker_screen.py`.
