ws4kp-linhanced/server/scripts/modules/radar.mjs

205 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-09-04 17:03:03 -05:00
// current weather conditions display
2022-11-22 16:19:10 -06:00
import STATUS from './status.mjs';
import { DateTime } from '../vendor/auto/luxon.mjs';
import { safeText } from './utils/fetch.mjs';
2022-11-22 16:29:10 -06:00
import WeatherDisplay from './weatherdisplay.mjs';
2022-12-21 15:17:50 -06:00
import { registerDisplay, timeZone } from './navigation.mjs';
2022-12-09 13:51:51 -06:00
import * as utils from './radar-utils.mjs';
2025-06-16 15:30:56 -05:00
import setTiles from './radar-tiles.mjs';
import processRadar from './radar-processor.mjs';
// Use OVERRIDE_RADAR_HOST if provided, otherwise default to mesonet
const RADAR_HOST = (typeof OVERRIDES !== 'undefined' ? OVERRIDES?.RADAR_HOST : undefined) || 'mesonet.agron.iastate.edu';
2020-09-04 17:03:03 -05:00
class Radar extends WeatherDisplay {
2020-10-29 16:44:28 -05:00
constructor(navId, elemId) {
super(navId, elemId, 'Local Radar');
2020-09-04 17:03:03 -05:00
2022-12-06 16:14:56 -06:00
this.okToDrawCurrentConditions = false;
this.okToDrawCurrentDateTime = false;
2020-09-04 17:03:03 -05:00
// set max images
this.dopplerRadarImageMax = 6;
2020-09-05 20:01:13 -05:00
// update timing
this.timing.baseDelay = 350;
2020-09-09 14:29:03 -05:00
this.timing.delay = [
2020-10-29 16:44:28 -05:00
{ time: 4, si: 5 },
{ time: 1, si: 0 },
{ time: 1, si: 1 },
{ time: 1, si: 2 },
{ time: 1, si: 3 },
{ time: 1, si: 4 },
{ time: 4, si: 5 },
{ time: 1, si: 0 },
{ time: 1, si: 1 },
{ time: 1, si: 2 },
{ time: 1, si: 3 },
{ time: 1, si: 4 },
{ time: 4, si: 5 },
{ time: 1, si: 0 },
{ time: 1, si: 1 },
{ time: 1, si: 2 },
{ time: 1, si: 3 },
{ time: 1, si: 4 },
{ time: 12, si: 5 },
2020-09-09 14:29:03 -05:00
];
2020-09-04 17:03:03 -05:00
}
2025-04-02 20:58:53 -05:00
async getData(weatherParameters, refresh) {
if (!super.getData(weatherParameters, refresh)) return;
2020-09-04 17:03:03 -05:00
2020-12-29 10:22:20 -06:00
// ALASKA AND HAWAII AREN'T SUPPORTED!
2025-04-02 20:58:53 -05:00
if (this.weatherParameters.state === 'AK' || this.weatherParameters.state === 'HI') {
2020-09-04 17:03:03 -05:00
this.setStatus(STATUS.noData);
return;
}
2025-05-23 22:14:48 -05:00
const baseUrl = `https://${RADAR_HOST}/archive/data/`;
const baseUrlEnd = '/GIS/uscomp/?F=0&P=n0r*.png'; // This URL returns an index of .png files for the given date
// Always get today's data
const today = DateTime.utc().startOf('day');
const todayStr = today.toFormat('yyyy/LL/dd');
const yesterday = today.minus({ days: 1 });
const yesterdayStr = yesterday.toFormat('yyyy/LL/dd');
const todayUrl = `${baseUrl}${todayStr}${baseUrlEnd}`;
// Get today's data, then we'll see if we need yesterday's
const todayList = await safeText(todayUrl);
// Count available images from today
let todayImageCount = 0;
if (todayList) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(todayList, 'text/html');
const anchors = xmlDoc.querySelectorAll('a');
todayImageCount = Array.from(anchors).filter((elem) => elem.innerHTML?.match(/n0r_\d{12}\.png/)).length;
2020-09-04 17:03:03 -05:00
}
// Only fetch yesterday's data if we don't have enough images from today
// or if it's very early in the day when recent images might still be from yesterday
const currentTimeUTC = DateTime.utc();
const minutesSinceMidnight = currentTimeUTC.hour * 60 + currentTimeUTC.minute;
const requiredTimeWindow = this.dopplerRadarImageMax * 5; // 5 minutes per image
const needYesterday = todayImageCount < this.dopplerRadarImageMax || minutesSinceMidnight < requiredTimeWindow;
// Build the final lists array
const lists = [];
if (needYesterday) {
const yesterdayUrl = `${baseUrl}${yesterdayStr}${baseUrlEnd}`;
const yesterdayList = await safeText(yesterdayUrl);
if (yesterdayList) {
lists.push(yesterdayList); // Add yesterday's data first
2020-12-29 10:22:20 -06:00
}
}
if (todayList) {
lists.push(todayList); // Add today's data
}
2020-12-29 10:22:20 -06:00
// convert to an array of png urls
2023-01-06 14:39:39 -06:00
const pngs = lists.flatMap((html, htmlIdx) => {
2020-12-29 10:22:20 -06:00
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(html, 'text/html');
// add the base url - reconstruct the URL for each list
2020-12-29 10:22:20 -06:00
const base = xmlDoc.createElement('base');
if (htmlIdx === 0 && needYesterday) {
// First item is yesterday's data when we fetched it
base.href = `${baseUrl}${yesterdayStr}${baseUrlEnd}`;
} else {
// This is today's data (or the only data if yesterday wasn't fetched)
base.href = `${baseUrl}${todayStr}${baseUrlEnd}`;
}
2020-12-29 10:22:20 -06:00
xmlDoc.head.append(base);
2023-01-06 14:39:39 -06:00
const anchors = xmlDoc.querySelectorAll('a');
2020-12-29 15:26:58 -06:00
const urls = [];
Array.from(anchors).forEach((elem) => {
2025-04-02 11:10:58 -05:00
if (elem.innerHTML?.match(/n0r_\d{12}\.png/)) {
2020-12-29 15:26:58 -06:00
urls.push(elem.href);
2020-12-29 10:22:20 -06:00
}
2020-12-29 15:26:58 -06:00
});
return urls;
2023-01-06 14:39:39 -06:00
});
2020-09-04 17:03:03 -05:00
// get the last few images
2023-05-31 23:11:12 -05:00
const timestampRegex = /_(\d{12})\.png/;
const sortedPngs = pngs.sort((a, b) => (a.match(timestampRegex)[1] < b.match(timestampRegex)[1] ? -1 : 1));
2020-12-29 15:26:58 -06:00
const urls = sortedPngs.slice(-(this.dopplerRadarImageMax));
2020-09-04 17:03:03 -05:00
// calculate offsets and sizes
2025-05-29 08:30:01 -05:00
const offsetX = 120 * 2;
const offsetY = 69 * 2;
2025-06-16 15:30:56 -05:00
const sourceXY = utils.getXYFromLatitudeLongitudeMap(this.weatherParameters);
2025-04-02 20:58:53 -05:00
const radarSourceXY = utils.getXYFromLatitudeLongitudeDoppler(this.weatherParameters, offsetX, offsetY);
2020-09-04 17:03:03 -05:00
2025-06-16 15:30:56 -05:00
// set up the base map and overlay tiles
setTiles({
sourceXY,
2025-06-16 15:30:56 -05:00
elemId: this.elemId,
});
2020-09-04 17:03:03 -05:00
// Load the most recent doppler radar images.
try {
const radarInfo = await Promise.all(urls.map(async (url) => {
const processedRadar = await processRadar({
url,
RADAR_HOST,
OVERRIDES,
radarSourceXY,
});
// store the time
const timeMatch = url.match(/_(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)\./);
const [, year, month, day, hour, minute] = timeMatch;
const time = DateTime.fromObject({
year,
month,
day,
hour,
minute,
}, {
zone: 'UTC',
}).setZone(timeZone());
const elem = this.fillTemplate('frame', { map: { type: 'img', src: processedRadar } });
return {
time,
elem,
};
}));
// put the elements in the container
const scrollArea = this.elem.querySelector('.scroll-area');
scrollArea.innerHTML = '';
scrollArea.append(...radarInfo.map((r) => r.elem));
// set max length
this.timing.totalScreens = radarInfo.length;
this.times = radarInfo.map((radar) => radar.time);
this.setStatus(STATUS.loaded);
} catch (_error) {
// Radar fetch failed - skip this display in animation by setting totalScreens = 0
this.timing.totalScreens = 0;
if (this.isEnabled) this.setStatus(STATUS.failed);
}
2020-09-04 17:03:03 -05:00
}
2020-09-05 20:01:13 -05:00
async drawCanvas() {
2020-09-04 17:03:03 -05:00
super.drawCanvas();
2022-09-05 10:52:30 -05:00
const time = this.times[this.screenIndex].toLocaleString(DateTime.TIME_SIMPLE);
const timePadded = time.length >= 8 ? time : `&nbsp;${time}`;
this.elem.querySelector('.header .right .time').innerHTML = timePadded;
2020-09-04 17:03:03 -05:00
2023-01-10 14:12:22 -06:00
// get image offset calculation
// is slides slightly because of scaling so we have to take a measurement from the rendered page
2023-08-14 21:31:58 -05:00
const actualFrameHeight = this.elem.querySelector('.frame').scrollHeight;
2023-01-10 14:12:22 -06:00
2022-08-05 16:23:22 -05:00
// scroll to image
2023-01-10 14:12:22 -06:00
this.elem.querySelector('.scroll-area').style.top = `${-this.screenIndex * actualFrameHeight}px`;
2022-08-05 16:23:22 -05:00
2020-09-04 17:03:03 -05:00
this.finishDraw();
}
2020-10-29 16:44:28 -05:00
}
2022-11-22 16:19:10 -06:00
2022-12-06 16:14:56 -06:00
// register display
registerDisplay(new Radar(11, 'radar'));