|
| 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 | + // Fast-path index: models ref → fundamentals ref → Set<bucket start ms>. |
| 72 | + // Records which (models, fundamentals, bucket) triples have already been fully |
| 73 | + // populated, so the incremental loop can be skipped on subsequent calls. |
| 74 | + const populatedFor = new WeakMap< |
| 75 | + Record<string, Constituent>, |
| 76 | + WeakMap<Fundamentals, Set<number>> |
| 77 | + >(); |
| 78 | + |
| 79 | + // models ref → exact ms → constituent name → d2r * model.value(baseAstro) |
| 80 | + const v0Cache = new WeakMap<Record<string, Constituent>, Map<number, Map<string, number>>>(); |
| 81 | + |
| 82 | + function bucketStart(time: Date): number { |
| 83 | + return Math.floor(time.getTime() / intervalMs) * intervalMs; |
| 84 | + } |
| 85 | + |
| 86 | + function getCachedAstro(time: Date): AstroData { |
| 87 | + const key = time.getTime(); |
| 88 | + let cached = astroCache.get(key); |
| 89 | + if (!cached) { |
| 90 | + cached = astro(time); |
| 91 | + astroCache.set(key, cached); |
| 92 | + } |
| 93 | + return cached; |
| 94 | + } |
| 95 | + |
| 96 | + const cache: CorrectionsCache = { |
| 97 | + interval, |
| 98 | + |
| 99 | + // Quantize time to bucket and return astro evaluated at bucket midpoint. |
| 100 | + getAstro(time: Date): AstroData { |
| 101 | + return getCachedAstro(new Date(bucketStart(time) + intervalMs / 2)); |
| 102 | + }, |
| 103 | + |
| 104 | + getV0(time: Date, models: Record<string, Constituent>): Map<string, number> { |
| 105 | + const key = time.getTime(); |
| 106 | + |
| 107 | + let byTime = v0Cache.get(models); |
| 108 | + if (!byTime) { |
| 109 | + byTime = new Map(); |
| 110 | + v0Cache.set(models, byTime); |
| 111 | + } |
| 112 | + |
| 113 | + let v0 = byTime.get(key); |
| 114 | + if (!v0) { |
| 115 | + const baseAstro = getCachedAstro(time); |
| 116 | + v0 = new Map<string, number>(); |
| 117 | + for (const name of Object.keys(models)) { |
| 118 | + v0.set(name, d2r * models[name].value(baseAstro)); |
| 119 | + } |
| 120 | + byTime.set(key, v0); |
| 121 | + } |
| 122 | + |
| 123 | + return v0; |
| 124 | + }, |
| 125 | + |
| 126 | + getCorrections( |
| 127 | + time: Date, |
| 128 | + models: Record<string, Constituent>, |
| 129 | + fundamentals: Fundamentals, |
| 130 | + ): CachedCorrections { |
| 131 | + const key = bucketStart(time); |
| 132 | + |
| 133 | + let byBucket = correctionsCache.get(fundamentals); |
| 134 | + if (!byBucket) { |
| 135 | + byBucket = new Map(); |
| 136 | + correctionsCache.set(fundamentals, byBucket); |
| 137 | + } |
| 138 | + |
| 139 | + let cached = byBucket.get(key); |
| 140 | + |
| 141 | + // Fast path: this (models, fundamentals, bucket) triple was already fully |
| 142 | + // populated — skip the loop and return immediately. |
| 143 | + if (cached && populatedFor.get(models)?.get(fundamentals)?.has(key)) { |
| 144 | + return cached; |
| 145 | + } |
| 146 | + |
| 147 | + // Slow path: create or incrementally update the shared corrections entry. |
| 148 | + if (!cached) { |
| 149 | + cached = { astro: cache.getAstro(time), corrections: new Map() }; |
| 150 | + byBucket.set(key, cached); |
| 151 | + } |
| 152 | + |
| 153 | + for (const name of Object.keys(models)) { |
| 154 | + if (!cached.corrections.has(name)) { |
| 155 | + cached.corrections.set(name, models[name].correction(cached.astro, fundamentals)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Mark this (models, fundamentals, bucket) triple as fully populated. |
| 160 | + let byFund = populatedFor.get(models); |
| 161 | + if (!byFund) { |
| 162 | + byFund = new WeakMap(); |
| 163 | + populatedFor.set(models, byFund); |
| 164 | + } |
| 165 | + let populated = byFund.get(fundamentals); |
| 166 | + if (!populated) { |
| 167 | + populated = new Set(); |
| 168 | + byFund.set(fundamentals, populated); |
| 169 | + } |
| 170 | + populated.add(key); |
| 171 | + |
| 172 | + return cached; |
| 173 | + }, |
| 174 | + }; |
| 175 | + |
| 176 | + return cache; |
| 177 | +} |
0 commit comments