"use strict";
// About page: track-record charts (test-year head-to-head at Indianapolis) from the forecast's scorecard.
const NS = "http://www.w3.org/2000/svg";
const $ = (s) => document.querySelector(s);
const el = (tag, attrs, parent, text) => {
const n = document.createElementNS(NS, tag);
for (const [k, v] of Object.entries(attrs || {})) n.setAttribute(k, v);
if (text != null) n.textContent = text;
if (parent) parent.appendChild(n);
return n;
};
const css = (n) => getComputedStyle(document.documentElement).getPropertyValue(n).trim();
let unit = "F";
try { unit = localStorage.getItem("kind-unit") || "F"; } catch (e) { /* ignore */ }
const F = unit === "F";
const LEAD_MID = { "1-6": 3.5, "7-12": 9.5, "13-24": 18.5, "25-36": 30.5, "37-48": 42.5, "49-72": 60.5 };
const LEAD_NAME = { "1-6": "1–6 h", "7-12": "7–12 h", "13-24": "13–24 h", "25-36": "25–36 h", "37-48": "37–48 h", "49-72": "49–72 h" };
function draw(rows) {
const vars = [["t", `Temperature, ${F ? "°F" : "°C"}`, F ? 1.8 : 1], ["td", `Dew point, ${F ? "°F" : "°C"}`, F ? 1.8 : 1], ["wspd", `Wind speed, ${F ? "mph" : "km/h"}`, F ? 2.23694 : 3.6]];
const sys = [["ours", "Ours", "--ours", 2.75], ["nbs", "Blend", "--nbs", 1.75], ["nws", "NWS", "--nws", 1.75]];
const box = $("#record");
const W = 300, H = 180, m = { l: 34, r: 50, t: 10, b: 26 };
for (const [v, title, k] of vars) {
const rs = rows.filter((r) => r.var === v).sort((a, b) => LEAD_MID[a.leads] - LEAD_MID[b.leads]);
if (!rs.length) continue;
const fig = document.createElement("figure");
fig.innerHTML = `${title}`;
const svg = el("svg", { width: W, height: H, viewBox: `0 0 ${W} ${H}`, role: "img", "aria-label": `${title}: average error by hours ahead` });
fig.appendChild(svg); box.appendChild(fig);
const hi = Math.max(...rs.flatMap((r) => [r.ours, r.nbs, r.nws])) * k * 1.08;
const X = (h) => m.l + h / 72 * (W - m.l - m.r), Y = (e) => H - m.b - e / hi * (H - m.t - m.b);
const step = hi > 4 ? 1 : 0.5;
for (let e = 0; e <= hi; e += step) {
el("line", { x1: m.l, x2: W - m.r, y1: Y(e), y2: Y(e), class: "grid" }, svg);
el("text", { x: m.l - 6, y: Y(e) + 4, "text-anchor": "end" }, svg, e.toFixed(step < 1 ? 1 : 0));
}
for (const h of [0, 24, 48, 72]) el("text", { x: X(h), y: H - 8, "text-anchor": "middle" }, svg, h ? `${h} h` : "0");
const ends = [];
for (const [key, name, col, w] of sys) {
const pts = rs.filter((r) => r[key] != null).map((r) => [X(LEAD_MID[r.leads]), Y(r[key] * k)]);
el("path", { d: "M" + pts.map((p) => p.join(",")).join("L"), fill: "none", stroke: css(col), "stroke-width": w, "stroke-linejoin": "round", "stroke-linecap": "round" }, svg);
ends.push({ y: pts[pts.length - 1][1], name, key });
}
ends.sort((a, b) => a.y - b.y);
for (let i = 1; i < ends.length; i++) if (ends[i].y - ends[i - 1].y < 13) ends[i].y = ends[i - 1].y + 13;
for (const e of ends) el("text", { x: W - m.r + 8, y: e.y + 4, class: `lab ${e.key === "ours" ? "ours" : ""}` }, svg, e.name);
}
// the temperature numbers, for reference
const t = rows.filter((r) => r.var === "t").sort((a, b) => LEAD_MID[a.leads] - LEAD_MID[b.leads]);
const k = F ? 1.8 : 1, u = F ? "°F" : "°C";
$("#record-table").innerHTML = `| Temperature error, ${u} | ${t.map((r) => `${LEAD_NAME[r.leads]} | `).join("")}
|---|
` +
[["Ours", "ours"], ["National Blend", "nbs"], ["NWS", "nws"]].map(([n, key]) => `| ${n} | ${t.map((r) => {
const best = Math.min(r.ours, r.nbs, r.nws);
return `${(r[key] * k).toFixed(1)} | `;
}).join("")}
`).join("") + "";
}
fetch("/forecast.json", { cache: "no-store" }).then((r) => r.json()).then((d) => {
draw(d.scorecard || []);
if (d.model) {
const z = (iso) => `${iso.slice(5, 10)} ${iso.slice(11, 13)}Z`;
const runs = Object.entries(d.guidance || {}).map(([k, g]) => `${k} ${g.run ? z(g.run) : "none"}`).join(", ");
$("#inputs").textContent = `Current Indianapolis forecast starts from the ${z(d.origin)} observation, using ${runs}. Model Indra-3M.`;
}
});