Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix station list parsing and raise error if we parsed 0 stations #18

Merged
merged 2 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions scraper/src/loadStations.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ module.exports = async function loadStations (baseMinYear) {
const url =
'https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/daily/kl/recent/KL_Tageswerte_Beschreibung_Stationen.txt';
const raw = (await got(url, { encoding: 'latin1' })).body;
return parseFixedWidth(raw, {
skip: 3,
widths: [5, 9, 9, 15, 12, 10, 42, 22],
const stations = parseFixedWidth(raw, {
// The format was changed sometime between 2024-04-15 and 2024-05-01
//skip: 3,
//widths: [5, 9, 9, 15, 12, 10, 42, 22],
skip: 2,
widths: [21, 9, 9, 14, 12, 10, 81, 843],
names: ['id', 'from', 'to', 'altitude', 'lat', 'lon', 'name', 'state'],
})
.map(station => ({
Expand All @@ -37,6 +40,11 @@ module.exports = async function loadStations (baseMinYear) {
.filter(station => !STATION_BLACKLIST.includes(station.name))
.filter(d => dayjs(d.from).year() <= baseMinYear && dayjs().diff(d.to, 'day') < 5)
.sort((a, b) => (a.slug > b.slug ? 1 : b.slug > a.slug ? -1 : 0));

if (stations.length === 0) {
throw new Error('No stations found');
}
return stations;
};

function parseFixedWidth (data, { skip = 0, widths = [], names = [], trim = true }) {
Expand Down