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

Update to ust-ist adapter for 'time beginning' measurements #1099

Merged
merged 3 commits into from
Apr 3, 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
65 changes: 35 additions & 30 deletions src/adapters/ust-ist.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ import log from '../lib/logger.js';

export const name = 'ust-ist';

// the dataUrl only works for the current date - 1 day.
// a list of endpoints can be found at https://api.ust.is/aq/a
// the old endpoint https://api.ust.is/aq/a/getLatest is not returning data anymore
const yesterday = DateTime.utc().minus({ days: 1 }).toISODate(); // format "YYYY-MM-DD"
const stationsUrl = 'https://api.ust.is/aq/a/getStations';
const dataUrl = `https://api.ust.is/aq/a/getDate/date/${yesterday}`;
const dataUrl = `https://api.ust.is/aq/a/getLatest`;

/**
* Fetches air quality data for Iceland from a specific date and compiles it into a structured format.
Expand All @@ -44,7 +41,7 @@ export async function fetchData(source, cb) {

// Skip processing this station if metadata is missing
if (!stationMeta) {
console.warn(`Metadata missing for station ID: ${stationId}. Skipping...`);
log.warn(`Metadata missing for station ID: ${stationId}. Skipping...`);
return acc;
}

Expand All @@ -65,7 +62,7 @@ export async function fetchData(source, cb) {

return acc.concat(latestMeasurements.map(m => ({ ...baseMeta, ...m })));
}, []);
log.debug(measurements[0]);
console.debug("Example measurements", measurements.slice(-5));
cb(null, { name: 'unused', measurements });
} catch (e) {
cb(e);
Expand Down Expand Up @@ -102,28 +99,36 @@ function parseParams(params) {
const validParams = Object.keys(params).filter(p => acceptableParameters.includes(p.toLowerCase().replace('.', '')));

return validParams.flatMap(p => {
const measurements = Object.keys(params[p])
.filter(key => !isNaN(parseInt(key))) // Filter out keys that are not indices
.map(index => {
const measurement = params[p][index];
const date = DateTime.fromFormat(measurement.endtime.trimEnd(), 'yyyy-LL-dd HH:mm:ss', { zone: 'Atlantic/Reykjavik' });

const resolution = params[p].resolution === '1h'
? { value: 1, unit: 'hours' }
: {};

return {
date: {
utc: date.toUTC().toISO({ suppressMilliseconds: true }),
local: date.toISO({ suppressMilliseconds: true })
},
parameter: p.toLowerCase().replace('.', ''),
value: parseFloat(measurement.value),
unit: params[p].unit,
averagingPeriod: resolution
};
});

return measurements;
const measurements = Object.keys(params[p])
.filter(key => !isNaN(parseInt(key))) // Filter out keys that are not indices
.map(index => {
const measurement = params[p][index];
// arguement for assuming time beginning even though the variable name is 'endtime'
// ----------------------------------------------------------------------------------
// When we look at their endpoint for a full days worth of data e.g. https://api.ust.is/aq/a/getDate/date/2024-04-02
// We see 24 hours with the first hour having an endtime of 00:00:00 and the last hour being 23:00:00
// and so we assume that the data here is time beginning (i.e. the data for 23:00:00 is the average from 11pm to midnight)
// And then when we look at the latest data we see essentially the same thing
// where the two endpoints overlap the data for the overlapping hours match
// and we dont see data for the previous hour, always for 2 hours back, suggesting the previous hour is marked time beginning
const date = DateTime.fromFormat(measurement.endtime.trimEnd(), 'yyyy-LL-dd HH:mm:ss', { zone: 'Atlantic/Reykjavik' }).plus({ hours: 1 });

const resolution = params[p].resolution === '1h'
? { value: 1, unit: 'hours' }
: {};

return {
date: {
utc: date.toUTC().toISO({ suppressMilliseconds: true }),
local: date.toISO({ suppressMilliseconds: true })
},
parameter: p.toLowerCase().replace('.', ''),
value: parseFloat(measurement.value),
unit: params[p].unit,
averagingPeriod: resolution
};
});

return measurements;
});
}
}
Loading