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

204 lines
8.3 KiB
JavaScript
Raw Normal View History

2020-09-04 13:02:20 -05:00
// current weather conditions display
2022-11-22 16:19:10 -06:00
import { distance as calcDistance, directionToNSEW } from './utils/calc.mjs';
import { safeJson, safePromiseAll } from './utils/fetch.mjs';
2022-11-22 16:19:10 -06:00
import STATUS from './status.mjs';
import { locationCleanup } from './utils/string.mjs';
2025-02-23 23:29:39 -06:00
import { temperature, windSpeed } from './utils/units.mjs';
2022-11-22 16:29:10 -06:00
import WeatherDisplay from './weatherdisplay.mjs';
2022-12-06 16:14:56 -06:00
import { registerDisplay } from './navigation.mjs';
import augmentObservationWithMetar from './utils/metar.mjs';
2025-02-23 23:29:39 -06:00
import settings from './settings.mjs';
import { debugFlag } from './utils/debug.mjs';
2020-09-04 13:02:20 -05:00
class LatestObservations extends WeatherDisplay {
2020-10-29 16:44:28 -05:00
constructor(navId, elemId) {
2022-11-21 21:50:22 -06:00
super(navId, elemId, 'Latest Observations', true);
2020-09-04 13:02:20 -05:00
// constants
this.MaximumRegionalStations = 7;
}
2025-04-02 22:10:59 -05:00
async getData(weatherParameters, refresh) {
if (!super.getData(weatherParameters, refresh)) return;
// latest observations does a silent refresh but will not fall back to previously fetched data
// this is intentional because up to 30 stations are available to pull data from
2020-09-25 09:55:29 -05:00
2020-09-04 13:02:20 -05:00
// calculate distance to each station
2025-05-29 08:30:01 -05:00
const stationsByDistance = Object.values(StationInfo).map((station) => {
2025-04-02 22:10:59 -05:00
const distance = calcDistance(station.lat, station.lon, this.weatherParameters.latitude, this.weatherParameters.longitude);
2020-10-29 16:44:28 -05:00
return { ...station, distance };
2020-09-04 13:02:20 -05:00
});
// sort the stations by distance
2020-10-29 16:44:28 -05:00
const sortedStations = stationsByDistance.sort((a, b) => a.distance - b.distance);
2020-09-04 13:02:20 -05:00
// try up to 30 regional stations
2020-10-29 16:44:28 -05:00
const regionalStations = sortedStations.slice(0, 30);
2020-09-04 13:02:20 -05:00
// Fetch stations sequentially in batches to avoid unnecessary API calls.
// We start with the 7 closest stations and only fetch more if some fail,
// stopping as soon as we have 7 valid stations with data.
const actualConditions = [];
let lastStation = Math.min(regionalStations.length, 7);
let firstStation = 0;
while (actualConditions.length < 7 && (lastStation) <= regionalStations.length) {
// Sequential fetching is intentional here - we want to try closest stations first
// and only fetch additional batches if needed, rather than hitting all 30 stations at once
// eslint-disable-next-line no-await-in-loop
const someStations = await this.getStations(regionalStations.slice(firstStation, lastStation));
actualConditions.push(...someStations);
// update counters
firstStation += lastStation;
lastStation = Math.min(regionalStations.length + 1, firstStation + 7 - actualConditions.length);
}
2020-09-04 13:02:20 -05:00
// cut down to the maximum of 7
2020-10-29 16:44:28 -05:00
this.data = actualConditions.slice(0, this.MaximumRegionalStations);
2020-09-04 13:02:20 -05:00
// test for at least one station
2023-01-06 14:39:39 -06:00
if (this.data.length === 0) {
this.setStatus(STATUS.noData);
return;
}
2020-09-17 16:34:38 -05:00
this.setStatus(STATUS.loaded);
2020-09-04 13:02:20 -05:00
}
// This is a class method because it needs access to the instance's `stillWaiting` method
async getStations(stations) {
// Use centralized safe Promise handling to avoid unhandled AbortError rejections
const stationData = await safePromiseAll(stations.map(async (station) => {
try {
const data = await safeJson(`https://api.weather.gov/stations/${station.id}/observations/latest`, {
retryCount: 1,
stillWaiting: () => this.stillWaiting(),
});
if (!data) {
if (debugFlag('verbose-failures')) {
console.log(`Failed to get Latest Observations for station ${station.id}`);
}
return false;
}
// Check if the observation data is old
const observationTime = new Date(data.properties.timestamp);
const ageInMinutes = (new Date() - observationTime) / (1000 * 60);
if (ageInMinutes > 180 && debugFlag('latestobservations')) {
console.warn(`Latest Observations for station ${station.id} are ${ageInMinutes.toFixed(0)} minutes old (from ${observationTime.toISOString()})`);
}
// Enhance observation data with METAR parsing for missing fields
const originalData = { ...data.properties };
data.properties = augmentObservationWithMetar(data.properties);
const metarFields = [
{ name: 'temperature', check: (orig, metar) => orig.temperature.value === null && metar.temperature.value !== null },
{ name: 'windSpeed', check: (orig, metar) => orig.windSpeed.value === null && metar.windSpeed.value !== null },
{ name: 'windDirection', check: (orig, metar) => orig.windDirection.value === null && metar.windDirection.value !== null },
];
const augmentedData = data.properties;
const metarReplacements = metarFields.filter((field) => field.check(originalData, augmentedData)).map((field) => field.name);
if (debugFlag('latestobservations') && metarReplacements.length > 0) {
console.log(`Latest Observations for station ${station.id} were augmented with METAR data for ${metarReplacements.join(', ')}`);
}
// test data quality
const requiredFields = [
{ name: 'temperature', check: (props) => props.temperature?.value === null },
{ name: 'windSpeed', check: (props) => props.windSpeed?.value === null },
{ name: 'windDirection', check: (props) => props.windDirection?.value === null },
{ name: 'textDescription', check: (props) => props.textDescription === null || props.textDescription === '' },
];
const missingFields = requiredFields.filter((field) => field.check(augmentedData)).map((field) => field.name);
if (missingFields.length > 0) {
if (debugFlag('latestobservations')) {
console.log(`Latest Observations for station ${station.id} are missing required fields: ${missingFields.join(', ')}`);
}
return false;
}
// format the return values
return {
...data.properties,
StationId: station.id,
city: station.city,
};
} catch (error) {
console.error(`Unexpected error getting latest observations for station ${station.id}: ${error.message}`);
return false;
}
}));
// filter false (no data or other error)
return stationData.filter((d) => d);
}
2020-09-04 13:02:20 -05:00
async drawCanvas() {
super.drawCanvas();
const conditions = this.data;
// sort array by station name
2020-10-29 16:44:28 -05:00
const sortedConditions = conditions.sort((a, b) => ((a.Name < b.Name) ? -1 : 1));
2020-09-04 13:02:20 -05:00
2025-02-23 23:29:39 -06:00
if (settings.units.value === 'us') {
this.elem.querySelector('.column-headers .temp.english').classList.add('show');
this.elem.querySelector('.column-headers .temp.metric').classList.remove('show');
} else {
this.elem.querySelector('.column-headers .temp.english').classList.remove('show');
this.elem.querySelector('.column-headers .temp.metric').classList.add('show');
}
// get unit converters
const windConverter = windSpeed();
const temperatureConverter = temperature();
2020-09-04 13:02:20 -05:00
2022-08-04 12:49:04 -05:00
const lines = sortedConditions.map((condition) => {
2022-11-22 16:19:10 -06:00
const windDirection = directionToNSEW(condition.windDirection.value);
2020-09-04 13:02:20 -05:00
2025-02-23 23:29:39 -06:00
const Temperature = temperatureConverter(condition.temperature.value);
const WindSpeed = windConverter(condition.windSpeed.value);
2020-09-04 13:02:20 -05:00
2023-01-06 14:39:39 -06:00
const fill = {
location: locationCleanup(condition.city).substr(0, 14),
temp: Temperature,
weather: shortenCurrentConditions(condition.textDescription).substr(0, 9),
};
2020-09-04 13:02:20 -05:00
if (WindSpeed > 0) {
2022-08-04 12:49:04 -05:00
fill.wind = windDirection + (Array(6 - windDirection.length - WindSpeed.toString().length).join(' ')) + WindSpeed.toString();
2020-09-04 13:02:20 -05:00
} else if (WindSpeed === 'NA') {
2022-08-04 12:49:04 -05:00
fill.wind = 'NA';
2020-09-04 13:02:20 -05:00
} else {
2022-08-04 12:49:04 -05:00
fill.wind = 'Calm';
2020-09-04 13:02:20 -05:00
}
2022-08-04 12:49:04 -05:00
return this.fillTemplate('observation-row', fill);
2020-09-04 13:02:20 -05:00
});
2022-08-04 12:49:04 -05:00
const linesContainer = this.elem.querySelector('.observation-lines');
linesContainer.innerHTML = '';
linesContainer.append(...lines);
2020-09-04 13:02:20 -05:00
this.finishDraw();
}
2020-10-29 16:44:28 -05:00
}
2022-12-09 13:51:51 -06:00
const shortenCurrentConditions = (_condition) => {
let condition = _condition;
condition = condition.replace(/Light/, 'L');
condition = condition.replace(/Heavy/, 'H');
condition = condition.replace(/Partly/, 'P');
condition = condition.replace(/Mostly/, 'M');
condition = condition.replace(/Few/, 'F');
condition = condition.replace(/Thunderstorm/, 'T\'storm');
condition = condition.replace(/ in /, '');
condition = condition.replace(/Vicinity/, '');
condition = condition.replace(/ and /, ' ');
condition = condition.replace(/Freezing Rain/, 'Frz Rn');
condition = condition.replace(/Freezing/, 'Frz');
condition = condition.replace(/Unknown Precip/, '');
condition = condition.replace(/L Snow Fog/, 'L Snw/Fog');
condition = condition.replace(/ with /, '/');
return condition;
};
2022-12-06 16:14:56 -06:00
// register display
2022-12-14 16:28:33 -06:00
registerDisplay(new LatestObservations(2, 'latest-observations'));