|
| 1 | +import { expect } from 'vitest' |
| 2 | +import fs from 'fs/promises' |
| 3 | +import tidePredictor from '@neaps/tide-predictor' |
| 4 | +import db from '@neaps/tide-database' |
| 5 | +import { createWriteStream } from 'fs' |
| 6 | +import { join } from 'path' |
| 7 | + |
| 8 | +const __dirname = new URL('.', import.meta.url).pathname |
| 9 | + |
| 10 | +const stations = db.filter( |
| 11 | + (station) => |
| 12 | + // TODO: Update this to test subordinate stations too. |
| 13 | + // Need to switch from `getWaterLevelAtTime` to `getExtremesPrediction` and compare time/level. |
| 14 | + station.type === 'reference' && |
| 15 | + station.source.source_url.includes('noaa.gov') |
| 16 | +) |
| 17 | + |
| 18 | +// Create a directory for test cache |
| 19 | +await fs.mkdir('./.test-cache', { recursive: true }) |
| 20 | + |
| 21 | +interface Stat { |
| 22 | + station: string |
| 23 | + count: number |
| 24 | + mae: number |
| 25 | + rmse: number |
| 26 | + bias: number |
| 27 | +} |
| 28 | + |
| 29 | +const stats: Stat[] = [] |
| 30 | + |
| 31 | +console.log(`Testing tide predictions against ${stations.length} NOAA stations`) |
| 32 | + |
| 33 | +for (const station of stations) { |
| 34 | + // Catch error and return no levels if failing to fetch data. There is a test later to ensure enough stations are tested. |
| 35 | + const { levels } = await getStation(station.source.id).catch(() => ({ |
| 36 | + levels: {} |
| 37 | + })) |
| 38 | + const tideStation = tidePredictor(station.harmonic_constituents, { |
| 39 | + phaseKey: 'phase_UTC' |
| 40 | + }) |
| 41 | + |
| 42 | + // No predictions available |
| 43 | + if (!levels.predictions) continue |
| 44 | + |
| 45 | + let count = 0 |
| 46 | + let sumError = 0 |
| 47 | + let sumAbsError = 0 |
| 48 | + let sumSqError = 0 |
| 49 | + |
| 50 | + levels.predictions.forEach((prediction: { t: string; v: string }) => { |
| 51 | + const expected = parseFloat(prediction.v) |
| 52 | + |
| 53 | + const { level: actual } = tideStation.getWaterLevelAtTime({ |
| 54 | + time: new Date(prediction.t) |
| 55 | + }) |
| 56 | + |
| 57 | + const error = actual - expected |
| 58 | + |
| 59 | + count += 1 |
| 60 | + sumError += error |
| 61 | + sumAbsError += Math.abs(error) |
| 62 | + sumSqError += error * error |
| 63 | + }) |
| 64 | + |
| 65 | + const mae = sumAbsError / count |
| 66 | + const rmse = Math.sqrt(sumSqError / count) |
| 67 | + const bias = sumError / count |
| 68 | + |
| 69 | + stats.push({ station: station.source.id, count, mae, rmse, bias }) |
| 70 | + process.stdout.write('.') |
| 71 | +} |
| 72 | + |
| 73 | +// Write stats to file for later analysis |
| 74 | +const summary = createWriteStream(join(__dirname, 'noaa.csv')) |
| 75 | +summary.write('station,count,mae_m,rmse_m,bias_m\n') |
| 76 | +stats.forEach(({ station, count, mae, rmse, bias }) => { |
| 77 | + summary.write( |
| 78 | + [station, count, mae.toFixed(4), rmse.toFixed(4), bias.toFixed(4)].join( |
| 79 | + ',' |
| 80 | + ) + '\n' |
| 81 | + ) |
| 82 | +}) |
| 83 | +summary.end() |
| 84 | + |
| 85 | +// Baseline expectations based on current performance. The goal should be to move these toward zero over time. |
| 86 | +const maeValues = stats.map((s) => s.mae).sort((a, b) => a - b) |
| 87 | +const medianMAE = maeValues[Math.floor(stats.length / 2)] |
| 88 | +const p90MAE = maeValues[Math.floor(stats.length * 0.9)] |
| 89 | +const p95MAE = maeValues[Math.floor(stats.length * 0.95)] |
| 90 | + |
| 91 | +expect(medianMAE, 'MAE p50').toBeLessThan(0.03) // 3 cm |
| 92 | +expect(p90MAE, 'MAE p90').toBeLessThan(0.06) // 6 cm |
| 93 | +expect(p95MAE, 'MAE p95').toBeLessThan(0.08) // 8 cm |
| 94 | +expect(stats.length, 'Total stations').toBeGreaterThanOrEqual(1100) // Ensure enough stations were tested |
| 95 | + |
| 96 | +async function makeRequest(url: string) { |
| 97 | + const res = await fetch(url) |
| 98 | + if (!res.ok) throw new Error(`Request failed with status ${res.status}`) |
| 99 | + return res.json() |
| 100 | +} |
| 101 | + |
| 102 | +async function getStation(station: string) { |
| 103 | + const filePath = `./.test-cache/${station}.json` |
| 104 | + |
| 105 | + try { |
| 106 | + return await fs.readFile(filePath, 'utf-8').then((data) => JSON.parse(data)) |
| 107 | + } catch { |
| 108 | + const levels = await makeRequest( |
| 109 | + `https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=recent&station=${station}&product=predictions&datum=MTL&time_zone=gmt&units=metric&format=json` |
| 110 | + ) |
| 111 | + |
| 112 | + const data = { levels } |
| 113 | + await fs.writeFile(filePath, JSON.stringify(data)) |
| 114 | + return data |
| 115 | + } |
| 116 | +} |
0 commit comments