feat: nutrient_cost tool — cheapest fertilizer per lb of N/P/K (#1)
Co-authored-by: claude <claude@jpaul.io> Co-committed-by: claude <claude@jpaul.io>
This commit was merged in pull request #1.
This commit is contained in:
@@ -22,6 +22,12 @@ def _ton(cents: Optional[int]) -> str:
|
||||
return f"${cents / 100:.2f}"
|
||||
|
||||
|
||||
def _per_lb(cents: Optional[int]) -> str:
|
||||
if cents is None:
|
||||
return "—"
|
||||
return f"${cents / 100:.2f}"
|
||||
|
||||
|
||||
def _basis(cents: Optional[int]) -> str:
|
||||
if cents is None:
|
||||
return "—"
|
||||
@@ -207,6 +213,57 @@ def fmt_price_series(payload: dict, max_points: int = 60) -> str:
|
||||
return "\n".join(head + body) + "\n"
|
||||
|
||||
|
||||
_NUTRIENT_LABEL = {"n": "Nitrogen (N)", "p2o5": "Phosphate (P₂O₅)", "k2o": "Potash (K₂O)"}
|
||||
|
||||
|
||||
def fmt_nutrient_cost(payload: dict) -> str:
|
||||
"""Cheapest fertilizer per pound of N / P2O5 / K2O for a region."""
|
||||
geo = payload.get("geo") or "Cornbelt"
|
||||
src = payload.get("source") or "USDA AgTransport"
|
||||
products = payload.get("products") or []
|
||||
cheapest = payload.get("cheapest") or {}
|
||||
if not products:
|
||||
return f"### Fertilizer value per nutrient — {geo}\n\nNo data on file.\n"
|
||||
by_item = {p["item"]: p for p in products}
|
||||
|
||||
lines = [f"### Fertilizer value per pound of nutrient — {geo}", ""]
|
||||
for nut in ("n", "p2o5", "k2o"):
|
||||
it = cheapest.get(nut)
|
||||
prod = by_item.get(it or "")
|
||||
if prod is not None:
|
||||
c = (prod.get("cost_per_lb") or {}).get(nut)
|
||||
lines.append(
|
||||
f"- **Cheapest {_NUTRIENT_LABEL[nut]}:** "
|
||||
f"{prod.get('label') or it} at {_per_lb(c)}/lb"
|
||||
)
|
||||
lines += ["", "| Product | Grade | $/ton | $/lb N | $/lb P₂O₅ | $/lb K₂O |",
|
||||
"|---|---|---:|---:|---:|---:|"]
|
||||
|
||||
def _nkey(p: dict):
|
||||
v = (p.get("cost_per_lb") or {}).get("n")
|
||||
return (v is None, v if v is not None else 0)
|
||||
|
||||
for p in sorted(products, key=_nkey): # cheapest N first, blanks last
|
||||
a = p.get("analysis") or {}
|
||||
cpl = p.get("cost_per_lb") or {}
|
||||
grade = (a.get("grade") or "") + ("*" if a.get("grade_assumed") else "")
|
||||
lines.append(
|
||||
f"| {p.get('label') or p['item']} | {grade} | {_ton(p.get('price_cents_per_ton'))} | "
|
||||
f"{_per_lb(cpl.get('n'))} | {_per_lb(cpl.get('p2o5'))} | {_per_lb(cpl.get('k2o'))} |"
|
||||
)
|
||||
|
||||
period = next((p.get("period") for p in products if p.get("period")), None)
|
||||
assumed = any((p.get("analysis") or {}).get("grade_assumed") for p in products)
|
||||
foot = f"_Source: {src}"
|
||||
if period:
|
||||
foot += f" · as of {_ym(period)}"
|
||||
foot += " · $/lb = $/ton ÷ (analysis% × 2000 lb)"
|
||||
if assumed:
|
||||
foot += " · *UAN grade assumed 32-0-0"
|
||||
lines.append("\n" + foot + "_")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
# ---------- futures quote + change ----------
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user