OpenDataVisualizer/Schaad.OpenData.Weather/Analysis/WeatherAnalysis.cs
Claudio Schaad bd2981ffb8 Add live weather layer from MeteoSwiss stations (#7)
- New Schaad.OpenData.Weather project: MeteoSwissClient reads the
  10-minute CSV of the five SwissMetNet stations in Thurgau every 10
  minutes (station list once a day) and backfills the last 7 days from
  the yearly file when the cache has less than 6 days
- History kept as JSON per day (60 days); a failing station does not
  stop the others
- API: /api/weather/meta, /stations (current values, 1 h/24 h rain,
  24 h min/max, strongest gust), /stations/{abbr}/history
- Combined map: "Wetter (live)" layer with station badges (temperature,
  24 h rain or wind), wind arrows, rain marker, station panel with
  hourly 7-day chart, overview and municipality context entries
- Docs: docs/datasets/wetter.md, overview in docs/datasets.md,
  CLAUDE.md; Docker and appsettings wiring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-11-19 05:04:46 +00:00

62 lines
2.7 KiB
C#

using Schaad.OpenData.Weather.Models;
using Schaad.OpenData.Weather.Storage;
namespace Schaad.OpenData.Weather.Analysis;
public static class WeatherAnalysis
{
private static readonly TimeSpan CurrentTolerance = TimeSpan.FromMinutes(30);
public static IReadOnlyList<WeatherStationView> BuildStations(WeatherSnapshot snapshot)
{
var byStation = snapshot.Readings.ToLookup(r => r.StationAbbr);
return snapshot.Stations
.Select(station => ToView(station, byStation[station.Abbr].ToList()))
.ToList();
}
public static IReadOnlyList<WeatherHistoryPoint> BuildHistory(WeatherSnapshot snapshot, string abbr, DateTimeOffset from) =>
snapshot.Readings
.Where(r => r.StationAbbr == abbr && r.Time >= from)
.Select(r => new WeatherHistoryPoint(r.Time, r.Temperature, r.Precipitation, r.WindSpeed, r.WindGust))
.ToList();
private static WeatherStationView ToView(WeatherStation station, IReadOnlyList<WeatherReading> readings)
{
var latest = readings.Count > 0 ? readings[^1] : null;
var recent = latest is null ? [] : readings.Where(r => r.Time > latest.Time - CurrentTolerance).Reverse().ToList();
var lastHour = latest is null ? [] : readings.Where(r => r.Time > latest.Time.AddHours(-1)).ToList();
var lastDay = latest is null ? [] : readings.Where(r => r.Time > latest.Time.AddDays(-1)).ToList();
return new WeatherStationView(
station.Abbr,
station.Name,
station.Canton,
station.HeightMasl,
station.Lat,
station.Lon,
latest?.Time,
Current(recent, r => r.Temperature),
Current(recent, r => r.Humidity),
Current(recent, r => r.WindSpeed),
Current(recent, r => r.WindGust),
Current(recent, r => r.WindDirection),
Current(recent, r => r.Pressure),
Sum(lastHour, r => r.Precipitation),
Sum(lastDay, r => r.Precipitation),
Sum(lastDay, r => r.Sunshine),
lastDay.Select(r => r.Temperature).Min(),
lastDay.Select(r => r.Temperature).Max(),
lastDay.Select(r => r.WindGust).Max(),
readings.Count > 0 ? readings[0].Time : null);
}
private static double? Current(IReadOnlyList<WeatherReading> newestFirst, Func<WeatherReading, double?> value) =>
newestFirst.Select(value).FirstOrDefault(v => v.HasValue);
private static double? Sum(IReadOnlyList<WeatherReading> readings, Func<WeatherReading, double?> value)
{
var values = readings.Select(value).Where(v => v.HasValue).ToList();
return values.Count == 0 ? null : Math.Round(values.Sum(v => v!.Value), 1);
}
}