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-06 22:14:56 +00:00
|
|
|
import { registerDisplay } 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
|
2022-11-22 22:19:10 +00:00
|
|
|
const radarHtml = await text(url, { cors: true });
|
2020-12-29 16:22:20 +00:00
|
|
|
return radarHtml;
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Unable to get list of radars');
|
|
|
|
console.error(e);
|
|
|
|
this.setStatus(STATUS.failed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}))).filter((d) => d);
|
|
|
|
|
2020-09-04 22:03:03 +00:00
|
|
|
// convert to an array of gif urls
|
2020-12-29 16:22:20 +00:00
|
|
|
const pngs = lists.map((html, htmlIdx) => {
|
|
|
|
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);
|
|
|
|
const anchors = xmlDoc.getElementsByTagName('a');
|
2020-12-29 21:26:58 +00:00
|
|
|
const urls = [];
|
|
|
|
Array.from(anchors).forEach((elem) => {
|
|
|
|
if (elem.innerHTML?.includes('.png') && elem.innerHTML?.includes('n0r_')) {
|
|
|
|
urls.push(elem.href);
|
2020-12-29 16:22:20 +00:00
|
|
|
}
|
2020-12-29 21:26:58 +00:00
|
|
|
});
|
|
|
|
return urls;
|
2020-12-29 16:22:20 +00:00
|
|
|
}).flat();
|
2020-09-04 22:03:03 +00:00
|
|
|
|
|
|
|
// get the last few images
|
2020-12-29 21:26:58 +00:00
|
|
|
const sortedPngs = pngs.sort((a, b) => (Date(a) < Date(b) ? -1 : 1));
|
|
|
|
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 {
|
2020-10-02 02:35:49 +00:00
|
|
|
time = DateTime.fromHTTP(response.headers.get('last-modified')).setZone();
|
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;
|
2022-11-14 20:32:51 +00:00
|
|
|
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);
|
2020-09-09 20:23:19 +00:00
|
|
|
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 : ` ${time}`;
|
|
|
|
this.elem.querySelector('.header .right .time').innerHTML = timePadded;
|
2020-09-04 22:03:03 +00:00
|
|
|
|
2022-08-05 21:23:22 +00:00
|
|
|
// scroll to image
|
|
|
|
this.elem.querySelector('.scroll-area').style.top = `${-this.screenIndex * 371}px`;
|
|
|
|
|
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-07 21:36:02 +00:00
|
|
|
registerDisplay(new Radar(9, 'radar'));
|