mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-21 10:42:22 +00:00
Show JTWC cyclones: western Pacific, Indian Ocean and Southern Hemisphere (#1891)
* Show JTWC warnings in Cyclone advisories Cyclone advisories now also read the Joint Typhoon Warning Center (keyless, public domain) for the western North Pacific, North Indian Ocean and Southern Hemisphere, beside NHC and CPHC. - server/providers/jtwc.js reads JTWC's warnings feed, each storm's JMV 3.0 warning (.tcw) and its overlay (.kmz). /api/cyclones merges them with NHC's: one storm per id, NHC's advisory where NHC lists the storm. A failed JTWC read keeps its last storms, marked stale, and never hides NHC's. - Storms carry an agency. A JTWC warning has no cone; its 34-knot wind danger area is drawn as its own dashed area and named for the selected storm. - A JTWC storm's card notes that the regional centre's warning is official and that JTWC winds are 1-minute averages. Source and coverage wording names both agencies. - Each storm is served while its own advisory is usable (12 hours for NHC, 13 for JTWC). * Keep one JTWC failure from affecting other storms or NHC - A storm whose JTWC warning cannot be read or parsed is left out and the others are shown; the read fails only when every listed storm's does. - JTWC is read under its own 8-second deadline. A failed or slow JTWC read keeps JTWC's last good storms and never delays, fails or stales the NHC answer. - Danger-swath longitudes past ±180 (a ring crossing the antimeridian) are wrapped into -180..180 instead of dropping the ring. * Add the changelog entry for JTWC cyclones
This commit is contained in:
1 parent
ae212e5b63
commit
5608b12a25
33 files changed
+1699
-201
No files matched your search
@@ -1,4 +1,8 @@
|
||||
import { readResponseTextCapped } from './common/http.js';
|
||||
import {
|
||||
readResponseBytesCapped,
|
||||
readResponseTextCapped,
|
||||
} from './common/http.js';
|
||||
import { JTWC_USABLE_MS, readJtwcStorms } from './jtwc.js';
|
||||
|
||||
const STATUS_URL = 'https://www.nhc.noaa.gov/CurrentStorms.json';
|
||||
const GIS =
|
||||
@@ -7,7 +11,14 @@ const HOUR = 3600_000;
|
||||
// Data, not UI copy: src/layers/cyclones/index.js keeps the same English in
|
||||
// msg() and translates it when shown (likewise 'Cyclone data unavailable').
|
||||
const COVERAGE =
|
||||
'Atlantic and eastern/central North Pacific; not worldwide cyclone coverage.';
|
||||
'NHC/CPHC: Atlantic and eastern/central North Pacific. JTWC: western North Pacific, North Indian Ocean and Southern Hemisphere.';
|
||||
const SOURCE = 'NOAA NHC / CPHC · JTWC';
|
||||
const ATTRIBUTION =
|
||||
'NOAA/NWS National Hurricane Center / Central Pacific Hurricane Center; Joint Typhoon Warning Center (U.S. Navy / U.S. Air Force)';
|
||||
/** How long a storm's advisory stays usable after issuance, by agency. */
|
||||
export function cycloneUsableMs(storm) {
|
||||
return storm?.agency === 'jtwc' ? JTWC_USABLE_MS : 12 * HOUR;
|
||||
}
|
||||
const LAYERS = [
|
||||
{ id: 5, cap: 512 * 1024, count: 500, kind: 'points' },
|
||||
{ id: 6, cap: 512 * 1024, count: 32, kind: 'track' },
|
||||
@@ -114,6 +125,7 @@ export function parseCycloneStatus(payload, now = Date.now()) {
|
||||
const positionAt = iso(raw.lastUpdate, now);
|
||||
return {
|
||||
id: raw.id,
|
||||
agency: 'nhc',
|
||||
name: text(raw.name),
|
||||
classification: text(raw.classification, 16),
|
||||
basin: raw.id.slice(0, 2).toUpperCase(),
|
||||
@@ -138,6 +150,7 @@ export function parseCycloneStatus(payload, now = Date.now()) {
|
||||
forecastPoints: [],
|
||||
track: null,
|
||||
cone: null,
|
||||
dangerArea: null,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -262,8 +275,13 @@ export function cycloneProxy({
|
||||
fetchImpl = fetch,
|
||||
now = () => Date.now(),
|
||||
timeoutMs = 12_000,
|
||||
// JTWC's own deadline, inside NHC's: a slow JTWC never holds NHC's answer.
|
||||
jtwcTimeoutMs = 8_000,
|
||||
readJtwc = readJtwcStorms,
|
||||
} = {}) {
|
||||
let cache = null;
|
||||
// JTWC's storms from its last good read, reused while a read fails.
|
||||
let jtwcCache = null;
|
||||
let operation = null;
|
||||
let attemptedAt = -Infinity;
|
||||
async function upstream(url, cap, signal) {
|
||||
@@ -286,7 +304,64 @@ export function cycloneProxy({
|
||||
signal.throwIfAborted();
|
||||
return result;
|
||||
}
|
||||
// A JTWC product: text or bytes; null when JTWC has none posted (403/404).
|
||||
async function jtwcRead(url, kind, signal) {
|
||||
signal.throwIfAborted();
|
||||
const response = await fetchImpl(url, {
|
||||
signal,
|
||||
redirect: 'error',
|
||||
headers: { 'User-Agent': 'Gods Eye View (public JTWC warning context)' },
|
||||
});
|
||||
if (
|
||||
kind === 'bytes' &&
|
||||
(response.status === 403 || response.status === 404)
|
||||
) {
|
||||
await response.body?.cancel();
|
||||
return null;
|
||||
}
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel();
|
||||
throw new Error('cyclone_upstream_unavailable');
|
||||
}
|
||||
return kind === 'bytes'
|
||||
? readResponseBytesCapped(response, 4 * 1024 * 1024, signal)
|
||||
: readResponseTextCapped(response, 256 * 1024, signal);
|
||||
}
|
||||
/**
|
||||
* JTWC's storms, read under their own deadline. A read that fails or runs
|
||||
* out of time gives JTWC's last good storms still usable, so it never fails,
|
||||
* delays past its deadline or stales NHC's answer.
|
||||
*/
|
||||
async function jtwc(signal) {
|
||||
const controller = new AbortController();
|
||||
const abort = () => controller.abort();
|
||||
signal.addEventListener('abort', abort, { once: true });
|
||||
const timer = setTimeout(abort, jtwcTimeoutMs);
|
||||
try {
|
||||
const storms = await Promise.race([
|
||||
readJtwc((url, kind) => jtwcRead(url, kind, controller.signal), now()),
|
||||
new Promise((_, reject) =>
|
||||
controller.signal.addEventListener(
|
||||
'abort',
|
||||
() => reject(new Error('jtwc_timeout')),
|
||||
{ once: true },
|
||||
),
|
||||
),
|
||||
]);
|
||||
jtwcCache = storms;
|
||||
return storms;
|
||||
} catch {
|
||||
return (jtwcCache || []).filter(
|
||||
(storm) => now() - Date.parse(storm.issuedAt) <= JTWC_USABLE_MS,
|
||||
);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
signal.removeEventListener('abort', abort);
|
||||
}
|
||||
}
|
||||
async function refresh(signal) {
|
||||
// JTWC is read beside NHC and never rejects.
|
||||
const pendingJtwc = jtwc(signal);
|
||||
let storms = parseCycloneStatus(
|
||||
await upstream(STATUS_URL, 128 * 1024, signal),
|
||||
now(),
|
||||
@@ -323,6 +398,14 @@ export function cycloneProxy({
|
||||
}));
|
||||
}
|
||||
}
|
||||
// One storm per id: NHC's advisory where NHC lists the storm (a storm
|
||||
// crossing 180° keeps its id), JTWC's elsewhere.
|
||||
const fromJtwc = await pendingJtwc;
|
||||
const listed = new Set(storms.map((storm) => storm.id));
|
||||
storms = [
|
||||
...storms,
|
||||
...fromJtwc.filter((storm) => !listed.has(storm.id)),
|
||||
].slice(0, 32);
|
||||
signal.throwIfAborted();
|
||||
cache = { storms, fetchedAt: now() };
|
||||
return cache;
|
||||
@@ -365,9 +448,8 @@ export function cycloneProxy({
|
||||
function describe(value, stale = false) {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
source: 'NOAA NHC / CPHC',
|
||||
attribution:
|
||||
'NOAA/NWS National Hurricane Center / Central Pacific Hurricane Center',
|
||||
source: SOURCE,
|
||||
attribution: ATTRIBUTION,
|
||||
coverage: COVERAGE,
|
||||
fetchedAt: value?.fetchedAt ?? null,
|
||||
stale: stale || !value,
|
||||
@@ -402,13 +484,16 @@ export function cycloneProxy({
|
||||
json(200, describe(await acquire(controller.signal)));
|
||||
} catch (error) {
|
||||
if (error.status === 429) return json(429, { error: 'cyclone_busy' });
|
||||
const usable =
|
||||
cache &&
|
||||
now() - cache.fetchedAt <= 12 * HOUR &&
|
||||
cache.storms.every(
|
||||
(storm) => now() - Date.parse(storm.issuedAt) <= 12 * HOUR,
|
||||
);
|
||||
json(200, describe(usable ? cache : null, true));
|
||||
// Each storm is served while its own advisory is usable.
|
||||
const usable = cache &&
|
||||
now() - cache.fetchedAt <= 12 * HOUR && {
|
||||
...cache,
|
||||
storms: cache.storms.filter(
|
||||
(storm) =>
|
||||
now() - Date.parse(storm.issuedAt) <= cycloneUsableMs(storm),
|
||||
),
|
||||
};
|
||||
json(200, describe(usable || null, true));
|
||||
}
|
||||
} finally {
|
||||
res.removeListener?.('close', close);
|
||||
|
||||
Reference in new issue
Block a user