|
| 1 | +import astro from "./astronomy/index.js"; |
| 2 | +import type { AstroData } from "./astronomy/index.js"; |
| 3 | +import { d2r } from "./astronomy/constants.js"; |
| 4 | +import type { Constituent } from "./constituents/types.js"; |
| 5 | +import type { Fundamentals, NodalCorrection } from "./node-corrections/types.js"; |
| 6 | + |
| 7 | +/** Cached node corrections for a single time bucket. The same object reference |
| 8 | + * is returned for all times that fall within the same bucket, enabling callers |
| 9 | + * to detect bucket transitions via `!==` reference comparison. */ |
| 10 | +export interface CachedCorrections { |
| 11 | + readonly astro: AstroData; |
| 12 | + readonly corrections: Map<string, NodalCorrection>; |
| 13 | +} |
| 14 | + |
| 15 | +/** A reusable node corrections cache. Pass to `createTidePredictor` to share |
| 16 | + * astronomical computations across multiple station predictors. */ |
| 17 | +export interface CorrectionsCache { |
| 18 | + readonly interval: number; // quantization interval in hours |
| 19 | + /** Quantized astro — evaluated at bucket midpoint. Used for f/u corrections. */ |
| 20 | + getAstro(time: Date): AstroData; |
| 21 | + /** V0 equilibrium arguments (d2r * model.value(baseAstro)) for all models, |
| 22 | + * keyed by constituent name. Computed once per (models, start time) pair and |
| 23 | + * shared across all predictors with the same start time. */ |
| 24 | + getV0(time: Date, models: Record<string, Constituent>): Map<string, number>; |
| 25 | + getCorrections( |
| 26 | + time: Date, |
| 27 | + models: Record<string, Constituent>, |
| 28 | + fundamentals: Fundamentals, |
| 29 | + ): CachedCorrections; |
| 30 | +} |
| 31 | + |
| 32 | +export interface CorrectionsCacheOptions { |
| 33 | + /** Quantization interval in hours. Default: 24. |
| 34 | + * Node corrections change by <0.01% per day, so 24h introduces <0.1mm error. */ |
| 35 | + interval?: number; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Create a reusable node corrections cache. |
| 40 | + * |
| 41 | + * When predicting tides for many stations at the same time, pass the same cache |
| 42 | + * to each `createTidePredictor` call. Astronomical computations and constituent |
| 43 | + * node corrections are computed once per time bucket and shared across all |
| 44 | + * station predictors. |
| 45 | + * |
| 46 | + * @example |
| 47 | + * ```ts |
| 48 | + * import { createCorrectionsCache, createTidePredictor } from "@neaps/tide-predictor"; |
| 49 | + * |
| 50 | + * const cache = createCorrectionsCache(); |
| 51 | + * |
| 52 | + * for (const station of stations) { |
| 53 | + * const predictor = createTidePredictor(station.constituents, { cache }); |
| 54 | + * results.push(predictor.getWaterLevelAtTime({ time })); |
| 55 | + * } |
| 56 | + * ``` |
| 57 | + */ |
| 58 | +export function createCorrectionsCache({ |
| 59 | + interval = 24, |
| 60 | +}: CorrectionsCacheOptions = {}): CorrectionsCache { |
| 61 | + const intervalMs = interval * 3_600_000; |
| 62 | + |
| 63 | + // exact ms → AstroData evaluated at that precise time (for V0 arguments) |
| 64 | + const astroCache = new Map<number, AstroData>(); |
| 65 | + |
| 66 | + // fundamentals ref → bucket start (ms) → CachedCorrections |
| 67 | + // The corrections Map is populated incrementally across calls so different |
| 68 | + // models dicts can share the same CachedCorrections per (fundamentals, bucket). |
| 69 | + const correctionsCache = new WeakMap<Fundamentals, Map<number, CachedCorrections>>(); |
| 70 | + |
| 71 | + // models ref → exact ms → constituent name → d2r * model.value(baseAstro) |
| 72 | + const v0Cache = new WeakMap<Record<string, Constituent>, Map<number, Map<string, number>>>(); |
| 73 | + |
| 74 | + function bucketStart(time: Date): number { |
| 75 | + return Math.floor(time.getTime() / intervalMs) * intervalMs; |
| 76 | + } |
| 77 | + |
| 78 | + function getCachedAstro(time: Date): AstroData { |
| 79 | + const key = time.getTime(); |
| 80 | + let cached = astroCache.get(key); |
| 81 | + if (!cached) { |
| 82 | + cached = astro(time); |
| 83 | + astroCache.set(key, cached); |
| 84 | + } |
| 85 | + return cached; |
| 86 | + } |
| 87 | + |
| 88 | + const cache: CorrectionsCache = { |
| 89 | + interval, |
| 90 | + |
| 91 | + // Quantize time to bucket and return astro evaluated at bucket midpoint. |
| 92 | + getAstro(time: Date): AstroData { |
| 93 | + return getCachedAstro(new Date(bucketStart(time) + intervalMs / 2)); |
| 94 | + }, |
| 95 | + |
| 96 | + getV0(time: Date, models: Record<string, Constituent>): Map<string, number> { |
| 97 | + const key = time.getTime(); |
| 98 | + |
| 99 | + let byTime = v0Cache.get(models); |
| 100 | + if (!byTime) { |
| 101 | + byTime = new Map(); |
| 102 | + v0Cache.set(models, byTime); |
| 103 | + } |
| 104 | + |
| 105 | + let v0 = byTime.get(key); |
| 106 | + if (!v0) { |
| 107 | + const baseAstro = getCachedAstro(time); |
| 108 | + v0 = new Map<string, number>(); |
| 109 | + for (const name of Object.keys(models)) { |
| 110 | + v0.set(name, d2r * models[name].value(baseAstro)); |
| 111 | + } |
| 112 | + byTime.set(key, v0); |
| 113 | + } |
| 114 | + |
| 115 | + return v0; |
| 116 | + }, |
| 117 | + |
| 118 | + getCorrections( |
| 119 | + time: Date, |
| 120 | + models: Record<string, Constituent>, |
| 121 | + fundamentals: Fundamentals, |
| 122 | + ): CachedCorrections { |
| 123 | + const key = bucketStart(time); |
| 124 | + |
| 125 | + let byBucket = correctionsCache.get(fundamentals); |
| 126 | + if (!byBucket) { |
| 127 | + byBucket = new Map(); |
| 128 | + correctionsCache.set(fundamentals, byBucket); |
| 129 | + } |
| 130 | + |
| 131 | + const cached = byBucket.get(key); |
| 132 | + if (cached) return cached; |
| 133 | + |
| 134 | + const astro = cache.getAstro(time); |
| 135 | + const corrections = new Map<string, NodalCorrection>(); |
| 136 | + for (const name of Object.keys(models)) { |
| 137 | + corrections.set(name, models[name].correction(astro, fundamentals)); |
| 138 | + } |
| 139 | + |
| 140 | + const entry: CachedCorrections = { astro, corrections }; |
| 141 | + byBucket.set(key, entry); |
| 142 | + return entry; |
| 143 | + }, |
| 144 | + }; |
| 145 | + |
| 146 | + return cache; |
| 147 | +} |
0 commit comments