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

227 lines
6.9 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 { loadImg } from './utils/image.mjs';
import { text } from './utils/fetch.mjs';
import { rewriteUrl } from './utils/cors.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';
2020-09-04 17:03:03 -05:00
class Radar extends WeatherDisplay {
2020-10-29 16:44:28 -05:00
constructor(navId, elemId) {
2022-11-21 21:50:22 -06:00
super(navId, elemId, 'Local Radar', true);
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;
}
// get the base map
2025-05-14 14:50:02 -05:00
const src = 'images/maps/radar.jpg';
2022-11-22 16:19:10 -06:00
this.baseMap = await loadImg(src);
2020-09-04 17:03:03 -05:00
2020-12-29 10:22:20 -06:00
const baseUrl = 'https://mesonet.agron.iastate.edu/archive/data/';
2020-12-29 15:26:58 -06:00
const baseUrlEnd = '/GIS/uscomp/';
2020-12-29 10:22:20 -06:00
const baseUrls = [];
let date = DateTime.utc().minus({ days: 1 }).startOf('day');
2020-09-04 17:03:03 -05:00
2020-12-29 15:26:58 -06:00
// make urls for yesterday and today
while (date <= DateTime.utc().startOf('day')) {
2020-12-29 10:22:20 -06:00
baseUrls.push(`${baseUrl}${date.toFormat('yyyy/LL/dd')}${baseUrlEnd}`);
date = date.plus({ days: 1 });
2020-09-04 17:03:03 -05:00
}
2020-12-29 10:22:20 -06:00
const lists = (await Promise.all(baseUrls.map(async (url) => {
try {
2025-04-02 11:10:58 -05:00
// get a list of available radars
2023-01-06 14:39:39 -06:00
return text(url, { cors: true });
} catch (error) {
2020-12-29 10:22:20 -06:00
console.log('Unable to get list of radars');
2023-01-06 14:39:39 -06:00
console.error(error);
2020-12-29 10:22:20 -06:00
this.setStatus(STATUS.failed);
return false;
}
}))).filter((d) => d);
2020-09-04 17:03:03 -05:00
// convert to an array of gif 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
const base = xmlDoc.createElement('base');
base.href = baseUrls[htmlIdx];
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
let offsetX = 120;
let offsetY = 69;
2020-12-29 15:26:58 -06:00
const width = 2550;
const height = 1600;
offsetX *= 2;
offsetY *= 2;
2025-04-02 20:58:53 -05:00
const sourceXY = utils.getXYFromLatitudeLongitudeMap(this.weatherParameters, offsetX, offsetY);
2020-09-04 17:03:03 -05:00
// calculate radar offsets
2020-12-29 15:26:58 -06:00
const radarOffsetX = 120;
const radarOffsetY = 70;
2025-04-02 20:58:53 -05:00
const radarSourceXY = utils.getXYFromLatitudeLongitudeDoppler(this.weatherParameters, offsetX, offsetY);
2020-12-29 15:26:58 -06:00
const radarSourceX = radarSourceXY.x / 2;
const radarSourceY = radarSourceXY.y / 2;
2020-09-04 17:03:03 -05:00
// Load the most recent doppler radar images.
2020-09-08 20:07:09 -05:00
const radarInfo = await Promise.all(urls.map(async (url) => {
2020-09-04 17:03:03 -05:00
// create destination context
const canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 367;
const context = canvas.getContext('2d');
context.imageSmoothingEnabled = false;
2025-04-02 20:58:53 -05:00
// create working context for manipulation
const workingCanvas = document.createElement('canvas');
workingCanvas.width = width;
workingCanvas.height = height;
const workingContext = workingCanvas.getContext('2d');
workingContext.imageSmoothingEnabled = false;
2020-09-04 17:03:03 -05:00
// get the image
2022-11-22 16:19:10 -06:00
const response = await fetch(rewriteUrl(url));
2020-10-01 21:35:49 -05:00
// test response
if (!response.ok) throw new Error(`Unable to fetch radar error ${response.status} ${response.statusText} from ${response.url}`);
// get the blob
const blob = await response.blob();
2020-09-08 20:07:09 -05:00
// store the time
2020-12-29 15:26:58 -06:00
const timeMatch = url.match(/_(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)\./);
2020-09-08 20:07:09 -05:00
let time;
if (timeMatch) {
const [, year, month, day, hour, minute] = timeMatch;
time = DateTime.fromObject({
year,
month,
day,
hour,
minute,
2022-03-01 15:54:19 -06:00
}, {
2020-09-08 20:07:09 -05:00
zone: 'UTC',
2024-10-09 21:29:12 -05:00
}).setZone(timeZone());
2020-09-08 20:07:09 -05:00
} else {
2022-12-21 15:17:50 -06:00
time = DateTime.fromHTTP(response.headers.get('last-modified')).setZone(timeZone());
2020-09-08 20:07:09 -05:00
}
2020-09-04 17:03:03 -05:00
// assign to an html image element
2022-11-22 16:19:10 -06:00
const imgBlob = await loadImg(blob);
2020-09-04 17:03:03 -05:00
// draw the entire image
2020-12-29 15:26:58 -06:00
workingContext.clearRect(0, 0, width, 1600);
workingContext.drawImage(imgBlob, 0, 0, width, 1600);
2020-09-04 17:03:03 -05:00
// get the base map
2025-04-02 20:58:53 -05:00
context.drawImage(this.baseMap, sourceXY.x, sourceXY.y, offsetX * 2, offsetY * 2, 0, 0, 640, 367);
2020-09-04 17:03:03 -05:00
2020-09-07 22:06:44 -05:00
// crop the radar image
const cropCanvas = document.createElement('canvas');
cropCanvas.width = 640;
cropCanvas.height = 367;
const cropContext = cropCanvas.getContext('2d', { willReadFrequently: true });
2020-09-07 22:06:44 -05:00
cropContext.imageSmoothingEnabled = false;
cropContext.drawImage(workingCanvas, radarSourceX, radarSourceY, (radarOffsetX * 2), (radarOffsetY * 2.33), 0, 0, 640, 367);
2020-09-08 10:05:46 -05:00
// clean the image
2022-12-09 13:51:51 -06:00
utils.removeDopplerRadarImageNoise(cropContext);
2020-09-07 22:06:44 -05:00
// merge the radar and map
2022-12-09 13:51:51 -06:00
utils.mergeDopplerRadarImage(context, cropContext);
2020-09-04 17:03:03 -05:00
2022-08-05 16:23:22 -05:00
const elem = this.fillTemplate('frame', { map: { type: 'img', src: canvas.toDataURL() } });
2020-09-08 20:07:09 -05:00
return {
canvas,
time,
2022-08-05 16:23:22 -05:00
elem,
2020-09-08 20:07:09 -05:00
};
2020-09-04 17:03:03 -05:00
}));
2022-08-05 16:23:22 -05:00
// put the elements in the container
const scrollArea = this.elem.querySelector('.scroll-area');
scrollArea.innerHTML = '';
scrollArea.append(...radarInfo.map((r) => r.elem));
2020-09-05 20:01:13 -05:00
// set max length
2020-09-08 20:07:09 -05:00
this.timing.totalScreens = radarInfo.length;
2020-09-05 20:01:13 -05:00
// store the images
2020-10-29 16:44:28 -05:00
this.data = radarInfo.map((radar) => radar.canvas);
2020-09-09 14:29:03 -05:00
2020-10-29 16:44:28 -05:00
this.times = radarInfo.map((radar) => radar.time);
this.setStatus(STATUS.loaded);
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
2022-12-14 16:28:33 -06:00
registerDisplay(new Radar(10, 'radar'));