ws4kp/datagenerators/stations.js

74 lines
2.3 KiB
JavaScript
Raw Permalink 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');
2022-12-12 21:41:28 +00:00
const states = require('./stations-states');
const chunk = require('./chunk');
2020-09-25 20:11:19 +00:00
// skip stations starting with these letters
const skipStations = ['U', 'C', 'H', 'W', 'Y', 'T', 'S', 'M', 'O', 'L', 'A', 'F', 'B', 'N', 'V', 'R', 'D', 'E', 'I', 'G', 'J'];
2020-09-25 20:11:19 +00:00
// immediately invoked function so we can access async/await
const start = async () => {
2022-12-12 21:41:28 +00:00
// chunk the list of states
const chunkStates = chunk(states, 5);
2020-09-25 20:11:19 +00:00
2022-12-12 21:41:28 +00:00
// store output
2022-11-22 16:45:17 +00:00
const output = {};
2022-12-12 21:41:28 +00:00
// process all chunks
for (let i = 0; i < chunkStates.length; i += 1) {
const stateChunk = chunkStates[i];
// loop through states
stateChunk.forEach(async (state) => {
try {
let stations;
let next = `https://api.weather.gov/stations?state=${state}`;
do {
// get list and parse the JSON
// eslint-disable-next-line no-await-in-loop
const stationsRaw = await https(next);
stations = JSON.parse(stationsRaw);
// filter stations for 4 letter identifiers
const stationsFiltered4 = stations.features.filter((station) => station.properties.stationIdentifier.match(/^[A-Z]{4}$/));
// filter against starting letter
const stationsFiltered = stationsFiltered4.filter((station) => !skipStations.includes(station.properties.stationIdentifier.slice(0, 1)));
2022-12-12 21:41:28 +00:00
// add each resulting station to the output
stationsFiltered.forEach((station) => {
const id = station.properties.stationIdentifier;
if (output[id]) {
console.log(`Duplicate station: ${state}-${id}`);
return;
}
output[id] = {
id,
city: station.properties.name,
state,
lat: station.geometry.coordinates[1],
lon: station.geometry.coordinates[0],
};
});
next = stations?.pagination?.next;
// write the output
// write the output
2022-12-12 21:41:28 +00:00
fs.writeFileSync(path.join(__dirname, 'output/stations.json'), JSON.stringify(output, null, 2));
2020-09-25 20:11:19 +00:00
}
2022-12-12 21:41:28 +00:00
while (next && stations.features.length > 0);
console.log(`Complete: ${state}`);
return true;
} catch (e) {
console.error(`Unable to get state: ${state}`);
return false;
}
});
}
2020-09-25 20:11:19 +00:00
};
// immediately invoked function allows access to async
(async () => {
await start();
})();