144 lines
4.7 KiB
HTML
144 lines
4.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>METAR Practice</title>
|
|
<style>
|
|
body {
|
|
font-family: monospace;
|
|
padding: 2em;
|
|
}
|
|
#metar-display {
|
|
font-size: 1.2em;
|
|
margin-bottom: 1em;
|
|
min-height: 3em;
|
|
}
|
|
|
|
@media (max-width: 600px) {
|
|
#metar-display {
|
|
min-height: 6em;
|
|
}
|
|
}
|
|
button {
|
|
font-size: 1em;
|
|
padding: 0.5em 1em;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="app">
|
|
<h1>METAR Practice</h1>
|
|
|
|
<div id="metar-display"></div>
|
|
<button id="new-metar-btn">New METAR</button>
|
|
</div>
|
|
<script>
|
|
const metarDisplay = document.getElementById('metar-display');
|
|
const newMetarBtn = document.getElementById('new-metar-btn');
|
|
const metars = [];
|
|
|
|
function parseMetar(metarString) {
|
|
const decoded = {};
|
|
const parts = metarString.split(/\s+/).filter(Boolean);
|
|
let index = 0;
|
|
|
|
// Airport
|
|
if (parts[index] && parts[index].length === 4 && parts[index].match(/^[A-Z][A-Z0-9]{3}$/)) {
|
|
decoded.airport = parts[index++];
|
|
} else {
|
|
console.error('Failed to parse airport in:', metarString);
|
|
}
|
|
|
|
// Time
|
|
if (parts[index] && parts[index].endsWith('Z')) {
|
|
decoded.time = parts[index++];
|
|
} else {
|
|
console.error('Failed to parse time in:', metarString);
|
|
}
|
|
|
|
// AUTO
|
|
if (parts[index] && parts[index] === 'AUTO') {
|
|
decoded.auto = true;
|
|
index++;
|
|
}
|
|
|
|
// Wind
|
|
if (parts[index] && parts[index].endsWith('KT')) {
|
|
decoded.wind = parts[index++];
|
|
}
|
|
|
|
// The rest is semi-ordered. Find remarks first.
|
|
const remainingParts = parts.slice(index);
|
|
const rmkIndex = remainingParts.indexOf('RMK');
|
|
let mainParts = remainingParts;
|
|
if (rmkIndex !== -1) {
|
|
decoded.remarks = remainingParts.slice(rmkIndex + 1).join(' ');
|
|
mainParts = remainingParts.slice(0, rmkIndex);
|
|
}
|
|
|
|
// Identify other parts from what's left.
|
|
const cloudRegex = /^(SKC|CLR|FEW|SCT|BKN|OVC)(\d{3})?(CB|TCU)?$/;
|
|
const tempDewRegex = /^(M?\d{2})\/(M?\d{2})$/;
|
|
const altimeterRegex = /^A(\d{4})$/;
|
|
|
|
decoded.weather = [];
|
|
decoded.clouds = [];
|
|
|
|
mainParts.forEach(part => {
|
|
if (tempDewRegex.test(part)) {
|
|
if (!decoded.temp_dew) decoded.temp_dew = part;
|
|
} else if (altimeterRegex.test(part)) {
|
|
if (!decoded.altimeter) decoded.altimeter = part;
|
|
} else if (cloudRegex.test(part)) {
|
|
decoded.clouds.push(part);
|
|
} else if (part.endsWith('SM') && !decoded.visibility) {
|
|
decoded.visibility = part;
|
|
} else {
|
|
decoded.weather.push(part);
|
|
}
|
|
});
|
|
|
|
if (!decoded.altimeter) console.error('Failed to parse Altimeter in:', metarString);
|
|
|
|
return decoded;
|
|
}
|
|
|
|
function displayNewMetar() {
|
|
if (metars.length === 0) {
|
|
metarDisplay.textContent = 'No METARs loaded.';
|
|
return;
|
|
}
|
|
const randomIndex = Math.floor(Math.random() * metars.length);
|
|
const metarString = metars[randomIndex];
|
|
metarDisplay.textContent = metarString;
|
|
|
|
const decodedMetar = parseMetar(metarString);
|
|
}
|
|
|
|
fetch('metars.txt')
|
|
.then(response => response.text())
|
|
.then(data => {
|
|
const lines = data.split('\n');
|
|
lines.forEach(line => {
|
|
if (line.includes('METAR')) {
|
|
const metarString = line.substring(line.indexOf('METAR') + 6).replace('=', '').trim();
|
|
if (metarString) {
|
|
metars.push(metarString);
|
|
}
|
|
}
|
|
});
|
|
console.log(`Loaded ${metars.length} METARs.`);
|
|
displayNewMetar(); // Display initial METAR
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching or parsing metars.txt:', error)
|
|
metarDisplay.textContent = 'Could not load METARs.';
|
|
});
|
|
|
|
newMetarBtn.addEventListener('click', displayNewMetar);
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|