Files
ws4kp/server/scripts/modules/regionalforecast-utils.mjs
T

118 lines
4.2 KiB
JavaScript

import smallIcon from './icons/small.mjs';
import preloadImg from './utils/preload-image.mjs';
import { safeJson } from './utils/fetch.mjs';
import { temperature as temperatureUnit } from './utils/units.mjs';
import augmentObservationWithMetar from './utils/metar.mjs';
import { debugFlag } from './utils/debug.mjs';
import { enhanceObservationWithMapClick } from './utils/mapclick.mjs';
const buildForecast = (forecast, city) => {
// get a unit converter
const temperatureConverter = temperatureUnit('us');
const icon = smallIcon(forecast.icon, !forecast.isDaytime);
preloadImg(icon);
return {
daytime: forecast.isDaytime,
temperature: temperatureConverter(forecast.temperature || 0),
name: formatCity(city.city),
icon,
x: city.pxy[0],
y: city.pxy[1],
time: forecast.startTime,
coercedRight: city.coercedRight,
};
};
// fallback method to get a station ID for a provided city
// temporary while stations.json expires for users
const getStationId = async (city) => {
// get stations using centralized safe handling
const { point } = city;
const stations = await safeJson(`https://api.weather.gov/gridpoints/${point.wfo}/${point.x},${point.y}/stations?limit=10`);
if (!stations || !stations.features || stations.features.length === 0) {
if (debugFlag('verbose-failures')) {
console.warn(`Unable to get regional stations for ${city.city}`);
}
return false;
}
// get the first station with a 4-letter id (generally has appropriate data)
const station4Letter = stations.features.find((station) => {
if (station?.properties?.stationIdentifier?.length === 4) return station.properties;
return false;
});
if (!station4Letter) return false;
const stationId = station4Letter.properties.stationIdentifier;
return stationId;
};
const getRegionalObservation = async (city) => {
const stationId = city?.id ?? await getStationId(city);
if (!stationId) return false;
try {
// get the observation data using centralized safe handling
const observation = await safeJson(`https://api.weather.gov/stations/${stationId}/observations/latest`, { retryCount: 1, timeout: 5000 });
if (!observation) {
if (debugFlag('verbose-failures')) {
console.warn(`Unable to get regional observations for station ${stationId}`);
}
return false;
}
// Enhance observation data with METAR parsing for missing fields
let augmentedObservation = augmentObservationWithMetar(observation.properties);
// Define required fields for regional observations (more lenient than current weather)
const requiredFields = [
{ name: 'temperature', check: (props) => props.temperature?.value === null },
{ name: 'textDescription', check: (props) => props.textDescription === null || props.textDescription === '' },
{ name: 'icon', check: (props) => props.icon === null },
];
// Use enhanced observation with MapClick fallback
const enhancedResult = await enhanceObservationWithMapClick(augmentedObservation, {
requiredFields,
stationId,
debugContext: 'regionalforecast',
});
augmentedObservation = enhancedResult.data;
const { missingRequired, missingOptional } = enhancedResult;
// Check final data quality
if ((missingRequired.length + missingOptional.length) > 0) {
if (debugFlag('regionalforecast')) {
console.log(`Regional Observations for station ${stationId} is missing fields: ${[...missingRequired, ...missingOptional].join(', ')} (skipping)`);
}
return false;
}
// preload the image
if (!augmentedObservation.icon) return false;
// the icon url carries day/night (/icons/land/day/...), which smallIcon reads when no override is given
const icon = smallIcon(augmentedObservation.icon);
if (!icon) return false;
// add the icon url to the dataset
augmentedObservation.ws4icon = icon;
augmentedObservation.coercedRight = city.coercedRight;
// return the observation
return augmentedObservation;
} catch (error) {
console.error(`Unexpected error getting Regional Observation for ${city.city}: ${error.message}`);
return false;
}
};
// to fit on the map, remove anything after punctuation and then limit to 15 characters
const formatCity = (city) => city.match(/[^,/;\\-]*/)[0].substr(0, 12);
export {
buildForecast,
getRegionalObservation,
formatCity,
};