@@ -2,40 +2,67 @@ export interface Reading {
22 stationId: string;
33 takenAt: Date;
44 temperature: number;
55 humidity: number | null;
66 pressure: number;
77 windSpeed: number;
88 windDirection: number;
gust?: number;
910 rainfall: number;
1011 uvIndex: number | null;
1112}
1213
1314function round(value: number, digits = 1): number {
1415 const factor = 10 ** digits;
1516 return Math.round(value * factor) / factor;
1617}
1718
1819export function parseReading(line: string): Reading {
const [stationId, takenAt, temperature, humidity, pressure, windSpeed, windDirection, rainfall = "0", uvIndex = ""] =
line.split(",");
const columns = line.split(",");
if (columns.length < 7) {
throw new ReadingError(line, `expected 7 columns, found ${columns.length}`);
}
const [stationId, takenAt, temperature, humidity, pressure, windSpeed, windDirection, rainfall = "0", uvIndex = "", gust = ""] =
columns;
2128
2229 return {
2330 stationId,
2431 takenAt: new Date(takenAt),
temperature: round(Number(temperature)),
temperature: round(numeric(line, "temperature", temperature)),
2633 humidity: humidity === "" ? null : round(Math.min(100, Math.max(0, Number(humidity)))),
pressure: round(Number(pressure)),
windSpeed: round(Number(windSpeed)),
windDirection: Number(windDirection),
pressure: round(numeric(line, "pressure", pressure)),
windSpeed: round(numeric(line, "wind speed", windSpeed)),
windDirection: numeric(line, "wind direction", windDirection),
gust: gust === "" ? undefined : numeric(line, "gust", gust),
3038 rainfall: round(Number(rainfall)),
3139 uvIndex: uvIndex === "" ? null : Number(uvIndex),
3240 };
3341}
3442
export class ReadingError extends Error {
constructor(
readonly line: string,
readonly problem: string,
) {
super(`Invalid reading (${problem}): ${line}`);
}
}
function numeric(line: string, column: string, raw: string): number {
const value = Number(raw);
if (raw.trim() === "" || Number.isNaN(value)) {
throw new ReadingError(line, `${column} is not a number`);
}
return value;
}
3562export function dewPoint(reading: Reading): number | null {
3663 if (reading.humidity === null) {
3764 return null;
3865 }
3966
4067 const a = 17.27;
4168 const b = 237.7;