From 6ec80bb43644c9157a1c30d260d3b496ec589f99 Mon Sep 17 00:00:00 2001 From: luen Date: Wed, 4 Nov 2026 09:25:20 +1000 Subject: [PATCH 1/2] Charts: add nursery month-hour temperature calendars (#127) --- AGENTS.md | 2 + e2e/thermal-calendar.spec.ts | 27 ++++++ src/lib/components/ThermalCalendar.svelte | 97 +++++++++++++++++++ .../server/analytics/thermalCalendar.test.ts | 21 ++++ src/lib/server/analytics/thermalCalendar.ts | 31 ++++++ src/lib/server/import/index.ts | 2 +- src/routes/water-quality/+page.server.ts | 2 + src/routes/water-quality/+page.svelte | 2 + 8 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 e2e/thermal-calendar.spec.ts create mode 100644 src/lib/components/ThermalCalendar.svelte create mode 100644 src/lib/server/analytics/thermalCalendar.test.ts create mode 100644 src/lib/server/analytics/thermalCalendar.ts diff --git a/AGENTS.md b/AGENTS.md index f71c8a0..64d0356 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -87,6 +87,8 @@ samples/fixture/ synthetic workbooks for tests/CI (committed) ### Cloud cover (#104) +- Month-hour nursery heatmaps (#127) use `logger_diel.n` as contributing site-hour days, not sensor samples. Mask cells with fewer than ten days and keep a common 15–40 °C scale; pooled historical means cannot establish threshold-exceedance duration. + - ERA5 `cloud_evening` covers every set; the crew's 1–4 `cloud_cover` score exists only in 2020–22 and tracks ERA5 (score 1 ≈ 43%, 4 ≈ 94%). `moonlight()` in `analytics/cloud.ts` = illumination × sin(moon altitude) × clear-sky fraction. - Cloud vs water temperature must be compared within the same month: the cloudy wet season is also the warmest, so raw correlations are ~0 while within-month ones are strongly negative. diff --git a/e2e/thermal-calendar.spec.ts b/e2e/thermal-calendar.spec.ts new file mode 100644 index 0000000..d1b5219 --- /dev/null +++ b/e2e/thermal-calendar.spec.ts @@ -0,0 +1,27 @@ +import { expect, test } from '@playwright/test'; +import AxeBuilder from '@axe-core/playwright'; + +test('temperature calendar supports keyboard, CSV and mobile in both themes', async ({ page }) => { + await page.setViewportSize({ width: 375, height: 800 }); + for (const theme of ['light', 'dark'] as const) { + await page.emulateMedia({ colorScheme: theme }); + await page.goto('/water-quality'); + const card = page + .locator('figure') + .filter({ has: page.getByRole('heading', { name: /^Month and hour:/ }) }); + await expect(card).toBeVisible(); + const chart = card.locator('svg'); + await chart.focus(); + await page.keyboard.press('ArrowRight'); + await expect(card.locator('.tip')).toBeVisible(); + await page.keyboard.press('Escape'); + await expect(card.getByRole('button', { name: /Download CSV/ })).toBeVisible(); + const result = await new AxeBuilder({ page }).include('main').analyze(); + expect( + result.violations.filter((v) => ['serious', 'critical'].includes(v.impact ?? '')) + ).toEqual([]); + expect( + await page.evaluate(() => document.documentElement.scrollWidth - innerWidth) + ).toBeLessThanOrEqual(1); + } +}); diff --git a/src/lib/components/ThermalCalendar.svelte b/src/lib/components/ThermalCalendar.svelte new file mode 100644 index 0000000..21b5451 --- /dev/null +++ b/src/lib/components/ThermalCalendar.svelte @@ -0,0 +1,97 @@ + + +
+

Nursery temperature through the year

+

+ Historical month-by-hour averages, pooled across logged years. Each cell needs at least ten + observed days. Years and months have unequal coverage; this is neither a live forecast nor a + comparison of nursery safety. Existing logger exclusions apply, and very shallow loggers may be + exposed at low tide. +

+ {#if site} + + + +
+
+ String(h))} + yLabels={months} + values={site.values} + domain={[15, 40]} + format={(v) => `${v.toFixed(1)} °C`} + xTitle="Local hour" + yTitle="Month" + /> +
+
+
+ 15 °C{#each [1, 2, 3, 4, 5, 6, 7] as k (k)}{/each}40 °C +
+
+ {:else}

No nursery logger observations available.

{/if} +
+ + diff --git a/src/lib/server/analytics/thermalCalendar.test.ts b/src/lib/server/analytics/thermalCalendar.test.ts new file mode 100644 index 0000000..d61644e --- /dev/null +++ b/src/lib/server/analytics/thermalCalendar.test.ts @@ -0,0 +1,21 @@ +import { expect, it } from 'vitest'; +import type { Dataset } from '../data'; +import { thermalCalendar } from './thermalCalendar'; + +it('keeps the full calendar, masks sparse cells and preserves site-hour support', () => { + const d = { + sites: [{ id: 'a', name: 'A' }], + loggerDiel: [ + { siteId: 'a', month: 12, hour: 23, temp: 29, n: 10 }, + { siteId: 'a', month: 1, hour: 0, temp: 31, n: 9 } + ] + } as Pick; + const [s] = thermalCalendar(d); + expect(s.values).toHaveLength(12); + expect(s.values[0]).toHaveLength(24); + expect(s.values[0][0]).toBeNull(); + expect(s.values[11][23]).toBe(29); + expect(s.rows[0].days).toBe(9); + expect(s.rows[1]).toMatchObject({ days: 0, temperature: null }); + expect(thermalCalendar({ sites: [], loggerDiel: [] })).toEqual([]); +}); diff --git a/src/lib/server/analytics/thermalCalendar.ts b/src/lib/server/analytics/thermalCalendar.ts new file mode 100644 index 0000000..53485b0 --- /dev/null +++ b/src/lib/server/analytics/thermalCalendar.ts @@ -0,0 +1,31 @@ +import type { Dataset } from '../data'; + +/** A month-hour needs ten observed days. n counts site-hours, not sensor samples. */ +export const MIN_THERMAL_DAYS = 10; +export function thermalCalendar(d: Pick) { + return d.sites + .filter((s) => d.loggerDiel.some((r) => r.siteId === s.id)) + .map((s) => { + const records = new Map( + d.loggerDiel.filter((r) => r.siteId === s.id).map((r) => [`${r.month}|${r.hour}`, r]) + ); + const rows = Array.from({ length: 12 }, (_, m) => + Array.from({ length: 24 }, (_, hour) => { + const r = records.get(`${m + 1}|${hour}`); + return { + month: m + 1, + hour, + days: r?.n ?? 0, + temperature: r && r.n >= MIN_THERMAL_DAYS && Number.isFinite(r.temp) ? r.temp : null + }; + }) + ); + return { + id: s.id, + name: s.name, + rows: rows.flat(), + values: rows.map((rs) => rs.map((r) => r.temperature)) + }; + }) + .sort((a, b) => a.name.localeCompare(b.name)); +} diff --git a/src/lib/server/import/index.ts b/src/lib/server/import/index.ts index 3cfd073..6ce12c6 100644 --- a/src/lib/server/import/index.ts +++ b/src/lib/server/import/index.ts @@ -73,7 +73,7 @@ import { SITE_DEFS } from './sites'; import { readWorkbook } from './xlsx'; /** Bump when import logic changes so existing caches rebuild on the next start. */ -export const IMPORTER_VERSION = '8'; +export const IMPORTER_VERSION = '9'; const MIGRATIONS = import.meta.glob('/drizzle/*.sql', { query: '?raw', diff --git a/src/routes/water-quality/+page.server.ts b/src/routes/water-quality/+page.server.ts index 28eba35..46ef162 100644 --- a/src/routes/water-quality/+page.server.ts +++ b/src/routes/water-quality/+page.server.ts @@ -1,4 +1,5 @@ import { memo } from '#lib/server/data'; +import { thermalCalendar } from '#lib/server/analytics/thermalCalendar'; import { activeNurseries, boxOf, round, siteName } from '#lib/server/analytics/common'; import { coverage, @@ -127,6 +128,7 @@ export const load: PageServerLoad = () => : null; return { + thermalCalendar: thermalCalendar(d), insitu, temp: by('temp', 20, 40), ph: by('ph', 7, 9), diff --git a/src/routes/water-quality/+page.svelte b/src/routes/water-quality/+page.svelte index 93a7cc8..21816fa 100644 --- a/src/routes/water-quality/+page.svelte +++ b/src/routes/water-quality/+page.svelte @@ -1,4 +1,5 @@

Nursery temperature through the year

- Historical month-by-hour averages, pooled across logged years. Each cell needs at least ten - observed days. Years and months have unequal coverage; this is neither a live forecast nor a - comparison of nursery safety. Existing logger exclusions apply, and very shallow loggers may be - exposed at low tide. + Mean logger temperature for each month and hour of the day, pooled over every year a nursery was + logged. A cell needs at least ten logged days. Nurseries were logged in different years and for + different lengths of time (under each chart), so years and months don't count equally. These are + long-run averages, not a forecast, and not a ranking of nurseries. Downloads left out above stay + out here, and a very shallow logger may be out of the water at low tide.

{#if site} String(h))} - yLabels={months} + yLabels={MONTH_NAMES} values={site.values} - domain={[15, 40]} + domain={calendar.domain} format={(v) => `${v.toFixed(1)} °C`} xTitle="Local hour" yTitle="Month" /> -
- 15 °C{#each [1, 2, 3, 4, 5, 6, 7] as k (k)} + {lo} °C{#each [1, 2, 3, 4, 5, 6, 7] as k (k)}{/each}40 °C + >{/each}{hi} °C
{:else}

No nursery logger observations available.

{/if} diff --git a/src/lib/components/charts/Heatmap.svelte b/src/lib/components/charts/Heatmap.svelte index 7d6ea1f..c790a63 100644 --- a/src/lib/components/charts/Heatmap.svelte +++ b/src/lib/components/charts/Heatmap.svelte @@ -50,8 +50,9 @@ const absMax = $derived(Math.max(Math.abs(lo), Math.abs(hi)) || 1); function fill(v: number | null): { color: string; opacity: number; dark: boolean } { - if (v == null || !Number.isFinite(v)) - return { color: 'var(--surface-2)', opacity: 0.6, dark: false }; + // No data: an empty dashed cell, never a colour from the scale (the lightest steps are + // close to --surface-2, in dark mode especially). + if (v == null || !Number.isFinite(v)) return { color: 'none', opacity: 1, dark: false }; if (mode === 'div') { const t = Math.max(-1, Math.min(1, v / absMax)); if (Math.abs(t) < 0.06) return { color: 'var(--div-mid)', opacity: 1, dark: false }; @@ -118,6 +119,9 @@ rx="3" fill={f.color} fill-opacity={f.opacity} + stroke={f.color === 'none' ? 'var(--axis)' : undefined} + stroke-dasharray={f.color === 'none' ? '3 2' : undefined} + pointer-events={f.color === 'none' ? 'all' : undefined} role="presentation" data-mark={r * xLabels.length + c} onpointermove={(e) => show(pointerIn(e), r, c)} diff --git a/src/lib/server/analytics/thermalCalendar.test.ts b/src/lib/server/analytics/thermalCalendar.test.ts index d61644e..04f1418 100644 --- a/src/lib/server/analytics/thermalCalendar.test.ts +++ b/src/lib/server/analytics/thermalCalendar.test.ts @@ -2,20 +2,52 @@ import { expect, it } from 'vitest'; import type { Dataset } from '../data'; import { thermalCalendar } from './thermalCalendar'; +type Input = Pick; + it('keeps the full calendar, masks sparse cells and preserves site-hour support', () => { const d = { sites: [{ id: 'a', name: 'A' }], loggerDiel: [ { siteId: 'a', month: 12, hour: 23, temp: 29, n: 10 }, { siteId: 'a', month: 1, hour: 0, temp: 31, n: 9 } + ], + loggerDaily: [ + { siteId: 'a', date: '2020-12-02' }, + { siteId: 'a', date: '2019-12-01' } ] - } as Pick; - const [s] = thermalCalendar(d); + } as unknown as Input; + const { sites, domain } = thermalCalendar(d); + const [s] = sites; expect(s.values).toHaveLength(12); expect(s.values[0]).toHaveLength(24); expect(s.values[0][0]).toBeNull(); expect(s.values[11][23]).toBe(29); expect(s.rows[0].days).toBe(9); expect(s.rows[1]).toMatchObject({ days: 0, temperature: null }); - expect(thermalCalendar({ sites: [], loggerDiel: [] })).toEqual([]); + expect(s).toMatchObject({ days: 2, first: '2019-12-01', last: '2020-12-02' }); + // The masked 31 °C cell doesn't stretch the scale; a single value still gets a span. + expect(domain).toEqual([29, 30]); + expect(thermalCalendar({ sites: [], loggerDiel: [], loggerDaily: [] })).toEqual({ + domain: [15, 40], + sites: [] + }); +}); + +it('shares one domain across nurseries, rounded out to whole degrees', () => { + const cell = (siteId: string, month: number, temp: number) => ({ + siteId, + month, + hour: 12, + temp, + n: 20 + }); + const d = { + sites: [ + { id: 'a', name: 'A' }, + { id: 'b', name: 'B' } + ], + loggerDiel: [cell('a', 1, 26.4), cell('a', 2, 28), cell('b', 1, 31.2)], + loggerDaily: [] + } as unknown as Input; + expect(thermalCalendar(d).domain).toEqual([26, 32]); }); diff --git a/src/lib/server/analytics/thermalCalendar.ts b/src/lib/server/analytics/thermalCalendar.ts index 53485b0..09d41fd 100644 --- a/src/lib/server/analytics/thermalCalendar.ts +++ b/src/lib/server/analytics/thermalCalendar.ts @@ -2,8 +2,14 @@ import type { Dataset } from '../data'; /** A month-hour needs ten observed days. n counts site-hours, not sensor samples. */ export const MIN_THERMAL_DAYS = 10; -export function thermalCalendar(d: Pick) { - return d.sites + +/** + * Month × hour mean temperature per nursery (#127), from `logger_diel`. One colour domain for + * every nursery so they compare, taken from the supported cells (whole degrees): the 15–40 °C + * plausibility window would squeeze the lagoon's few degrees into two colour steps. + */ +export function thermalCalendar(d: Pick) { + const sites = d.sites .filter((s) => d.loggerDiel.some((r) => r.siteId === s.id)) .map((s) => { const records = new Map( @@ -20,12 +26,25 @@ export function thermalCalendar(d: Pick) { }; }) ); + const days = d.loggerDaily.filter((r) => r.siteId === s.id).map((r) => r.date); + days.sort(); return { id: s.id, name: s.name, + days: days.length, + first: days[0] ?? null, + last: days.at(-1) ?? null, rows: rows.flat(), values: rows.map((rs) => rs.map((r) => r.temperature)) }; }) .sort((a, b) => a.name.localeCompare(b.name)); + const cells = sites + .flatMap((s) => s.rows.map((r) => r.temperature)) + .filter((v): v is number => v != null); + const domain: [number, number] = cells.length + ? [Math.floor(Math.min(...cells)), Math.ceil(Math.max(...cells))] + : [15, 40]; + if (domain[0] === domain[1]) domain[1] += 1; + return { domain, sites }; } diff --git a/src/routes/water-quality/+page.svelte b/src/routes/water-quality/+page.svelte index c975af0..9a3bbc1 100644 --- a/src/routes/water-quality/+page.svelte +++ b/src/routes/water-quality/+page.svelte @@ -28,7 +28,6 @@ )} to {monthYear(ins.summary.last)}.{/if}

- {#if ins}
@@ -168,6 +167,8 @@
{/if} + +