OpenDataVisualizer/Schaad.OpenData.Hydrology/Analysis/HydrologyAnalysis.cs
Claudio Schaad a838b17dc1 Add live water layer from BAFU hydrology data
- New Schaad.OpenData.Hydrology project: LindasHydroClient fetches the
  current level, discharge, water temperature and danger level of nine
  BAFU stations around Thurgau from LINDAS every 10 minutes
- LINDAS has no history, so the server keeps readings per day (60 days)
  and fills gaps from the 7-day plot JSON of hydrodaten.admin.ch
- API: /api/hydrology/meta, /stations, /stations/{id}/history
- Combined map: "Gewässer (live)" layer with river circles (size by
  discharge, colour by danger level or water temperature), lake level
  badges, station panel with 7-day chart, overview and municipality
  context entries
- Docs: docs/datasets/gewaesser.md, overview in docs/datasets.md,
  CLAUDE.md; Docker and appsettings wiring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:40:33 +01:00

49 lines
1.9 KiB
C#

using Schaad.OpenData.Hydrology.Models;
using Schaad.OpenData.Hydrology.Storage;
namespace Schaad.OpenData.Hydrology.Analysis;
public static class HydrologyAnalysis
{
private static readonly TimeSpan CompareTolerance = TimeSpan.FromHours(1);
public static IReadOnlyList<StationView> BuildStations(HydrologySnapshot snapshot)
{
var byStation = snapshot.Readings.ToLookup(r => r.StationId);
return snapshot.Stations
.Select(station => ToView(station, byStation[station.Id].ToList()))
.ToList();
}
public static IReadOnlyList<HistoryPoint> BuildHistory(HydrologySnapshot snapshot, string stationId, DateTimeOffset from) =>
snapshot.Readings
.Where(r => r.StationId == stationId && r.Time >= from)
.Select(r => new HistoryPoint(r.Time, r.WaterLevel, r.Discharge, r.WaterTemperature))
.ToList();
private static StationView ToView(HydroStation station, IReadOnlyList<HydroReading> readings)
{
var latest = readings.Count > 0 ? readings[^1] : null;
var dayBefore = latest is null ? null : Closest(readings, latest.Time.AddDays(-1));
return new StationView(
station.Id,
station.Name,
station.WaterBody,
station.Kind,
station.Lat,
station.Lon,
latest?.Time,
latest?.WaterLevel,
latest?.Discharge,
latest?.WaterTemperature,
latest?.DangerLevel,
latest?.WaterLevel - dayBefore?.WaterLevel,
dayBefore?.Discharge,
readings.Count > 0 ? readings[0].Time : null);
}
private static HydroReading? Closest(IReadOnlyList<HydroReading> readings, DateTimeOffset target) =>
readings
.Where(r => (r.Time - target).Duration() <= CompareTolerance)
.MinBy(r => (r.Time - target).Duration());
}