# Recipe: your own algorithm

Dynamik keeps the trusted functions (propagation, visibility, coverage, revisit, links, paths). You write everything around them. Choose where your code runs.

| You need | Use | Why |
|---|---|---|
| One function Dynamik has, once or twice | `compute` | Saved, recomputed when inputs change (coverage and revisit when run again), drawn with its preset, `summary` in the answer |
| The same function in a loop, a sweep, or more than 3 dependent calls | `program_run` | One call; the loop runs next to the Engine with no model turn per step; saved and re-runnable |
| Statistics, comparisons, charts, a report on results | `data_read` (`export`), then your own sandbox | You are better at downstream analysis; do not ask Dynamik for it |
| An algorithm on data with no Dynamik function inside | Your own sandbox, then `data_put` the result | It is drawn and kept, marked custom |
| Satellite positions | Never your own code | `compute` uses reference-checked propagation: SGP4 for GP sets, J2 for designs, chosen for you |

Programs are not open to every account yet. On Starter, `program_run` answers `plan_required` (Pro); while programs are switched off or internal-only it answers `unavailable`. Say so once, then do the loop with `compute` calls (`keep:false` for a screen).

## Program rules

- Python 3.12 with numpy, scipy, pandas, pyarrow, shapely, h3, pyproj and networkx. No network; files only in the run's private scratch (the working directory, also `TMPDIR`; there is no `/tmp`), at most 64 MB a file and gone after the run; no writes to the project from inside.
- Inputs are declared in the call: `inputs:{"fleet":"starlink","sites":"gateways"}`. Inside, `dk.layer("fleet")`. Undeclared reads are not possible: a program's identity is known before it runs.
- Engine calls are `dk.engine.<fn>(...)` with the same names and units as `compute`. Positions always come from the Engine. Propagate a declared satellite input yourself and name the model: `dk.engine.propagate(fleet, start=dk.clock.start, duration=..., step=..., model="j2")` for a design, `model="sgp4"` for a GP set. A function given the layer itself propagates it with SGP4, which refuses a design. A Walker made inside the program (`dk.engine.walker`) propagates with J2 on its own.
- `dk.params(...)` defaults become the form a person edits. `dk.clock` is the project clock captured when you last sent `source`, `params` or `refresh`, never wall time; a plain rerun `program_run {project, program}` keeps it, and a params change takes the project clock as it is now. Random numbers are seeded.
- Outputs: `dk.emit.points | features | cells | movers | links | table` and `dk.emit.chart`. An Engine result emitted as it is, from inputs that are themselves checked, stays "Dynamik, checked" (`assurance: "supported"`); anything built in code, or computed from custom data such as an uploaded CSV, is "custom". The window shows which.
- `dk.result({...})` returns a small JSON to you, inside `untrusted`. `print` goes to the run log. Put what you will report in `dk.result`, not in `print`.

Signatures and per-plan limits: `describe {topic:"program"}`.

## Workflow

1. Bring the data first (`data_put`). A program cannot fetch anything.
2. `program_run {project, name, inputs, source}` saves it and runs it. It waits up to 25 s; a longer run continues and answers `state:"running"`; read it with `project_open {project, run}`.
3. Read `inputs` (the release each was read at), `outputs` (rows and `assurance` per slot) and `run` (cpu, Engine calls). Then render one result.
4. On `program_failed`: `hints` names the line (`/source:<line>`) when there is one, and `details.kind` says what failed (`exception`, or a limit such as `cpu_time` or `memory`). Fix with `program_run {project, program, source}` (same program, new source), or `undo {project}`.
5. Change only the parameters: `program_run {project, program, params:{...}}`. No need to resend the source. The run takes the project clock as it is now.

`check:true` is optional and costs a call: it only parses (syntax, imports, emit slots, parameter defaults, limits) and runs nothing. A run without it refuses bad source the same way, and no check catches a runtime error, so a short program is usually just run.

## Example

`examples/best_sites.py`: the highest elevation mask at which each candidate site keeps at least 2 satellites in view 99% of a day. A sweep of 9 masks over one propagation: 10 Engine calls, one `program_run`.
