Functional Weave
Code in Rust

agri.fertiliser-application@1.0.0

README.md

2,014 bytes · view raw

# agri.fertiliser-application

How much of a fertiliser product to spread to meet nitrogen (N), phosphate
(P2O5) and potash (K2O) targets, given the product's analysis, and what that
rate actually supplies. A 20-10-10 compound for 100 kg N, 60 kg P2O5 and
60 kg K2O per hectare is 500 kg/ha, limited by nitrogen, and leaves P2O5 and
K2O each 10 kg/ha short.

**This is arithmetic, not agronomic advice.** It does not work out what a
crop needs (that is RB209 or a FACTS-qualified adviser's job, from soil
indices, previous crop and yield expectation), check NVZ limits or closed
periods, or choose the product.

## Limiting nutrient first

For each nutrient with a target above zero and a non-zero analysis, the
product needed is target / (analysis %) x 100 kg/ha. The rate chosen is the
**smallest** of those, so no nutrient goes on beyond its target; that nutrient
is `limitingNutrient`. The others show in `balance` as a shortfall (negative)
to make up with a straight product. A targeted nutrient the product does not
contain cannot limit the rate and is simply a shortfall. A tie goes to the
first in N, P, K order.

## Units and rounding

- Analysis is the declared percentage by weight of N, P2O5 and K2O (the oxide
  forms UK labels and RB209 use, not elemental P and K), each taken to 0.1%.
- Targets are whole kg/ha.
- The rate is target x 1000 / (analysis in tenths of a percent), exact, then
  rounded half up to 0.1 kg/ha: 1 kg N from a 16% product is 6.25, set to 6.3.
- `supplied` is computed from that rounded rate (what the spreader delivers),
  to 0.1 kg/ha, and `balance` is supplied minus target. The limiting
  nutrient's balance can therefore be a tenth or so either side of zero.
- `totalProductKg` is the rounded rate x the area (taken to 0.0001 ha),
  rounded half up to 0.1 kg. No allowance for headlands, overlaps or bag
  sizes.

## Limits

Each analysis figure 0 to 100%, together at most 100%; targets 0 to 1000
kg/ha, at least one above zero; area more than 0 and at most 10,000 ha.