- 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>
88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
using System.Globalization;
|
|
using System.Net;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Hydrology.Models;
|
|
|
|
namespace Schaad.OpenData.Hydrology.Source;
|
|
|
|
public class HydrodatenPlotClient(HttpClient httpClient, IOptions<HydrologyOptions> options)
|
|
{
|
|
private const string LevelTrace = "Wasserstand";
|
|
private const string DischargeTrace = "Abfluss";
|
|
private const string TemperatureTrace = "Temperatur";
|
|
private const int StepMinutes = 10;
|
|
|
|
public virtual async Task<IReadOnlyList<HydroReading>> FetchRecentAsync(string stationId, CancellationToken cancellationToken)
|
|
{
|
|
if (stationId.Length == 0 || !stationId.All(char.IsAsciiDigit))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
var baseUrl = options.Value.PlotsUrl.TrimEnd('/');
|
|
var levels = await ReadTracesAsync($"{baseUrl}/p_q_7days/{stationId}_p_q_7days_de.json", cancellationToken);
|
|
var temperatures = await ReadTracesAsync($"{baseUrl}/temperature_7days/{stationId}_temperature_7days_de.json", cancellationToken);
|
|
|
|
var level = levels.GetValueOrDefault(LevelTrace) ?? [];
|
|
var discharge = levels.GetValueOrDefault(DischargeTrace) ?? [];
|
|
var temperature = temperatures.GetValueOrDefault(TemperatureTrace) ?? [];
|
|
|
|
return level.Keys
|
|
.Concat(discharge.Keys)
|
|
.Concat(temperature.Keys)
|
|
.Distinct()
|
|
.Order()
|
|
.Select(time => new HydroReading(
|
|
stationId,
|
|
time,
|
|
level.GetValueOrDefault(time),
|
|
discharge.GetValueOrDefault(time),
|
|
temperature.GetValueOrDefault(time),
|
|
null))
|
|
.ToList();
|
|
}
|
|
|
|
private async Task<Dictionary<string, Dictionary<DateTimeOffset, double?>>> ReadTracesAsync(string url, CancellationToken cancellationToken)
|
|
{
|
|
var traces = new Dictionary<string, Dictionary<DateTimeOffset, double?>>();
|
|
using var response = await httpClient.GetAsync(url, cancellationToken);
|
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
|
{
|
|
return traces;
|
|
}
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
|
using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
|
|
if (!document.RootElement.TryGetProperty("plot", out var plot) || !plot.TryGetProperty("data", out var data))
|
|
{
|
|
return traces;
|
|
}
|
|
|
|
foreach (var trace in data.EnumerateArray())
|
|
{
|
|
if (!trace.TryGetProperty("name", out var name) || name.GetString() is not { } traceName
|
|
|| !trace.TryGetProperty("x", out var xs) || !trace.TryGetProperty("y", out var ys))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var values = new Dictionary<DateTimeOffset, double?>();
|
|
foreach (var (x, y) in xs.EnumerateArray().Zip(ys.EnumerateArray()))
|
|
{
|
|
if (x.GetString() is { } text
|
|
&& DateTimeOffset.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var time)
|
|
&& time.Minute % StepMinutes == 0
|
|
&& y.ValueKind == JsonValueKind.Number)
|
|
{
|
|
values[time] = y.GetDouble();
|
|
}
|
|
}
|
|
|
|
traces[traceName] = values;
|
|
}
|
|
|
|
return traces;
|
|
}
|
|
}
|