ws4kp/server/scripts/modules/radar.mjs

230 lines
7 KiB
JavaScript
Raw Normal View History

2020-09-04 22:03:03 +00:00
// current weather conditions display
2022-11-22 22:19:10 +00: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 22:29:10 +00:00
import WeatherDisplay from './weatherdisplay.mjs';
2022-12-21 21:17:50 +00:00
import { registerDisplay, timeZone } from './navigation.mjs';
2022-12-09 19:51:51 +00:00
import * as utils from './radar-utils.mjs';
2020-09-04 22:03:03 +00:00
class Radar extends WeatherDisplay {
2020-10-29 21:44:28 +00:00
constructor(navId, elemId) {
2022-11-22 03:50:22 +00:00
super(navId, elemId, 'Local Radar', true);
2020-09-04 22:03:03 +00:00
2022-12-06 22:14:56 +00:00
this.okToDrawCurrentConditions = false;
this.okToDrawCurrentDateTime = false;
2020-09-04 22:03:03 +00:00
// set max images
this.dopplerRadarImageMax = 6;
2020-09-06 01:01:13 +00:00
// update timing
this.timing.baseDelay = 350;
2020-09-09 19:29:03 +00:00
this.timing.delay = [
2020-10-29 21:44:28 +00: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 19:29:03 +00:00
];
2020-09-04 22:03:03 +00:00
}
2022-03-01 22:19:08 +00:00
async getData(_weatherParameters) {
2022-12-07 17:02:51 +00:00
if (!super.getData(_weatherParameters)) return;
2022-03-01 22:19:08 +00:00
const weatherParameters = _weatherParameters ?? this.weatherParameters;
2020-09-04 22:03:03 +00:00
2020-12-29 16:22:20 +00:00
// ALASKA AND HAWAII AREN'T SUPPORTED!
if (weatherParameters.state === 'AK' || weatherParameters.state === 'HI') {
2020-09-04 22:03:03 +00:00
this.setStatus(STATUS.noData);
return;
}
// get the base map
let src = 'images/4000RadarMap2.jpg';
2020-12-29 16:22:20 +00:00
if (weatherParameters.State === 'HI') src = 'images/HawaiiRadarMap2.png';
2022-11-22 22:19:10 +00:00
this.baseMap = await loadImg(src);
2020-09-04 22:03:03 +00:00
2020-12-29 16:22:20 +00:00
const baseUrl = 'https://mesonet.agron.iastate.edu/archive/data/';
2020-12-29 21:26:58 +00:00
const baseUrlEnd = '/GIS/uscomp/';
2020-12-29 16:22:20 +00:00
const baseUrls = [];
let date = DateTime.utc().minus({ days: 1 }).startOf('day');
2020-09-04 22:03:03 +00:00
2020-12-29 21:26:58 +00:00
// make urls for yesterday and today
while (date <= DateTime.utc().startOf('day')) {
2020-12-29 16:22:20 +00:00
baseUrls.push(`${baseUrl}${date.toFormat('yyyy/LL/dd')}${baseUrlEnd}`);
date = date.plus({ days: 1 });
2020-09-04 22:03:03 +00:00
}
2020-12-29 16:22:20 +00:00
const lists = (await Promise.all(baseUrls.map(async (url) => {
try {
// get a list of available radars
2023-01-06 20:39:39 +00:00
return text(url, { cors: true });
} catch (error) {
2020-12-29 16:22:20 +00:00
console.log('Unable to get list of radars');
2023-01-06 20:39:39 +00:00
console.error(error);
2020-12-29 16:22:20 +00:00
this.setStatus(STATUS.failed);
return false;
}
}))).filter((d) => d);
2020-09-04 22:03:03 +00:00
// convert to an array of gif urls
2023-01-06 20:39:39 +00:00
const pngs = lists.flatMap((html, htmlIdx) => {
2020-12-29 16:22:20 +00: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 20:39:39 +00:00
const anchors = xmlDoc.querySelectorAll('a');
2020-12-29 21:26:58 +00:00
const urls = [];
Array.from(anchors).forEach((elem) => {
2023-06-01 04:11:12 +00:00
if (elem.innerHTML?.match(/n0r_\d{12}\.png/)) {
2020-12-29 21:26:58 +00:00
urls.push(elem.href);
2020-12-29 16:22:20 +00:00
}
2020-12-29 21:26:58 +00:00
});
return urls;
2023-01-06 20:39:39 +00:00
});
2020-09-04 22:03:03 +00:00
// get the last few images
2023-06-01 04:11:12 +00: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 21:26:58 +00:00
const urls = sortedPngs.slice(-(this.dopplerRadarImageMax));
2020-09-04 22:03:03 +00:00
// calculate offsets and sizes
let offsetX = 120;
let offsetY = 69;
2020-12-29 21:26:58 +00:00
const width = 2550;
const height = 1600;
offsetX *= 2;
offsetY *= 2;
2022-12-09 19:51:51 +00:00
const sourceXY = utils.getXYFromLatitudeLongitudeMap(weatherParameters, offsetX, offsetY);
2020-09-04 22:03:03 +00: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;
// calculate radar offsets
2020-12-29 21:26:58 +00:00
const radarOffsetX = 120;
const radarOffsetY = 70;
2022-12-09 19:51:51 +00:00
const radarSourceXY = utils.getXYFromLatitudeLongitudeDoppler(weatherParameters, offsetX, offsetY);
2020-12-29 21:26:58 +00:00
const radarSourceX = radarSourceXY.x / 2;
const radarSourceY = radarSourceXY.y / 2;
2020-09-04 22:03:03 +00:00
// Load the most recent doppler radar images.
2020-09-09 01:07:09 +00:00
const radarInfo = await Promise.all(urls.map(async (url) => {
2020-09-04 22:03:03 +00:00
// create destination context
const canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 367;
const context = canvas.getContext('2d');
context.imageSmoothingEnabled = false;
// get the image
2022-11-22 22:19:10 +00:00
const response = await fetch(rewriteUrl(url));
2020-10-02 02:35:49 +00: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-09 01:07:09 +00:00
// store the time
2020-12-29 21:26:58 +00:00
const timeMatch = url.match(/_(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)\./);
2020-09-09 01:07:09 +00:00
let time;
if (timeMatch) {
const [, year, month, day, hour, minute] = timeMatch;
time = DateTime.fromObject({
year,
month,
day,
hour,
minute,
2022-03-01 21:54:19 +00:00
}, {
2020-09-09 01:07:09 +00:00
zone: 'UTC',
2022-03-01 22:19:08 +00:00
}).setZone();
2020-09-09 01:07:09 +00:00
} else {
2022-12-21 21:17:50 +00:00
time = DateTime.fromHTTP(response.headers.get('last-modified')).setZone(timeZone());
2020-09-09 01:07:09 +00:00
}
2020-09-04 22:03:03 +00:00
// assign to an html image element
2022-11-22 22:19:10 +00:00
const imgBlob = await loadImg(blob);
2020-09-04 22:03:03 +00:00
// draw the entire image
2020-12-29 21:26:58 +00:00
workingContext.clearRect(0, 0, width, 1600);
workingContext.drawImage(imgBlob, 0, 0, width, 1600);
2020-09-04 22:03:03 +00:00
// get the base map
2020-10-29 21:44:28 +00:00
context.drawImage(await this.baseMap, sourceXY.x, sourceXY.y, offsetX * 2, offsetY * 2, 0, 0, 640, 367);
2020-09-04 22:03:03 +00:00
2020-09-08 03:06:44 +00: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-08 03:06:44 +00:00
cropContext.imageSmoothingEnabled = false;
cropContext.drawImage(workingCanvas, radarSourceX, radarSourceY, (radarOffsetX * 2), (radarOffsetY * 2.33), 0, 0, 640, 367);
2020-09-08 15:05:46 +00:00
// clean the image
2022-12-09 19:51:51 +00:00
utils.removeDopplerRadarImageNoise(cropContext);
2020-09-08 03:06:44 +00:00
// merge the radar and map
2022-12-09 19:51:51 +00:00
utils.mergeDopplerRadarImage(context, cropContext);
2020-09-04 22:03:03 +00:00
2022-08-05 21:23:22 +00:00
const elem = this.fillTemplate('frame', { map: { type: 'img', src: canvas.toDataURL() } });
2020-09-09 01:07:09 +00:00
return {
canvas,
time,
2022-08-05 21:23:22 +00:00
elem,
2020-09-09 01:07:09 +00:00
};
2020-09-04 22:03:03 +00:00
}));
2022-08-05 21:23:22 +00: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-06 01:01:13 +00:00
// set max length
2020-09-09 01:07:09 +00:00
this.timing.totalScreens = radarInfo.length;
2020-09-06 01:01:13 +00:00
// store the images
2020-10-29 21:44:28 +00:00
this.data = radarInfo.map((radar) => radar.canvas);
2020-09-09 19:29:03 +00:00
2020-10-29 21:44:28 +00:00
this.times = radarInfo.map((radar) => radar.time);
this.setStatus(STATUS.loaded);
2020-09-04 22:03:03 +00:00
}
2020-09-06 01:01:13 +00:00
async drawCanvas() {
2020-09-04 22:03:03 +00:00
super.drawCanvas();
2022-09-05 15:52:30 +00: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 22:03:03 +00:00
2023-01-10 20:12:22 +00:00
// get image offset calculation
// is slides slightly because of scaling so we have to take a measurement from the rendered page
2023-08-15 02:31:58 +00:00
const actualFrameHeight = this.elem.querySelector('.frame').scrollHeight;
2023-01-10 20:12:22 +00:00
2022-08-05 21:23:22 +00:00
// scroll to image
2023-01-10 20:12:22 +00:00
this.elem.querySelector('.scroll-area').style.top = `${-this.screenIndex * actualFrameHeight}px`;
2022-08-05 21:23:22 +00:00
2020-09-04 22:03:03 +00:00
this.finishDraw();
}
2020-10-29 21:44:28 +00:00
}
2022-11-22 22:19:10 +00:00
2022-12-06 22:14:56 +00:00
// register display
2022-12-14 22:28:33 +00:00
registerDisplay(new Radar(10, 'radar'));