ws4kp/datagenerators/stations.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-09-25 20:11:19 +00:00
// list all stations in a single file
// only find stations with 4 letter codes
const fs = require('fs');
const path = require('path');
2022-11-22 16:45:17 +00:00
const https = require('./https');
2020-09-25 20:11:19 +00:00
// immediately invoked function so we can access async/await
const start = async () => {
// load the list of states
const states = ['AK', 'NC', 'VA', 'TX', 'GA', 'PR'];
// const states = require('./stations-states.js');
2022-11-22 16:45:17 +00:00
const output = {};
2020-09-25 20:11:19 +00:00
// loop through states
await Promise.all(states.map(async (state) => {
try {
// get list and parse the JSON
const stationsRaw = await https(`https://api.weather.gov/stations?state=${state}`);
const stationsAll = JSON.parse(stationsRaw).features;
// filter stations for 4 letter identifiers
2022-11-22 16:45:17 +00:00
const stations = stationsAll.filter((station) => station.properties.stationIdentifier.match(/^[A-Z]{4}$/));
2020-09-25 20:11:19 +00:00
// add each resulting station to the output
2022-11-22 16:45:17 +00:00
stations.forEach((station) => {
2020-09-25 20:11:19 +00:00
const id = station.properties.stationIdentifier;
if (output[id]) {
console.log(`Duplicate station: ${state}-${id}`);
return;
}
output[id] = {
id,
city: station.properties.name,
2022-11-22 16:45:17 +00:00
state,
2020-09-25 20:11:19 +00:00
lat: station.geometry.coordinates[1],
lon: station.geometry.coordinates[0],
};
});
console.log(`Complete: ${state}`);
} catch (e) {
console.error(`Unable to get state: ${state}`);
return false;
}
}));
// write the output
fs.writeFileSync(path.join(__dirname, 'output/stations.js'), JSON.stringify(output, null, 2));
};
// immediately invoked function allows access to async
(async () => {
await start();
})();