diff --git a/app/public/alert-info.js b/app/public/alert-info.js new file mode 100644 index 0000000..ea4495e --- /dev/null +++ b/app/public/alert-info.js @@ -0,0 +1,101 @@ +/* Shared plain-language alert helpers (used by the weather app and the station dashboard) */ +(() => { +'use strict'; +const GLOSSARY = { + 'heat advisory': 'Dangerously hot conditions expected. Limit time outdoors, drink plenty of water, and check on children, older adults and pets.', + 'excessive heat watch': 'Conditions are favorable for life-threatening heat in the next day or two. Plan ahead.', + 'excessive heat warning': 'Life-threatening heat is occurring or imminent. Avoid outdoor activity and stay cool.', + 'extreme heat watch': 'Conditions are favorable for life-threatening heat in the next day or two. Plan ahead.', + 'extreme heat warning': 'Life-threatening heat is occurring or imminent. Avoid outdoor activity and stay cool.', + 'severe thunderstorm watch': 'Storms capable of damaging wind or large hail are possible in the area. Stay alert and be ready to take shelter.', + 'severe thunderstorm warning': 'A severe storm with damaging wind and/or large hail is happening or imminent. Move indoors away from windows.', + 'tornado watch': 'Conditions are favorable for tornadoes. Stay alert and know where you would take shelter.', + 'tornado warning': 'A tornado is occurring or imminent. Take shelter immediately in the lowest, most interior room.', + 'special weather statement': 'A short notice about a significant but non-warning weather event, often a strong storm, gusty winds, dense fog or sudden changes.', + 'flood watch': 'Flooding is possible. Be prepared to move to higher ground and avoid low-lying areas.', + 'flood advisory': 'Minor flooding is occurring or expected. Avoid flooded roads and low spots.', + 'flood warning': 'Flooding is happening or imminent. Move to higher ground and never drive through flood water.', + 'flash flood watch': 'Flash flooding is possible. Be ready to move to higher ground quickly.', + 'flash flood warning': 'Flash flooding is occurring or imminent. Move to higher ground now.', + 'wind advisory': 'Sustained or gusty winds that can blow around unsecured objects and make driving difficult for high-profile vehicles.', + 'high wind watch': 'Damaging winds are possible. Secure outdoor objects.', + 'high wind warning': 'Damaging winds are occurring or imminent. Secure objects and avoid travel if possible.', + 'freeze watch': 'A freeze is possible. Protect sensitive plants, pets and exposed pipes.', + 'freeze warning': 'Temperatures at or below freezing are expected. Protect plants, pets and exposed pipes.', + 'frost advisory': 'Frost is expected and can damage sensitive plants.', + 'hard freeze warning': 'Severe freezing temperatures are expected. Protect plants, pets and pipes.', + 'winter storm watch': 'Heavy snow and/or ice is possible. Prepare for hazardous travel.', + 'winter storm warning': 'Heavy snow and/or ice is occurring or imminent. Travel may be dangerous or impossible.', + 'winter weather advisory': 'Snow, sleet or freezing rain will cause slippery roads and travel difficulty.', + 'ice storm warning': 'Significant ice accumulation is expected. Power outages and dangerous travel are likely.', + 'blizzard warning': 'Heavy snow with strong winds and very low visibility. Avoid travel.', + 'wind chill advisory': 'Dangerously cold wind chills are expected. Dress in layers and limit time outside.', + 'wind chill warning': 'Life-threatening wind chills are expected. Frostbite can occur quickly.', + 'dense fog advisory': 'Fog is reducing visibility to a quarter mile or less. Slow down and use low-beam headlights.', + 'dense smoke advisory': 'Smoke is reducing visibility and air quality.', + 'air quality alert': 'Air quality is unhealthy for some or all people. Limit prolonged outdoor exertion.', + 'red flag warning': 'Dry, windy and warm conditions create a high wildfire risk. Avoid open flames and outdoor burning.', + 'fire weather watch': 'Conditions may soon create a high wildfire risk.', + 'rip current statement': 'Dangerous rip currents at the beach. Stay out of the water or swim near a lifeguard.', + 'coastal flood advisory': 'Minor coastal flooding is expected.', + 'coastal flood warning': 'Significant coastal flooding is occurring or imminent.', + 'small craft advisory': 'Winds or seas hazardous to small boats.', + 'tropical storm watch': 'Tropical storm conditions are possible within about 48 hours.', + 'tropical storm warning': 'Tropical storm conditions are expected within about 36 hours.', + 'hurricane watch': 'Hurricane conditions are possible within about 48 hours.', + 'hurricane warning': 'Hurricane conditions are expected within about 36 hours.', + 'hazardous weather outlook': 'An outlook of possible hazardous weather in the next several days.', + 'severe weather statement': 'An update on an ongoing severe storm warning.', + 'flood statement': 'An update on an ongoing flood warning.', + 'storm surge watch': 'Life-threatening storm surge flooding is possible within about 48 hours. Know your evacuation zone and be ready to leave.', + 'storm surge warning': 'Life-threatening storm surge flooding is expected within about 36 hours. Follow evacuation orders from local officials immediately.', + 'hurricane local statement': 'Local details on a hurricane threat: expected winds, rain, surge and tornado risk for your area, and what officials are advising.', + 'tropical cyclone statement': 'Local details on a tropical storm or hurricane threat for your area.', + 'tropical depression watch': 'A tropical depression may affect the area; heavy rain and gusty wind are possible.', + 'extreme wind warning': 'Extreme, hurricane-force winds (115+ mph) are about to hit. Take shelter now in the strongest part of a sturdy building.', + 'hurricane force wind watch': 'Hurricane-force winds are possible over marine areas. Stay in port.', + 'hurricane force wind warning': 'Hurricane-force winds are expected over marine areas. Dangerous for all vessels.', + 'storm warning': 'Strong marine winds (48–63 knots) expected. Dangerous for all vessels.', + 'gale warning': 'Strong marine winds (34–47 knots) expected. Hazardous for small and large vessels.', + 'gale watch': 'Gale-force marine winds are possible.', + 'special marine warning': 'Short-lived, hazardous marine weather (strong winds, waterspouts, hail) is happening. Seek safe harbor.', + 'high surf advisory': 'Large breaking waves make swimming and surfing dangerous.', + 'high surf warning': 'Very large breaking waves are expected: dangerous and life-threatening at the shore.', + 'beach hazards statement': 'Hazards at the beach such as rip currents or high surf. Swim near a lifeguard.', + 'lake wind advisory': 'Gusty lake winds can create hazardous waves for small boats.', + 'dust storm warning': 'Blowing dust is reducing visibility to near zero. Pull off the road and turn off your lights.', + 'tornado emergency': 'A confirmed large and destructive tornado is moving toward a populated area. Take cover immediately.', + 'flash flood emergency': 'Catastrophic, life-threatening flash flooding is happening. Move to higher ground now.', +}; + +function meaning(event) { + const e = String(event || '').toLowerCase().trim(); + if (GLOSSARY[e]) return GLOSSARY[e]; + if (e.endsWith('watch')) return 'A Watch: conditions are favorable for this hazard. Stay alert and be ready to act.'; + if (e.endsWith('warning')) return 'A Warning: this hazard is happening or imminent. Take action now.'; + if (e.endsWith('advisory')) return 'An Advisory: conditions may cause inconvenience or danger if you are not careful.'; + if (e.endsWith('statement')) return 'An informational statement about developing or ongoing weather.'; + return ''; +} +// NWS text is often structured "* WHAT...", "* WHERE...", "* WHEN..."; lead with the WHAT part +function firstSentence(t, max = 220) { + const what = String(t || '').match(/\*\s*WHAT\.\.\.([\s\S]*?)(?:\n\s*\n|\*\s*[A-Z ]+\.\.\.|$)/); + const c = (what ? what[1] : String(t || '')).replace(/\s+/g, ' ').trim(); + const m = c.match(/^.*?[.!?](\s|$)/); + const r = (m ? m[0] : c).trim(); + return r.length > max ? r.slice(0, max - 3) + '…' : r; +} +const area = (a) => String(a || '').replace(/;\s*/g, ', '); +const when = (iso) => (iso ? new Date(iso).toLocaleString([], { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }) : ''); +const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); +// "ends in 3h 10m" / "ended" for active alerts +function remaining(iso) { + if (!iso) return ''; + const ms = Date.parse(iso) - Date.now(); + if (!Number.isFinite(ms)) return ''; + if (ms <= 0) return 'expired'; + const m = Math.round(ms / 60000); + return m < 60 ? `ends in ${m} min` : m < 1440 ? `ends in ${Math.floor(m / 60)}h ${m % 60}m` : `ends in ${Math.round(m / 144) / 10}d`.replace('.0d', 'd'); +} +window.AlertInfo = { meaning, firstSentence, area, when, esc, remaining, GLOSSARY }; +})(); diff --git a/app/public/app-ui.css b/app/public/app-ui.css index 18910e4..6322ad7 100644 --- a/app/public/app-ui.css +++ b/app/public/app-ui.css @@ -298,3 +298,29 @@ body.map-fs #tab-unified-map { padding: 0; } #unified-map .maplibregl-ctrl-top-right { margin-top: calc(var(--hud-top) + 166px); margin-right: max(var(--hud-side), env(safe-area-inset-right, 0px)); } #unified-map .maplibregl-ctrl-top-right .maplibregl-ctrl { margin: 0; } @media (pointer: coarse) { #unified-map .maplibregl-ctrl-top-right { display: none; } } + +/* ── Alerts tab: hurricane cards + rich alert cards ── */ +.hurricane-storm-card { border-radius: 16px; padding: 0.9rem 1rem; } +.hurricane-name { font-weight: 800; font-size: 1.05rem; } +.hurricane-stats b { color: var(--ink); font-size: 0.95rem; } +.hurricane-stats small { opacity: .75; } +.hurricane-meta { display: flex; flex-wrap: wrap; gap: 0.3rem 0.9rem; align-items: center; margin-top: 0.5rem; font-size: 0.74rem; color: var(--ink-light); } +.hurricane-link { color: var(--accent); font-weight: 600; text-decoration: none; } +.hurricane-link:hover { text-decoration: underline; } +.hurricane-map-hint { margin-left: auto; color: var(--accent); font-weight: 600; } + +.alert-rich { border-radius: 16px; padding: 0.95rem 1.05rem; } +.alert-why { font-size: 0.95rem; font-weight: 600; line-height: 1.4; margin: 0.35rem 0 0.5rem; } +.alert-facts { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; font-size: 0.8rem; color: var(--ink-light); margin-bottom: 0.55rem; } +.alert-left { font-weight: 700; color: var(--accent); } +.alert-meaning { font-size: 0.84rem; line-height: 1.45; padding: 0.55rem 0.75rem; border-radius: 12px; background: color-mix(in srgb, var(--accent) 9%, transparent); margin-bottom: 0.5rem; } +.alert-rich .alert-action { font-size: 0.86rem; line-height: 1.45; } +.alert-more { margin-top: 0.6rem; border-top: 1px dashed var(--border); padding-top: 0.5rem; } +.alert-more summary { cursor: pointer; font-size: 0.8rem; font-weight: 700; color: var(--accent); min-height: 36px; display: flex; align-items: center; list-style: none; } +.alert-more summary::-webkit-details-marker { display: none; } +.alert-more summary::before { content: '▸'; margin-right: 0.4rem; transition: transform .15s; } +.alert-more[open] summary::before { transform: rotate(90deg); } +.alert-full { font-size: 0.84rem; line-height: 1.5; } +.alert-full p { margin: 0 0 0.6rem; } +.alert-pre { white-space: pre-wrap; } +.alert-meta { font-size: 0.74rem; color: var(--ink-light); } diff --git a/app/public/app.js b/app/public/app.js index 3622108..28da076 100644 --- a/app/public/app.js +++ b/app/public/app.js @@ -62,55 +62,104 @@ function parseNhcCoord(str) { let hurricaneStorms = []; +// ── NHC storm model ────────────────────────────────────────────────────────── +// NHC's CurrentStorms.json reports `intensity` in KNOTS and `movementSpeed` in mph. +const KT_TO_MPH = 1.15078; +const STORM_CLASS = { + TD: 'Tropical Depression', TS: 'Tropical Storm', HU: 'Hurricane', TY: 'Typhoon', + STD: 'Subtropical Depression', STS: 'Subtropical Storm', SD: 'Subtropical Depression', SS: 'Subtropical Storm', + PTC: 'Potential Tropical Cyclone', PC: 'Post-Tropical Cyclone', EX: 'Extratropical Cyclone', +}; +const SAFFIR_COLORS = { td: '#5dade2', ts: '#27ae60', 1: '#f1c40f', 2: '#f39c12', 3: '#e74c3c', 4: '#c0392b', 5: '#9b59b6', ex: '#95a5a6' }; +const compass16 = deg => ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW'][Math.round(deg / 22.5) % 16]; +function saffirCategory(kt) { return kt >= 137 ? 5 : kt >= 113 ? 4 : kt >= 96 ? 3 : kt >= 83 ? 2 : kt >= 64 ? 1 : 0; } +// Colour for a point on the forecast track, from its own forecast wind (knots) +function windColor(kt) { return kt >= 64 ? SAFFIR_COLORS[Math.max(1, saffirCategory(kt))] : kt >= 34 ? SAFFIR_COLORS.ts : SAFFIR_COLORS.td; } +function escHtml(v) { return String(v ?? '').replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); } +function fmtLatLon(lat, lon) { + const la = isNaN(lat) ? '--' : `${Math.abs(lat).toFixed(1)}°${lat < 0 ? 'S' : 'N'}`; + const lo = isNaN(lon) ? '--' : `${Math.abs(lon).toFixed(1)}°${lon < 0 ? 'W' : 'E'}`; + return `${la}, ${lo}`; +} +function milesBetween(lat1, lon1, lat2, lon2) { + const R = 3958.8, r = Math.PI / 180, dLat = (lat2 - lat1) * r, dLon = (lon2 - lon1) * r; + const x = Math.sin(dLat / 2) ** 2 + Math.cos(lat1 * r) * Math.cos(lat2 * r) * Math.sin(dLon / 2) ** 2; + return 2 * R * Math.asin(Math.sqrt(x)); +} +function bearingDeg(lat1, lon1, lat2, lon2) { + const r = Math.PI / 180, y = Math.sin((lon2 - lon1) * r) * Math.cos(lat2 * r); + const x = Math.cos(lat1 * r) * Math.sin(lat2 * r) - Math.sin(lat1 * r) * Math.cos(lat2 * r) * Math.cos((lon2 - lon1) * r); + return (Math.atan2(y, x) / r + 360) % 360; +} + +function stormInfo(s) { + const cls = String(s.classification || '').toUpperCase(); + const kt = parseFloat(s.intensity); + const hasKt = Number.isFinite(kt); + const mph = hasKt ? Math.round(kt * KT_TO_MPH) : null; + const isHurr = cls === 'HU' || cls === 'TY'; + const cat = isHurr && hasKt ? Math.max(1, saffirCategory(kt)) : isHurr ? 1 : 0; + const base = STORM_CLASS[cls] || 'Tropical Cyclone'; + const label = cat ? `Category ${cat} ${cls === 'TY' ? 'Typhoon' : 'Hurricane'}${cat >= 3 ? ' (major)' : ''}` : base; + const badge = cat ? `cat-${cat}` : (cls === 'TS' || cls === 'STS' || cls === 'SS') ? 'cat-ts' : (cls === 'TD' || cls === 'STD' || cls === 'SD' || cls === 'PTC') ? 'cat-td' : 'cat-ex'; + const color = cat ? SAFFIR_COLORS[cat] : badge === 'cat-ts' ? SAFFIR_COLORS.ts : badge === 'cat-td' ? SAFFIR_COLORS.td : SAFFIR_COLORS.ex; + const lat = Number.isFinite(s.latitudeNumeric) ? s.latitudeNumeric : parseNhcCoord(s.latitude); + const lon = Number.isFinite(s.longitudeNumeric) ? s.longitudeNumeric : parseNhcCoord(s.longitude); + const title = s.name ? `${base} ${s.name}` : base; + let movement = ''; + if (Number.isFinite(s.movementSpeed)) { + movement = s.movementSpeed < 1 ? 'Nearly stationary' + : `Moving ${Number.isFinite(s.movementDir) ? compass16(s.movementDir) + ' ' : ''}at ${Math.round(s.movementSpeed)} mph`; + } + return { cls, kt: hasKt ? kt : null, mph, cat, label, base, title, badge, color, lat, lon, movement }; +} + function renderHurricanePanel(storms) { const panel = document.getElementById('hurricane-panel'); if (!panel) return; if (!storms || !storms.length) { panel.innerHTML = ''; return; } hurricaneStorms = storms; - const catLabel = c => { - if (!c) return ''; - const up = c.toUpperCase(); - if (up === 'HU') return 'cat-1'; - if (up === 'TS') return 'cat-ts'; - if (up === 'TD') return 'cat-td'; - const m = c.match(/(\d)/); - if (m) return `cat-${m[1]}`; - return 'cat-td'; - }; - const catName = c => { - if (!c) return 'Tropical'; - const up = c.toUpperCase(); - if (up === 'HU') return 'Hurricane'; - if (up === 'TS') return 'Tropical Storm'; - if (up === 'TD') return 'Tropical Depression'; - return c; - }; + const here = state.currentCity; panel.innerHTML = storms.map(s => { - const lat = parseNhcCoord(s.latitude); - const lon = parseNhcCoord(s.longitude); + const i = stormInfo(s); + let away = ''; + if (here && !isNaN(i.lat) && !isNaN(i.lon)) { + const mi = milesBetween(here.latitude, here.longitude, i.lat, i.lon); + const dir = compass16(bearingDeg(here.latitude, here.longitude, i.lat, i.lon)); + away = `≈ ${Math.round(state.units === 'us' ? mi : mi * 1.60934).toLocaleString()} ${state.units === 'us' ? 'mi' : 'km'} ${dir} of ${escHtml(here.name || 'you')}`; + } + const updated = s.lastUpdate ? new Date(s.lastUpdate).toLocaleString([], { weekday: 'short', hour: 'numeric', minute: '2-digit' }) : ''; + const adv = s.publicAdvisory; return ` -
Headline: ${AI.esc(p.headline)}
` : ''} + ${areaAll ? `Area: ${AI.esc(areaAll)}
` : ''} + ${p.description ? `Details:\n${AI.esc(p.description.trim())}
` : ''} + ${p.instruction ? `Instructions:\n${AI.esc(p.instruction.trim())}
` : ''} + ${meta.length ? `` : ''} +