119 lines
6.0 KiB
JavaScript
119 lines
6.0 KiB
JavaScript
// ════════════════════════════════════════════════════════════════════════
|
|
// compute.js — Build the per-hour display rows from the raw API data.
|
|
//
|
|
// Pure-ish function: feed in (forecast, airQuality, location, vehicleType,
|
|
// vehicleVent, buildingType) and get back { hourlyRows, days, utcOffsetMs }.
|
|
//
|
|
// Open-Meteo with timezone=auto returns local wall-clock strings like
|
|
// "2026-05-13T14:00" — no Z suffix. Two forms are used in each row:
|
|
// • String slices (iso.slice(...)) for display & day grouping
|
|
// • A true UTC Date (dt) for solarElevationDeg (which uses .getUTC*).
|
|
// ════════════════════════════════════════════════════════════════════════
|
|
|
|
import {
|
|
vaporPressureHpa, solarElevationDeg, calcTmrt, utciApprox,
|
|
calcConcreteTemp, calcVehicleInteriorTemp,
|
|
calcIndoorTempPass, calcManagedIndoorTempPass,
|
|
} from './physics.js';
|
|
import { windCompass8, uvSplit, cloudCategory, precipPenalty } from './utils.js';
|
|
|
|
export function buildHourlyRows({ forecast, airQuality, location, vehicleType, vehicleVent, buildingType }) {
|
|
const utcOffsetMs = (forecast?.utc_offset_seconds ?? 0) * 1000;
|
|
|
|
// Build a fast lookup map from the air quality hourly data: ISO string → index.
|
|
// Normalise to "YYYY-MM-DDTHH" (13 chars) so forecast timestamps like
|
|
// "2026-05-17T14:00" match AQ timestamps like "2026-05-17T14".
|
|
const aqTimeMap = {};
|
|
if (airQuality?.hourly?.time) {
|
|
airQuality.hourly.time.forEach((t, i) => { aqTimeMap[t.slice(0, 13)] = i; });
|
|
}
|
|
const getAq = (field, iso) => {
|
|
if (!airQuality?.hourly?.[field]) return null;
|
|
const i = aqTimeMap[iso.slice(0, 13)];
|
|
if (i === undefined) return null;
|
|
return airQuality.hourly[field][i] ?? null;
|
|
};
|
|
|
|
const hourlyRows = forecast ? forecast.hourly.time.map((iso, i) => {
|
|
const h = forecast.hourly;
|
|
const Ta = h.temperature_2m[i];
|
|
const RH = h.relative_humidity_2m[i];
|
|
const dew = h.dew_point_2m ? h.dew_point_2m[i] : null;
|
|
const va = h.wind_speed_10m[i];
|
|
const wd = h.wind_direction_10m ? h.wind_direction_10m[i] : null;
|
|
const gust = h.wind_gusts_10m ? h.wind_gusts_10m[i] : null;
|
|
const dir = h.direct_radiation[i] || 0;
|
|
const dif = h.diffuse_radiation[i] || 0;
|
|
const glob = h.shortwave_radiation[i] || 0;
|
|
const cc = h.cloud_cover[i];
|
|
const ccLow = h.cloud_cover_low ? h.cloud_cover_low[i] : null;
|
|
const ccMid = h.cloud_cover_mid ? h.cloud_cover_mid[i] : null;
|
|
const ccHigh = h.cloud_cover_high ? h.cloud_cover_high[i] : null;
|
|
const uv = h.uv_index ? (h.uv_index[i] || 0) : 0;
|
|
const precip = h.precipitation[i] || 0;
|
|
const snow = h.snowfall[i] || 0;
|
|
const soilT0 = h.soil_temperature_0cm ? h.soil_temperature_0cm[i] : null;
|
|
const soilT6 = h.soil_temperature_6cm ? h.soil_temperature_6cm[i] : null;
|
|
const soilM = h.soil_moisture_0_to_1cm ? h.soil_moisture_0_to_1cm[i] : null;
|
|
const concreteT = calcConcreteTemp(Ta, glob, va);
|
|
// iso is a local wall-clock string e.g. "2026-05-13T14:00" (no Z).
|
|
// For display we slice the string directly — no Date object needed.
|
|
// For solarElevationDeg (which uses .getUTC* internally) we need the
|
|
// true UTC instant: treat the local time as UTC then subtract the offset.
|
|
// e.g. Brisbane UTC+10: local 14:00 → parse as UTC 14:00 → subtract 10h → UTC 04:00 ✓
|
|
const dt = new Date(Date.parse(iso + 'Z') - utcOffsetMs);
|
|
const elev = solarElevationDeg(location.lat, location.lon, dt);
|
|
const vehicleT = calcVehicleInteriorTemp(Ta, glob, elev, vehicleType, vehicleVent);
|
|
const eh = vaporPressureHpa(Ta, RH);
|
|
const Tmrt = calcTmrt(Ta, dir, dif, glob, elev);
|
|
const utci = utciApprox(Ta, Tmrt, va, eh);
|
|
const utciAdj = utci + precipPenalty(precip, snow, va);
|
|
// Derived
|
|
const compass = windCompass8(wd);
|
|
const { uvA, uvB } = uvSplit(uv, elev);
|
|
const cloudCat = cloudCategory(cc, ccLow, ccMid, ccHigh);
|
|
// Visibility from the main forecast API (metres → km).
|
|
const visKm = (() => { const v = h.visibility ? h.visibility[i] : null; return v != null ? v / 1000 : null; })();
|
|
const aqi = getAq('european_aqi', iso);
|
|
const grassPollen = getAq('grass_pollen', iso);
|
|
const birchPollen = getAq('birch_pollen', iso);
|
|
const alderPollen = getAq('alder_pollen', iso);
|
|
const mugwortPollen= getAq('mugwort_pollen', iso);
|
|
const olivePollen = getAq('olive_pollen', iso);
|
|
const ragweedPollen= getAq('ragweed_pollen', iso);
|
|
return {
|
|
iso, dt, Ta, RH, dew, va, wd, gust, dir, dif, glob,
|
|
cc, ccLow, ccMid, ccHigh, cloudCat,
|
|
uv, uvA, uvB,
|
|
precip, snow,
|
|
soilT0, soilT6, soilM, concreteT, vehicleT,
|
|
elev, Tmrt, utci, utciAdj, eh, compass,
|
|
visKm, aqi,
|
|
grassPollen, birchPollen, alderPollen, mugwortPollen, olivePollen, ragweedPollen,
|
|
};
|
|
}) : [];
|
|
|
|
// Two-pass indoor temperature: needs the full hourly arrays so thermal
|
|
// lag can look back at previous hours. Run after hourlyRows is built,
|
|
// then stamp each row with its indoorT value.
|
|
if (hourlyRows.length > 0) {
|
|
const TaArr = hourlyRows.map(r => r.Ta);
|
|
const globArr = hourlyRows.map(r => r.glob);
|
|
const elevArr = hourlyRows.map(r => r.elev);
|
|
const indoorTemps = calcIndoorTempPass(TaArr, globArr, elevArr, buildingType);
|
|
const managedTemps = calcManagedIndoorTempPass(TaArr, globArr, elevArr, buildingType);
|
|
hourlyRows.forEach((r, i) => { r.indoorT = indoorTemps[i]; r.managedT = managedTemps[i]; });
|
|
}
|
|
|
|
// Group those hourly rows into days for the day tabs.
|
|
const days = [];
|
|
hourlyRows.forEach(row => {
|
|
const key = row.iso.slice(0, 10);
|
|
let day = days.find(d => d.key === key);
|
|
if (!day) { day = { key, rows: [] }; days.push(day); }
|
|
day.rows.push(row);
|
|
});
|
|
|
|
return { hourlyRows, days, utcOffsetMs };
|
|
}
|